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>
17#include <boost/lexical_cast.hpp>
18#include <boost/shared_ptr.hpp>
19
20namespace isc {
21namespace util {
22namespace str {
23
25
31public:
32 StringTokenError(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) {}
34};
35
44void normalizeSlash(std::string& name);
45
46
55std::string trim(const std::string& instring);
56
57
85std::vector<std::string> tokens(const std::string& text,
86 const std::string& delim = std::string(" \t\n"),
87 bool escape = false);
88
89
100inline char toUpper(char chr) {
101 return (static_cast<char>(std::toupper(static_cast<int>(chr))));
102}
103
104
110inline void uppercase(std::string& text) {
111 std::transform(text.begin(), text.end(), text.begin(),
113}
114
125inline char toLower(char chr) {
126 return (static_cast<char>(std::tolower(static_cast<int>(chr))));
127}
128
134inline void lowercase(std::string& text) {
135 std::transform(text.begin(), text.end(), text.begin(),
137}
138
139
150std::string format(const std::string& format,
151 const std::vector<std::string>& args);
152
153
163std::string getToken(std::istringstream& iss);
164
186template <typename NumType, int BitSize>
187NumType
188tokenToNum(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
219std::vector<uint8_t>
220quotedStringToBinary(const std::string& quoted_string);
221
236void
237decodeColonSeparatedHexString(const std::string& hex_string,
238 std::vector<uint8_t>& binary);
239
255void
256decodeFormattedHexString(const std::string& hex_string,
257 std::vector<uint8_t>& binary);
258
260class StringSanitizerImpl;
261
269public:
270
284 StringSanitizer(const std::string& char_set,
285 const std::string& char_replacement);
286
291
299 std::string scrub(const std::string& original);
300private:
302 StringSanitizerImpl* impl_;
303};
304
305typedef boost::shared_ptr<StringSanitizer> StringSanitizerPtr;
306
307} // namespace str
308} // namespace util
309} // namespace isc
310
311#endif // STRUTIL_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Implements a regular expression based string scrubber.
Definition: strutil.h:268
std::string scrub(const std::string &original)
Returns a scrubbed copy of a given string.
Definition: strutil.cc:405
A Set of C++ Utilities for Manipulating Strings.
Definition: strutil.h:30
StringTokenError(const char *file, size_t line, const char *what)
Definition: strutil.h:32
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void normalizeSlash(std::string &name)
Normalize Backslash.
Definition: strutil.cc:41
char toUpper(char chr)
Uppercase Character.
Definition: strutil.h:100
char toLower(char chr)
Lowercase Character.
Definition: strutil.h:125
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Definition: strutil.cc:157
void lowercase(std::string &text)
Lowercase String.
Definition: strutil.h:134
std::string getToken(std::istringstream &iss)
Returns one token from the given stringstream.
Definition: strutil.cc:186
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
NumType tokenToNum(const std::string &num_token)
Converts a string token to an unsigned integer.
Definition: strutil.h:188
boost::shared_ptr< StringSanitizer > StringSanitizerPtr
Definition: strutil.h:305
std::vector< uint8_t > quotedStringToBinary(const std::string &quoted_string)
Converts a string in quotes into vector.
Definition: strutil.cc:196
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
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
vector< string > tokens(const std::string &text, const std::string &delim, bool escape)
Split String into Tokens.
Definition: strutil.cc:77
void uppercase(std::string &text)
Uppercase String.
Definition: strutil.h:110
Defines the logger used by the top-level component of kea-dhcp-ddns.