SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // Some conversion methods (from strings to other) 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 #ifndef TplConvert_h 00023 #define TplConvert_h 00024 00025 00026 // =========================================================================== 00027 // included modules 00028 // =========================================================================== 00029 #ifdef _MSC_VER 00030 #include <windows_config.h> 00031 #else 00032 #include <config.h> 00033 #endif 00034 00035 #include <string> 00036 #include <cmath> 00037 #include <climits> 00038 #include <utils/common/UtilExceptions.h> 00039 #include <utils/common/StdDefs.h> 00040 00041 00042 // =========================================================================== 00043 // class definitions 00044 // =========================================================================== 00050 template<class E> 00051 class TplConvert { 00052 public: 00053 // conversion methods without a length 00056 static std::string _2str(const E* const data) { 00057 return _2str(data, getLength(data)); 00058 } 00059 00060 00066 static int _2int(const E* const data) { 00067 return _2int(data, INT_MAX); 00068 } 00069 00070 00076 static long _2long(const E* const data) { 00077 return _2long(data, INT_MAX); 00078 } 00079 00080 00086 static SUMOReal _2SUMOReal(const E* const data) { 00087 return _2SUMOReal(data, INT_MAX); 00088 } 00089 00090 00096 static bool _2bool(const E* const data) { 00097 return _2bool(data, 1); 00098 } 00099 00100 00104 static char* _2charp(const E* const data) { 00105 return _2charp(data, getLength(data)); 00106 } 00107 00108 00109 // conversion methods with a length 00113 static std::string _2str(const E* const data, unsigned length) { 00114 if (data == 0) { 00115 throw EmptyData(); 00116 } 00117 if (length == 0) { 00118 return ""; 00119 } 00120 char* buf = new char[length + 1]; 00121 unsigned i = 0; 00122 for (i = 0; i < length; i++) { 00123 if ((int) data[i] > 255) { 00124 buf[i] = 63; // rudimentary damage control, replace with '?' 00125 } else { 00126 buf[i] = (char) data[i]; 00127 } 00128 } 00129 buf[i] = 0; 00130 std::string ret = buf; 00131 delete[] buf; 00132 return ret; 00133 } 00134 00135 00141 static int _2int(const E* const data, unsigned length) { 00142 if (data == 0 || length == 0 || data[0] == 0) { 00143 throw EmptyData(); 00144 } 00145 int sgn = 1; 00146 unsigned i = 0; 00147 if (data[0] == '+') { 00148 i++; 00149 } 00150 if (data[0] == '-') { 00151 i++; 00152 sgn = -1; 00153 } 00154 int val = 0; 00155 for (; i < length && data[i] != 0; i++) { 00156 val = val * 10; 00157 char akt = (char) data[i]; 00158 if (akt < '0' || akt > '9') { 00159 throw NumberFormatException(); 00160 } 00161 val = val + akt - 48; 00162 } 00163 if (i == 0) { 00164 throw EmptyData(); 00165 } 00166 return val * sgn; 00167 } 00168 00169 00175 static long _2long(const E* const data, unsigned length) { 00176 if (data == 0 || length == 0 || data[0] == 0) { 00177 throw EmptyData(); 00178 } 00179 long sgn = 1; 00180 unsigned i = 0; 00181 if (data[0] == '+') { 00182 i++; 00183 } 00184 if (data[0] == '-') { 00185 i++; 00186 sgn = -1; 00187 } 00188 long ret = 0; 00189 for (; i < length && data[i] != 0; i++) { 00190 ret = ret * 10; 00191 char akt = (char) data[i]; 00192 if (akt < '0' || akt > '9') { 00193 throw NumberFormatException(); 00194 } 00195 ret = ret + akt - 48; 00196 } 00197 if (i == 0) { 00198 throw EmptyData(); 00199 } 00200 return ret * sgn; 00201 } 00202 00203 00209 static SUMOReal _2SUMOReal(const E* const data, unsigned length) { 00210 if (data == 0 || length == 0 || data[0] == 0) { 00211 throw EmptyData(); 00212 } 00213 SUMOReal ret = 0; 00214 unsigned i = 0; 00215 SUMOReal sgn = 1; 00216 if (data[0] == '+') { 00217 i++; 00218 } 00219 if (data[0] == '-') { 00220 i++; 00221 sgn = -1; 00222 } 00223 for (; i < length && data[i] != 0 && data[i] != '.' && data[i] != ',' && data[i] != 'e' && data[i] != 'E'; i++) { 00224 ret = ret * 10; 00225 char akt = (char) data[i]; 00226 if (akt < '0' || akt > '9') { 00227 throw NumberFormatException(); 00228 } 00229 ret = ret + akt - 48; 00230 } 00231 // check what has happened - end of string, e or decimal point 00232 if ((char) data[i] != '.' && (char) data[i] != ',' && data[i] != 'e' && data[i] != 'E') { 00233 if (i == 0) { 00234 throw EmptyData(); 00235 } 00236 return ret * sgn; 00237 } 00238 if (data[i] == 'e' || data[i] == 'E') { 00239 // no decimal point, just an exponent 00240 try { 00241 int exp = _2int(data + i + 1, length - i - 1); 00242 SUMOReal exp2 = (SUMOReal) pow(10.0, exp); 00243 return ret * sgn * exp2; 00244 } catch (EmptyData&) { 00245 // the exponent was empty 00246 throw NumberFormatException(); 00247 } 00248 } 00249 SUMOReal div = 10; 00250 // skip the dot 00251 i++; 00252 // parse values behin decimal point 00253 for (; i < length && data[i] != 0 && data[i] != 'e' && data[i] != 'E'; i++) { 00254 char akt = (char) data[i]; 00255 if (akt < '0' || akt > '9') { 00256 throw NumberFormatException(); 00257 } 00258 ret = ret + ((SUMOReal)(akt - 48)) / div; 00259 div = div * 10; 00260 } 00261 if (data[i] != 'e' && data[i] != 'E') { 00262 // no exponent 00263 return ret * sgn; 00264 } 00265 // eponent and decimal dot 00266 try { 00267 int exp = _2int(data + i + 1, length - i - 1); 00268 SUMOReal exp2 = (SUMOReal) pow(10.0, exp); 00269 return ret * sgn * exp2; 00270 } catch (EmptyData&) { 00271 // the exponent was empty 00272 throw NumberFormatException(); 00273 } 00274 } 00275 00276 00282 static bool _2bool(const E* const data, unsigned length) { 00283 if (data == 0 || length == 0 || data[0] == 0) { 00284 throw EmptyData(); 00285 } 00286 char akt = (char) data[0]; 00287 if (akt == '1' || akt == 'x' || akt == 't' || akt == 'T') { 00288 return true; 00289 } 00290 if (akt == '0' || akt == '-' || akt == 'f' || akt == 'F') { 00291 return false; 00292 } 00293 throw BoolFormatException(); 00294 } 00295 00296 00300 static char* _2charp(const E* const data, int length) { 00301 if (length == 0 || data == 0) { 00302 throw EmptyData(); 00303 } 00304 char* ret = new char[length + 1]; 00305 unsigned i = 0; 00306 for (; i < length; i++) { 00307 ret[i] = (char) data[i]; 00308 } 00309 ret[i] = 0; 00310 return ret; 00311 } 00312 00313 00315 static unsigned getLength(const E* const data) { 00316 if (data == 0) { 00317 return 0; 00318 } 00319 unsigned i = 0; 00320 while (data[i] != 0) { 00321 i++; 00322 } 00323 return i; 00324 } 00325 00326 }; 00327 00328 00329 #endif 00330 00331 /****************************************************************************/ 00332