SUMO - Simulation of Urban MObility

TplConvert.h

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