Kea  1.5.0
strutil.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2018 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 
46 
55 std::string trim(const std::string& instring);
56 
57 
85 std::vector<std::string> tokens(const std::string& text,
86  const std::string& delim = std::string(" \t\n"),
87  bool escape = false);
88 
89 
100 inline char toUpper(char chr) {
101  return (static_cast<char>(std::toupper(static_cast<int>(chr))));
102 }
103 
104 
110 inline void uppercase(std::string& text) {
111  std::transform(text.begin(), text.end(), text.begin(),
113 }
114 
125 inline char toLower(char chr) {
126  return (static_cast<char>(std::tolower(static_cast<int>(chr))));
127 }
128 
134 inline void lowercase(std::string& text) {
135  std::transform(text.begin(), text.end(), text.begin(),
137 }
138 
139 
150 std::string format(const std::string& format,
151  const std::vector<std::string>& args);
152 
153 
163 std::string getToken(std::istringstream& iss);
164 
186 template <typename NumType, int BitSize>
187 NumType
188 tokenToNum(const std::string& num_token) {
189  NumType num;
190  try {
191  num = boost::lexical_cast<NumType>(num_token);
192  } catch (const boost::bad_lexical_cast&) {
193  isc_throw(StringTokenError, "Invalid SRV numeric parameter: " <<
194  num_token);
195  }
196  if (num < 0 || num >= (static_cast<NumType>(1) << BitSize)) {
197  isc_throw(StringTokenError, "Numeric SRV parameter out of range: " <<
198  num);
199  }
200  return (num);
201 }
202 
219 std::vector<uint8_t>
220 quotedStringToBinary(const std::string& quoted_string);
221 
236 void
237 decodeColonSeparatedHexString(const std::string& hex_string,
238  std::vector<uint8_t>& binary);
239 
255 void
256 decodeFormattedHexString(const std::string& hex_string,
257  std::vector<uint8_t>& binary);
258 
260 class StringSanitizerImpl;
261 
269 public:
270 
284  StringSanitizer(const std::string& char_set,
285  const std::string& char_replacement);
286 
291 
299  std::string scrub(const std::string& original);
300 private:
302  StringSanitizerImpl* impl_;
303 };
304 
305 typedef boost::shared_ptr<StringSanitizer> StringSanitizerPtr;
306 
307 } // namespace str
308 } // namespace util
309 } // namespace isc
310 
311 #endif // STRUTIL_H
void lowercase(std::string &text)
Lowercase String.
Definition: strutil.h:134
StringSanitizer(const std::string &char_set, const std::string &char_replacement)
Constructor.
Definition: strutil.cc:395
StringTokenError(const char *file, size_t line, const char *what)
Definition: strutil.h:32
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:266
#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:110
NumType tokenToNum(const std::string &num_token)
Converts a string token to an unsigned integer.
Definition: strutil.h:188
char toLower(char chr)
Lowercase Character.
Definition: strutil.h:125
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
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:268
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:405
char toUpper(char chr)
Uppercase Character.
Definition: strutil.h:100
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
boost::shared_ptr< StringSanitizer > StringSanitizerPtr
Definition: strutil.h:305
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157