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 
84  struct StrHashComp
85  {
88  bool operator()(const std::string& s1, const std::string& s2) const
89  {
90  return equals(s1, s2);
91  }
92  };
93 
94  struct insensitive
95  {
96  size_t CoreExport operator()(const std::string &s) const;
97  };
98 
100  {
101  bool CoreExport operator()(const std::string& a, const std::string& b) const;
102  };
103 
108  struct CoreExport irc_char_traits : public std::char_traits<char>
109  {
115  static bool eq(char c1st, char c2nd);
116 
122  static bool ne(char c1st, char c2nd);
123 
129  static bool lt(char c1st, char c2nd);
130 
138  static int compare(const char* str1, const char* str2, size_t n);
139 
146  static const char* find(const char* s1, int n, char c);
147  };
148 
151  typedef std::basic_string<char, irc_char_traits, std::allocator<char> > string;
152 
158  std::string CoreExport stringjoiner(const std::vector<std::string>& sequence, char separator = ' ');
159 
165  class CoreExport sepstream
166  {
167  protected:
170  std::string tokens;
173  char sep;
176  size_t pos;
180  public:
183  sepstream(const std::string &source, char separator, bool allowempty = false);
184 
189  bool GetToken(std::string& token);
190 
194  const std::string GetRemaining();
195 
199  bool StreamEnd();
200  };
201 
204  class CoreExport commasepstream : public sepstream
205  {
206  public:
209  commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
210  {
211  }
212  };
213 
216  class CoreExport spacesepstream : public sepstream
217  {
218  public:
221  spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
222  {
223  }
224  };
225 
238  class CoreExport tokenstream : private spacesepstream
239  {
240  public:
243  tokenstream(const std::string &source);
244 
249  bool GetToken(std::string &token);
250 
255  bool GetToken(int &token);
256 
261  bool GetToken(long &token);
262  };
263 
272  class CoreExport portparser
273  {
274  private:
275 
278  commasepstream sep;
279 
282  long in_range;
283 
286  long range_begin;
287 
290  long range_end;
291 
294  bool overlapped;
295 
299  std::set<long> overlap_set;
300 
303  bool Overlaps(long val);
304  public:
305 
310  portparser(const std::string &source, bool allow_overlapped = true);
311 
315  long GetToken();
316  };
317 }
bool allow_empty
Definition: hashcomp.h:179
bool operator()(const std::string &s1, const std::string &s2) const
Definition: hashcomp.h:88
Definition: hashcomp.h:69
Definition: hashcomp.h:94
Definition: hashcomp.h:99
bool equals(const std::string &s1, const std::string &s2)
Definition: hashcomp.cpp:121
std::string stringjoiner(const std::vector< std::string > &sequence, char separator=' ')
Definition: hashcomp.cpp:302
Definition: hashcomp.h:238
std::basic_string< char, irc_char_traits, std::allocator< char > > string
Definition: hashcomp.h:151
commasepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:209
Definition: hashcomp.h:165
Definition: hashcomp.h:216
std::string tokens
Definition: hashcomp.h:170
char sep
Definition: hashcomp.h:173
Definition: hashcomp.h:84
Definition: hashcomp.h:272
spacesepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:221
size_t pos
Definition: hashcomp.h:176
Definition: hashcomp.h:108
Definition: hashcomp.h:204