UConversion.cpp
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include "rtabmap/utilite/UConversion.h"
00021 
00022 #include <sstream>
00023 #include <string.h>
00024 #include <stdio.h>
00025 #include <stdlib.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 int uStr2Int(const std::string & str)
00118 {
00119         return atoi(str.c_str());
00120 }
00121 
00122 float uStr2Float(const std::string & str)
00123 {
00124         float value = 0.0f;
00125         std::istringstream istr(uReplaceChar(str, ',', '.').c_str());
00126         istr.imbue(std::locale("C"));
00127         istr >> value;
00128         return value;
00129 }
00130 
00131 double uStr2Double(const std::string & str)
00132 {
00133         double value = 0.0;
00134         std::istringstream istr(uReplaceChar(str, ',', '.').c_str());
00135         istr.imbue(std::locale("C"));
00136         istr >> value;
00137         return value;
00138 }
00139 
00140 std::string uBool2Str(bool boolean)
00141 {
00142         std::string s;
00143         if(boolean)
00144         {
00145                 s = "true";
00146         }
00147         else
00148         {
00149                 s = "false";
00150         }
00151         return s;
00152 }
00153 
00154 bool uStr2Bool(const char * str)
00155 {
00156         return !(str && (strcmp(str, "false") == 0 || strcmp(str, "FALSE") == 0 || strcmp(str, "0") == 0));
00157 }
00158 
00159 bool uStr2Bool(const std::string & str)
00160 {
00161         return !(str.compare("false") == 0 || str.compare("FALSE") == 0 || str.compare("0") == 0);
00162 }
00163 
00164 std::vector<unsigned char> uStr2Bytes(const std::string & str)
00165 {
00166         std::vector<unsigned char> bytes(str.size()+1);
00167         memcpy(bytes.data(), str.data(), str.size());
00168         bytes[bytes.size()-1] = '\0'; // null character
00169         return bytes;
00170 }
00171 
00172 std::string uBytes2Str(const std::vector<unsigned char> & bytes)
00173 {
00174         if(bytes.size())
00175         {
00176                 if(bytes[bytes.size()-1] != '\0')
00177                 {
00178                         std::vector<unsigned char> tmp = bytes;
00179                         tmp.push_back('\0');
00180                         return std::string((const char *)tmp.data());
00181                 }
00182                 return std::string((const char *)bytes.data());
00183         }
00184         return std::string();
00185 }
00186 
00187 std::string uBytes2Hex(const char * bytes, unsigned int bytesLen)
00188 {
00189         std::string hex;
00190         if(!bytes || bytesLen == 0)
00191         {
00192                 return hex;
00193         }
00194         const unsigned char * bytes_u = (const unsigned char*)(bytes);
00195 
00196         hex.resize(bytesLen*2);
00197         char * pHex = &hex[0];
00198         const unsigned char * pEnd = (bytes_u + bytesLen);
00199         for(const unsigned char * pChar = bytes_u; pChar != pEnd; ++pChar, pHex += 2)
00200         {
00201                 pHex[0] = uHex2Ascii(*pChar, 0);
00202                 pHex[1] = uHex2Ascii(*pChar, 1);
00203         }
00204         return hex;
00205 }
00206 
00207 std::vector<char> uHex2Bytes(const std::string & hex)
00208 {
00209         return uHex2Bytes(&hex[0], (int)hex.length());
00210 }
00211 
00212 std::vector<char> uHex2Bytes(const char * hex, int hexLen)
00213 {
00214         std::vector<char> bytes;
00215         if(!hex || hexLen % 2 || hexLen == 0)
00216         {
00217                 return bytes; // must be pair
00218         }
00219 
00220         unsigned int bytesLen = hexLen / 2;
00221         bytes.resize(bytesLen);
00222         unsigned char * pBytes = (unsigned char *)&bytes[0];
00223         const unsigned char * pHex = (const unsigned char *)hex;
00224 
00225         unsigned char * pEnd = (pBytes + bytesLen);
00226         for(unsigned char * pChar = pBytes; pChar != pEnd; pChar++, pHex += 2)
00227         {
00228                 *pChar = (uAscii2Hex(pHex[0]) << 4) | uAscii2Hex(pHex[1]);
00229         }
00230         return bytes;
00231 }
00232 
00233 // The hex str MUST not contains any null values (0x00)
00234 std::string uHex2Str(const std::string & hex)
00235 {
00236         std::vector<char> bytes = uHex2Bytes(hex);
00237         return std::string(&bytes[0], bytes.size());
00238 }
00239 
00240 static const char HEX2ASCII[256][2] =
00241 {
00242         {'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'},
00243         {'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'},
00244         {'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'},
00245         {'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'},
00246         {'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'},
00247         {'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'},
00248         {'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'},
00249         {'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'},
00250         {'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'},
00251         {'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'},
00252         {'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'},
00253         {'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'},
00254         {'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'},
00255         {'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'},
00256         {'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'},
00257         {'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'}
00258 };
00259 
00260 unsigned char uHex2Ascii(const unsigned char & c, bool rightPart)
00261 {
00262         if(rightPart)
00263         {
00264                 return HEX2ASCII[c][1];
00265         }
00266         else
00267         {
00268                 return HEX2ASCII[c][0];
00269         }
00270 }
00271 
00272 unsigned char uAscii2Hex(const unsigned char & c)
00273 {
00274         switch(c)
00275         {
00276         case '0':
00277         case '1':
00278         case '2':
00279         case '3':
00280         case '4':
00281         case '5':
00282         case '6':
00283         case '7':
00284         case '8':
00285         case '9':
00286                 return c-'0';
00287         case 'A':
00288         case 'B':
00289         case 'C':
00290         case 'D':
00291         case 'E':
00292         case 'F':
00293                 return c-'A'+10;
00294         case 'a':
00295         case 'b':
00296         case 'c':
00297         case 'd':
00298         case 'e':
00299         case 'f':
00300                 return c-'a'+10;
00301         default:
00302                 return 0x00;
00303         }
00304 }
00305 
00306 std::string uFormatv (const char *fmt, va_list args)
00307 {
00308     // Allocate a buffer on the stack that's big enough for us almost
00309     // all the time.  Be prepared to allocate dynamically if it doesn't fit.
00310     size_t size = 1024;
00311     std::vector<char> dynamicbuf(size);
00312     char *buf = &dynamicbuf[0];
00313 
00314     va_list argsTmp;
00315 
00316     while (1) {
00317 #if defined(_WIN32) && !defined(__MINGW32__)
00318         argsTmp = args;
00319 #else
00320         va_copy(argsTmp, args);
00321 #endif
00322 
00323         // Try to vsnprintf into our buffer.
00324 #ifdef _MSC_VER
00325         int needed = vsnprintf_s(buf, size, size, fmt, argsTmp);
00326 #else
00327         int needed = vsnprintf (buf, size, fmt, argsTmp);
00328 #endif
00329         va_end(argsTmp);
00330         // NB. C99 (which modern Linux and OS X follow) says vsnprintf
00331         // failure returns the length it would have needed.  But older
00332         // glibc and current Windows return -1 for failure, i.e., not
00333         // telling us how much was needed.
00334         if (needed < (int)size-1 && needed >= 0) {
00335             // It fit fine so we're done.
00336             return std::string (buf, (size_t) needed);
00337         }
00338 
00339         // vsnprintf reported that it wanted to write more characters
00340         // than we allotted.  So try again using a dynamic buffer.  This
00341         // doesn't happen very often if we chose our initial size well.
00342         size = needed>=0?needed+2:size*2;
00343         dynamicbuf.resize (size);
00344         buf = &dynamicbuf[0];
00345     }
00346     return std::string(); // would not reach this, but for compiler complaints...
00347 }
00348 
00349 std::string uFormat (const char *fmt, ...)
00350 {
00351         va_list args;
00352         va_start(args, fmt);
00353         std::string buf = uFormatv(fmt, args);
00354         va_end(args);
00355     return buf;
00356 }
00357 
00358 #ifdef _WIN32
00359 // returned whar_t * must be deleted : delete [] wText;
00360 wchar_t * createWCharFromChar(const char * text)
00361 {
00362         DWORD length = MultiByteToWideChar (CP_ACP, 0, text, -1, NULL, 0);
00363         wchar_t * wText = new wchar_t[length];
00364         MultiByteToWideChar (CP_ACP, 0, text, -1, wText, length );
00365         return wText;
00366 }
00367 
00368 // returned char * must be deleted : delete [] text;
00369 char * createCharFromWChar(const wchar_t * wText)
00370 {
00371         DWORD length = WideCharToMultiByte (CP_ACP, 0, wText, -1, NULL, 0, NULL, NULL);
00372         char * text = new char[length];
00373         WideCharToMultiByte (CP_ACP, 0, wText, -1, text, length, NULL, NULL);
00374         return text;
00375 }
00376 #endif


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:28