InspIRCd  3.0
hashcomp.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 linuxdaemon <[email protected]>
5  * Copyright (C) 2013, 2018-2019 Sadie Powell <[email protected]>
6  * Copyright (C) 2013 Adam <[email protected]>
7  * Copyright (C) 2012-2013, 2016 Attila Molnar <[email protected]>
8  * Copyright (C) 2012 Robby <[email protected]>
9  * Copyright (C) 2009 Uli Schlachter <[email protected]>
10  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
11  * Copyright (C) 2008-2009 Robin Burchell <[email protected]>
12  * Copyright (C) 2007 Dennis Friis <[email protected]>
13  * Copyright (C) 2005-2010 Craig Edwards <[email protected]>
14  *
15  * This file is part of InspIRCd. InspIRCd is free software: you can
16  * redistribute it and/or modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation, version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 
29 #pragma once
30 
31 #include "inspircd.h"
32 #include "convto.h"
33 
34 /*******************************************************
35  * This file contains classes and templates that deal
36  * with the comparison and hashing of 'irc strings'.
37  * An 'irc string' is a string which compares in a
38  * case insensitive manner, and as per RFC 1459 will
39  * treat [ identical to {, ] identical to }, and \
40  * as identical to |.
41  *
42  * There are functors that accept std::string and
43  * compare/hash them as type irc::string by using
44  * mapping arrays internally.
45  *******************************************************/
46 
53 CoreExport extern unsigned const char *national_case_insensitive_map;
54 
58 CoreExport extern unsigned const char rfc_case_insensitive_map[256];
59 
64 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
65 
68 namespace irc
69 {
77  CoreExport bool equals(const std::string& s1, const std::string& s2);
78 
84  CoreExport size_t find(const std::string& haystack, const std::string& needle);
85 
90  struct StrHashComp
91  {
94  bool operator()(const std::string& s1, const std::string& s2) const
95  {
96  return equals(s1, s2);
97  }
98  };
99 
100  struct insensitive
101  {
102  size_t CoreExport operator()(const std::string &s) const;
103  };
104 
106  {
107  bool CoreExport operator()(const std::string& a, const std::string& b) const;
108  };
109 
115  class CoreExport sepstream
116  {
117  protected:
120  std::string tokens;
123  char sep;
126  size_t pos;
130  public:
133  sepstream(const std::string &source, char separator, bool allowempty = false);
134 
139  bool GetToken(std::string& token);
140 
145  template<typename Numeric>
146  bool GetNumericToken(Numeric& token)
147  {
148  std::string str;
149  if (!GetToken(str))
150  return false;
151 
152  token = ConvToNum<Numeric>(str);
153  return true;
154  }
155 
159  const std::string GetRemaining();
160 
164  bool StreamEnd();
165 
170  bool Contains(const std::string& value);
171  };
172 
175  class CoreExport commasepstream : public sepstream
176  {
177  public:
180  commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
181  {
182  }
183  };
184 
187  class CoreExport spacesepstream : public sepstream
188  {
189  public:
192  spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
193  {
194  }
195  };
196 
209  class CoreExport tokenstream
210  {
211  private:
213  std::string message;
214 
216  size_t position;
217 
218  public:
220  tokenstream(const std::string& msg, size_t start = 0, size_t end = std::string::npos);
221 
223  std::string& GetMessage() { return message; }
224 
229  bool GetMiddle(std::string& token);
230 
235  bool GetTrailing(std::string& token);
236  };
237 
246  class CoreExport portparser
247  {
248  private:
249 
252  commasepstream sep;
253 
256  long in_range;
257 
260  long range_begin;
261 
264  long range_end;
265 
268  bool overlapped;
269 
273  std::set<long> overlap_set;
274 
277  bool Overlaps(long val);
278  public:
279 
284  portparser(const std::string &source, bool allow_overlapped = true);
285 
289  long GetToken();
290  };
291 }
Definition: hashcomp.h:176
commasepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:180
Definition: hashcomp.h:247
Definition: hashcomp.h:116
bool GetNumericToken(Numeric &token)
Definition: hashcomp.h:146
bool allow_empty
Definition: hashcomp.h:129
char sep
Definition: hashcomp.h:123
std::string tokens
Definition: hashcomp.h:120
size_t pos
Definition: hashcomp.h:126
Definition: hashcomp.h:188
spacesepstream(const std::string &source, bool allowempty=false)
Definition: hashcomp.h:192
Definition: hashcomp.h:210
std::string & GetMessage()
Definition: hashcomp.h:223
Definition: hashcomp.h:69
bool equals(const std::string &s1, const std::string &s2)
Definition: hashcomp.cpp:127
size_t find(const std::string &haystack, const std::string &needle)
Definition: hashcomp.cpp:137
Definition: hashcomp.h:91
bool operator()(const std::string &s1, const std::string &s2) const
Definition: hashcomp.h:94
Definition: hashcomp.h:106
Definition: hashcomp.h:101