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