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