]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/hashcomp.h
Fix this so it works, passes test case. Provide a method to query for a bit and to...
[user/henk/code/inspircd.git] / include / hashcomp.h
index 0a39deb1f7243359bc30553623a7901f26d149ea..73d9f53f245ee175f9e8385f3bb34322207e9179 100644 (file)
@@ -123,6 +123,102 @@ namespace irc
                bool operator()(const insp_inaddr &s1, const insp_inaddr &s2) const;
        };
 
+       /** irc::stringjoiner joins string lists into a string, using
+        * the given seperator string.
+        * This class can join a vector of std::string, a deque of
+        * std::string, or a const char** array, using overloaded
+        * constructors.
+        */
+       class stringjoiner
+       {
+        private:
+               /** Output string
+                */
+               std::string joined;
+        public:
+               /** Join elements of a vector, between (and including) begin and end
+                * @param seperator The string to seperate values with
+                * @param sequence One or more items to seperate
+                * @param begin The starting element in the sequence to be joined
+                * @param end The ending element in the sequence to be joined
+                */
+               stringjoiner(const std::string &seperator, const std::vector<std::string> &sequence, int begin, int end);
+               /** Join elements of a deque, between (and including) begin and end
+                * @param seperator The string to seperate values with
+                * @param sequence One or more items to seperate
+                * @param begin The starting element in the sequence to be joined
+                * @param end The ending element in the sequence to be joined
+                */
+               stringjoiner(const std::string &seperator, const std::deque<std::string> &sequence, int begin, int end);
+               /** Join elements of an array of char arrays, between (and including) begin and end
+                * @param seperator The string to seperate values with
+                * @param sequence One or more items to seperate
+                * @param begin The starting element in the sequence to be joined
+                * @param end The ending element in the sequence to be joined
+                */
+               stringjoiner(const std::string &seperator, const char** sequence, int begin, int end);
+
+               /** Get the joined sequence
+                * @return A reference to the joined string
+                */
+               std::string& GetJoined();
+       };
+
+       /** irc::modestacker stacks mode sequences into a list.
+        * It can then reproduce this list, clamped to a maximum of MAXMODES
+        * values per line.
+        */
+       class modestacker
+       {
+        private:
+               /** The mode sequence and its parameters
+                */
+               std::deque<std::string> sequence;
+               /** True if the mode sequence is initially adding
+                * characters, false if it is initially removing
+                * them
+                */
+               bool adding;
+        public:
+               /** Construct a new modestacker.
+                * @param add True if the stack is adding modes,
+                * false if it is removing them
+                */
+               modestacker(bool add);
+               /** Push a modeletter and its parameter onto the stack.
+                * No checking is performed as to if this mode actually
+                * requires a parameter. If you stack invalid mode
+                * sequences, they will be tidied if and when they are
+                * passed to a mode parser.
+                * @param modeletter The mode letter to insert
+                * @param parameter The parameter for the mode
+                */
+               void Push(char modeletter, const std::string &parameter);
+               /** Push a modeletter without parameter onto the stack.
+                * No checking is performed as to if this mode actually
+                * requires a parameter. If you stack invalid mode
+                * sequences, they will be tidied if and when they are
+                * passed to a mode parser.
+                * @param modeletter The mode letter to insert
+                */
+               void Push(char modeletter);
+               /** Push a '+' symbol onto the stack.
+                */
+               void PushPlus();
+               /** Push a '-' symbol onto the stack.
+                */
+               void PushMinus();
+               /** Return zero or more elements which form the
+                * mode line. This will be clamped to a max of
+                * MAXMODES+1 items (MAXMODES mode parameters and
+                * one mode sequence string).
+                * @param result The deque to populate. This will
+                * be cleared before it is used.
+                * @return The number of elements in the deque
+                */
+               int GetStackedLine(std::deque<std::string> &result);
+       };
+
        /** irc::tokenstream reads a string formatted as per RFC1459 and RFC2812.
         * It will split the string into 'tokens' each containing one parameter
         * from the string.
@@ -138,9 +234,17 @@ namespace irc
        class tokenstream
        {
         private:
+               /** Original string
+                */
                std::string tokens;
+               /** Last position of a seperator token
+                */
                std::string::iterator last_starting_position;
