InspIRCd  3.0
hashcomp.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2011 Adam <[email protected]>
5  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
6  * Copyright (C) 2007, 2009 Dennis Friis <[email protected]>
7  * Copyright (C) 2005-2009 Craig Edwards <[email protected]>
8  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
9  * Copyright (C) 2008 Pippijn van Steenhoven <[email protected]>
10  *
11  * This file is part of InspIRCd. InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 
25 #pragma once
26 
27 #include <cstring>
28 #include <string>
29 #include <vector>
30 #include <deque>
31 #include <map>
32 #include <set>
33 #include "inspircd.h"
34 
35 /*******************************************************
36  * This file contains classes and templates that deal
37  * with the comparison and hashing of 'irc strings'.
38  * An 'irc string' is a string which compares in a
39  * case insensitive manner, and as per RFC 1459 will
40  * treat [ identical to {, ] identical to }, and \
41  * as identical to |.
42  *
43  * There are functors that accept std::string and
44  * compare/hash them as type irc::string by using
45  * mapping arrays internally.
46  *******************************************************/
47 
54 CoreExport extern unsigned const char *national_case_insensitive_map;
55 
59 CoreExport extern unsigned const char rfc_case_insensitive_map[256];
60 
65 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
66 
69 namespace irc
70 {
78  CoreExport bool equals(const std::string& s1, const std::string& s2);
79 
85  CoreExport size_t find(const std::string& haystack, const std::string& needle);
86 
91  struct StrHashComp
92  {
95  bool operator()(const std::string& s1, const std::string& s2) const
96  {
97  return equals(s1, s2);
98  }
99  };
100 
101  struct insensitive
102  {
103  size_t CoreExport operator()(const std::string &s) const;
104  };
105 
106  struct insensitive_swo
107  {
108  bool CoreExport operator()(const std::string& a, const std::string& b) const;
109  };
110 
116  class CoreExport sepstream
117  {
118  protected:
121  std::string tokens;
124  char sep;
127  size_t pos;
130  bool allow_empty;
131  public:
134  sepstream(const std::string &source, char separator, bool allowempty = false);
135 
140  bool GetToken(std::string& token);
141 
145  const std::string GetRemaining();
146 
150  bool StreamEnd();
151 
156  bool Contains(const std::string& value);
157  };
158 
161  class CoreExport commasepstream : public sepstream
162  {
163  public:
166  commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
167  {
168  }
169  };
170 
173  class CoreExport spacesepstream : public sepstream
174  {
175  public:
178  spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
179  {
180  }
181  };
182 
195  class CoreExport tokenstream
196  {
197  private:
199  std::string message;
200 
202  size_t position;
203 
204  public:
206  tokenstream(const std::string& msg, size_t start = 0, size_t end = std::string::npos);
207 
209  std::string& GetMessage() { return message; }
210 
215  bool GetMiddle(std::string& token);
216 
221  bool GetTrailing(std::string& token);
222  };
223 
232  class CoreExport portparser
233  {
234  private:
235 
238  commasepstream sep;
239 
242  long in_range;
243 
246  long range_begin;
247 
250  long range_end;
251 
254  bool overlapped;
255 
259  std::set<long> overlap_set;
260 
263  bool Overlaps(long val);
264  public:
265 
270  portparser(const std::string &source, bool allow_overlapped = true);
271 
275  long GetToken();
276  };
277 }
irc::StrHashComp::operator()
bool operator()(const std::string &s1, const std::string &s2) const
Definition: hashcomp.h:94
irc::insensitive_swo
Definition: hashcomp.h:105
irc::sepstream
Definition: hashcomp.h:115
irc::equals
bool equals(const std::string &s1, const std::string &s2)
Definition: hashcomp.cpp:120
irc
Definition: hashcomp.h:68
irc::insensitive
Definition: hashcomp.h:100
irc::find
size_t find(const std::string &haystack, const std::string &needle)
Definition: hashcomp.cpp:130