8 #include <boost/algorithm/string.hpp>
10 #define strtoll _strtoi64
11 #define strtoull _strtoui64
14 std::ostringstream oss;
42 std::string
convert(
bool v) {
return v ?
"1" :
"0";}
50 std::string
convert(
const std::string& v) {
return boost::algorithm::trim_copy(v);}
51 bool convert(
const std::string& s,
bool& v) {
53 if (s ==
"1") {v =
true;
return true;}
54 if (s ==
"0") {v =
false;
return true;}
55 std::string b = boost::algorithm::trim_copy(s);
58 boost::algorithm::to_lower(b);
59 if (b ==
"1" || b ==
"on" || b ==
"t" || b ==
"true" || b ==
"y" || b ==
"yes") {
63 if (b ==
"0" || b ==
"off" || b ==
"f" || b ==
"false" || b ==
"n" || b ==
"no") {
67 throw std::invalid_argument(
"cannot convert to bool: " + s);
69 bool convert(
const std::string& s,
int& v) {
74 long int t = strtol(s.c_str(), &endptr, 10);
75 if (errno == ERANGE || (
sizeof(
int) <
sizeof(
long int) && (2147483647 < t || t < -2147483648))) {
77 throw std::invalid_argument(
"cannot convert to int, out of range: " + s);
80 throw std::invalid_argument(
"cannot convert to int, invalid chars: " + s);
81 if (s.c_str() == endptr)
86 bool convert(
const std::string& s,
unsigned int& v) {
91 unsigned long int t = strtoul(s.c_str(), &endptr, 10);
92 if (errno == ERANGE || (
sizeof(
int) <
sizeof(
long int) && 4294967295 < t)) {
94 throw std::invalid_argument(
"cannot convert to unsigned int, out of range: " + s);
97 throw std::invalid_argument(
"cannot convert to unsigned int, invalid chars: " + s);
98 if (s.c_str() == endptr)
103 bool convert(
const std::string& s,
unsigned long int& v) {
104 if (
sizeof(
unsigned long int) ==
sizeof(
unsigned int))
return convert(s, (
unsigned int&)v);
105 unsigned long long int i;
107 v = (
unsigned long int)i;
112 bool convert(
const std::string& s,
long long int& v) {
117 long long int t = strtoll(s.c_str(), &endptr, 10);
118 if (errno == ERANGE) {
120 throw std::invalid_argument(
"cannot convert to long long int, out of range: " + s);
123 throw std::invalid_argument(
"cannot convert to long long int, invalid chars: " + s);
124 if (s.c_str() == endptr)
129 bool convert(
const std::string& s,
unsigned long long int& v) {
134 unsigned long long int t = strtoull(s.c_str(), &endptr, 10);
135 if (errno == ERANGE) {
137 throw std::invalid_argument(
"cannot convert to unsigned long long int, out of range: " + s);
140 throw std::invalid_argument(
"cannot convert to unsigned long long int, invalid chars: " + s);
141 if (s.c_str() == endptr)
152 bool convert(
const std::string& s,
double& v) {
157 double t = strtod(s.c_str(), &endptr);
158 if (errno == ERANGE) {
160 throw std::invalid_argument(
"cannot convert to double, out of range: " + s);
163 throw std::invalid_argument(
"cannot convert to double, invalid chars: " + s);
164 if (s.c_str() == endptr)
169 bool convert(
const std::string& s, std::string& v) {
170 if (s.empty() ||
isspaces(s.c_str()))
172 v = boost::algorithm::trim_copy(s);
181 bool parse_hex(
const std::string& s,
size_t pos,
char& chr) {
182 if (s.size() < pos + 2)
185 unsigned int c = (
unsigned int)s[pos];
186 if (
'0' <= c && c <=
'9')
188 else if (
'A' <= c && c <=
'F')
189 v = (10 + (c -
'A')) << 4;
190 else if (
'a' <= c && c <=
'f')
191 v = (10 + (c -
'a')) << 4;
194 c = (
unsigned int)s[pos + 1];
195 if (
'0' <= c && c <=
'9')
197 else if (
'A' <= c && c <=
'F')
199 else if (
'a' <= c && c <=
'f')
207 unsigned int c = (
unsigned char)v & 0xF0;
209 s.insert(s.end(), (char)((9 < c) ? (c - 10) +
'A' : c +
'0'));
211 s.insert(s.end(), (char)((9 < c) ? (c - 10) +
'A' : c +
'0'));
std::string stringstream_convert(const T &v)
void append_hex(char v, std::string &s)
Convert the char v to hex and add the 2 chars to the end of s.
bool isspaces(const char *s)
Test if string is empty or all isspace.
std::string convert(const path &v)
URI path to string.
bool parse_hex(const std::string &s, size_t pos, char &chr)
Parse hex chars at pos, returning success, and set the char and advance first on success.