1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
23 #include "helperfuncs.h"
24 #include <ext/hash_map>
26 #define nspace __gnu_cxx
28 // from helperfuncs.cpp
29 extern const char lowermap[255];
31 /******************************************************
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:
48 * Case insensitivity: The hash_map will be case
51 * Scandanavian Comparisons: The characters [, ], \ will
52 * be considered the lowercase of {, } and |.
54 * This file also contains hashing methods for hashing
55 * in_addr structs, we use this if we want to cache IP
58 ******************************************************/
62 size_t nspace::hash<in_addr>::operator()(const struct in_addr &a) const
65 memcpy(&q,&a,sizeof(size_t));
69 size_t nspace::hash<string>::operator()(const string &s) const
72 static struct hash<const char *> strhash;
73 strlcpy(a,s.c_str(),MAXBUF);
78 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
80 unsigned char* n1 = (unsigned char*)s1.c_str();
81 unsigned char* n2 = (unsigned char*)s2.c_str();
82 for (; *n1 && *n2; n1++, n2++)
83 if (lowermap[*n1] != lowermap[*n2])
85 return (lowermap[*n1] == lowermap[*n2]);
88 bool irc::InAddr_HashComp::operator()(const in_addr &s1, const in_addr &s2) const
90 return (s1.s_addr == s1.s_addr);
93 /******************************************************
95 * This is the implementation of our special irc::string
96 * class which is a case-insensitive equivalent to
97 * std::string which is not only case-insensitive but
98 * can also do scandanavian comparisons, e.g. { = [, etc.
100 * This class depends on the global 'lowermap' which is
101 * initialized at startup by inspircd.cpp, and contains
102 * the 'scandanavian' casemappings for fast irc compare.
104 ******************************************************/
106 bool irc::irc_char_traits::eq(char c1st, char c2nd)
108 return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
111 bool irc::irc_char_traits::ne(char c1st, char c2nd)
113 return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
116 bool irc::irc_char_traits::lt(char c1st, char c2nd)
118 return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
121 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
123 for(unsigned int i = 0; i < n; i++)
125 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
128 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
131 if(*str1 == 0 || *str2 == 0)
140 std::string operator+ (std::string& leftval, irc::string& rightval)
142 return leftval + std::string(rightval.c_str());
145 irc::string operator+ (irc::string& leftval, std::string& rightval)
147 return leftval + irc::string(rightval.c_str());
150 bool operator== (std::string& leftval, irc::string& rightval)
152 return (leftval == std::string(rightval.c_str()));
155 bool operator== (irc::string& leftval, std::string& rightval)
157 return (rightval == std::string(leftval.c_str()));
160 const char* irc::irc_char_traits::find(const char* s1, int n, char c)
162 while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
167 /* See hashcomp.h if you care about these... */
168 std::ostream& operator<<(std::ostream &os, const irc::string &str)
170 return os << str.c_str();
173 std::istream& operator>>(std::istream &is, irc::string &str)
181 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
183 last_starting_position = tokens.begin();
187 irc::tokenstream::~tokenstream()
191 const std::string irc::tokenstream::GetToken()
193 std::string::iterator lsp = last_starting_position;
195 while (n != tokens.end())
197 if ((last_pushed) && (*n == ':'))
199 /* If we find a token thats not the first and starts with :,
200 * this is the last token on the line
202 std::string::iterator curr = ++n;
204 return std::string(curr, tokens.end());
209 if ((*n == ' ') || (n+1 == tokens.end()))
211 /* If we find a space, or end of string, this is the end of a token.
213 last_starting_position = n+1;
215 return std::string(lsp, n+1 == tokens.end() ? n+1 : n++);