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