Kea  2.3.5-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 <iomanip>
15 #include <sstream>
16 #include <vector>
17 #include <exceptions/exceptions.h>
18 #include <boost/lexical_cast.hpp>
19 #include <boost/shared_ptr.hpp>
20 
21 namespace isc {
22 namespace util {
23 namespace str {
24 
26 
31 class StringTokenError : public Exception {
32 public:
33  StringTokenError(const char* file, size_t line, const char* what) :
34  isc::Exception(file, line, what) {}
35 };
36 
45 void normalizeSlash(std::string& name);
46 
55 std::string trim(const std::string& instring);
56 
71 template<typename Iterator>
72 Iterator
73 seekTrimmed(Iterator begin, Iterator end, uint8_t trim_val) {
74  for ( ; end != begin && *(end - 1) == trim_val; --end);
75  return(end);
76 }
77 
105 std::vector<std::string> tokens(const std::string& text,
106  const std::string& delim = std::string(" \t\n"),
107  bool escape = false);
108 
119 inline char toUpper(char chr) {
120  return (static_cast<char>(std::toupper(static_cast<int>(chr))));
121 }
122 
128 inline void uppercase(std::string& text) {
129  std::transform(text.begin(), text.end(), text.begin(),
131 }
132 
143 inline char toLower(char chr) {
144  return (static_cast<char>(std::tolower(static_cast<int>(chr))));
145 }
146 
152 inline void lowercase(std::string& text) {
153  std::transform(text.begin(), text.end(), text.begin(),
155 }
156 
167 std::string format(const std::string& format,
168  const std::vector<std::string>& args);
169 
170 
180 std::string getToken(std::istringstream& iss);
181 
203 template <typename NumType, int BitSize>
204 NumType
205 tokenToNum(const std::string& num_token) {
206  NumType num;
207  try {
208  num = boost::lexical_cast<NumType>(num_token);
209  } catch (const boost::bad_lexical_cast&) {
210  isc_throw(StringTokenError, "Invalid SRV numeric parameter: " <<
211  num_token);
212  }
213  if (num < 0 || num >= (static_cast<NumType>(1) << BitSize)) {
214  isc_throw(StringTokenError, "Numeric SRV parameter out of range: " <<
215  num);
216  }
217  return (num);
218 }
219 
237 std::vector<uint8_t>
238 quotedStringToBinary(const std::string& quoted_string);
239 
258 void
259 decodeSeparatedHexString(const std::string& hex_string,
260  const std::string& sep,
261  std::vector<uint8_t>& binary);
262 
268 
273 void
274 decodeColonSeparatedHexString(const std::string& hex_string,
275  std::vector<uint8_t>& binary);
276 
295 void
296 decodeFormattedHexString(const std::string& hex_string,
297  std::vector<uint8_t>& binary);
298 
301 
303 typedef boost::shared_ptr<StringSanitizerImpl> StringSanitizerImplPtr;
304 
312 public:
313 
328  StringSanitizer(const std::string& char_set,
329  const std::string& char_replacement);
330 
334  ~StringSanitizer();
335 
344  std::string scrub(const std::string& original);
345 
351  static const uint32_t MAX_DATA_SIZE;
352 
353 private:
355  StringSanitizerImplPtr impl_;
356 };
357 
359 typedef boost::shared_ptr<StringSanitizer> StringSanitizerPtr;
360 
366 inline bool
367 isPrintable(const std::string& content) {
368  for (const auto& ch : content) {
369  if (isprint(static_cast<int>(ch)) == 0) {
370  return (false);
371  }
372  }
373  return (true);
374 }
375 
381 inline bool
382 isPrintable(const std::vector<uint8_t>& content) {
383  for (const auto& ch : content) {
384  if (isprint(static_cast<int>(ch)) == 0) {
385  return (false);
386  }
387  }
388  return (true);
389 }
390 
391 
397 std::string dumpAsHex(const uint8_t* data, size_t length);
398 
399 } // namespace str
400 } // namespace util
401 } // namespace isc
402 
403 #endif // STRUTIL_H
static const uint32_t MAX_DATA_SIZE
The maximum size for regex parameters.
Definition: strutil.h:351
void lowercase(std::string &text)
Lowercase String.
Definition: strutil.h:152
StringTokenError(const char *file, size_t line, const char *what)
Definition: strutil.h:33
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.
std::string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
Definition: strutil.cc:451
void uppercase(std::string &text)
Uppercase String.
Definition: strutil.h:128
NumType tokenToNum(const std::string &num_token)
Converts a string token to an unsigned integer.
Definition: strutil.h:205
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:143
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:31
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:311
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:300
char toUpper(char chr)
Uppercase Character.
Definition: strutil.h:119
Iterator seekTrimmed(Iterator begin, Iterator end, uint8_t trim_val)
Finds the "trimmed" end of a buffer.
Definition: strutil.h:73
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:359
bool isPrintable(const std::string &content)
Check if a string is printable.
Definition: strutil.h:367
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157