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 2001-2010 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             buf[i] = (char) data[i];
00121         }
00122         buf[i] = 0;
00123         std::string ret = buf;
00124         delete[] buf;
00125         return ret;
00126     }
00127 
00128 
00134     static int _2int(const E * const data, unsigned length) {
00135         if (data==0||length==0||data[0]==0) {
00136             throw EmptyData();
00137         }
00138         int sgn = 1;
00139         unsigned i=0;
00140         if (data[0]=='+') {
00141             i++;
00142         }
00143         if (data[0]=='-') {
00144             i++;
00145             sgn = -1;
00146         }
00147         int val = 0;
00148         for (; i<length&&data[i]!=0; i++) {
00149             val = val * 10;
00150             char akt = (char) data[i];
00151             if (akt<'0'||akt>'9') {
00152                 throw NumberFormatException();
00153             }
00154             val = val + akt - 48;
00155         }
00156         if (i==0) {
00157             throw EmptyData();
00158         }
00159         return val * sgn;
00160     }
00161 
00162 
00168     static long _2long(const E * const data, unsigned length) {
00169         if (data==0||length==0||data[0]==0) {
00170             throw EmptyData();
00171         }
00172         long sgn = 1;
00173         unsigned i=0;
00174         if (data[0]=='+') {
00175             i++;
00176         }
00177         if (data[0]=='-') {
00178             i++;
00179             sgn = -1;
00180         }
00181         long ret = 0;
00182         for (; i<length&&data[i]!=0; i++) {
00183             ret = ret * 10;
00184             char akt = (char) data[i];
00185             if (akt<'0'||akt>'9') {
00186                 throw NumberFormatException();
00187             }
00188             ret = ret + akt - 48;
00189         }
00190         if (i==0) {
00191             throw EmptyData();
00192         }
00193         return ret * sgn;
00194     }
00195 
00196 
00202     static SUMOReal _2SUMOReal(const E * const data, unsigned length) {
00203         if (data==0||length==0||data[0]==0) {
00204             throw EmptyData();
00205         }
00206         SUMOReal ret = 0;
00207         unsigned i = 0;
00208         SUMOReal sgn = 1;
00209         if (data[0]=='+') {
00210             i++;
00211         }
00212         if (data[0]=='-') {
00213             i++;
00214             sgn = -1;
00215         }
00216         for (; i<length&&data[i]!=0&&data[i]!='.'&&data[i]!=','&&data[i]!='e'&&data[i]!='E'; i++) {
00217             ret = ret * 10;
00218             char akt = (char) data[i];
00219             if (akt<'0'||akt>'9') {
00220                 throw NumberFormatException();
00221             }
00222             ret = ret + akt - 48;
00223         }
00224         // check what has happened - end of string, e or decimal point
00225         if ((char) data[i]!='.'&&(char) data[i]!=','&&data[i]!='e'&&data[i]!='E') {
00226             if (i==0) {
00227                 throw EmptyData();
00228             }
00229             return ret * sgn;
00230         }
00231         if (data[i]=='e'||data[i]=='E') {
00232             // no decimal point, just an exponent
00233             try {
00234                 int exp = _2int(data+i+1, length-i-1);
00235                 SUMOReal exp2 = (SUMOReal) pow(10.0, exp);
00236                 return ret*sgn*exp2;
00237             } catch (EmptyData&) {
00238                 // the exponent was empty
00239                 throw NumberFormatException();
00240             }
00241         }
00242         SUMOReal div = 10;
00243         // skip the dot
00244         i++;
00245         // parse values behin decimal point
00246         for (; i<length&&data[i]!=0&&data[i]!='e'&&data[i]!='E'; i++) {
00247             char akt = (char) data[i];
00248             if (akt<'0'||akt>'9') {
00249                 throw NumberFormatException();
00250             }
00251             ret = ret + ((SUMOReal)(akt - 48)) / div;
00252             div = div * 10;
00253         }
00254         if (data[i]!='e'&&data[i]!='E') {
00255             // no exponent
00256             return ret * sgn;
00257         }
00258         // eponent and decimal dot
00259         try {
00260             int exp = _2int(data+i+1, length-i-1);
00261             SUMOReal exp2 = (SUMOReal) pow(10.0, exp);
00262             return ret*sgn*exp2;
00263         } catch (EmptyData&) {
00264             // the exponent was empty
00265             throw NumberFormatException();
00266         }
00267     }
00268 
00269 
00275     static bool _2bool(const E * const data, unsigned length) {
00276         if (data==0||length==0||data[0]==0) {
00277             throw EmptyData();
00278         }
00279         char akt = (char) data[0];
00280         if (akt=='1' || akt=='x' || akt=='t' || akt=='T') {
00281             return true;
00282         }
00283         if (akt=='0' || akt=='-' || akt=='f' || akt=='F') {
00284             return false;
00285         }
00286         throw BoolFormatException();
00287     }
00288 
00289 
00293     static char *_2charp(const E * const data, int length) {
00294         if (length==0||data==0) {
00295             throw EmptyData();
00296         }
00297         char *ret = new char[length+1];
00298         unsigned i = 0;
00299         for (; i<length; i++) {
00300             ret[i] = (char) data[i];
00301         }
00302         ret[i] = 0;
00303         return ret;
00304     }
00305 
00306 
00308     static unsigned getLength(const E * const data) {
00309         if (data==0) {
00310             return 0;
00311         }
00312         unsigned i = 0;
00313         for (; data[i]!=0; i++);
00314         return i;
00315     }
00316 
00317 };
00318 
00319 
00320 #endif
00321 
00322 /****************************************************************************/
00323 

Generated on Wed May 5 00:06:37 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6