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-2015 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 <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:
55  template<class E>
56  static inline std::string _2str(const E* const data) {
57  return _2str(data, getLength(data));
58  }
59 
60 
63  static inline std::string _2str(const char* const data) {
64  if (data == 0) {
65  throw EmptyData();
66  }
67  return std::string(data);
68  }
69 
70 
74  template<class E>
75  static inline std::string _2str(const E* const data, unsigned length) {
76  if (data == 0) {
77  throw EmptyData();
78  }
79  if (length == 0) {
80  return "";
81  }
82  char* buf = new char[length + 1];
83  unsigned i = 0;
84  for (i = 0; i < length; i++) {
85  if ((int) data[i] > 255) {
86  buf[i] = 63; // rudimentary damage control, replace with '?'
87  } else {
88  buf[i] = (char) data[i];
89  }
90  }
91  buf[i] = 0;
92  std::string ret = buf;
93  delete[] buf;
94  return ret;
95  }
96 
97 
101  static inline std::string _2str(const char* const data, unsigned length) {
102  if (data == 0) {
103  throw EmptyData();
104  }
105  return std::string(data, length);
106  }
107 
108 
113  template<class E>
114  static int _2int(const E* const data) {
115  long long int result = _2long(data);
116  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
117  throw NumberFormatException();
118  }
119  return (int)result;
120  }
121 
122 
128  static int _str2int(const std::string& sData) {
129  return _2int(sData.c_str());
130  }
131 
132 
137  template<class E>
138  static int _hex2int(const E* const data) {
139  long long int result = _hex2long(data);
140  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
141  throw NumberFormatException();
142  }
143  return (int)result;
144  }
145 
151  static int _strHex2int(const std::string& sData) {
152  return _hex2int(sData.c_str());
153  }
154 
155 
160  template<class E>
161  static long long int _2long(const E* const data) {
162  if (data == 0 || data[0] == 0) {
163  throw EmptyData();
164  }
165  long long int sgn = 1;
166  unsigned i = 0;
167  if (data[0] == '+') {
168  i++;
169  }
170  if (data[0] == '-') {
171  i++;
172  sgn = -1;
173  }
174  long long int ret = 0;
175  for (; data[i] != 0; i++) {
176  ret *= 10;
177  // !!! need to catch overflows
178  char akt = (char) data[i];
179  if (akt < '0' || akt > '9') {
180  throw NumberFormatException();
181  }
182  ret += akt - 48;
183  }
184  if (i == 0) {
185  throw EmptyData();
186  }
187  return ret * sgn;
188  }
189 
190 
195  template<class E>
196  static long long int _hex2long(const E* const data) {
197  if (data == 0 || data[0] == 0) {
198  throw EmptyData();
199  }
200  long long int sgn = 1;
201  unsigned i = 0;
202  if (data[0] == '+') {
203  i++;
204  }
205  if (data[0] == '-') {
206  i++;
207  sgn = -1;
208  }
209  if (data[i] == '#') { // for html color codes
210  i++;
211  }
212  if (data[i] == '0' && (data[i + 1] == 'x' || data[i + 1] == 'X')) {
213  i += 2;
214  }
215  long long int ret = 0;
216  for (; data[i] != 0; i++) {
217  ret *= 16;
218  // !!! need to catch overflows
219  char akt = (char) data[i];
220  if (akt >= '0' && akt <= '9') {
221  ret += akt - '0';
222  } else if (akt >= 'A' && akt <= 'F') {
223  ret += akt - 'A' + 10;
224  } else if (akt >= 'a' && akt <= 'f') {
225  ret += akt - 'a' + 10;
226  } else {
227  throw NumberFormatException();
228  }
229  }
230  if (i == 0) {
231  throw EmptyData();
232  }
233  return ret * sgn;
234  }
235 
236 
241  template<class E>
242  static SUMOReal _2SUMOReal(const E* const data) {
243  if (data == 0 || data[0] == 0) {
244  throw EmptyData();
245  }
246  int i = 0;
247  SUMOReal sgn = 1;
248  if (data[0] == '+') {
249  i++;
250  }
251  if (data[0] == '-') {
252  i++;
253  sgn = -1;
254  }
255  // we try to parse it as a long long int storing the decimal point pos
256  int pointPos = -1;
257  int digits = std::numeric_limits<long long int>::digits10;
258  long long int ret = 0;
259  for (; data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
260  char akt = (char) data[i];
261  if (akt < '0' || akt > '9') {
262  if (pointPos < 0 && (akt == '.' || akt == ',')) {
263  pointPos = i;
264  continue;
265  }
266  throw NumberFormatException();
267  }
268  digits--;
269  if (digits >= 0) { // we skip the digits which don't fit into long long int
270  ret = ret * 10 + akt - 48;
271  }
272  }
273  int exponent = digits >= 0 ? 0 : -digits;
274  if (pointPos != -1) {
275  exponent += pointPos - i + 1;
276  }
277  // check what has happened - end of string or exponent
278  if (data[i] == 0) {
279  return ret * sgn * (SUMOReal) pow(10.0, exponent);
280  }
281  // now the exponent
282  try {
283  return ret * sgn * (SUMOReal) pow(10.0, _2int(data + i + 1) + exponent);
284  } catch (EmptyData&) {
285  // the exponent was empty
286  throw NumberFormatException();
287  }
288  }
289 
290 
296  static SUMOReal _str2SUMOReal(const std::string& sData) {
297  return _2SUMOReal(sData.c_str());
298  }
299 
300 
310  template<class E>
311  static bool _2bool(const E* const data) {
312  if (data == 0 || data[0] == 0) {
313  throw EmptyData();
314  }
315  std::string s = _2str(data);
316  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
317  if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
318  return true;
319  } else if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
320  return false;
321  } else {
322  throw BoolFormatException();
323  }
324  }
325 
326 
327  // conversion methods not throwing an exception
330  template<class E>
331  static std::string _2strSec(const E* const data,
332  const std::string& def) {
333  return _2strSec(data, getLength(data), def);
334  }
335 
336 
340  template<class E>
341  static int _2intSec(const E* const data, int def) {
342  if (data == 0 || data[0] == 0) {
343  return def;
344  }
345  return _2int(data);
346  }
347 
348 
352  template<class E>
353  static long long int _2longSec(const E* const data, long def) {
354  if (data == 0 || data[0] == 0) {
355  return def;
356  }
357  return _2long(data);
358  }
359 
360 
364  template<class E>
365  static SUMOReal _2SUMORealSec(const E* const data, SUMOReal def) {
366  if (data == 0 || data[0] == 0) {
367  return def;
368  }
369  return _2SUMOReal(data);
370  }
371 
372 
380  template<class E>
381  static bool _2boolSec(const E* const data, bool def) {
382  if (data == 0 || data[0] == 0) {
383  return def;
384  }
385  return _2bool(data);
386  }
387 
388 
392  template<class E>
393  static std::string _2strSec(const E* const data, int length,
394  const std::string& def) {
395  if (data == 0 || length == 0) {
396  return def;
397  }
398  return _2str(data, length);
399  }
400 
401 
403  template<class E>
404  static unsigned getLength(const E* const data) {
405  if (data == 0) {
406  return 0;
407  }
408  unsigned i = 0;
409  while (data[i] != 0) {
410  i++;
411  }
412  return i;
413  }
414 
415 };
416 
417 
418 #endif
419 
420 /****************************************************************************/
static bool _2boolSec(const E *const data, bool def)
Definition: TplConvert.h:381
static int _hex2int(const E *const data)
Definition: TplConvert.h:138
static std::string _2str(const E *const data, unsigned length)
Definition: TplConvert.h:75
static SUMOReal _str2SUMOReal(const std::string &sData)
Definition: TplConvert.h:296
#define min(a, b)
Definition: polyfonts.c:66
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:242
static bool _2bool(const E *const data)
Definition: TplConvert.h:311
static long long int _2longSec(const E *const data, long def)
Definition: TplConvert.h:353
static long long int _hex2long(const E *const data)
Definition: TplConvert.h:196
static SUMOReal _2SUMORealSec(const E *const data, SUMOReal def)
Definition: TplConvert.h:365
static std::string _2str(const char *const data)
Definition: TplConvert.h:63
static long long int _2long(const E *const data)
Definition: TplConvert.h:161
#define max(a, b)
Definition: polyfonts.c:65
static std::string _2str(const E *const data)
Definition: TplConvert.h:56
static int _str2int(const std::string &sData)
Definition: TplConvert.h:128
static std::string _2str(const char *const data, unsigned length)
Definition: TplConvert.h:101
static unsigned getLength(const E *const data)
Definition: TplConvert.h:404
static int _2int(const E *const data)
Definition: TplConvert.h:114
#define SUMOReal
Definition: config.h:214
static int _strHex2int(const std::string &sData)
Definition: TplConvert.h:151
static int _2intSec(const E *const data, int def)
Definition: TplConvert.h:341
static std::string _2strSec(const E *const data, const std::string &def)
Definition: TplConvert.h:331
static std::string _2strSec(const E *const data, int length, const std::string &def)
Definition: TplConvert.h:393