SUMO - Simulation of Urban MObility
TplConvert.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Some conversion methods (from strings to other)
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef TplConvert_h
23 #define TplConvert_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <sstream>
37 #include <cmath>
38 #include <limits>
39 #include <algorithm>
41 #include <utils/common/StdDefs.h>
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
52 class TplConvert {
53 public:
55 
56  static inline std::string _2str(const int var) {
58  std::ostringstream convert;
59  convert << var;
60  return convert.str();
61  }
62 
64  static inline std::string _2str(const double var) {
65  std::ostringstream convert;
66  convert << var;
67  return convert.str();
68  }
69 
71  static inline std::string _2str(const bool var) {
72  return (var == true ? "true" : "false");
73  }
74 
78  template<class E>
79  static inline std::string _2str(const E* const data) {
80  return _2str(data, getLength(data));
81  }
82 
86  static inline std::string _2str(const char* const data) {
87  if (data == 0) {
88  throw EmptyData();
89  }
90  return std::string(data);
91  }
92 
96  template<class E>
97  static inline std::string _2str(const E* const data, int length) {
98  if (data == 0) {
99  throw EmptyData();
100  }
101  if (length == 0) {
102  return "";
103  }
104  char* buf = new char[length + 1];
105  int i = 0;
106  for (i = 0; i < length; i++) {
107  if ((int) data[i] > 255) {
108  buf[i] = 63; // rudimentary damage control, replace with '?'
109  } else {
110  buf[i] = (char) data[i];
111  }
112  }
113  buf[i] = 0;
114  std::string ret = buf;
115  delete[] buf;
116  return ret;
117  }
118 
122  static inline std::string _2str(const char* const data, int length) {
123  if (data == 0) {
124  throw EmptyData();
125  }
126  return std::string(data, length);
127  }
128 
132  template<class E>
133  static std::string _2strSec(const E* const data, const std::string& def) {
134  return _2strSec(data, getLength(data), def);
135  }
136 
140  template<class E>
141  static std::string _2strSec(const E* const data, int length, const std::string& def) {
142  if (data == 0 || length == 0) {
143  return def;
144  }
145  return _2str(data, length);
146  }
148 
150 
151 
155  template<class E>
156  static int _2int(const E* const data) {
157  long long int result = _2long(data);
158  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
159  throw NumberFormatException();
160  }
161  return (int)result;
162  }
163 
168  static int _str2int(const std::string& sData) {
169  return _2int(sData.c_str());
170  }
171 
176  template<class E>
177  static int _hex2int(const E* const data) {
178  long long int result = _hex2long(data);
179  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
180  throw NumberFormatException();
181  }
182  return (int)result;
183  }
184 
189  static int _strHex2int(const std::string& sData) {
190  return _hex2int(sData.c_str());
191  }
192 
195  template<class E>
196  static int _2intSec(const E* const data, int def) {
197  if (data == 0 || data[0] == 0) {
198  return def;
199  }
200  return _2int(data);
201  }
203 
205 
206 
210  template<class E>
211  static long long int _2long(const E* const data) {
212  if (data == 0 || data[0] == 0) {
213  throw EmptyData();
214  }
215  long long int sgn = 1;
216  int i = 0;
217  if (data[0] == '+') {
218  i++;
219  }
220  if (data[0] == '-') {
221  i++;
222  sgn = -1;
223  }
224  long long int ret = 0;
225  for (; data[i] != 0; i++) {
226  ret *= 10;
227  // !!! need to catch overflows
228  char akt = (char) data[i];
229  if (akt < '0' || akt > '9') {
230  throw NumberFormatException();
231  }
232  ret += akt - 48;
233  }
234  if (i == 0) {
235  throw EmptyData();
236  }
237  return ret * sgn;
238  }
239 
244  static long long int _str2long(const std::string& sData) {
245  return _2long(sData.c_str());
246  }
247 
252  template<class E>
253  static long long int _hex2long(const E* const data) {
254  if (data == 0 || data[0] == 0) {
255  throw EmptyData();
256  }
257  long long int sgn = 1;
258  int i = 0;
259  if (data[0] == '+') {
260  i++;
261  }
262  if (data[0] == '-') {
263  i++;
264  sgn = -1;
265  }
266  if (data[i] == '#') { // for html color codes
267  i++;
268  }
269  if (data[i] == '0' && (data[i + 1] == 'x' || data[i + 1] == 'X')) {
270  i += 2;
271  }
272  long long int ret = 0;
273  for (; data[i] != 0; i++) {
274  ret *= 16;
275  // !!! need to catch overflows
276  char akt = (char) data[i];
277  if (akt >= '0' && akt <= '9') {
278  ret += akt - '0';
279  } else if (akt >= 'A' && akt <= 'F') {
280  ret += akt - 'A' + 10;
281  } else if (akt >= 'a' && akt <= 'f') {
282  ret += akt - 'a' + 10;
283  } else {
284  throw NumberFormatException();
285  }
286  }
287  if (i == 0) {
288  throw EmptyData();
289  }
290  return ret * sgn;
291  }
292 
296  template<class E>
297  static long long int _2longSec(const E* const data, long def) {
298  if (data == 0 || data[0] == 0) {
299  return def;
300  }
301  return _2long(data);
302  }
304 
306 
307 
311  template<class E>
312  static double _2double(const E* const data) {
313  if (data == 0 || data[0] == 0) {
314  throw EmptyData();
315  }
316  int i = 0;
317  double sgn = 1;
318  if (data[0] == '+') {
319  i++;
320  }
321  if (data[0] == '-') {
322  i++;
323  sgn = -1;
324  }
325  // we try to parse it as a long long int storing the decimal point pos
326  int pointPos = -1;
327  int digits = std::numeric_limits<long long int>::digits10;
328  long long int ret = 0;
329  for (; data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
330  char akt = (char) data[i];
331  if (akt < '0' || akt > '9') {
332  if (pointPos < 0 && (akt == '.' || akt == ',')) {
333  pointPos = i;
334  continue;
335  }
336  throw NumberFormatException();
337  }
338  digits--;
339  if (digits >= 0) { // we skip the digits which don't fit into long long int
340  ret = ret * 10 + akt - 48;
341  }
342  }
343  int exponent = digits >= 0 ? 0 : -digits;
344  if (pointPos != -1) {
345  exponent += pointPos - i + 1;
346  }
347  // check what has happened - end of string or exponent
348  if (data[i] == 0) {
349  return ret * sgn * (double) pow(10.0, exponent);
350  }
351  // now the exponent
352  try {
353  return ret * sgn * (double) pow(10.0, _2int(data + i + 1) + exponent);
354  } catch (EmptyData&) {
355  // the exponent was empty
356  throw NumberFormatException();
357  }
358  }
359 
364  static double _str2double(const std::string& sData) {
365  return _2double(sData.c_str());
366  }
367 
371  template<class E>
372  static double _2doubleSec(const E* const data, double def) {
373  if (data == 0 || data[0] == 0) {
374  return def;
375  }
376  return _2double(data);
377  }
379 
381 
382 
388  template<class E>
389  static bool _2bool(const E* const data) {
390  if (data == 0 || data[0] == 0) {
391  throw EmptyData();
392  }
393  std::string s = _2str(data);
394  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
395  if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
396  return true;
397  } else if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
398  return false;
399  } else {
400  throw BoolFormatException();
401  }
402  }
403 
408  static bool _str2Bool(const std::string& sData) {
409  return _2bool(sData.c_str());
410  }
411 
416  template<class E>
417  static bool _2boolSec(const E* const data, bool def) {
418  if (data == 0 || data[0] == 0) {
419  return def;
420  }
421  return _2bool(data);
422  }
424 
426  template<class E>
427  static int getLength(const E* const data) {
428  if (data == 0) {
429  return 0;
430  }
431  int i = 0;
432  while (data[i] != 0) {
433  i++;
434  }
435  return i;
436  }
437 };
438 
439 
440 #endif
441 
442 /****************************************************************************/
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:408
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:177
static std::string _2str(const char *const data, int length)
converts a char array into std::string considering the given length
Definition: TplConvert.h:122
static std::string _2str(const bool var)
convert bool to string
Definition: TplConvert.h:71
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:372
#define min(a, b)
Definition: polyfonts.c:66
*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:417
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:389
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:297
static int getLength(const E *const data)
returns the length of the string (the position of the 0-character)
Definition: TplConvert.h:427
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:253
static std::string _2str(const char *const data)
converts a 0-terminated char array into std::string
Definition: TplConvert.h:86
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:211
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:244
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:97
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:364
static std::string _2str(const double var)
convert double to string
Definition: TplConvert.h:64
#define max(a, b)
Definition: polyfonts.c:65
static std::string _2str(const E *const data)
converts a 0-terminated char-type array into std::string
Definition: TplConvert.h:79
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:168
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:156
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:312
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:189
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:196
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:57
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:133
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:141