+               /** Current string position
+                */
                std::string::iterator n;
+               /** True if the last value was an ending value
+                */
                bool last_pushed;
         public:
                /** Create a tokenstream and fill it with the provided data
@@ -154,31 +258,129 @@ namespace irc
                const std::string GetToken();
        };
 
-       /** irc::commasepstream allows for splitting comma seperated lists.
-        * Lists passed to irc::commasepstream should not contain spaces
-        * after the commas, or this will be taken to be part of the item
-        * data. Each successive call to commasepstream::GetToken() returns
+       /** irc::sepstream allows for splitting token seperated lists.
+        * Each successive call to sepstream::GetToken() returns
         * the next token, until none remain, at which point the method returns
         * an empty string.
         */
-       class commasepstream
+       class sepstream : public classbase
        {
         private:
+               /** Original string
+                */
                std::string tokens;
+               /** Last position of a seperator token
+                */
                std::string::iterator last_starting_position;
+               /** Current string position
+                */
                std::string::iterator n;
+               /** Seperator value
+                */
+               char sep;
         public:
-               /** Create a commasepstream and fill it with the provided data
+               /** Create a sepstream and fill it with the provided data
                 */
-               commasepstream(const std::string &source);
-               ~commasepstream();
+               sepstream(const std::string &source, char seperator);
+               virtual ~sepstream();
 
                /** Fetch the next token from the stream
                 * @returns The next token is returned, or an empty string if none remain
                 */
-               const std::string GetToken();
+               virtual const std::string GetToken();
        };
 
+       /** A derived form of sepstream, which seperates on commas
+        */
+       class commasepstream : public sepstream
+       {
+        public:
+               commasepstream(const std::string &source) : sepstream(source, ',')
+               {
+               }
+       };
+
+       /** A derived form of sepstream, which seperates on spaces
+        */
+       class spacesepstream : public sepstream
+       {
+        public:
+               spacesepstream(const std::string &source) : sepstream(source, ' ')
+               {
+               }
+       };
+
+       /** The portparser class seperates out a port range into integers.
+        * A port range may be specified in the input string in the form
+        * "6660,6661,6662-6669,7020". The end of the stream is indicated by
+        * a return value of 0 from portparser::GetToken(). If you attempt
+        * to specify an illegal range (e.g. one where start >= end, or
+        * start or end < 0) then GetToken() will return the first element
+        * of the pair of numbers.
+        */
+       class portparser : public classbase
+       {
+        private:
+               /** Used to split on commas
+                */
+               commasepstream* sep;
+               /** Current position in a range of ports
+                */
+               long in_range;
+               /** Starting port in a range of ports
+                */
+               long range_begin;
+               /** Ending port in a range of ports
+                */
+               long range_end;
+               /** Allow overlapped port ranges
+                */
+               bool overlapped;
+               /** Used to determine overlapping of ports
+                * without O(n) algorithm being used
+                */
+               std::map<long, bool> overlap_set;
+               /** Returns true if val overlaps an existing range
+                */
+               bool Overlaps(long val);
+        public:
+               /** Create a portparser and fill it with the provided data
+                * @param source The source text to parse from
+                * @param allow_overlapped Allow overlapped ranges
+                */
+               portparser(const std::string &source, bool allow_overlapped = true);
+               /** Frees the internal commasepstream object
+                */
+               ~portparser();
+               /** Fetch the next token from the stream
+                * @returns The next port number is returned, or 0 if none remain
+                */
+               long GetToken();
+       };
+
+       typedef std::pair<size_t, unsigned char> bitfield;
+
+       class dynamicbitmask : public classbase
+       {
+        private:
+               unsigned char* bits;
+               unsigned char* freebits;
+               size_t bits_size;
+        public:
+               dynamicbitmask();
+
+               ~dynamicbitmask();
+
+               bitfield Allocate();
+
+               bool Deallocate(bitfield &pos);
+
+               void Toggle(bitfield &pos, bool state);
+
+               bool Get(bitfield &pos);
+
+               size_t GetSize();
+       };
 
        /** The irc_char_traits class is used for RFC-style comparison of strings.
         * This class is used to implement irc::string, a case-insensitive, RFC-