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 <climits>
39 #include <utils/common/StdDefs.h>
40 
41 
42 // ===========================================================================
43 // class definitions
44 // ===========================================================================
50 template<class E>
51 class TplConvert {
52 public:
53  // conversion methods without a length
56  static std::string _2str(const E* const data) {
57  return _2str(data, getLength(data));
58  }
59 
60 
66  static int _2int(const E* const data) {
67  return _2int(data, INT_MAX);
68  }
69 
70 
76  static long _2long(const E* const data) {
77  return _2long(data, INT_MAX);
78  }
79 
80 
86  static SUMOReal _2SUMOReal(const E* const data) {
87  return _2SUMOReal(data, INT_MAX);
88  }
89 
90 
96  static bool _2bool(const E* const data) {
97  return _2bool(data, 1);
98  }
99 
100 
104  static char* _2charp(const E* const data) {
105  return _2charp(data, getLength(data));
106  }
107 
108 
109  // conversion methods with a length
113  static std::string _2str(const E* const data, unsigned length) {
114  if (data == 0) {
115  throw EmptyData();
116  }
117  if (length == 0) {
118  return "";
119  }
120  char* buf = new char[length + 1];
121  unsigned i = 0;
122  for (i = 0; i < length; i++) {
123  if ((int) data[i] > 255) {
124  buf[i] = 63; // rudimentary damage control, replace with '?'
125  } else {
126  buf[i] = (char) data[i];
127  }
128  }
129  buf[i] = 0;
130  std::string ret = buf;
131  delete[] buf;
132  return ret;
133  }
134 
135 
141  static int _2int(const E* const data, unsigned length) {
142  if (data == 0 || length == 0 || data[0] == 0) {
143  throw EmptyData();
144  }
145  int sgn = 1;
146  unsigned i = 0;
147  if (data[0] == '+') {
148  i++;
149  }
150  if (data[0] == '-') {
151  i++;
152  sgn = -1;
153  }
154  int val = 0;
155  for (; i < length && data[i] != 0; i++) {
156  val = val * 10;
157  char akt = (char) data[i];
158  if (akt < '0' || akt > '9') {
159  throw NumberFormatException();
160  }
161  val = val + akt - 48;
162  }
163  if (i == 0) {
164  throw EmptyData();
165  }
166  return val * sgn;
167  }
168 
169 
175  static long _2long(const E* const data, unsigned length) {
176  if (data == 0 || length == 0 || data[0] == 0) {
177  throw EmptyData();
178  }
179  long sgn = 1;
180  unsigned i = 0;
181  if (data[0] == '+') {
182  i++;
183  }
184  if (data[0] == '-') {
185  i++;
186  sgn = -1;
187  }
188  long ret = 0;
189  for (; i < length && data[i] != 0; i++) {
190  ret = ret * 10;
191  char akt = (char) data[i];
192  if (akt < '0' || akt > '9') {
193  throw NumberFormatException();
194  }
195  ret = ret + akt - 48;
196  }
197  if (i == 0) {
198  throw EmptyData();
199  }
200  return ret * sgn;
201  }
202 
203 
209  static SUMOReal _2SUMOReal(const E* const data, unsigned length) {
210  if (data == 0 || length == 0 || data[0] == 0) {
211  throw EmptyData();
212  }
213  SUMOReal ret = 0;
214  unsigned i = 0;
215  SUMOReal sgn = 1;
216  if (data[0] == '+') {
217  i++;
218  }
219  if (data[0] == '-') {
220  i++;
221  sgn = -1;
222  }
223  for (; i < length && data[i] != 0 && data[i] != '.' && data[i] != ',' && data[i] != 'e' && data[i] != 'E'; i++) {
224  ret = ret * 10;
225  char akt = (char) data[i];
226  if (akt < '0' || akt > '9') {
227  throw NumberFormatException();
228  }
229  ret = ret + akt - 48;
230  }
231  // check what has happened - end of string, e or decimal point
232  if ((char) data[i] != '.' && (char) data[i] != ',' && data[i] != 'e' && data[i] != 'E') {
233  if (i == 0) {
234  throw EmptyData();
235  }
236  return ret * sgn;
237  }
238  if (data[i] == 'e' || data[i] == 'E') {
239  // no decimal point, just an exponent
240  try {
241  int exp = _2int(data + i + 1, length - i - 1);
242  SUMOReal exp2 = (SUMOReal) pow(10.0, exp);
243  return ret * sgn * exp2;
244  } catch (EmptyData&) {
245  // the exponent was empty
246  throw NumberFormatException();
247  }
248  }
249  SUMOReal div = 10;
250  // skip the dot
251  i++;
252  // parse values behin decimal point
253  for (; i < length && data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) {
254  char akt = (char) data[i];
255  if (akt < '0' || akt > '9') {
256  throw NumberFormatException();
257  }
258  ret = ret + ((SUMOReal)(akt - 48)) / div;
259  div = div * 10;
260  }
261  if (data[i] != 'e' && data[i] != 'E') {
262  // no exponent
263  return ret * sgn;
264  }
265  // eponent and decimal dot
266  try {
267  int exp = _2int(data + i + 1, length - i - 1);
268  SUMOReal exp2 = (SUMOReal) pow(10.0, exp);
269  return ret * sgn * exp2;
270  } catch (EmptyData&) {
271  // the exponent was empty
272  throw NumberFormatException();
273  }
274  }
275 
276 
282  static bool _2bool(const E* const data, unsigned length) {
283  if (data == 0 || length == 0 || data[0] == 0) {
284  throw EmptyData();
285  }
286  char akt = (char) data[0];
287  if (akt == '1' || akt == 'x' || akt == 't' || akt == 'T') {
288  return true;
289  }
290  if (akt == '0' || akt == '-' || akt == 'f' || akt == 'F') {
291  return false;
292  }
293  throw BoolFormatException();
294  }
295 
296 
300  static char* _2charp(const E* const data, int length) {
301  if (length == 0 || data == 0) {
302  throw EmptyData();
303  }
304  char* ret = new char[length + 1];
305  unsigned i = 0;
306  for (; i < length; i++) {
307  ret[i] = (char) data[i];
308  }
309  ret[i] = 0;
310  return ret;
311  }
312 
313 
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 /****************************************************************************/
332