Kea  1.9.9-git
strutil.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2021 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 
235 std::vector<uint8_t>
236 quotedStringToBinary(const std::string& quoted_string);
237 
255 void
256 decodeSeparatedHexString(const std::string& hex_string,
257  const std::string& sep,
258  std::vector<uint8_t>& binary);
259 
265 
269 void
270 decodeColonSeparatedHexString(const std::string& hex_string,
271  std::vector<uint8_t>& binary);
272 
290 void
291 decodeFormattedHexString(const std::string& hex_string,
292  std::vector<uint8_t>& binary);
293 
295 class StringSanitizerImpl;
296 
304 public:
305 
319  StringSanitizer(const std::string& char_set,
320  const std::string& char_replacement);
321 
326 
334  std::string scrub(const std::string& original);
335 
341  static const uint32_t MAX_DATA_SIZE;
342 
343 private:
345  StringSanitizerImpl* impl_;
346 };
347 
348 typedef boost::shared_ptr<StringSanitizer> StringSanitizerPtr;
349 
354 inline bool
355 isPrintable(const std::string& content) {
356  for (const auto& ch : content) {
357  if (isprint(static_cast<int>(ch)) == 0) {
358  return (false);
359  }
360  }
361  return (true);
362 }
363 
368 inline bool
369 isPrintable(const std::vector<uint8_t>& content) {
370  for (const auto& ch : content) {
371  if (isprint(static_cast<int>(ch)) == 0) {
372  return (false);
373  }
374  }
375  return (true);
376 }
377 
378 } // namespace str
379 } // namespace util
380 } // namespace isc
381 
382 #endif // STRUTIL_H
static const uint32_t MAX_DATA_SIZE
The maximum size for regex parameters.
Definition: strutil.h:341
~StringSanitizer()
Destructor.
Definition: strutil.cc:442
void lowercase(std::string &text)
Lowercase String.
Definition: strutil.h:151
StringSanitizer(const std::string &char_set, const std::string &char_replacement)
Constructor.
Definition: strutil.cc:437
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
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
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
vector< string > tokens(const std::string &text, const std::string &delim, bool escape)
Split String into Tokens.
Definition: strutil.cc:77
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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:303
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
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
std::string scrub(const std::string &original)
Returns a scrubbed copy of a given string.
Definition: strutil.cc:447
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
Definition: strutil.h:348
bool isPrintable(const std::string &content)
Check if a string is printable.
Definition: strutil.h:355
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157