UConversion.cpp
Go to the documentation of this file.
00001 // Taken from UtiLite library r266 [www.utilite.googlecode.com]
00002 /*
00003 *  utilite is a cross-platform library with
00004 *  useful utilities for fast and small developing.
00005 *  Copyright (C) 2010  Mathieu Labbe
00006 *
00007 *  utilite is free library: you can redistribute it and/or modify
00008 *  it under the terms of the GNU Lesser General Public License as published by
00009 *  the Free Software Foundation, either version 3 of the License, or
00010 *  (at your option) any later version.
00011 *
00012 *  utilite is distributed in the hope that it will be useful,
00013 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 *  GNU Lesser General Public License for more details.
00016 *
00017 *  You should have received a copy of the GNU Lesser General Public License
00018 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "utilite/UConversion.h"
00022 
00023 #include <sstream>
00024 #include <string.h>
00025 #include <stdio.h>
00026 
00027 #ifdef WIN32
00028 #include <windows.h>
00029 #endif
00030 
00031 std::string uReplaceChar(const std::string & str, char before, char after)
00032 {
00033         std::string result = str;
00034         for(unsigned int i=0; i<result.size(); ++i)
00035         {
00036                 if(result[i] == before)
00037                 {
00038                         result[i] = after;
00039                 }
00040         }
00041         return result;
00042 }
00043 
00044 std::string uReplaceChar(const std::string & str, char before, const std::string & after)
00045 {
00046         std::string s;
00047         for(unsigned int i=0; i<str.size(); ++i)
00048         {
00049                 if(str.at(i) != before)
00050                 {
00051                         s.push_back(str.at(i));
00052                 }
00053                 else
00054                 {
00055                         s.append(after);
00056                 }
00057         }
00058         return s;
00059 }
00060 
00061 std::string uToUpperCase(const std::string & str)
00062 {
00063         std::string result = str;
00064         for(unsigned int i=0; i<result.size(); ++i)
00065         {
00066                 // only change case of ascii characters ('a' to 'z')
00067                 if(result[i] >= 'a' && result[i]<='z')
00068                 {
00069                         result[i] = result[i] - 'a' + 'A';
00070                 }
00071         }
00072         return result;
00073 }
00074 
00075 std::string uToLowerCase(const std::string & str)
00076 {
00077         std::string result = str;
00078         for(unsigned int i=0; i<result.size(); ++i)
00079         {
00080                 // only change case of ascii characters ('A' to 'Z')
00081                 if(result[i] >= 'A' && result[i]<='Z')
00082                 {
00083                         result[i] = result[i] - 'A' + 'a';
00084                 }
00085         }
00086         return result;
00087 }
00088 
00089 std::string uNumber2Str(unsigned int number)
00090 {
00091         std::stringstream s;
00092         s << number;
00093         return s.str();
00094 }
00095 
00096 std::string uNumber2Str(int number)
00097 {
00098         std::stringstream s;
00099         s << number;
00100         return s.str();
00101 }
00102 
00103 std::string uNumber2Str(float number)
00104 {
00105         std::stringstream s;
00106         s << number;
00107         return s.str();
00108 }
00109 
00110 std::string uNumber2Str(double number)
00111 {
00112         std::stringstream s;
00113         s << number;
00114         return s.str();
00115 }
00116 
00117 std::string uBool2Str(bool boolean)
00118 {
00119         std::string s;
00120         if(boolean)
00121         {
00122                 s = "true";
00123         }
00124         else
00125         {
00126                 s = "false";
00127         }
00128         return s;
00129 }
00130 
00131 bool uStr2Bool(const char * str)
00132 {
00133         return !(str && (strcmp(str, "false") == 0 || strcmp(str, "FALSE") == 0 || strcmp(str, "0") == 0));
00134 }
00135 
00136 std::string uBytes2Hex(const char * bytes, unsigned int bytesLen)
00137 {
00138         std::string hex;
00139         if(!bytes || bytesLen == 0)
00140         {
00141                 return hex;
00142         }
00143         const unsigned char * bytes_u = (const unsigned char*)(bytes);
00144 
00145         hex.resize(bytesLen*2);
00146         char * pHex = &hex[0];
00147         const unsigned char * pEnd = (bytes_u + bytesLen);
00148         for(const unsigned char * pChar = bytes_u; pChar != pEnd; ++pChar, pHex += 2)
00149         {
00150                 pHex[0] = uHex2Ascii(*pChar, 0);
00151                 pHex[1] = uHex2Ascii(*pChar, 1);
00152         }
00153         return hex;
00154 }
00155 
00156 std::vector<char> uHex2Bytes(const std::string & hex)
00157 {
00158         return uHex2Bytes(&hex[0], hex.length());
00159 }
00160 
00161 std::vector<char> uHex2Bytes(const char * hex, int hexLen)
00162 {
00163         std::vector<char> bytes;
00164         if(!hex || hexLen % 2 || hexLen == 0)
00165         {
00166                 return bytes; // must be pair
00167         }
00168 
00169         unsigned int bytesLen = hexLen / 2;
00170         bytes.resize(bytesLen);
00171         unsigned char * pBytes = (unsigned char *)&bytes[0];
00172         const unsigned char * pHex = (const unsigned char *)hex;
00173 
00174         unsigned char * pEnd = (pBytes + bytesLen);
00175         for(unsigned char * pChar = pBytes; pChar != pEnd; pChar++, pHex += 2)
00176         {
00177                 *pChar = (uAscii2Hex(pHex[0]) << 4) | uAscii2Hex(pHex[1]);
00178         }
00179         return bytes;
00180 }
00181 
00182 // The hex str MUST not contains any null values (0x00)
00183 std::string uHex2Str(const std::string & hex)
00184 {
00185         std::vector<char> bytes = uHex2Bytes(hex);
00186         return std::string(&bytes[0], bytes.size());
00187 }
00188 
00189 static const char HEX2ASCII[256][2] =
00190 {
00191         {'0','0'},{'0','1'},{'0','2'},{'0','3'},{'0','4'},{'0','5'},{'0','6'},{'0','7'},{'0','8'},{'0','9'},{'0','A'},{'0','B'},{'0','C'},{'0','D'},{'0','E'},{'0','F'},
00192         {'1','0'},{'1','1'},{'1','2'},{'1','3'},{'1','4'},{'1','5'},{'1','6'},{'1','7'},{'1','8'},{'1','9'},{'1','A'},{'1','B'},{'1','C'},{'1','D'},{'1','E'},{'1','F'},
00193         {'2','0'},{'2','1'},{'2','2'},{'2','3'},{'2','4'},{'2','5'},{'2','6'},{'2','7'},{'2','8'},{'2','9'},{'2','A'},{'2','B'},{'2','C'},{'2','D'},{'2','E'},{'2','F'},
00194         {'3','0'},{'3','1'},{'3','2'},{'3','3'},{'3','4'},{'3','5'},{'3','6'},{'3','7'},{'3','8'},{'3','9'},{'3','A'},{'3','B'},{'3','C'},{'3','D'},{'3','E'},{'3','F'},
00195         {'4','0'},{'4','1'},{'4','2'},{'4','3'},{'4','4'},{'4','5'},{'4','6'},{'4','7'},{'4','8'},{'4','9'},{'4','A'},{'4','B'},{'4','C'},{'4','D'},{'4','E'},{'4','F'},
00196         {'5','0'},{'5','1'},{'5','2'},{'5','3'},{'5','4'},{'5','5'},{'5','6'},{'5','7'},{'5','8'},{'5','9'},{'5','A'},{'5','B'},{'5','C'},{'5','D'},{'5','E'},{'5','F'},
00197         {'6','0'},{'6','1'},{'6','2'},{'6','3'},{'6','4'},{'6','5'},{'6','6'},{'6','7'},{'6','8'},{'6','9'},{'6','A'},{'6','B'},{'6','C'},{'6','D'},{'6','E'},{'6','F'},
00198         {'7','0'},{'7','1'},{'7','2'},{'7','3'},{'7','4'},{'7','5'},{'7','6'},{'7','7'},{'7','8'},{'7','9'},{'7','A'},{'7','B'},{'7','C'},{'7','D'},{'7','E'},{'7','F'},
00199         {'8','0'},{'8','1'},{'8','2'},{'8','3'},{'8','4'},{'8','5'},{'8','6'},{'8','7'},{'8','8'},{'8','9'},{'8','A'},{'8','B'},{'8','C'},{'8','D'},{'8','E'},{'8','F'},
00200         {'9','0'},{'9','1'},{'9','2'},{'9','3'},{'9','4'},{'9','5'},{'9','6'},{'9','7'},{'9','8'},{'9','9'},{'9','A'},{'9','B'},{'9','C'},{'9','D'},{'9','E'},{'9','F'},
00201         {'A','0'},{'A','1'},{'A','2'},{'A','3'},{'A','4'},{'A','5'},{'A','6'},{'A','7'},{'A','8'},{'A','9'},{'A','A'},{'A','B'},{'A','C'},{'A','D'},{'A','E'},{'A','F'},
00202         {'B','0'},{'B','1'},{'B','2'},{'B','3'},{'B','4'},{'B','5'},{'B','6'},{'B','7'},{'B','8'},{'B','9'},{'B','A'},{'B','B'},{'B','C'},{'B','D'},{'B','E'},{'B','F'},
00203         {'C','0'},{'C','1'},{'C','2'},{'C','3'},{'C','4'},{'C','5'},{'C','6'},{'C','7'},{'C','8'},{'C','9'},{'C','A'},{'C','B'},{'C','C'},{'C','D'},{'C','E'},{'C','F'},
00204         {'D','0'},{'D','1'},{'D','2'},{'D','3'},{'D','4'},{'D','5'},{'D','6'},{'D','7'},{'D','8'},{'D','9'},{'D','A'},{'D','B'},{'D','C'},{'D','D'},{'D','E'},{'D','F'},
00205         {'E','0'},{'E','1'},{'E','2'},{'E','3'},{'E','4'},{'E','5'},{'E','6'},{'E','7'},{'E','8'},{'E','9'},{'E','A'},{'E','B'},{'E','C'},{'E','D'},{'E','E'},{'E','F'},
00206         {'F','0'},{'F','1'},{'F','2'},{'F','3'},{'F','4'},{'F','5'},{'F','6'},{'F','7'},{'F','8'},{'F','9'},{'F','A'},{'F','B'},{'F','C'},{'F','D'},{'F','E'},{'F','F'}
00207 };
00208 
00209 unsigned char uHex2Ascii(const unsigned char & c, bool rightPart)
00210 {
00211         if(rightPart)
00212         {
00213                 return HEX2ASCII[c][1];
00214         }
00215         else
00216         {
00217                 return HEX2ASCII[c][0];
00218         }
00219 }
00220 
00221 unsigned char uAscii2Hex(const unsigned char & c)
00222 {
00223         switch(c)
00224         {
00225         case '0':
00226         case '1':
00227         case '2':
00228         case '3':
00229         case '4':
00230         case '5':
00231         case '6':
00232         case '7':
00233         case '8':
00234         case '9':
00235                 return c-'0';
00236         case 'A':
00237         case 'B':
00238         case 'C':
00239         case 'D':
00240         case 'E':
00241         case 'F':
00242                 return c-'A'+10;
00243         case 'a':
00244         case 'b':
00245         case 'c':
00246         case 'd':
00247         case 'e':
00248         case 'f':
00249                 return c-'a'+10;
00250         default:
00251                 return 0x00;
00252         }
00253 }
00254 
00255 std::string uFormatv (const char *fmt, va_list args)
00256 {
00257     // Allocate a buffer on the stack that's big enough for us almost
00258     // all the time.  Be prepared to allocate dynamically if it doesn't fit.
00259     size_t size = 1024;
00260     std::vector<char> dynamicbuf(size);
00261     char *buf = &dynamicbuf[0];
00262 
00263     va_list argsTmp;
00264 
00265     while (1) {
00266 #if defined(WIN32) && !defined(__MINGW32__)
00267         argsTmp = args;
00268 #else
00269         va_copy(argsTmp, args);
00270 #endif
00271 
00272         // Try to vsnprintf into our buffer.
00273         int needed = vsnprintf (buf, size, fmt, argsTmp);
00274         va_end(argsTmp);
00275         // NB. C99 (which modern Linux and OS X follow) says vsnprintf
00276         // failure returns the length it would have needed.  But older
00277         // glibc and current Windows return -1 for failure, i.e., not
00278         // telling us how much was needed.
00279         if (needed < (int)size-1 && needed >= 0) {
00280             // It fit fine so we're done.
00281             return std::string (buf, (size_t) needed);
00282         }
00283 
00284         // vsnprintf reported that it wanted to write more characters
00285         // than we allotted.  So try again using a dynamic buffer.  This
00286         // doesn't happen very often if we chose our initial size well.
00287         size = needed>=0?needed+2:size*2;
00288         dynamicbuf.resize (size);
00289         buf = &dynamicbuf[0];
00290     }
00291     return std::string(); // would not reach this, but for compiler complaints...
00292 }
00293 
00294 std::string uFormat (const char *fmt, ...)
00295 {
00296         va_list args;
00297         va_start(args, fmt);
00298         std::string buf = uFormatv(fmt, args);
00299         va_end(args);
00300     return buf;
00301 }
00302 
00303 #ifdef WIN32
00304 // returned whar_t * must be deleted : delete [] wText;
00305 wchar_t * createWCharFromChar(const char * text)
00306 {
00307         DWORD length = MultiByteToWideChar (CP_ACP, 0, text, -1, NULL, 0);
00308         wchar_t * wText = new wchar_t[length];
00309         MultiByteToWideChar (CP_ACP, 0, text, -1, wText, length );
00310         return wText;
00311 }
00312 
00313 // returned char * must be deleted : delete [] text;
00314 char * createCharFromWChar(const wchar_t * wText)
00315 {
00316         DWORD length = WideCharToMultiByte (CP_ACP, 0, wText, -1, NULL, 0, NULL, NULL);
00317         char * text = new char[length];
00318         WideCharToMultiByte (CP_ACP, 0, wText, -1, text, length, NULL, NULL);
00319         return text;
00320 }
00321 #endif


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Feb 11 2016 22:57:56