UConversion.cpp
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 #include "rtabmap/utilite/UStl.h"
22 
23 #include <sstream>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <iomanip>
28 
29 #ifdef _WIN32
30 #include <windows.h>
31 #endif
32 
33 std::string uReplaceChar(const std::string & str, char before, char after)
34 {
35  std::string result = str;
36  for(unsigned int i=0; i<result.size(); ++i)
37  {
38  if(result[i] == before)
39  {
40  result[i] = after;
41  }
42  }
43  return result;
44 }
45 
46 std::string uReplaceChar(const std::string & str, char before, const std::string & after)
47 {
48  std::string s;
49  for(unsigned int i=0; i<str.size(); ++i)
50  {
51  if(str.at(i) != before)
52  {
53  s.push_back(str.at(i));
54  }
55  else
56  {
57  s.append(after);
58  }
59  }
60  return s;
61 }
62 
63 std::string uToUpperCase(const std::string & str)
64 {
65  std::string result = str;
66  for(unsigned int i=0; i<result.size(); ++i)
67  {
68  // only change case of ascii characters ('a' to 'z')
69  if(result[i] >= 'a' && result[i]<='z')
70  {
71  result[i] = result[i] - 'a' + 'A';
72  }
73  }
74  return result;
75 }
76 
77 std::string uToLowerCase(const std::string & str)
78 {
79  std::string result = str;
80  for(unsigned int i=0; i<result.size(); ++i)
81  {
82  // only change case of ascii characters ('A' to 'Z')
83  if(result[i] >= 'A' && result[i]<='Z')
84  {
85  result[i] = result[i] - 'A' + 'a';
86  }
87  }
88  return result;
89 }
90 
91 std::string uNumber2Str(unsigned int number)
92 {
93  std::stringstream s;
94  s << number;
95  return s.str();
96 }
97 
98 std::string uNumber2Str(int number)
99 {
100  std::stringstream s;
101  s << number;
102  return s.str();
103 }
104 
105 std::string uNumber2Str(float number, int precision, bool fixed)
106 {
107  std::stringstream s;
108  s << std::setprecision(precision);
109  if(fixed)
110  s << std::fixed;
111  s << number;
112  return s.str();
113 }
114 
115 std::string uNumber2Str(double number, int precision, bool fixed)
116 {
117  std::stringstream s;
118  s << std::setprecision(precision);
119  if(fixed)
120  s << std::fixed;
121  s << number;
122  return s.str();
123 }
124 
125 int uStr2Int(const std::string & str)
126 {
127  if(uStrContains(uToLowerCase(str), "true"))
128  {
129  return 1;
130  }
131  else if(uStrContains(uToLowerCase(str), "false"))
132  {
133  return 0;
134  }
135  return atoi(str.c_str());
136 }
137 
138 float uStr2Float(const std::string & str)
139 {
140  float value = 0.0f;
141  std::istringstream istr(uReplaceChar(str, ',', '.').c_str());
142  istr.imbue(std::locale("C"));
143  istr >> value;
144  return value;
145 }
146 
147 double uStr2Double(const std::string & str)
148 {
149  double value = 0.0;
150  std::istringstream istr(uReplaceChar(str, ',', '.').c_str());
151  istr.imbue(std::locale("C"));
152  istr >> value;
153  return value;
154 }
155 
156 std::string uBool2Str(bool boolean)
157 {
158  std::string s;
159  if(boolean)
160  {
161  s = "true";
162  }
163  else
164  {
165  s = "false";
166  }
167  return s;
168 }
169 
170 bool uStr2Bool(const char * str)
171 {
172  return !(str && (uStrContains(uToLowerCase(str), "false") || strcmp(str, "0") == 0));
173 }
174 
175 bool uStr2Bool(const std::string & str)
176 {
177  return !(uStrContains(uToLowerCase(str), "false") || str.compare("0") == 0);
178 }
179 
180 std::vector<unsigned char> uStr2Bytes(const std::string & str)
181 {
182  std::vector<unsigned char> bytes(str.size()+1);
183  memcpy(bytes.data(), str.data(), str.size());
184  bytes[bytes.size()-1] = '\0'; // null character
185  return bytes;
186 }
187 
188 std::string uBytes2Str(const std::vector<unsigned char> & bytes)
189 {
190  if(bytes.size())
191  {
192  if(bytes[bytes.size()-1] != '\0')
193  {
194  std::vector<unsigned char> tmp = bytes;
195  tmp.push_back('\0');
196  return std::string((const char *)tmp.data());
197  }
198  return std::string((const char *)bytes.data());
199  }
200  return std::string();
201 }
202 
203 std::string uBytes2Hex(const char * bytes, unsigned int bytesLen)
204 {
205  std::string hex;
206  if(!bytes || bytesLen == 0)
207  {
208  return hex;
209  }
210  const unsigned char * bytes_u = (const unsigned char*)(bytes);
211 
212  hex.resize(bytesLen*2);
213  char * pHex = &hex[0];
214  const unsigned char * pEnd = (bytes_u + bytesLen);
215  for(const unsigned char * pChar = bytes_u; pChar != pEnd; ++pChar, pHex += 2)
216  {
217  pHex[0] = uHex2Ascii(*pChar, 0);
218  pHex[1] = uHex2Ascii(*pChar, 1);
219  }
220  return hex;
221 }
222 
223 std::vector<char> uHex2Bytes(const std::string & hex)
224 {
225  return uHex2Bytes(&hex[0], (int)hex.length());
226 }
227 
228 std::vector<char> uHex2Bytes(const char * hex, int hexLen)
229 {
230  std::vector<char> bytes;
231  if(!hex || hexLen % 2 || hexLen == 0)
232  {
233  return bytes; // must be pair
234  }
235 
236  unsigned int bytesLen = hexLen / 2;
237  bytes.resize(bytesLen);
238  unsigned char * pBytes = (unsigned char *)&bytes[0];
239  const unsigned char * pHex = (const unsigned char *)hex;
240 
241  unsigned char * pEnd = (pBytes + bytesLen);
242  for(unsigned char * pChar = pBytes; pChar != pEnd; pChar++, pHex += 2)
243  {
244  *pChar = (uAscii2Hex(pHex[0]) << 4) | uAscii2Hex(pHex[1]);
245  }
246  return bytes;
247 }
248 
249 // The hex str MUST not contains any null values (0x00)
250 std::string uHex2Str(const std::string & hex)
251 {
252  std::vector<char> bytes = uHex2Bytes(hex);
253  return std::string(&bytes[0], bytes.size());
254 }
255 
256 static const char HEX2ASCII[256][2] =
257 {
258  {'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'},
259  {'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'},
260  {'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'},
261  {'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'},
262  {'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'},
263  {'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'},
264  {'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'},
265  {'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'},
266  {'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'},
267  {'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'},
268  {'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'},
269  {'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'},
270  {'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'},
271  {'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'},
272  {'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'},
273  {'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'}
274 };
275 
276 unsigned char uHex2Ascii(const unsigned char & c, bool rightPart)
277 {
278  if(rightPart)
279  {
280  return HEX2ASCII[c][1];
281  }
282  else
283  {
284  return HEX2ASCII[c][0];
285  }
286 }
287 
288 unsigned char uAscii2Hex(const unsigned char & c)
289 {
290  switch(c)
291  {
292  case '0':
293  case '1':
294  case '2':
295  case '3':
296  case '4':
297  case '5':
298  case '6':
299  case '7':
300  case '8':
301  case '9':
302  return c-'0';
303  case 'A':
304  case 'B':
305  case 'C':
306  case 'D':
307  case 'E':
308  case 'F':
309  return c-'A'+10;
310  case 'a':
311  case 'b':
312  case 'c':
313  case 'd':
314  case 'e':
315  case 'f':
316  return c-'a'+10;
317  default:
318  return 0x00;
319  }
320 }
321 
322 std::string uFormatv (const char *fmt, va_list args)
323 {
324  // Allocate a buffer on the stack that's big enough for us almost
325  // all the time. Be prepared to allocate dynamically if it doesn't fit.
326  size_t size = 1024;
327  std::vector<char> dynamicbuf(size);
328  char *buf = &dynamicbuf[0];
329 
330  va_list argsTmp;
331 
332  while (1) {
333 #if defined(_WIN32) && !defined(__MINGW32__)
334  argsTmp = args;
335 #else
336  va_copy(argsTmp, args);
337 #endif
338 
339  // Try to vsnprintf into our buffer.
340 #ifdef _MSC_VER
341  int needed = vsnprintf_s(buf, size, size, fmt, argsTmp);
342 #else
343  int needed = vsnprintf (buf, size, fmt, argsTmp);
344 #endif
345  va_end(argsTmp);
346  // NB. C99 (which modern Linux and OS X follow) says vsnprintf
347  // failure returns the length it would have needed. But older
348  // glibc and current Windows return -1 for failure, i.e., not
349  // telling us how much was needed.
350  if (needed < (int)size-1 && needed >= 0) {
351  // It fit fine so we're done.
352  return std::string (buf, (size_t) needed);
353  }
354 
355  // vsnprintf reported that it wanted to write more characters
356  // than we allotted. So try again using a dynamic buffer. This
357  // doesn't happen very often if we chose our initial size well.
358  size = needed>=0?needed+2:size*2;
359  dynamicbuf.resize (size);
360  buf = &dynamicbuf[0];
361  }
362  return std::string(); // would not reach this, but for compiler complaints...
363 }
364 
365 std::string uFormat (const char *fmt, ...)
366 {
367  va_list args;
368  va_start(args, fmt);
369  std::string buf = uFormatv(fmt, args);
370  va_end(args);
371  return buf;
372 }
373 
374 #ifdef _WIN32
375 // returned whar_t * must be deleted : delete [] wText;
376 wchar_t * createWCharFromChar(const char * text)
377 {
378  DWORD length = MultiByteToWideChar (CP_ACP, 0, text, -1, NULL, 0);
379  wchar_t * wText = new wchar_t[length];
380  MultiByteToWideChar (CP_ACP, 0, text, -1, wText, length );
381  return wText;
382 }
383 
384 // returned char * must be deleted : delete [] text;
385 char * createCharFromWChar(const wchar_t * wText)
386 {
387  DWORD length = WideCharToMultiByte (CP_ACP, 0, wText, -1, NULL, 0, NULL, NULL);
388  char * text = new char[length];
389  WideCharToMultiByte (CP_ACP, 0, wText, -1, text, length, NULL, NULL);
390  return text;
391 }
392 #endif
#define NULL
std::string uFormat(const char *fmt,...)
static const char HEX2ASCII[256][2]
std::vector< char > uHex2Bytes(const std::string &hex)
std::string uReplaceChar(const std::string &str, char before, char after)
Definition: UConversion.cpp:33
double uStr2Double(const std::string &str)
std::string uToUpperCase(const std::string &str)
Definition: UConversion.cpp:63
Some conversion functions.
std::string uHex2Str(const std::string &hex)
bool uStrContains(const std::string &string, const std::string &substring)
Definition: UStl.h:787
std::string uBool2Str(bool boolean)
Wrappers of STL for convenient functions.
int uStr2Int(const std::string &str)
std::string uNumber2Str(unsigned int number)
Definition: UConversion.cpp:91
unsigned char uAscii2Hex(const unsigned char &c)
std::string uBytes2Hex(const char *bytes, unsigned int bytesLen)
precision
Definition: precision.hpp:33
bool uStr2Bool(const char *str)
float uStr2Float(const std::string &str)
std::string uToLowerCase(const std::string &str)
Definition: UConversion.cpp:77
std::vector< unsigned char > uStr2Bytes(const std::string &str)
GLM_FUNC_DECL genType::value_type length(genType const &x)
std::string uFormatv(const char *fmt, va_list args)
std::string uBytes2Str(const std::vector< unsigned char > &bytes)
unsigned char uHex2Ascii(const unsigned char &c, bool rightPart)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:58