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 
77  template<class E>
78  static inline std::string _2str(const E* const data) {
79  return _2str(data, getLength(data));
80  }
81 
84  static inline std::string _2str(const char* const data) {
85  if (data == 0) {
86  throw EmptyData();
87  }
88  return std::string(data);
89  }
90 
93  template<class E>
94  static inline std::string _2str(const E* const data, int length) {
95  if (data == 0) {
96  throw EmptyData();
97  }
98  if (length == 0) {
99  return "";
100  }
101  char* buf = new char[length + 1];
102  int i = 0;
103  for (i = 0; i < length; i++) {
104  if ((int) data[i] > 255) {
105  buf[i] = 63; // rudimentary damage control, replace with '?'
106  } else {
107  buf[i] = (char) data[i];
108  }
109  }
110  buf[i] = 0;
111  std::string ret = buf;
112  delete[] buf;
113  return ret;
114  }
115 
118  static inline std::string _2str(const char* const data, int length) {
119  if (data == 0) {
120  throw EmptyData();
121  }
122  return std::string(data, length);
123  }
124 
127  template<class E>
128  static std::string _2strSec(const E* const data, const std::string& def) {
129  return _2strSec(data, getLength(data), def);
130  }
131 
134  template<class E>
135  static std::string _2strSec(const E* const data, int length, const std::string& def) {
136  if (data == 0 || length == 0) {
137  return def;
138  }
139  return _2str(data, length);
140  }
142 
144 
145  template<class E>
149  static int _2int(const E* const data) {
150  long long int result = _2long(data);
151  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
152  throw NumberFormatException();
153  }
154  return (int)result;
155  }
156 
160  static int _str2int(const std::string& sData) {
161  return _2int(sData.c_str());
162  }
163 
167  template<class E>
168  static int _hex2int(const E* const data) {
169  long long int result = _hex2long(data);
170  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
171  throw NumberFormatException();
172  }
173  return (int)result;
174  }
175 
179  static int _strHex2int(const std::string& sData) {
180  return _hex2int(sData.c_str());
181  }
182 
185  template<class E>
186  static int _2intSec(const E* const data, int def) {
187  if (data == 0 || data[0] == 0) {
188  return def;
189  }
190  return _2int(data);
191  }
193 
195 
196  template<class E>
200  static long long int _2long(const E* const data) {
201  if (data == 0 || data[0] == 0) {
202  throw EmptyData();
203  }
204  long long int sgn = 1;
205  int i = 0;
206  if (data[0] == '+') {
207  i++;
208  }
209  if (data[0] == '-') {
210  i++;
211  sgn = -1;
212  }
213  long long int ret = 0;
214  for (; data[i] != 0; i++) {
215  ret *= 10;
216  // !!! need to catch overflows
217  char akt = (char) data[i];
218  if (akt < '0' || akt > '9') {
219  throw NumberFormatException();
220  }
221  ret += akt - 48;
222  }
223  if (i == 0) {
224  throw EmptyData();
225  }
226  return ret * sgn;
227  }
228 
232  static long long int _str2long(const std::string& sData) {
233  return _2long(sData.c_str());
234  }
235 
239  template<class E>
240  static long long int _hex2long(const E* const data) {
241  if (data == 0 || data[0] == 0) {
242  throw EmptyData();
243  }
244  long long int sgn = 1;
245  int i = 0;
246  if (data[0] == '+') {
247  i++;
248  }
249  if (data[0] == '-') {
250  i++;
251  sgn = -1;
252  }
253  if (data[i] == '#') { // for html color codes
254  i++;
255  }
256  if (data[i] == '0' && (data[i + 1] == 'x' || data[i + 1] == 'X')) {
257  i += 2;
258  }
259  long long int ret = 0;
260  for (; data[i] != 0; i++) {
261  ret *= 16;
262  // !!! need to catch overflows
263  char akt = (char) data[i];
264  if (akt >= '0' && akt <= '9') {
265  ret += akt - '0';
266  } else if (akt >= 'A' && akt <= 'F') {
267  ret += akt - 'A' + 10;
268  } else if (akt >= 'a' && akt <= 'f') {
269  ret += akt - 'a' + 10;
270  } else {
271  throw NumberFormatException();
272  }
273  }
274  if (i == 0) {
275  throw EmptyData();
276  }
277  return ret * sgn;
278  }
279 
282  template<class E>
283  static long long int _2longSec(const E* const data, long def) {
284  if (data == 0 || data[0] == 0) {
285  return def;
286  }
287  return _2long(data);
288  }
290 
292 
293  template<class E>
297  static double _2double(const E* const data) {
298  if (data == 0 || data[0] == 0) {
299  throw EmptyData();
300  }
301  int i = 0;
302  double sgn = 1;
303  if (data[0] == '+') {
304  i++;
305  }
306  if (data[0] == '-') {
307  i++;
308  sgn = -1;
309  }
310  // we try to parse it as a long long int storing the decimal point pos
311  int pointPos = -1;
312  int digits = std::numeric_limits<long long int>::digits10;
313  long long int ret = 0;
314  for (; data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
315  char akt = (char) data[i];
316  if (akt < '0' || akt > '9') {
317  if (pointPos < 0 && (akt == '.' || akt == ',')) {
318  pointPos = i;
319  continue;
320  }
321  throw NumberFormatException();
322  }
323  digits--;
324  if (digits >= 0) { // we skip the digits which don't fit into long long int
325  ret = ret * 10 + akt - 48;
326  }
327  }
328  int exponent = digits >= 0 ? 0 : -digits;
329  if (pointPos != -1) {
330  exponent += pointPos - i + 1;
331  }
332  // check what has happened - end of string or exponent
333  if (data[i] == 0) {
334  return ret * sgn * (double) pow(10.0, exponent);
335  }
336  // now the exponent
337  try {
338  return ret * sgn * (double) pow(10.0, _2int(data + i + 1) + exponent);
339  } catch (EmptyData&) {
340  // the exponent was empty
341  throw NumberFormatException();
342  }
343  }
344 
348  static double _str2double(const std::string& sData) {
349  return _2double(sData.c_str());
350  }
351 
354  template<class E>
355  static double _2doubleSec(const E* const data, double def) {
356  if (data == 0 || data[0] == 0) {
357  return def;
358  }
359  return _2double(data);
360  }
362 
364 
365  template<class E>
371  static bool _2bool(const E* const data) {
372  if (data == 0 || data[0] == 0) {
373  throw EmptyData();
374  }
375  std::string s = _2str(data);
376  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
377  if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
378  return true;
379  } else if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
380  return false;
381  } else {
382  throw BoolFormatException();
383  }
384  }
385 
389  static bool _str2Bool(const std::string& sData) {
390  return _2bool(sData.c_str());
391  }
392 
397  template<class E>
398  static bool _2boolSec(const E* const data, bool def) {
399  if (data == 0 || data[0] == 0) {
400  return def;
401  }
402  return _2bool(data);
403  }
405 
407  template<class E>
408  static int getLength(const E* const data) {
409  if (data == 0) {
410  return 0;
411  }
412  int i = 0;
413  while (data[i] != 0) {
414  i++;
415  }
416  return i;
417  }
418 
419 };
420 
421 
422 #endif
423 
424 /****************************************************************************/
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:389
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:168
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:355
#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:398
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:371
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:283
static int getLength(const E *const data)
returns the length of the string (the position of the 0-character)
Definition: TplConvert.h:408
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:240
static std::string _2str(const char *const data)
converts a 0-terminated char array into std::string
Definition: TplConvert.h:84
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:200
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:232
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:94
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:348
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: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:160
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:149
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
*static std::string _2str(const char *const data, int length)
converts a char array into std::string considering the given length
Definition: TplConvert.h:118
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:179
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:186
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:128
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:135