SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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 SUMOReal 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  template<class E>
233  static long long int _hex2long(const E* const data) {
234  if (data == 0 || data[0] == 0) {
235  throw EmptyData();
236  }
237  long long int sgn = 1;
238  int i = 0;
239  if (data[0] == '+') {
240  i++;
241  }
242  if (data[0] == '-') {
243  i++;
244  sgn = -1;
245  }
246  if (data[i] == '#') { // for html color codes
247  i++;
248  }
249  if (data[i] == '0' && (data[i + 1] == 'x' || data[i + 1] == 'X')) {
250  i += 2;
251  }
252  long long int ret = 0;
253  for (; data[i] != 0; i++) {
254  ret *= 16;
255  // !!! need to catch overflows
256  char akt = (char) data[i];
257  if (akt >= '0' && akt <= '9') {
258  ret += akt - '0';
259  } else if (akt >= 'A' && akt <= 'F') {
260  ret += akt - 'A' + 10;
261  } else if (akt >= 'a' && akt <= 'f') {
262  ret += akt - 'a' + 10;
263  } else {
264  throw NumberFormatException();
265  }
266  }
267  if (i == 0) {
268  throw EmptyData();
269  }
270  return ret * sgn;
271  }
272 
275  template<class E>
276  static long long int _2longSec(const E* const data, long def) {
277  if (data == 0 || data[0] == 0) {
278  return def;
279  }
280  return _2long(data);
281  }
283 
285 
286  template<class E>
290  static SUMOReal _2SUMOReal(const E* const data) {
291  if (data == 0 || data[0] == 0) {
292  throw EmptyData();
293  }
294  int i = 0;
295  SUMOReal sgn = 1;
296  if (data[0] == '+') {
297  i++;
298  }
299  if (data[0] == '-') {
300  i++;
301  sgn = -1;
302  }
303  // we try to parse it as a long long int storing the decimal point pos
304  int pointPos = -1;
305  int digits = std::numeric_limits<long long int>::digits10;
306  long long int ret = 0;
307  for (; data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
308  char akt = (char) data[i];
309  if (akt < '0' || akt > '9') {
310  if (pointPos < 0 && (akt == '.' || akt == ',')) {
311  pointPos = i;
312  continue;
313  }
314  throw NumberFormatException();
315  }
316  digits--;
317  if (digits >= 0) { // we skip the digits which don't fit into long long int
318  ret = ret * 10 + akt - 48;
319  }
320  }
321  int exponent = digits >= 0 ? 0 : -digits;
322  if (pointPos != -1) {
323  exponent += pointPos - i + 1;
324  }
325  // check what has happened - end of string or exponent
326  if (data[i] == 0) {
327  return ret * sgn * (SUMOReal) pow(10.0, exponent);
328  }
329  // now the exponent
330  try {
331  return ret * sgn * (SUMOReal) pow(10.0, _2int(data + i + 1) + exponent);
332  } catch (EmptyData&) {
333  // the exponent was empty
334  throw NumberFormatException();
335  }
336  }
337 
341  static SUMOReal _str2SUMOReal(const std::string& sData) {
342  return _2SUMOReal(sData.c_str());
343  }
344 
347  template<class E>
348  static SUMOReal _2SUMORealSec(const E* const data, SUMOReal def) {
349  if (data == 0 || data[0] == 0) {
350  return def;
351  }
352  return _2SUMOReal(data);
353  }
355 
357 
358  template<class E>
364  static bool _2bool(const E* const data) {
365  if (data == 0 || data[0] == 0) {
366  throw EmptyData();
367  }
368  std::string s = _2str(data);
369  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
370  if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
371  return true;
372  } else if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
373  return false;
374  } else {
375  throw BoolFormatException();
376  }
377  }
378 
382  static bool _str2Bool(const std::string& sData) {
383  return _2bool(sData.c_str());
384  }
385 
390  template<class E>
391  static bool _2boolSec(const E* const data, bool def) {
392  if (data == 0 || data[0] == 0) {
393  return def;
394  }
395  return _2bool(data);
396  }
398 
400  template<class E>
401  static int getLength(const E* const data) {
402  if (data == 0) {
403  return 0;
404  }
405  int i = 0;
406  while (data[i] != 0) {
407  i++;
408  }
409  return i;
410  }
411 
412 };
413 
414 
415 #endif
416 
417 /****************************************************************************/
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:382
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 SUMOReal _str2SUMOReal(const std::string &sData)
converts a string into the SUMOReal value described by it by calling the char-type converter ...
Definition: TplConvert.h:341
static std::string _2str(const bool var)
convert bool to string
Definition: TplConvert.h:71
#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 SUMOReal value described by it
Definition: TplConvert.h:391
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:290
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:364
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:276
static int getLength(const E *const data)
returns the length of the string (the position of the 0-character)
Definition: TplConvert.h:401
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:233
static SUMOReal _2SUMORealSec(const E *const data, SUMOReal def)
converts a 0-terminated char-type array into the SUMOReal value described by it
Definition: TplConvert.h:348
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 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
#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 std::string _2str(const char *const data, int length)
converts a char array into std::string considering the given length
Definition: TplConvert.h:118
#define SUMOReal
Definition: config.h:214
static std::string _2str(const SUMOReal var)
convert SUMOReal to string
Definition: TplConvert.h:64
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