conversions.cpp

Go to the documentation of this file.
00001 /* MAINATINER: Bernard CURENT_EDITOR: Bernard */
00002 
00003 #include <cstdlib>
00004 #include <cerrno>
00005 #include <sstream>
00006 #include "conversions.h"
00007 #include "log.h"
00008 
00009 using namespace std;
00010 
00011 /*****************************************************************************/
00012 string itos(int i) 
00013 {
00014         stringstream s;
00015         s << i;
00016         return s.str();
00017 }
00018 
00019 /*****************************************************************************/
00020 string iptos(unsigned long ip)
00021 {
00022         
00023 //      unsigned long l = ntohl(ip);
00024 
00025         stringstream s;
00026         /*
00027         s << ((l & 0xFF000000) >> 24)  << "."
00028           << ((l &   0xFF0000) >> 16)  << "."
00029           << ((l &     0xFF00) >>  8)  << "."
00030           << ((l &       0xFF) >>  0);
00031         */
00032         return s.str();
00033 }
00034 
00035 
00036 
00037 /*****************************************************************************/
00038 bool try_get_long(const string& value, long &i)
00039 {
00040         char *endptr;
00041         const char *orig = value.c_str();
00042         errno = 0;
00043         i = strtol(orig, &endptr, 0);
00044         if (errno != 0) 
00045         {
00046                 g_log.perror("try_get_long strtol");
00047                 return false;
00048         }
00049         if (orig == endptr) return false;
00050         if (*endptr == '\0') return true;
00051         
00052         // ?
00053         return false;
00054 }
00055 
00056 /*****************************************************************************/
00057 bool try_get_double(const string& value, double &d)
00058 {
00059         char *endptr;
00060         const char *orig = value.c_str();
00061         errno = 0;
00062         d = strtod(orig, &endptr);
00063         if (errno != 0) 
00064         {
00065                 g_log.perror("try_get_double strtod");
00066                 return false;
00067         }
00068         if (orig == endptr) return false;
00069         if (*endptr == '\0') return true;
00070         
00071         return false;
00072 }
00073 
00074 
00075 /*****************************************************************************/
00076 bool try_get_bool(const string& value, bool &b)
00077 {
00078         if (value == "1" || value == "true" || value == "yes") 
00079         {
00080                 b = true;
00081                 return true;
00082         }
00083         if (value == "0" || value == "false" || value == "no") 
00084         {
00085                 b = false;
00086                 return true;
00087         }
00088         return false;
00089 }
00090 
00091 
00092 /*****************************************************************************/
00093 double get_double(const string& value)
00094 {
00095         double d;
00096         if (!try_get_double(value,  d)) 
00097         {
00098                 g_log.error() << "get_float Unable to convert " << value << " into float!" << endl;
00099                 g_log.die("get_float Conversion MUST be OK");
00100         }
00101         return d;
00102 }
00103 
00104 
00105 /*****************************************************************************/
00106 long get_long(const string& value)
00107 {
00108         long i;
00109         if (!try_get_long(value,  i)) 
00110         {
00111                 g_log.error() << "get_long Unable to convert " << value << " into long!" << endl;
00112                 g_log.die("get_int Conversion MUST be OK");
00113         }
00114         return i;
00115 }
00116 
00117 
00118 /*****************************************************************************/
00119 bool  get_bool(const string& value)
00120 {
00121         bool i;
00122         if (!try_get_bool(value,  i)) 
00123         {
00124                 g_log.error() << "get_bool Unable to convert " << value << " into bool!" << endl;
00125                 g_log.die("get_bool Conversion MUST be OK");
00126         }
00127         return i;
00128 }
00129 
00130 
00131 /*****************************************************************************/
00132 // UTF-8 decoding table:
00133 //
00134 //      Scalar value                     UTF-8
00135 //                                 1st byte 2nd byte 3rd byte 4th byte
00136 // 00000000 00000000 0xxxxxxx      0xxxxxxx
00137 // 00000000 00000yyy yyxxxxxx      110yyyyy 10xxxxxx
00138 // 00000000 zzzzyyyy yyxxxxxx      1110zzzz 10yyyyyy 10xxxxxx
00139 // 000uuuuu zzzzyyyy yyxxxxxx      11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
00140 void utf8tows(const char *utf8, std::wstring &ws)
00141 {
00142 //      TIN
00143                 
00144         // assertL(utf8 != 0);
00145         
00146         ws.erase();
00147 
00148 //      TVARIABLE(utf8);
00149         
00150         while (*utf8 != '\0') 
00151         {
00152 
00153 //              TVARIABLE(utf8);
00154                 
00155                 int     ln = 0;
00156                 wchar_t  w = 0;
00157                 
00158                 // Processing first byte - determine how many chars will 
00159                 // create one wchar_t
00160                 if ( (*utf8 & 0xF8) == 0xF0 ) // is as 11110xxx
00161                 {
00162                         ln = 3;
00163                         w  = *utf8 & 0x07;
00164                 }
00165                 
00166                 if ( (*utf8 & 0xF0) == 0xE0 ) // is as 1110xxxx
00167                 {
00168                         ln = 2;
00169                         w  = *utf8 & 0x0F;
00170                 }
00171                 
00172                 if ( (*utf8 & 0xE0) == 0xC0 ) // is as 110xxxxx
00173                 {
00174                         ln = 1;
00175                         w  = *utf8 & 0x1F;
00176                 }
00177                 
00178                 if (ln == 0) 
00179                 {
00180                         w =  *utf8 & 0x7F;
00181                         if ( (*utf8 & 0x80) != 0) // is as 1111 1xxx
00182                         {
00183                                 LOGE << "ERROR IN UTF8 CONTERSION!" << endl;
00184                         }
00185                 }
00186 
00187                 // processing other chars for one wchar_t
00188                 for (; ln > 0; --ln)  // add bytes 10xx xxxx
00189                 {
00190                         ++utf8;
00191 
00192                         if (*utf8 == '\0') 
00193                         {
00194                                 LOGE << "ERROR IN UTF8 CONTERSION!" << endl;
00195                                 break;
00196                         }
00197                         if ( (*utf8 & 0xC0) != 0x80) // must be as 10xx xxxx
00198                         {
00199                                 LOGE << "ERROR IN UTF8 CONTERSION!" << endl;
00200                                 break;
00201                         }
00202                         w <<= 6;
00203                         w += *utf8 & 0x3F;
00204                 }
00205         
00206         
00207                 // Storing decoded wchar_t 
00208                 ws += w;
00209                 
00210                 // utf8 may be == '\0' because of errors in processing
00211                 // other character
00212                 if (*utf8 != '\0') 
00213                 {
00214                         ++utf8;
00215                 }
00216         }
00217 }
00218 
00219 
00220 /*****************************************************************************/
00221 
00222 /*****************************************************************************/
00223 // UTF-8 decoding table:
00224 //
00225 //      Scalar value                     UTF-8
00226 //                                 1st byte 2nd byte 3rd byte 4th byte
00227 // 00000000 00000000 0xxxxxxx      0xxxxxxx
00228 // 00000000 00000yyy yyxxxxxx      110yyyyy 10xxxxxx
00229 // 00000000 zzzzyyyy yyxxxxxx      1110zzzz 10yyyyyy 10xxxxxx
00230 // 000uuuuu zzzzyyyy yyxxxxxx      11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
00231 
00232 // append one character
00240 void appendW(long w, std::string &utf8)
00241 {
00242         int ln = 0;
00243         
00244         if (w <= 0x7F) 
00245         {
00246                 utf8 += (char)(w & 0x7F);
00247                 return;
00248         }
00249 
00250         if (w <= 0x7FF)
00251         {
00252                 utf8 += (char)( ((w >> 6)&0x1F) | 0xC0); // 110yyyyy
00253                 w &= 0x3F; // 0011 1111
00254                 ln = 1;
00255         }
00256         else if (w <= 0xFFFF)
00257         {
00258                 utf8 += (char)( ((w >> 12)&0x0F) | 0xE0); // 1110zzzz
00259                 w &= 0x0FFF; // 0000 1111  1111 1111
00260                 ln = 2;
00261         }
00262         else if (w <= 0x1FFFFF)
00263         {
00264                 utf8 += (char)( ((w >> 18)&0x07) | 0xF0); // 11110uuu
00265                 w &= 0x03FFFF; // 0000 0011   1111 1111   1111 1111
00266                 ln = 3;
00267         }
00268         else 
00269         {
00270                 LOGE << "Invalid unicode character - too large number!" << endl;
00271                 return;
00272         }
00273         
00274         while (ln-- > 0)
00275         {
00276                 utf8 += (char)(((w >> (ln*6))&0x3F) | 0x80); // 10xxxxxx
00277         }
00278 }
00279 
00280 //TODO wstoutf8
00281 void wstoutf8(const std::wstring &ws, std::string &utf8)
00282 {
00283         utf8.erase();
00284 
00285         for (unsigned int i = 0; i < ws.length(); i++)
00286                 appendW(ws[i], utf8);
00287 }
00288 
00289 

Generated on Wed Apr 12 13:55:27 2006 for bjs by  doxygen 1.4.5