]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/hashcomp.cpp
More tweaks
[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 #ifdef GCC3
25 #include <ext/hash_map>
26 #else
27 #include <hash_map>
28 #endif
29
30 #ifdef GCC3
31 #define nspace __gnu_cxx
32 #else
33 #define nspace std
34 #endif
35
36 // from helperfuncs.cpp
37 extern const char lowermap[255];
38
39 /******************************************************
40  *
41  * The hash functions of InspIRCd are the centrepoint
42  * of the entire system. If these functions are
43  * inefficient or wasteful, the whole program suffers
44  * as a result. A lot of C programmers in the ircd
45  * scene spend a lot of time debating (arguing) about
46  * the best way to write hash functions to hash irc
47  * nicknames, channels etc.
48  * We are lucky as C++ developers as hash_map does
49  * a lot of this for us. It does intellegent memory
50  * requests, bucketing, search functions, insertion
51  * and deletion etc. All we have to do is write some
52  * overloaded comparison and hash value operators which
53  * cause it to act in an irc-like way. The features we
54  * add to the standard hash_map are:
55  *
56  * Case insensitivity: The hash_map will be case
57  * insensitive.
58  *
59  * Scandanavian Comparisons: The characters [, ], \ will
60  * be considered the lowercase of {, } and |.
61  *
62  * This file also contains hashing methods for hashing
63  * in_addr structs, we use this if we want to cache IP
64  * addresses.
65  *
66  ******************************************************/
67
68 using namespace std;
69
70 size_t nspace::hash<in_addr>::operator()(const struct in_addr &a) const
71 {
72         size_t q;
73         memcpy(&q,&a,sizeof(size_t));
74         return q;
75 }
76
77 size_t nspace::hash<string>::operator()(const string &s) const
78 {
79         char a[MAXBUF];
80         static struct hash<const char *> strhash;
81         strlcpy(a,s.c_str(),MAXBUF);
82         strlower(a);
83         return strhash(a);
84 }
85
86 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
87 {
88         irc::string a = s1.c_str();
89         irc::string b = s2.c_str();
90         return (a == b);
91 }
92
93 bool irc::InAddr_HashComp::operator()(const in_addr &s1, const in_addr &s2) const
94 {
95         return (s1.s_addr == s1.s_addr);
96 }
97
98 /******************************************************
99  *
100  * This is the implementation of our special irc::string
101  * class which is a case-insensitive equivalent to
102  * std::string which is not only case-insensitive but
103  * can also do scandanavian comparisons, e.g. { = [, etc.
104  *
105  * This class depends on the global 'lowermap' which is
106  * initialized at startup by inspircd.cpp, and contains
107  * the 'scandanavian' casemappings for fast irc compare.
108  *
109  ******************************************************/
110
111 bool irc::irc_char_traits::eq(char c1st, char c2nd)
112 {
113         return lowermap[(unsigned)c1st] == lowermap[(unsigned)c2nd];
114 }
115
116 bool irc::irc_char_traits::ne(char c1st, char c2nd)
117 {
118         return lowermap[(unsigned)c1st] != lowermap[(unsigned)c2nd];
119 }
120
121 bool irc::irc_char_traits::lt(char c1st, char c2nd)
122 {
123         return lowermap[(unsigned)c1st] < lowermap[(unsigned)c2nd];
124 }
125
126 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
127 {
128         for(unsigned int i = 0; i < n; i++)
129         {
130                 if(lowermap[(unsigned)*str1] > lowermap[(unsigned)*str2])
131                         return 1;
132
133                 if(lowermap[(unsigned)*str1] < lowermap[(unsigned)*str2])
134                         return -1;
135
136                 if(*str1 == 0 || *str2 == 0)
137                         return 0;
138
139                 str1++;
140                 str2++;
141         }
142         return 0;
143 }
144
145 std::string operator+ (std::string& leftval, irc::string& rightval)
146 {
147         return leftval + std::string(rightval.c_str());
148 }
149
150 irc::string operator+ (irc::string& leftval, std::string& rightval)
151 {
152         return leftval + irc::string(rightval.c_str());
153 }
154
155 bool operator== (std::string& leftval, irc::string& rightval)
156 {
157         return (leftval == std::string(rightval.c_str()));
158 }
159
160 bool operator== (irc::string& leftval, std::string& rightval)
161 {
162         return (rightval == irc::string(leftval.c_str()));
163 }
164
165 const char* irc::irc_char_traits::find(const char* s1, int  n, char c)
166 {
167         while(n-- > 0 && lowermap[(unsigned)*s1] != lowermap[(unsigned)c])
168                 s1++;
169         return s1;
170 }
171
172 /* See hashcomp.h if you care about these... */
173 std::ostream& operator<<(std::ostream &os, const irc::string &str)
174 {
175         return os << str.c_str();
176 }
177
178 std::istream& operator>>(std::istream &is, irc::string &str)
179 {
180         std::string tmp;
181         is >> tmp;
182         str = tmp.c_str();
183         return is;
184 }