Kea 2.2.0
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>
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
54std::string trim(const std::string& instring);
55
70template<typename Iterator>
71Iterator
72seekTrimmed(Iterator begin, Iterator end, uint8_t trim_val) {
73 for ( ; end != begin && *(end - 1) == trim_val; --end);
74 return(end);
75}
76
104std::vector<std::string> tokens(const std::string& text,
105 const std::string& delim = std::string(" \t\n"),
106 bool escape = false);
107
118inline char toUpper(char chr) {
119 return (static_cast<char>(std::toupper(static_cast<int>(chr))));
120}
121
127inline void uppercase(std::string& text) {
128 std::transform(text.begin(), text.end(), text.begin(),
130}
131
142inline char toLower(char chr) {
143 return (static_cast<char>(std::tolower(static_cast<int>(chr))));
144}
145
151inline void lowercase(std::string& text) {
152 std::transform(text.begin(), text.end(), text.begin(),
154}
155
166std::string format(const std::string& format,
167 const std::vector<std::string>& args);
168
169
179std::string getToken(std::istringstream& iss);
180
202template <typename NumType, int BitSize>
203NumType
204tokenToNum(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
236std::vector<uint8_t>
237quotedStringToBinary(const std::string& quoted_string);
238
257void
258decodeSeparatedHexString(const std::string& hex_string,
259 const std::string& sep,
260 std::vector<uint8_t>& binary);
261
267
272void
273decodeColonSeparatedHexString(const std::string& hex_string,
274 std::vector<uint8_t>& binary);
275
294void
295decodeFormattedHexString(const std::string& hex_string,
296 std::vector<uint8_t>& binary);
297
299class StringSanitizerImpl;
300
302typedef boost::shared_ptr<StringSanitizerImpl> StringSanitizerImplPtr;
303
311public:
312
327 StringSanitizer(const std::string& char_set,
328 const std::string& char_replacement);
329
334
343 std::string scrub(const std::string& original);
344
350 static const uint32_t MAX_DATA_SIZE;
351
352private:
355};
356
358typedef boost::shared_ptr<StringSanitizer> StringSanitizerPtr;
359
365inline bool
366isPrintable(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
380inline bool
381isPrintable(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
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:310
std::string scrub(const std::string &original)
Returns a scrubbed copy of a given string.
Definition: strutil.cc:447
static const uint32_t MAX_DATA_SIZE
The maximum size for regex parameters.
Definition: strutil.h:350
StringSanitizer(const std::string &char_set, const std::string &char_replacement)
Constructor.
Definition: strutil.cc:438
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:118
char toLower(char chr)
Lowercase Character.
Definition: strutil.h:142
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:151
std::string getToken(std::istringstream &iss)
Returns one token from the given stringstream.
Definition: strutil.cc:186
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
bool isPrintable(const std::string &content)
Check if a string is printable.
Definition: strutil.h:366
NumType tokenToNum(const std::string &num_token)
Converts a string token to an unsigned integer.
Definition: strutil.h:204
boost::shared_ptr< StringSanitizer > StringSanitizerPtr
Type representing the pointer to the StringSanitizer.
Definition: strutil.h:358
boost::shared_ptr< StringSanitizerImpl > StringSanitizerImplPtr
Type representing the pointer to the StringSanitizerImpl.
Definition: strutil.h:299
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
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
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:127
Defines the logger used by the top-level component of kea-lfc.