]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/hashcomp.h
f6c9368d6263768bf02327182641167c3652ee46
[user/henk/code/inspircd.git] / include / hashcomp.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef _HASHCOMP_H_
18 #define _HASHCOMP_H_
19
20 #include "inspircd_config.h"
21 #include "socket.h"
22 #include "hash_map.h"
23
24 /*******************************************************
25  * This file contains classes and templates that deal
26  * with the comparison and hashing of 'irc strings'.
27  * An 'irc string' is a string which compares in a
28  * case insensitive manner, and as per RFC 1459 will
29  * treat [ identical to {, ] identical to }, and \
30  * as identical to |.
31  *
32  * Our hashing functions are designed  to accept
33  * std::string and compare/hash them as type irc::string
34  * by converting them internally. This makes them
35  * backwards compatible with other code which is not
36  * aware of irc::string.
37  *******************************************************/
38  
39 using namespace std;
40
41 namespace nspace
42 {
43         template<> struct hash<in_addr>
44         {
45                 size_t operator()(const struct in_addr &a) const;
46         };
47
48         template<> struct hash<std::string>
49         {
50                 size_t operator()(const string &s) const;
51         };
52 }
53
54 /** The irc namespace contains a number of helper classes.
55  */
56 namespace irc
57 {
58
59         /** This class returns true if two strings match.
60          * Case sensitivity is ignored, and the RFC 'character set'
61          * is adhered to
62          */
63         struct StrHashComp
64         {
65                 /** The operator () does the actual comparison in hash_map
66                  */
67                 bool operator()(const std::string& s1, const std::string& s2) const;
68         };
69
70
71         /** This class returns true if two in_addr structs match.
72          * Checking is done by copying both into a size_t then doing a
73          * numeric comparison of the two.
74          */
75         struct InAddr_HashComp
76         {
77                 /** The operator () does the actual comparison in hash_map
78                  */
79                 bool operator()(const in_addr &s1, const in_addr &s2) const;
80         };
81
82         class tokenstream
83         {
84          private:
85                 std::string tokens;
86                 std::string::iterator last_starting_position;
87                 std::string::iterator n;
88                 bool last_pushed;
89          public:
90                 tokenstream(const std::string &source);
91                 ~tokenstream();
92
93                 const std::string GetToken();
94         };
95
96         class commasepstream
97         {
98          private:
99                 std::string tokens;
100                 std::string::iterator last_starting_position;
101                 std::string::iterator n;
102          public:
103                 commasepstream(const std::string &source);
104                 ~commasepstream();
105
106                 const std::string GetToken();
107         };
108
109
110         /** The irc_char_traits class is used for RFC-style comparison of strings.
111          * This class is used to implement irc::string, a case-insensitive, RFC-
112          * comparing string class.
113          */
114         struct irc_char_traits : std::char_traits<char> {
115
116                 /** Check if two chars match
117                  */
118                 static bool eq(char c1st, char c2nd);
119
120                 /** Check if two chars do NOT match
121                  */
122                 static bool ne(char c1st, char c2nd);
123
124                 /** Check if one char is less than another
125                  */
126                 static bool lt(char c1st, char c2nd);
127
128                 /** Compare two strings of size n
129                  */
130                 static int compare(const char* str1, const char* str2, size_t n);
131
132                 /** Find a char within a string up to position n
133                  */
134                 static const char* find(const char* s1, int  n, char c);
135         };
136
137         /** This typedef declares irc::string based upon irc_char_traits
138          */
139         typedef basic_string<char, irc_char_traits, allocator<char> > string;
140 }
141
142 /* Define operators for using >> and << with irc::string to an ostream on an istream. */
143 /* This was endless fun. No. Really. */
144 /* It was also the first core change Ommeh made, if anyone cares */
145
146 std::ostream& operator<<(std::ostream &os, const irc::string &str);
147 std::istream& operator>>(std::istream &is, irc::string &str);
148
149 /* Define operators for + and == with irc::string to std::string for easy assignment
150  * and comparison - Brain
151  */
152
153 std::string operator+ (std::string& leftval, irc::string& rightval);
154 irc::string operator+ (irc::string& leftval, std::string& rightval);
155 bool operator== (std::string& leftval, irc::string& rightval);
156 bool operator== (irc::string& leftval, std::string& rightval);
157
158 #endif