Kea  2.1.7-git
strutil.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef STRUTIL_H
8 #define STRUTIL_H
9 
10 #include <algorithm>
11 #include <cctype>
12 #include <stdint.h>
13 #include <string>
14 #include <sstream>
15 #include <vector>
16 #include <exceptions/exceptions.h>
17 #include <boost/lexical_cast.hpp>
18 #include <boost/shared_ptr.hpp>
19 
20 namespace isc {
21 namespace util {
22 namespace str {
23 
25 
30 class StringTokenError : public Exception {
31 public:
32  StringTokenError(const char* file, size_t line, const char* what) :
33  isc::Exception(file, line, what) {}
34 };
35 
44 void normalizeSlash(std::string& name);
45 
54 std::string trim(const std::string& instring);
55 
70 template<typename Iterator>
71 Iterator
72 seekTrimmed(Iterator begin, Iterator end, uint8_t trim_val) {
73  for ( ; end != begin && *(end - 1) == trim_val; --end);
74  return(end);
75 }
76 
104 std::vector<std::string> tokens(const std::string& text,
105  const std::string& delim = std::string(" \t\n"),
106  bool escape = false);
107 
118 inline char toUpper(char chr) {
119  return (static_cast<char>(std::toupper(static_cast<int>(chr))));
120 }
121 
127 inline void uppercase(std::string& text) {
128  std::transform(text.begin(), text.end(), text.begin(),
130 }
131 
142 inline char toLower(char chr) {
143  return (static_cast<char>(std::tolower(static_cast<int>(chr))));
144 }
145 
151 inline void lowercase(std::string& text) {
152  std::transform(text.begin(), text.end(), text.begin(),
154 }
155 
166 std::string format(const std::string& format,
167  const std::vector<std::string>& args);
168 
169 
179 std::string getToken(std::istringstream& iss);
180 
202 template <typename NumType, int BitSize>
203 NumType
204 tokenToNum(const std::string& num_token) {
205  NumType num;
206  try {
207  num = boost::lexical_cast<NumType>(num_token);
208  } catch (const boost::bad_lexical_cast&) {
209  isc_throw(StringTokenError, "Invalid SRV numeric parameter: " <<
210  num_token);
211  }
212  if (num < 0 || num >= (static_cast<NumType>(1) << BitSize)) {
213  isc_throw(StringTokenError, "Numeric SRV parameter out of range: " <<
214  num);
215  }
216  return (num);
217 }
218 
236 std::vector<uint8_t>
237 quotedStringToBinary(const std::string& quoted_string);
238 
257 void
258 decodeSeparatedHexString(const std::string& hex_string,
259  const std::string& sep,
260  std::vector<uint8_t>& binary);
261 
267 
272 void
273 decodeColonSeparatedHexString(const std::string& hex_string,
274  std::vector<uint8_t>& binary);
275 
294 void
295 decodeFormattedHexString(const std::string& hex_string,
296  std::vector<uint8_t>& binary);
297 
300 
302 typedef boost::shared_ptr<StringSanitizerImpl> StringSanitizerImplPtr;
303 
311 public:
312 
327  StringSanitizer(const std::string& char_set,
328  const std::string& char_replacement);
329 
333  ~StringSanitizer();
334 
343  std::string scrub(const std::string& original);
344 
350  static const uint32_t MAX_DATA_SIZE;
351 
352 private:
354  StringSanitizerImplPtr impl_;
355 };
356 
358 typedef boost::shared_ptr<StringSanitizer> StringSanitizerPtr;
359 
365 inline bool
366 isPrintable(const std::string& content) {
367  for (const auto& ch : content) {
368  if (isprint(static_cast<int>(ch)) == 0) {
369  return (false);
370  }
371  }
372  return (true);
373 }
374 
380 inline bool
381 isPrintable(const std::vector<uint8_t>& content) {
382  for (const auto& ch : content) {
383  if (isprint(static_cast<int>(ch)) == 0) {
384  return (false);
385  }
386  }
387  return (true);
388 }
389 
390 } // namespace str
391 } // namespace util
392 } // namespace isc
393 
394 #endif // STRUTIL_H
static const uint32_t MAX_DATA_SIZE
The maximum size for regex parameters.
Definition: strutil.h:350
void lowercase(std::string &text)
Lowercase String.
Definition: strutil.h:151
StringTokenError(const char *file, size_t line, const char *what)
Definition: strutil.h:32
void decodeSeparatedHexString(const std::string &hex_string, const std::string &sep, std::vector< uint8_t > &binary)
Converts a string of separated hexadecimal digits into a vector.
Definition: strutil.cc:221
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
void decodeFormattedHexString(const std::string &hex_string, std::vector< uint8_t > &binary)
Converts a formatted string of hexadecimal digits into a vector.
Definition: strutil.cc:273
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void uppercase(std::string &text)
Uppercase String.
Definition: strutil.h:127
NumType tokenToNum(const std::string &num_token)
Converts a string token to an unsigned integer.
Definition: strutil.h:204
vector< string > tokens(const std::string &text, const std::string &delim, bool escape)
Split String into Tokens.
Definition: strutil.cc:77
char toLower(char chr)
Lowercase Character.
Definition: strutil.h:142
std::string getToken(std::istringstream &iss)
Returns one token from the given stringstream.
Definition: strutil.cc:186
void normalizeSlash(std::string &name)
Normalize Backslash.
Definition: strutil.cc:41
A Set of C++ Utilities for Manipulating Strings.
Definition: strutil.h:30
std::vector< uint8_t > quotedStringToBinary(const std::string &quoted_string)
Converts a string in quotes into vector.
Definition: strutil.cc:196
Implements a regular expression based string scrubber.
Definition: strutil.h:310
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
void decodeColonSeparatedHexString(const std::string &hex_string, std::vector< uint8_t > &binary)
Converts a string of hexadecimal digits with colons into a vector.
Definition: strutil.cc:215
boost::shared_ptr< StringSanitizerImpl > StringSanitizerImplPtr
Type representing the pointer to the StringSanitizerImpl.
Definition: strutil.h:299
char toUpper(char chr)
Uppercase Character.
Definition: strutil.h:118
Iterator seekTrimmed(Iterator begin, Iterator end, uint8_t trim_val)
Finds the "trimmed" end of a buffer.
Definition: strutil.h:72
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
boost::shared_ptr< StringSanitizer > StringSanitizerPtr
Type representing the pointer to the StringSanitizer.
Definition: strutil.h:358
bool isPrintable(const std::string &content)
Check if a string is printable.
Definition: strutil.h:366
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157