00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "rtabmap/utilite/UConversion.h"
00021
00022 #include <sstream>
00023 #include <string.h>
00024 #include <stdio.h>
00025
00026 #ifdef _WIN32
00027 #include <windows.h>
00028 #endif
00029
00030 std::string uReplaceChar(const std::string & str, char before, char after)
00031 {
00032 std::string result = str;
00033 for(unsigned int i=0; i<result.size(); ++i)
00034 {
00035 if(result[i] == before)
00036 {
00037 result[i] = after;
00038 }
00039 }
00040 return result;
00041 }
00042
00043 std::string uReplaceChar(const std::string & str, char before, const std::string & after)
00044 {
00045 std::string s;
00046 for(unsigned int i=0; i<str.size(); ++i)
00047 {
00048 if(str.at(i) != before)
00049 {
00050 s.push_back(str.at(i));
00051 }
00052 else
00053 {
00054 s.append(after);
00055 }
00056 }
00057 return s;
00058 }
00059
00060 std::string uToUpperCase(const std::string & str)
00061 {
00062 std::string result = str;
00063 for(unsigned int i=0; i<result.size(); ++i)
00064 {
00065
00066 if(result[i] >= 'a' && result[i]<='z')
00067 {
00068 result[i] = result[i] - 'a' + 'A';
00069 }
00070 }
00071 return result;
00072 }
00073
00074 std::string uToLowerCase(const std::string & str)
00075 {
00076 std::string result = str;
00077 for(unsigned int i=0; i<result.size(); ++i)
00078 {
00079
00080 if(result[i] >= 'A' && result[i]<='Z')
00081 {
00082 result[i] = result[i] - 'A' + 'a';
00083 }
00084 }
00085 return result;
00086 }
00087
00088 std::string uNumber2Str(unsigned int number)
00089 {
00090 std::stringstream s;
00091 s << number;
00092 return s.str();
00093 }
00094
00095 std::string uNumber2Str(int number)
00096 {
00097 std::stringstream s;
00098 s << number;
00099 return s.str();
00100 }
00101
00102 std::string uNumber2Str(float number)
00103 {
00104 std::stringstream s;
00105 s << number;
00106 return s.str();
00107 }
00108
00109 std::string uNumber2Str(double number)
00110 {
00111 std::stringstream s;
00112 s << number;
00113 return s.str();
00114 }
00115
00116 float uStr2Float(const std::string & str)
00117 {
00118 float value = 0.0f;
00119 std::istringstream istr(uReplaceChar(str, ',', '.').c_str());
00120 istr.imbue(std::locale("C"));
00121 istr >> value;
00122 return value;
00123 }
00124
00125 double uStr2Double(const std::string & str)
00126 {
00127 double value = 0.0;
00128 std::istringstream istr(uReplaceChar(str, ',', '.').c_str());
00129 istr.imbue(std::locale("C"));
00130 istr >> value;
00131 return value;
00132 }
00133
00134 std::string uBool2Str(bool boolean)
00135 {
00136 std::string s;
00137 if(boolean)
00138 {
00139 s = "true";
00140 }
00141 else
00142 {
00143 s = "false";
00144 }
00145 return s;
00146 }
00147
00148 bool uStr2Bool(const char * str)
00149 {
00150 return !(str && (strcmp(str, "false") == 0 || strcmp(str, "FALSE") == 0 || strcmp(str, "0") == 0));
00151 }
00152
00153 std::vector<unsigned char> uStr2Bytes(const std::string & str)
00154 {
00155 std::vector<unsigned char> bytes(str.size()+1);
00156 memcpy(bytes.data(), str.data(), str.size());
00157 bytes[bytes.size()-1] = '\0';
00158 return bytes;
00159 }
00160
00161 std::string uBytes2Str(const std::vector<unsigned char> & bytes)
00162 {
00163 if(bytes.size())
00164 {
00165 if(bytes[bytes.size()-1] != '\0')
00166 {
00167 std::vector<unsigned char> tmp = bytes;
00168 tmp.push_back('\0');
00169 return std::string((const char *)tmp.data());
00170 }
00171 return std::string((const char *)bytes.data());
00172 }
00173 return std::string();
00174 }
00175
00176 std::string uBytes2Hex(const char * bytes, unsigned int bytesLen)
00177 {
00178 std::string hex;
00179 if(!bytes || bytesLen == 0)
00180 {
00181 return hex;
00182 }
00183 const unsigned char * bytes_u = (const unsigned char*)(bytes);
00184
00185 hex.resize(bytesLen*2);
00186 char * pHex = &hex[0];
00187 const unsigned char * pEnd = (bytes_u + bytesLen);
00188 for(const unsigned char * pChar = bytes_u; pChar != pEnd; ++pChar, pHex += 2)
00189 {
00190 pHex[0] = uHex2Ascii(*pChar, 0);
00191 pHex[1] = uHex2Ascii(*pChar, 1);
00192 }
00193 return hex;
00194 }
00195
00196 std::vector<char> uHex2Bytes(const std::string & hex)
00197 {
00198 return uHex2Bytes(&hex[0], (int)hex.length());
00199 }
00200
00201 std::vector<char> uHex2Bytes(const char * hex, int hexLen)
00202 {
00203 std::vector<char> bytes;
00204 if(!hex || hexLen % 2 || hexLen == 0)
00205 {
00206 return bytes;
00207 }
00208
00209 unsigned int bytesLen = hexLen / 2;
00210 bytes.resize(bytesLen);
00211 unsigned char * pBytes = (unsigned char *)&bytes[0];
00212 const unsigned char * pHex = (const unsigned char *)hex;
00213
00214 unsigned char * pEnd = (pBytes + bytesLen);
00215 for(unsigned char * pChar = pBytes; pChar != pEnd; pChar++, pHex += 2)
00216 {
00217 *pChar = (uAscii2Hex(pHex[0]) << 4) | uAscii2Hex(pHex[1]);
00218 }
00219 return bytes;
00220 }
00221
00222
00223 std::string uHex2Str(const std::string & hex)
00224 {
00225 std::vector<char> bytes = uHex2Bytes(hex);
00226 return std::string(&bytes[0], bytes.size());
00227 }
00228
00229 static const char HEX2ASCII[256][2] =
00230 {
00231 {'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'},
00232 {'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'},
00233 {'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'},
00234 {'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'},
00235 {'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'},
00236 {'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'},
00237 {'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'},
00238 {'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'},
00239 {'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'},
00240 {'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'},
00241 {'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'},
00242 {'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'},
00243 {'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'},
00244 {'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'},
00245 {'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'},
00246 {'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'}
00247 };
00248
00249 unsigned char uHex2Ascii(const unsigned char & c, bool rightPart)
00250 {
00251 if(rightPart)
00252 {
00253 return HEX2ASCII[c][1];
00254 }
00255 else
00256 {
00257 return HEX2ASCII[c][0];
00258 }
00259 }
00260
00261 unsigned char uAscii2Hex(const unsigned char & c)
00262 {
00263 switch(c)
00264 {
00265 case '0':
00266 case '1':
00267 case '2':
00268 case '3':
00269 case '4':
00270 case '5':
00271 case '6':
00272 case '7':
00273 case '8':
00274 case '9':
00275 return c-'0';
00276 case 'A':
00277 case 'B':
00278 case 'C':
00279 case 'D':
00280 case 'E':
00281 case 'F':
00282 return c-'A'+10;
00283 case 'a':
00284 case 'b':
00285 case 'c':
00286 case 'd':
00287 case 'e':
00288 case 'f':
00289 return c-'a'+10;
00290 default:
00291 return 0x00;
00292 }
00293 }
00294
00295 std::string uFormatv (const char *fmt, va_list args)
00296 {
00297
00298
00299 size_t size = 1024;
00300 std::vector<char> dynamicbuf(size);
00301 char *buf = &dynamicbuf[0];
00302
00303 va_list argsTmp;
00304
00305 while (1) {
00306 #if defined(_WIN32) && !defined(__MINGW32__)
00307 argsTmp = args;
00308 #else
00309 va_copy(argsTmp, args);
00310 #endif
00311
00312
00313 #ifdef _MSC_VER
00314 int needed = vsnprintf_s(buf, size, size, fmt, argsTmp);
00315 #else
00316 int needed = vsnprintf (buf, size, fmt, argsTmp);
00317 #endif
00318 va_end(argsTmp);
00319
00320
00321
00322
00323 if (needed < (int)size-1 && needed >= 0) {
00324
00325 return std::string (buf, (size_t) needed);
00326 }
00327
00328
00329
00330
00331 size = needed>=0?needed+2:size*2;
00332 dynamicbuf.resize (size);
00333 buf = &dynamicbuf[0];
00334 }
00335 return std::string();
00336 }
00337
00338 std::string uFormat (const char *fmt, ...)
00339 {
00340 va_list args;
00341 va_start(args, fmt);
00342 std::string buf = uFormatv(fmt, args);
00343 va_end(args);
00344 return buf;
00345 }
00346
00347 #ifdef _WIN32
00348
00349 wchar_t * createWCharFromChar(const char * text)
00350 {
00351 DWORD length = MultiByteToWideChar (CP_ACP, 0, text, -1, NULL, 0);
00352 wchar_t * wText = new wchar_t[length];
00353 MultiByteToWideChar (CP_ACP, 0, text, -1, wText, length );
00354 return wText;
00355 }
00356
00357
00358 char * createCharFromWChar(const wchar_t * wText)
00359 {
00360 DWORD length = WideCharToMultiByte (CP_ACP, 0, wText, -1, NULL, 0, NULL, NULL);
00361 char * text = new char[length];
00362 WideCharToMultiByte (CP_ACP, 0, wText, -1, text, length, NULL, NULL);
00363 return text;
00364 }
00365 #endif