]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/hashcomp.cpp
278ea0044e41cca8cbdd05be2d905f53cf47c93e
[user/henk/code/inspircd.git] / src / hashcomp.cpp
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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include <string>
22 #include "hashcomp.h"
23 #include "helperfuncs.h"
24 #include <ext/hash_map>
25
26 #define nspace __gnu_cxx
27
28 // from helperfuncs.cpp
29 extern const char lowermap[255];
30
31 /******************************************************
32  *
33  * The hash functions of InspIRCd are the centrepoint
34  * of the entire system. If these functions are
35  * inefficient or wasteful, the whole program suffers
36  * as a result. A lot of C programmers in the ircd
37  * scene spend a lot of time debating (arguing) about
38  * the best way to write hash functions to hash irc
39  * nicknames, channels etc.
40  * We are lucky as C++ developers as hash_map does
41  * a lot of this for us. It does intellegent memory
42  * requests, bucketing, search functions, insertion
43  * and deletion etc. All we have to do is write some
44  * overloaded comparison and hash value operators which
45  * cause it to act in an irc-like way. The features we
46  * add to the standard hash_map are:
47  *
48  * Case insensitivity: The hash_map will be case
49  * insensitive.
50  *
51  * Scandanavian Comparisons: The characters [, ], \ will
52  * be considered the lowercase of {, } and |.
53  *
54  * This file also contains hashing methods for hashing
55  * insp_inaddr structs, we use this if we want to cache IP
56  * addresses.
57  *
58  ******************************************************/
59
60 using namespace std;
61 using namespace irc::sockets;
62
63 size_t nspace::hash<insp_inaddr>::operator()(const insp_inaddr &a) const
64 {
65         size_t q;
66         memcpy(&q,&a,sizeof(size_t));
67         return q;
68 }
69
70 size_t nspace::hash<string>::operator()(const string &s) const
71 {
72         char a[MAXBUF];
73         static struct hash<const char *> strhash;
74         strlcpy(a,s.c_str(),MAXBUF);
75         strlower(a);
76         return strhash(a);
77 }
78
79 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
80 {
81         unsigned char* n1 = (unsigned char*)s1.c_str();
82         unsigned char* n2 = (unsigned char*)s2.c_str();
83         for (; *n1 && *n2; n1++, n2++)
84                 if (lowermap[*n1] != lowermap[*n2])
85                         return false;
86         return (lowermap[*n1] == lowermap[*n2]);
87 }
88
89 bool irc::InAddr_HashComp::operator()(const insp_inaddr &s1, const insp_inaddr &s2) const
90 {
91 #ifdef IPV6
92         for (int n = 0; n < 16; n++)
93                 if (s2.s6_addr[n] != s1.s6_addr[n])
94                         return false;
95         return true;
96 #else
97         return (s1.s_addr == s1.s_addr);
98 #endif
99 }
100
101 /******************************************************
102  *
103  * This is the implementation of our special irc::string
104  * class which is a case-insensitive equivalent to
105  * std::string which is not only case-insensitive but
106  * can also do scandanavian comparisons, e.g. { = [, etc.
107  *
108  * This class depends on the global 'lowermap' which is
109  * initialized at startup by inspircd.cpp, and contains
110  * the 'scandanavian' casemappings for fast irc compare.
111  *
112  ******************************************************/
113
114 bool irc::irc_char_traits::eq(char c1st, char c2nd)
115 {
116         return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
117 }
118
119 bool irc::irc_char_traits::ne(char c1st, char c2nd)
120 {
121         return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
122 }
123
124 bool irc::irc_char_traits::lt(char c1st, char c2nd)
125 {
126         return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
127 }
128
129 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
130 {
131         for(unsigned int i = 0; i < n; i++)
132         {
133                 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
134                         return 1;
135
136                 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
137                         return -1;
138
139                 if(*str1 == 0 || *str2 == 0)
140                         return 0;
141
142                 str1++;
143                 str2++;
144         }
145         return 0;
146 }
147
148 std::string operator+ (std::string& leftval, irc::string& rightval)
149 {
150         return leftval + std::string(rightval.c_str());
151 }
152
153 irc::string operator+ (irc::string& leftval, std::string& rightval)
154 {
155         return leftval + irc::string(rightval.c_str());
156 }
157
158 bool operator== (std::string& leftval, irc::string& rightval)
159 {
160         return (leftval == std::string(rightval.c_str()));
161 }
162
163 bool operator== (irc::string& leftval, std::string& rightval)
164 {
165         return (rightval == std::string(leftval.c_str()));
166 }
167
168 const char* irc::irc_char_traits::find(const char* s1, int  n, char c)
169 {
170         while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
171                 s1++;
172         return s1;
173 }
174
175 /* See hashcomp.h if you care about these... */
176 std::ostream& operator<<(std::ostream &os, const irc::string &str)
177 {
178         return os << str.c_str();
179 }
180
181 std::istream& operator>>(std::istream &is, irc::string &str)
182 {
183         std::string tmp;
184         is >> tmp;
185         str = tmp.c_str();
186         return is;
187 }
188
189 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
190 {
191         /* Remove trailing spaces, these muck up token parsing */
192         while (tokens.find_last_of(' ') == tokens.length() - 1)
193                 tokens.erase(tokens.end() - 1);
194
195         /* Record starting position and current position */
196         last_starting_position = tokens.begin();
197         n = tokens.begin();
198 }
199
200 irc::tokenstream::~tokenstream()
201 {
202 }
203
204 const std::string irc::tokenstream::GetToken()
205 {
206         std::string::iterator lsp = last_starting_position;
207
208         while (n != tokens.end())
209         {
210                 if ((last_pushed) && (*n == ':'))
211                 {
212                         /* If we find a token thats not the first and starts with :,
213                          * this is the last token on the line
214                          */
215                         std::string::iterator curr = ++n;
216                         n = tokens.end();
217                         return std::string(curr, tokens.end());
218                 }
219
220                 last_pushed = false;
221
222                 if ((*n == ' ') || (n+1 == tokens.end()))
223                 {
224                         /* If we find a space, or end of string, this is the end of a token.
225                          */
226                         last_starting_position = n+1;
227                         last_pushed = true;
228                         return std::string(lsp, n+1 == tokens.end() ? n+1  : n++);
229                 }
230
231                 n++;
232         }
233         return "";
234 }
235
236 irc::commasepstream::commasepstream(const std::string &source) : tokens(source)
237 {
238         last_starting_position = tokens.begin();
239         n = tokens.begin();
240 }
241
242 const std::string irc::commasepstream::GetToken()
243 {
244         std::string::iterator lsp = last_starting_position;
245
246         while (n != tokens.end())
247         {
248                 if ((*n == ',') || (n+1 == tokens.end()))
249                 {
250                         last_starting_position = n+1;
251                         return std::string(lsp, n+1 == tokens.end() ? n+1  : n++);
252                 }
253
254                 n++;
255         }
256
257         return "";
258 }
259
260 irc::commasepstream::~commasepstream()
261 {
262 }