SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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.sourceforge.net/
12 // Copyright (C) 2001-2012 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 SUMOLong _2long(const E* const data) {
129  if (data == 0 || data[0] == 0) {
130  throw EmptyData();
131  }
132  SUMOLong sgn = 1;
133  unsigned i = 0;
134  if (data[0] == '+') {
135  i++;
136  }
137  if (data[0] == '-') {
138  i++;
139  sgn = -1;
140  }
141  SUMOLong ret = 0;
142  for (; data[i] != 0; i++) {
143  ret *= 10;
144  // !!! need to catch overflows
145  char akt = (char) data[i];
146  if (akt < '0' || akt > '9') {
147  throw NumberFormatException();
148  }
149  ret += akt - 48;
150  }
151  if (i == 0) {
152  throw EmptyData();
153  }
154  return ret * sgn;
155  }
156 
157 
162  template<class E>
163  static SUMOReal _2SUMOReal(const E* const data) {
164  if (data == 0 || data[0] == 0) {
165  throw EmptyData();
166  }
167  unsigned i = 0;
168  SUMOReal sgn = 1;
169  if (data[0] == '+') {
170  i++;
171  }
172  if (data[0] == '-') {
173  i++;
174  sgn = -1;
175  }
176  // we try to parse it as a SUMOLong storing the decimal point pos
177  int pointPos = -1;
178  int digits = std::numeric_limits<SUMOLong>::digits10;
179  SUMOLong ret = 0;
180  for (; data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
181  char akt = (char) data[i];
182  if (akt < '0' || akt > '9') {
183  if (pointPos < 0 && (akt == '.' || akt == ',')) {
184  pointPos = i;
185  continue;
186  }
187  throw NumberFormatException();
188  }
189  digits--;
190  if (digits >= 0) { // we skip the digits which don't fit into SUMOLong
191  ret = ret * 10 + akt - 48;
192  }
193  }
194  int exponent = digits >= 0 ? 0 : -digits;
195  if (pointPos != -1) {
196  exponent += pointPos - i + 1;
197  }
198  // check what has happened - end of string or exponent
199  if (data[i] == 0) {
200  return ret * sgn * (SUMOReal) pow(10.0, exponent);
201  }
202  // now the exponent
203  try {
204  return ret * sgn * (SUMOReal) pow(10.0, _2int(data + i + 1) + exponent);
205  } catch (EmptyData&) {
206  // the exponent was empty
207  throw NumberFormatException();
208  }
209  }
210 
211 
221  template<class E>
222  static bool _2bool(const E* const data) {
223  if (data == 0 || data[0] == 0) {
224  throw EmptyData();
225  }
226  std::string s = _2str(data);
227  std::transform(s.begin(), s.end(), s.begin(), tolower);
228  if (s == "1" || s == "yes" || s == "true" || s == "on" || s == "x" || s == "t") {
229  return true;
230  } else if (s == "0" || s == "no" || s == "false" || s == "off" || s == "-" || s == "f") {
231  return false;
232  } else {
233  throw BoolFormatException();
234  }
235  }
236 
237 
238  // conversion methods not throwing an exception
241  template<class E>
242  static std::string _2strSec(const E* const data,
243  const std::string& def) {
244  return _2strSec(data, getLength(data), def);
245  }
246 
247 
251  template<class E>
252  static int _2intSec(const E* const data, int def) {
253  if (data == 0 || data[0] == 0) {
254  return def;
255  }
256  return _2int(data);
257  }
258 
259 
263  template<class E>
264  static SUMOLong _2longSec(const E* const data, long def) {
265  if (data == 0 || data[0] == 0) {
266  return def;
267  }
268  return _2long(data);
269  }
270 
271 
275  template<class E>
276  static SUMOReal _2SUMORealSec(const E* const data, SUMOReal def) {
277  if (data == 0 || data[0] == 0) {
278  return def;
279  }
280  return _2SUMOReal(data);
281  }
282 
283 
291  template<class E>
292  static bool _2boolSec(const E* const data, bool def) {
293  if (data == 0 || data[0] == 0) {
294  return def;
295  }
296  return _2bool(data);
297  }
298 
299 
303  template<class E>
304  static std::string _2strSec(const E* const data, int length,
305  const std::string& def) {
306  if (data == 0 || length == 0) {
307  return def;
308  }
309  return _2str(data, length);
310  }
311 
312 
314  template<class E>
315  static unsigned getLength(const E* const data) {
316  if (data == 0) {
317  return 0;
318  }
319  unsigned i = 0;
320  while (data[i] != 0) {
321  i++;
322  }
323  return i;
324  }
325 
326 };
327 
328 
329 #endif
330 
331 /****************************************************************************/