]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/hashcomp.h
a9bf15ab9f091755dc0262acd2a6d2644b95f2f2
[user/henk/code/inspircd.git] / include / hashcomp.h
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 #ifndef _HASHCOMP_H_
18 #define _HASHCOMP_H_
19
20 #include "inspircd_config.h"
21
22 /*******************************************************
23  * This file contains classes and templates that deal
24  * with the comparison and hashing of 'irc strings'.
25  * An 'irc string' is a string which compares in a
26  * case insensitive manner, and as per RFC 1459 will
27  * treat [ identical to {, ] identical to }, and \
28  * as identical to |.
29  *
30  * Our hashing functions are designed  to accept
31  * std::string and compare/hash them as type irc::string
32  * by converting them internally. This makes them
33  * backwards compatible with other code which is not
34  * aware of irc::string.
35  *******************************************************/
36
37 #ifdef GCC3
38 #include <ext/hash_map>
39 #else
40 #include <hash_map>
41 #endif
42
43 #ifdef GCC3
44 #define nspace __gnu_cxx
45 #else
46 #define nspace std
47 #endif
48
49 using namespace std;
50
51 namespace nspace
52 {
53 #ifdef GCC34
54         template<> struct hash<in_addr>
55 #else
56         template<> struct nspace::hash<in_addr>
57 #endif
58         {
59                 size_t operator()(const struct in_addr &a) const;
60         };
61 #ifdef GCC34
62         template<> struct hash<string>
63 #else
64         template<> struct nspace::hash<string>
65 #endif
66         {
67                 size_t operator()(const string &s) const;
68         };
69 }
70
71 /** The irc namespace contains a number of helper classes.
72  */
73 namespace irc
74 {
75
76         /** This class returns true if two strings match.
77          * Case sensitivity is ignored, and the RFC 'character set'
78          * is adhered to
79          */
80         struct StrHashComp
81         {
82                 /** The operator () does the actual comparison in hash_map
83                  */
84                 bool operator()(const std::string& s1, const std::string& s2) const;
85         };
86
87
88         /** This class returns true if two in_addr structs match.
89          * Checking is done by copying both into a size_t then doing a
90          * numeric comparison of the two.
91          */
92         struct InAddr_HashComp
93         {
94                 /** The operator () does the actual comparison in hash_map
95                  */
96                 bool operator()(const in_addr &s1, const in_addr &s2) const;
97         };
98
99
100         /** The irc_char_traits class is used for RFC-style comparison of strings.
101          * This class is used to implement irc::string, a case-insensitive, RFC-
102          * comparing string class.
103          */
104         struct irc_char_traits : std::char_traits<char> {
105
106                 /** Check if two chars match
107                  */
108                 static bool eq(char c1st, char c2nd);
109
110                 /** Check if two chars do NOT match
111                  */
112                 static bool ne(char c1st, char c2nd);
113
114                 /** Check if one char is less than another
115                  */
116                 static bool lt(char c1st, char c2nd);
117
118                 /** Compare two strings of size n
119                  */
120                 static int compare(const char* str1, const char* str2, size_t n);
121
122                 /** Find a char within a string up to position n
123                  */
124                 static const char* find(const char* s1, int  n, char c);
125         };
126
127         /** This typedef declares irc::string based upon irc_char_traits
128          */
129         typedef basic_string<char, irc_char_traits, allocator<char> > string;
130 }
131
132 #endif