All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
utils.cpp
Go to the documentation of this file.
1 // Copyright symbx 2007.
2 #include "utils.h"
3 #include <stdlib.h>
4 #include <ctype.h>
5 #include <errno.h>
6 #include <sstream>
7 #include <stdexcept>
8 #include <boost/algorithm/string.hpp>
9 #ifdef _WIN32
10 #define strtoll _strtoi64
11 #define strtoull _strtoui64
12 #endif
13 template<typename T> std::string stringstream_convert(const T& v) {
14  std::ostringstream oss;
15  oss << v;
16  return oss.str();
17 }
18 //static const char iso_8859_1_nonctl_nonpercnt_chars_[] = { // graphic + space + soft-hyphen - percent
19 // 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
20 // 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
21 // 1,1,1,1,1,0,1,1, 1,1,1,1,1,1,1,1,
22 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, // 3f
23 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
24 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
25 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
26 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,0, // 7f
27 // 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
28 // 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
29 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
30 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
31 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
32 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
33 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
34 // 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
35 //};
36 namespace te
37 {
38  namespace common
39  {
40  namespace uri
41  {
42  std::string convert(bool v) {return v ? "1" : "0";}
43  std::string convert(int v) {return stringstream_convert(v);}
44  std::string convert(unsigned int v) {return stringstream_convert(v);}
45  std::string convert(long int v) {return stringstream_convert(v);}
46  std::string convert(unsigned long int v) {return stringstream_convert(v);}
47  std::string convert(long long int v) {return stringstream_convert(v);}
48  std::string convert(unsigned long long int v) {return stringstream_convert(v);}
49  std::string convert(double v) {return stringstream_convert(v);}
50  std::string convert(const std::string& v) {return boost::algorithm::trim_copy(v);}
51  bool convert(const std::string& s, bool& v) {
52  // Try common cases first.
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);
56  if (b.empty())
57  return false;
58  boost::algorithm::to_lower(b);
59  if (b == "1" || b == "on" || b == "t" || b == "true" || b == "y" || b == "yes") {
60  v = true;
61  return true;
62  }
63  if (b == "0" || b == "off" || b == "f" || b == "false" || b == "n" || b == "no") {
64  v = false;
65  return true;
66  }
67  throw std::invalid_argument("cannot convert to bool: " + s);
68  }
69  bool convert(const std::string& s, int& v) {
70  if (s.empty())
71  return false;
72  char* endptr;
73  errno = 0;
74  long int t = strtol(s.c_str(), &endptr, 10);
75  if (errno == ERANGE || (sizeof(int) < sizeof(long int) && (2147483647 < t || t < -2147483648))) {
76  errno = 0;
77  throw std::invalid_argument("cannot convert to int, out of range: " + s);
78  }
79  if (*endptr && !isspaces(endptr))
80  throw std::invalid_argument("cannot convert to int, invalid chars: " + s);
81  if (s.c_str() == endptr)
82  return false;
83  v = t;
84  return true;
85  }
86  bool convert(const std::string& s, unsigned int& v) {
87  if (s.empty())
88  return false;
89  char* endptr;
90  errno = 0;
91  unsigned long int t = strtoul(s.c_str(), &endptr, 10);
92  if (errno == ERANGE || (sizeof(int) < sizeof(long int) && 4294967295 < t)) {
93  errno = 0;
94  throw std::invalid_argument("cannot convert to unsigned int, out of range: " + s);
95  }
96  if (*endptr && !isspaces(endptr))
97  throw std::invalid_argument("cannot convert to unsigned int, invalid chars: " + s);
98  if (s.c_str() == endptr)
99  return false;
100  v = t;
101  return true;
102  }
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;
106  if (convert(s, i)) {
107  v = (unsigned long int)i;
108  return true;
109  }
110  return false;
111  }
112  bool convert(const std::string& s, long long int& v) {
113  if (s.empty())
114  return false;
115  char* endptr;
116  errno = 0;
117  long long int t = strtoll(s.c_str(), &endptr, 10);
118  if (errno == ERANGE) {
119  errno = 0;
120  throw std::invalid_argument("cannot convert to long long int, out of range: " + s);
121  }
122  if (*endptr && !isspaces(endptr))
123  throw std::invalid_argument("cannot convert to long long int, invalid chars: " + s);
124  if (s.c_str() == endptr)
125  return false;
126  v = t;
127  return true;
128  }
129  bool convert(const std::string& s, unsigned long long int& v) {
130  if (s.empty())
131  return false;
132  char* endptr;
133  errno = 0;
134  unsigned long long int t = strtoull(s.c_str(), &endptr, 10);
135  if (errno == ERANGE) {
136  errno = 0;
137  throw std::invalid_argument("cannot convert to unsigned long long int, out of range: " + s);
138  }
139  if (*endptr && !isspaces(endptr))
140  throw std::invalid_argument("cannot convert to unsigned long long int, invalid chars: " + s);
141  if (s.c_str() == endptr)
142  return false;
143  v = t;
144  return true;
145  }
146  # ifndef _WIN32
147  // std::string convert(std::ptrdiff_t v) {
148 // if (sizeof(std::ptrdiff_t) == sizeof(unsigned int)) return convert((unsigned int)v);
149 // return convert((unsigned long long int)v);
150  // }
151  # endif
152  bool convert(const std::string& s, double& v) {
153  if (s.empty())
154  return false;
155  char* endptr;
156  errno = 0;
157  double t = strtod(s.c_str(), &endptr);
158  if (errno == ERANGE) {
159  errno = 0;
160  throw std::invalid_argument("cannot convert to double, out of range: " + s);
161  }
162  if (*endptr && !isspaces(endptr))
163  throw std::invalid_argument("cannot convert to double, invalid chars: " + s);
164  if (s.c_str() == endptr)
165  return false;
166  v = t;
167  return true;
168  }
169  bool convert(const std::string& s, std::string& v) {
170  if (s.empty() || isspaces(s.c_str()))
171  return false;
172  v = boost::algorithm::trim_copy(s);
173  return true;
174  }
175  bool isspaces(const char* s) {
176  for (; *s; ++s)
177  if (!::isspace(*s))
178  return false;
179  return true;
180  }
181  bool parse_hex(const std::string& s, size_t pos, char& chr) {
182  if (s.size() < pos + 2)
183  return false;
184  unsigned int v;
185  unsigned int c = (unsigned int)s[pos];
186  if ('0' <= c && c <= '9')
187  v = (c - '0') << 4;
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;
192  else
193  return false;
194  c = (unsigned int)s[pos + 1];
195  if ('0' <= c && c <= '9')
196  v += c - '0';
197  else if ('A' <= c && c <= 'F')
198  v += 10 + (c - 'A');
199  else if ('a' <= c && c <= 'f')
200  v += 10 + (c - 'a');
201  else
202  return false;
203  chr = (char)v; // Set output.
204  return true;
205  }
206  void append_hex(char v, std::string& s) {
207  unsigned int c = (unsigned char)v & 0xF0;
208  c >>= 4;
209  s.insert(s.end(), (char)((9 < c) ? (c - 10) + 'A' : c + '0'));
210  c = v & 0x0F;
211  s.insert(s.end(), (char)((9 < c) ? (c - 10) + 'A' : c + '0'));
212  }
213  }
214  }
215 }
std::string stringstream_convert(const T &v)
Definition: utils.cpp:13
void append_hex(char v, std::string &s)
Convert the char v to hex and add the 2 chars to the end of s.
Definition: utils.cpp:206
bool isspaces(const char *s)
Test if string is empty or all isspace.
Definition: utils.cpp:175
std::string convert(const path &v)
URI path to string.
Definition: path.cpp:219
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.
Definition: utils.cpp:181