SUMO - Simulation of Urban MObility
TplConvert.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Some conversion methods (from strings to other)
20 /****************************************************************************/
21 #ifndef TplConvert_h
22 #define TplConvert_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <sstream>
36 #include <cmath>
37 #include <limits>
38 #include <algorithm>
40 #include <utils/common/StdDefs.h>
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
51 class TplConvert {
52 public:
54 
55  static inline std::string _2str(const int var) {
57  std::ostringstream convert;
58  convert << var;
59  return convert.str();
60  }
61 
63  static inline std::string _2str(const double var) {
64  std::ostringstream convert;
65  convert << var;
66  return convert.str();
67  }
68 
70  static inline std::string _2str(const bool var) {
71  return (var == true ? "true" : "false");
72  }
73 
77  template<class E>
78  static inline std::string _2str(const E* const data) {
79  return _2str(data, getLength(data));
80  }
81 
85  static inline std::string _2str(const char* const data) {
86  if (data == 0) {
87  throw EmptyData();
88  }
89  return std::string(data);
90  }
91 
95  template<class E>
96  static inline std::string _2str(const E* const data, int length) {
97  if (data == 0) {
98  throw EmptyData();
99  }
100  if (length == 0) {
101  return "";
102  }
103  char* buf = new char[length + 1];
104  int i = 0;
105  for (i = 0; i < length; i++) {
106  if ((int) data[i] > 255) {
107  buf[i] = 63; // rudimentary damage control, replace with '?'
108  } else {
109  buf[i] = (char) data[i];
110  }
111  }
112  buf[i] = 0;
113  std::string ret = buf;
114  delete[] buf;
115  return ret;
116  }
117 
121  static inline std::string _2str(const char* const data, int length) {
122  if (data == 0) {
123  throw EmptyData();
124  }
125  return std::string(data, length);
126  }
127 
131  template<class E>
132  static std::string _2strSec(const E* const data, const std::string& def) {
133  return _2strSec(data, getLength(data), def);
134  }
135 
139  template<class E>
140  static std::string _2strSec(const E* const data, int length, const std::string& def) {
141  if (data == 0 || length == 0) {
142  return def;
143  }
144  return _2str(data, length);
145  }
147 
149 
150 
154  template<class E>
155  static int _2int(const E* const data) {
156  long long int result = _2long(data);
157  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
158  throw NumberFormatException();
159  }
160  return (int)result;
161  }
162 
167  static int _str2int(const std::string& sData) {
168  return _2int(sData.c_str());
169  }
170 
175  template<class E>
176  static int _hex2int(const E* const data) {
177  long long int result = _hex2long(data);
178  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
179  throw NumberFormatException();
180  }
181  return (int)result;
182  }
183 
188  static int _strHex2int(const std::string& sData) {
189  return _hex2int(sData.c_str());
190  }
191 
194  template<class E>
195  static int _2intSec(const E* const data, int def) {
196  if (data == 0 || data[0] == 0) {
197  return def;
198  }
199  return _2int(data);
200  }
202 
204 
205 
209  template<class E>
210  static long long int _2long(const E* const data) {
211  if (data == 0 || data[0] == 0) {
212  throw EmptyData();
213  }
214  long long int sgn = 1;
215  int i = 0;
216  if (data[0] == '+') {
217  i++;
218  }
219  if (data[0] == '-') {
220  i++;
221  sgn = -1;
222  }
223  long long int ret = 0;
224  for (; data[i] != 0; i++) {
225  ret *= 10;
226  // !!! need to catch overflows
227  char akt = (char) data[i];
228  if (akt < '0' || akt > '9') {
229  throw NumberFormatException();
230  }
231  ret += akt - 48;
232  }
233  if (i == 0) {
234  throw EmptyData();
235  }
236  return ret * sgn;
237  }
238 
243  static long long int _str2long(const std::string& sData) {
244  return _2long(sData.c_str());
245  }
246 
251  template<class E>
252  static long long int _hex2long(const E* const data) {
253  if (data == 0 || data[0] == 0) {
254  throw EmptyData();
255  }
256  long long int sgn = 1;
257  int i = 0;
258  if (data[0] == '+') {
259  i++;
260  }
261  if (data[0] == '-') {
262  i++;
263  sgn = -1;
264  }
265  if (data[i] == '#') { // for html color codes
266  i++;
267  }
268  if (data[i] == '0' && (data[i + 1] == 'x' || data[i + 1] == 'X')) {
269  i += 2;
270  }
271  long long int ret = 0;
272  for (; data[i] != 0; i++) {
273  ret *= 16;
274  // !!! need to catch overflows
275  char akt = (char) data[i];
276  if (akt >= '0' && akt <= '9') {
277  ret += akt - '0';
278  } else if (akt >= 'A' && akt <= 'F') {
279  ret += akt - 'A' + 10;
280  } else if (akt >= 'a' && akt <= 'f') {
281  ret += akt - 'a' + 10;
282  } else {
283  throw NumberFormatException();
284  }
285  }
286  if (i == 0) {
287  throw EmptyData();
288  }
289  return ret * sgn;
290  }
291 
295  template<class E>
296  static long long int _2longSec(const E* const data, long def) {
297  if (data == 0 || data[0] == 0) {
298  return def;
299  }
300  return _2long(data);
301  }
303 
305 
306 
310  template<class E>
311  static double _2double(const E* const data) {
312  if (data == 0 || data[0] == 0) {
313  throw EmptyData();
314  }
315  int i = 0;
316  double sgn = 1;
317  if (data[0] == '+') {
318  i++;
319  }
320  if (data[0] == '-') {
321  i++;
322  sgn = -1;
323  }
324  // we try to parse it as a long long int storing the decimal point pos
325  int pointPos = -1;
326  int digits = std::numeric_limits<long long int>::digits10;
327  long long int ret = 0;
328  for (; data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
329  char akt = (char) data[i];
330  if (akt < '0' || akt > '9') {
331  if (pointPos < 0 && (akt == '.' || akt == ',')) {
332  pointPos = i;
333  continue;
334  }
335  throw NumberFormatException();
336  }
337  digits--;
338  if (digits >= 0) { // we skip the digits which don't fit into long long int
339  ret = ret * 10 + akt - 48;
340  }
341  }
342  int exponent = digits >= 0 ? 0 : -digits;
343  if (pointPos != -1) {
344  exponent += pointPos - i + 1;
345  }
346  // check what has happened - end of string or exponent
347  if (data[i] == 0) {
348  return ret * sgn * (double) pow(10.0, exponent);
349  }
350  // now the exponent
351  try {
352  return ret * sgn * (double) pow(10.0, _2int(data + i + 1) + exponent);
353  } catch (EmptyData&) {
354  // the exponent was empty
355  throw NumberFormatException();
356  }
357  }
358 
363  static double _str2double(const std::string& sData) {
364  return _2double(sData.c_str());
365  }
366 
370  template<class E>
371  static double _2doubleSec(const E* const data, double def) {
372  if (data == 0 || data[0] == 0) {
373  return def;
374  }
375  return _2double(data);
376  }
378 
380 
381 
387  template<class E>
388  static bool _2bool(const E* const data) {
389  if (data == 0 || data[0] == 0) {
390  throw EmptyData();
391  }
392  std::string s = _2str(data);
393  // Don't use std::transform(..., ::tolower) due a C4244 Warning in MSVC17
394  for (int i = 0; i < (int)s.length(); i++) {
395  s[i] = (char)::tolower((char)s[i]);
396  }
397  if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
398  return true;
399  } else if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
400  return false;
401  } else {
402  throw BoolFormatException();
403  }
404  }
405 
410  static bool _str2Bool(const std::string& sData) {
411  return _2bool(sData.c_str());
412  }
413 
418  template<class E>
419  static bool _2boolSec(const E* const data, bool def) {
420  if (data == 0 || data[0] == 0) {
421  return def;
422  }
423  return _2bool(data);
424  }
426 
428  template<class E>
429  static int getLength(const E* const data) {
430  if (data == 0) {
431  return 0;
432  }
433  int i = 0;
434  while (data[i] != 0) {
435  i++;
436  }
437  return i;
438  }
439 };
440 
441 
442 #endif
443 
444 /****************************************************************************/
static bool _str2Bool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter ...
Definition: TplConvert.h:410
static int _hex2int(const E *const data)
converts a char-type array with a hex value into the integer value described by it ...
Definition: TplConvert.h:176
static std::string _2str(const char *const data, int length)
converts a char array into std::string considering the given length
Definition: TplConvert.h:121
static std::string _2str(const bool var)
convert bool to string
Definition: TplConvert.h:70
static double _2doubleSec(const E *const data, double def)
converts a 0-terminated char-type array into the double value described by it
Definition: TplConvert.h:371
*static bool _2boolSec(const E *const data, bool def)
converts a 0-terminated char-type array into the double value described by it
Definition: TplConvert.h:419
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:388
static long long int _2longSec(const E *const data, long def)
converts a 0-terminated char-type array into the long value described by it
Definition: TplConvert.h:296
static int getLength(const E *const data)
returns the length of the string (the position of the 0-character)
Definition: TplConvert.h:429
static long long int _hex2long(const E *const data)
converts a char-type array with a hex value into the long value described by it
Definition: TplConvert.h:252
static std::string _2str(const char *const data)
converts a 0-terminated char array into std::string
Definition: TplConvert.h:85
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:210
static long long int _str2long(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter, which
Definition: TplConvert.h:243
static std::string _2str(const E *const data, int length)
converts a char-type array into std::string considering the given length
Definition: TplConvert.h:96
static double _str2double(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
Definition: TplConvert.h:363
static std::string _2str(const double var)
convert double to string
Definition: TplConvert.h:63
static std::string _2str(const E *const data)
converts a 0-terminated char-type array into std::string
Definition: TplConvert.h:78
static int _str2int(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
Definition: TplConvert.h:167
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:155
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
static int _strHex2int(const std::string &sData)
converts a string with a hex value into the integer value described by it by calling the char-type co...
Definition: TplConvert.h:188
static int _2intSec(const E *const data, int def)
converts a 0-terminated char-type array into the integer value described by it
Definition: TplConvert.h:195
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:56
static std::string _2strSec(const E *const data, const std::string &def)
converts a 0-terminated char-type array into std::string
Definition: TplConvert.h:132
static std::string _2strSec(const E *const data, int length, const std::string &def)
converts a char-type array into std::string considering the given length
Definition: TplConvert.h:140