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


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Dec 12 2022 03:20:10