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