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