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-sim.org/
12 // Copyright (C) 2001-2014 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  SUMOLong 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 
127  template<class E>
128  static int _hex2int(const E* const data) {
129  SUMOLong result = _hex2long(data);
130  if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
131  throw NumberFormatException();
132  }
133  return (int)result;
134  }
135 
136 
141  template<class E>
142  static SUMOLong _2long(const E* const data) {
143  if (data == 0 || data[0] == 0) {
144  throw EmptyData();
145  }
146  SUMOLong sgn = 1;
147  unsigned i = 0;
148  if (data[0] == '+') {
149  i++;
150  }
151  if (data[0] == '-') {
152  i++;
153  sgn = -1;
154  }
155  SUMOLong ret = 0;
156  for (; data[i] != 0; i++) {
157  ret *= 10;
158  // !!! need to catch overflows
159  char akt = (char) data[i];
160  if (akt < '0' || akt > '9') {
161  throw NumberFormatException();
162  }
163  ret += akt - 48;
164  }
165  if (i == 0) {
166  throw EmptyData();
167  }
168  return ret * sgn;
169  }
170 
171 
176  template<class E>
177  static SUMOLong _hex2long(const E* const data) {
178  if (data == 0 || data[0] == 0) {
179  throw EmptyData();
180  }
181  SUMOLong sgn = 1;
182  unsigned i = 0;
183  if (data[0] == '+') {
184  i++;
185  }
186  if (data[0] == '-') {
187  i++;
188  sgn = -1;
189  }
190  if (data[i] == '#') { // for html color codes
191  i++;
192  }
193  if (data[i] == '0' && (data[i + 1] == 'x' || data[i + 1] == 'X')) {
194  i += 2;
195  }
196  SUMOLong ret = 0;
197  for (; data[i] != 0; i++) {
198  ret *= 16;
199  // !!! need to catch overflows
200  char akt = (char) data[i];
201  if (akt >= '0' && akt <= '9') {
202  ret += akt - '0';
203  } else if (akt >= 'A' && akt <= 'F') {
204  ret += akt - 'A' + 10;
205  } else if (akt >= 'a' && akt <= 'f') {
206  ret += akt - 'a' + 10;
207  } else {
208  throw NumberFormatException();
209  }
210  }
211  if (i == 0) {
212  throw EmptyData();
213  }
214  return ret * sgn;
215  }
216 
217 
222  template<class E>
223  static SUMOReal _2SUMOReal(const E* const data) {
224  if (data == 0 || data[0] == 0) {
225  throw EmptyData();
226  }
227  int i = 0;
228  SUMOReal sgn = 1;
229  if (data[0] == '+') {
230  i++;
231  }
232  if (data[0] == '-') {
233  i++;
234  sgn = -1;
235  }
236  // we try to parse it as a SUMOLong storing the decimal point pos
237  int pointPos = -1;
238  int digits = std::numeric_limits<SUMOLong>::digits10;
239  SUMOLong ret = 0;
240  for (; data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
241  char akt = (char) data[i];
242  if (akt < '0' || akt > '9') {
243  if (pointPos < 0 && (akt == '.' || akt == ',')) {
244  pointPos = i;
245  continue;
246  }
247  throw NumberFormatException();
248  }
249  digits--;
250  if (digits >= 0) { // we skip the digits which don't fit into SUMOLong
251  ret = ret * 10 + akt - 48;
252  }
253  }
254  int exponent = digits >= 0 ? 0 : -digits;
255  if (pointPos != -1) {
256  exponent += pointPos - i + 1;
257  }
258  // check what has happened - end of string or exponent
259  if (data[i] == 0) {
260  return ret * sgn * (SUMOReal) pow(10.0, exponent);
261  }
262  // now the exponent
263  try {
264  return ret * sgn * (SUMOReal) pow(10.0, _2int(data + i + 1) + exponent);
265  } catch (EmptyData&) {
266  // the exponent was empty
267  throw NumberFormatException();
268  }
269  }
270 
271 
281  template<class E>
282  static bool _2bool(const E* const data) {
283  if (data == 0 || data[0] == 0) {
284  throw EmptyData();
285  }
286  std::string s = _2str(data);
287  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
288  if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
289  return true;
290  } else if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
291  return false;
292  } else {
293  throw BoolFormatException();
294  }
295  }
296 
297 
298  // conversion methods not throwing an exception
301  template<class E>
302  static std::string _2strSec(const E* const data,
303  const std::string& def) {
304  return _2strSec(data, getLength(data), def);
305  }
306 
307 
311  template<class E>
312  static int _2intSec(const E* const data, int def) {
313  if (data == 0 || data[0] == 0) {
314  return def;
315  }
316  return _2int(data);
317  }
318 
319 
323  template<class E>
324  static SUMOLong _2longSec(const E* const data, long def) {
325  if (data == 0 || data[0] == 0) {
326  return def;
327  }
328  return _2long(data);
329  }
330 
331 
335  template<class E>
336  static SUMOReal _2SUMORealSec(const E* const data, SUMOReal def) {
337  if (data == 0 || data[0] == 0) {
338  return def;
339  }
340  return _2SUMOReal(data);
341  }
342 
343 
351  template<class E>
352  static bool _2boolSec(const E* const data, bool def) {
353  if (data == 0 || data[0] == 0) {
354  return def;
355  }
356  return _2bool(data);
357  }
358 
359 
363  template<class E>
364  static std::string _2strSec(const E* const data, int length,
365  const std::string& def) {
366  if (data == 0 || length == 0) {
367  return def;
368  }
369  return _2str(data, length);
370  }
371 
372 
374  template<class E>
375  static unsigned getLength(const E* const data) {
376  if (data == 0) {
377  return 0;
378  }
379  unsigned i = 0;
380  while (data[i] != 0) {
381  i++;
382  }
383  return i;
384  }
385 
386 };
387 
388 
389 #endif
390 
391 /****************************************************************************/
static bool _2boolSec(const E *const data, bool def)
Definition: TplConvert.h:352
static int _hex2int(const E *const data)
Definition: TplConvert.h:128
static std::string _2str(const E *const data, unsigned length)
Definition: TplConvert.h:75
#define min(a, b)
Definition: polyfonts.c:66
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
static bool _2bool(const E *const data)
Definition: TplConvert.h:282
static SUMOReal _2SUMORealSec(const E *const data, SUMOReal def)
Definition: TplConvert.h:336
static std::string _2str(const char *const data)
Definition: TplConvert.h:63
static SUMOLong _hex2long(const E *const data)
Definition: TplConvert.h:177
static SUMOLong _2long(const E *const data)
Definition: TplConvert.h:142
#define max(a, b)
Definition: polyfonts.c:65
static std::string _2str(const E *const data)
Definition: TplConvert.h:56
#define SUMOLong
Definition: config.h:212
static std::string _2str(const char *const data, unsigned length)
Definition: TplConvert.h:101
static SUMOLong _2longSec(const E *const data, long def)
Definition: TplConvert.h:324
static unsigned getLength(const E *const data)
Definition: TplConvert.h:375
static int _2int(const E *const data)
Definition: TplConvert.h:114
#define SUMOReal
Definition: config.h:215
static int _2intSec(const E *const data, int def)
Definition: TplConvert.h:312
static std::string _2strSec(const E *const data, const std::string &def)
Definition: TplConvert.h:302
static std::string _2strSec(const E *const data, int length, const std::string &def)
Definition: TplConvert.h:364