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 #include "convto.h"
35 
36 /*******************************************************
37  * This file contains classes and templates that deal
38  * with the comparison and hashing of 'irc strings'.
39  * An 'irc string' is a string which compares in a
40  * case insensitive manner, and as per RFC 1459 will
41  * treat [ identical to {, ] identical to }, and \
42  * as identical to |.
43  *
44  * There are functors that accept std::string and
45  * compare/hash them as type irc::string by using
46  * mapping arrays internally.
47  *******************************************************/
48 
55 CoreExport extern unsigned const char *national_case_insensitive_map;
56 
60 CoreExport extern unsigned const char rfc_case_insensitive_map[256];
61 
66 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
67 
70 namespace irc
71 {
79  CoreExport bool equals(const std::string& s1, const std::string& s2);
80 
86  CoreExport size_t find(const std::string& haystack, const std::string& needle);
87 
92  struct StrHashComp
93  {
96  bool operator()(const std::string& s1, const std::string& s2) const
97  {
98  return equals(s1, s2);
99  }
100  };
101 
102  struct insensitive
103  {
104  size_t CoreExport operator()(const std::string &s) const;
105  };
106 
107  struct insensitive_swo
108  {
109  bool CoreExport operator()(const std::string& a, const std::string& b) const;
110  };
111 
117  class CoreExport sepstream
118  {
119  protected:
122  std::string tokens;
125  char sep;
128  size_t pos;
131  bool allow_empty;
132  public:
135  sepstream(const std::string &source, char separator, bool allowempty = false);
136 
141  bool GetToken(std::string& token);
142 
147  template<typename Numeric>
148  bool GetNumericToken(Numeric& token)
149  {
150  std::string str;
151  if (!GetToken(str))
152  return false;
153 
154  token = ConvToNum<Numeric>(str);
155  return true;
156  }
157 
161  const std::string GetRemaining();
162 
166  bool StreamEnd();
167 
172  bool Contains(const std::string& value);
173  };
174 
177  class CoreExport commasepstream : public sepstream
178  {
179  public:
182  commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
183  {
184  }
185  };
186 
189  class CoreExport spacesepstream : public sepstream
190  {
191  public:
194  spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
195  {
196  }
197  };
198 
211  class CoreExport tokenstream
212  {
213  private:
215  std::string message;
216 
218  size_t position;
219 
220  public:
222  tokenstream(const std::string& msg, size_t start = 0, size_t end = std::string::npos);
223 
225  std::string& GetMessage() { return message; }
226 
231  bool GetMiddle(std::string& token);
232 
237  bool GetTrailing(std::string& token);
238  };
239 
248  class CoreExport portparser
249  {
250  private:
251 
254  commasepstream sep;
255 
258  long in_range;
259 
262  long range_begin;
263 
266  long range_end;
267 
270  bool overlapped;
271 
275  std::set<long> overlap_set;
276 
279  bool Overlaps(long val);
280  public:
281 
286  portparser(const std::string &source, bool allow_overlapped = true);
287 
291  long GetToken();
292  };
293 }
irc::StrHashComp::operator()
bool operator()(const std::string &s1, const std::string &s2) const
Definition: hashcomp.h:95
irc::insensitive_swo
Definition: hashcomp.h:106
irc::sepstream
Definition: hashcomp.h:116
irc::equals
bool equals(const std::string &s1, const std::string &s2)
Definition: hashcomp.cpp:120
irc
Definition: hashcomp.h:69
irc::insensitive
Definition: hashcomp.h:101
irc::find
size_t find(const std::string &haystack, const std::string &needle)
Definition: hashcomp.cpp:130