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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Some conversion methods (from strings to other)
18 /****************************************************************************/
19 #ifndef TplConvert_h
20 #define TplConvert_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <sstream>
30 #include <cmath>
31 #include <limits>
32 #include <algorithm>
34 #include <utils/common/StdDefs.h>
35 
36 
37 // ===========================================================================
38 // class definitions
39 // ===========================================================================
45 class TplConvert {
46 public:
48 
49  static inline std::string _2str(const int var) {
51  std::ostringstream convert;
52  convert << var;
53  return convert.str();
54  }
55 
57  static inline std::string _2str(const double var) {
58  std::ostringstream convert;
59  convert << var;
60  return convert.str();
61  }
62 
64  static inline std::string _2str(const bool var) {
65  return (var == true ? "true" : "false");
66  }
67 
71  template<class E>
72  static inline std::string _2str(const E* const data) {
73  return _2str(data, getLength(data));
74  }
75 
79  static inline std::string _2str(const char* const data) {
80  if (data == 0) {
81  throw EmptyData();
82  }
83  return std::string(data);
84  }
85 
89  template<class E>
90  static inline std::string _2str(const E* const data, int length) {
91  if (data == 0) {
92  throw EmptyData();
93  }
94  if (length == 0) {
95  return "";
96  }
97  char* buf = new char[length + 1];
98  int i = 0;
99  for (i = 0; i < length; i++) {
100  if ((int) data[i] > 255) {
101  buf[i] = 63; // rudimentary damage control, replace with '?'
102  } else {
103  buf[i] = (char) data[i];
104  }
105  }
106  buf[i] = 0;
107  std::string ret = buf;
108  delete[] buf;
109  return ret;
110  }
111 
115  static inline std::string _2str(const char* const data, int length) {
116  if (data == 0) {
117  throw EmptyData();
118  }
119  return std::string(data, length);
120  }
121 
125  template<class E>
126  static std::string _2strSec(const E* const data, const std::string& def) {
127  return _2strSec(data, getLength(data), def);
128  }
129 
133  template<class E>
134  static std::string _2strSec(const E* const data, int length, const std::string& def) {
135  if (data == 0 || length == 0) {
136  return def;
137  }
138  return _2str(data, length);
139  }
141 
143 
144 
148  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 
161  static int _str2int(const std::string& sData) {
162  return _2int(sData.c_str());
163  }
164 
169  template<class E>
170  static int _hex2int(const E* const data) {
171  long long int result = _hex2long(data);
172  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
173  throw NumberFormatException();
174  }
175  return (int)result;
176  }
177 
182  static int _strHex2int(const std::string& sData) {
183  return _hex2int(sData.c_str());
184  }
185 
188  template<class E>
189  static int _2intSec(const E* const data, int def) {
190  if (data == 0 || data[0] == 0) {
191  return def;
192  }
193  return _2int(data);
194  }
196 
198 
199 
203  template<class E>
204  static long long int _2long(const E* const data) {
205  if (data == 0 || data[0] == 0) {
206  throw EmptyData();
207  }
208  long long int sgn = 1;
209  int i = 0;
210  if (data[0] == '+') {
211  i++;
212  }
213  if (data[0] == '-') {
214  i++;
215  sgn = -1;
216  }
217  long long int ret = 0;
218  for (; data[i] != 0; i++) {
219  ret *= 10;
220  // !!! need to catch overflows
221  char akt = (char) data[i];
222  if (akt < '0' || akt > '9') {
223  throw NumberFormatException();
224  }
225  ret += akt - 48;
226  }
227  if (i == 0) {
228  throw EmptyData();
229  }
230  return ret * sgn;
231  }
232 
237  static long long int _str2long(const std::string& sData) {
238  return _2long(sData.c_str());
239  }
240 
245  template<class E>
246  static long long int _hex2long(const E* const data) {
247  if (data == 0 || data[0] == 0) {
248  throw EmptyData();
249  }
250  long long int sgn = 1;
251  int i = 0;
252  if (data[0] == '+') {
253  i++;
254  }
255  if (data[0] == '-') {
256  i++;
257  sgn = -1;
258  }
259  if (data[i] == '#') { // for html color codes
260  i++;
261  }
262  if (data[i] == '0' && (data[i + 1] == 'x' || data[i + 1] == 'X')) {
263  i += 2;
264  }
265  long long int ret = 0;
266  for (; data[i] != 0; i++) {
267  ret *= 16;
268  // !!! need to catch overflows
269  char akt = (char) data[i];
270  if (akt >= '0' && akt <= '9') {
271  ret += akt - '0';
272  } else if (akt >= 'A' && akt <= 'F') {
273  ret += akt - 'A' + 10;
274  } else if (akt >= 'a' && akt <= 'f') {
275  ret += akt - 'a' + 10;
276  } else {
277  throw NumberFormatException();
278  }
279  }
280  if (i == 0) {
281  throw EmptyData();
282  }
283  return ret * sgn;
284  }
285 
289  template<class E>
290  static long long int _2longSec(const E* const data, long def) {
291  if (data == 0 || data[0] == 0) {
292  return def;
293  }
294  return _2long(data);
295  }
297 
299 
300 
304  template<class E>
305  static double _2double(const E* const data) {
306  if (data == 0 || data[0] == 0) {
307  throw EmptyData();
308  }
309  int i = 0;
310  double sgn = 1;
311  if (data[0] == '+') {
312  i++;
313  }
314  if (data[0] == '-') {
315  i++;
316  sgn = -1;
317  }
318  // we try to parse it as a long long int storing the decimal point pos
319  int pointPos = -1;
320  int digits = std::numeric_limits<long long int>::digits10;
321  long long int ret = 0;
322  for (; data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
323  char akt = (char) data[i];
324  if (akt < '0' || akt > '9') {
325  if (pointPos < 0 && (akt == '.' || akt == ',')) {
326  pointPos = i;
327  continue;
328  }
329  throw NumberFormatException();
330  }
331  digits--;
332  if (digits >= 0) { // we skip the digits which don't fit into long long int
333  ret = ret * 10 + akt - 48;
334  }
335  }
336  int exponent = digits >= 0 ? 0 : -digits;
337  if (pointPos != -1) {
338  exponent += pointPos - i + 1;
339  }
340  // check what has happened - end of string or exponent
341  if (data[i] == 0) {
342  return ret * sgn * (double) pow(10.0, exponent);
343  }
344  // now the exponent
345  try {
346  return ret * sgn * (double) pow(10.0, _2int(data + i + 1) + exponent);
347  } catch (EmptyData&) {
348  // the exponent was empty
349  throw NumberFormatException();
350  }
351  }
352 
357  static double _str2double(const std::string& sData) {
358  return _2double(sData.c_str());
359  }
360 
364  template<class E>
365  static double _2doubleSec(const E* const data, double def) {
366  if (data == 0 || data[0] == 0) {
367  return def;
368  }
369  return _2double(data);
370  }
372 
374 
375 
381  template<class E>
382  static bool _2bool(const E* const data) {
383  if (data == 0 || data[0] == 0) {
384  throw EmptyData();
385  }
386  std::string s = _2str(data);
387  // Don't use std::transform(..., ::tolower) due a C4244 Warning in MSVC17
388  for (int i = 0; i < (int)s.length(); i++) {
389  s[i] = (char)::tolower((char)s[i]);
390  }
391  if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
392  return true;
393  } else if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
394  return false;
395  } else {
396  throw BoolFormatException();
397  }
398  }
399 
404  static bool _str2Bool(const std::string& sData) {
405  return _2bool(sData.c_str());
406  }
407 
412  template<class E>
413  static bool _2boolSec(const E* const data, bool def) {
414  if (data == 0 || data[0] == 0) {
415  return def;
416  }
417  return _2bool(data);
418  }
420 
422  template<class E>
423  static int getLength(const E* const data) {
424  if (data == 0) {
425  return 0;
426  }
427  int i = 0;
428  while (data[i] != 0) {
429  i++;
430  }
431  return i;
432  }
433 };
434 
435 
436 #endif
437 
438 /****************************************************************************/
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:404
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:170
static std::string _2str(const char *const data, int length)
converts a char array into std::string considering the given length
Definition: TplConvert.h:115
static std::string _2str(const bool var)
convert bool to string
Definition: TplConvert.h:64
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:365
*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:413
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:382
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:290
static int getLength(const E *const data)
returns the length of the string (the position of the 0-character)
Definition: TplConvert.h:423
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:246
static std::string _2str(const char *const data)
converts a 0-terminated char array into std::string
Definition: TplConvert.h:79
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:204
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:237
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:90
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:357
static std::string _2str(const double var)
convert double to string
Definition: TplConvert.h:57
static std::string _2str(const E *const data)
converts a 0-terminated char-type array into std::string
Definition: TplConvert.h:72
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:161
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:305
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:182
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:189
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:50
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:126
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:134