Open Core Interface - MLPI
MLPI-MotionLogicProgrammingInterface(mlpiCore)  1.26.2
wchar16.h
Go to the documentation of this file.
1 #ifndef __WCHAR16_H__
2 #define __WCHAR16_H__
3 
4 // -----------------------------------------------------------------------
5 // MLPI - <wchar16.h>
6 // -----------------------------------------------------------------------
7 // Copyright (c) 2013 Bosch Rexroth. All rights reserved.
8 // Redistribution and use in source and binary forms of this MLPI software
9 // (SW) provided to you, with or without modification, are permitted
10 // without prior approval provided that the following conditions are met:
11 //
12 // 1. Redistributions of source code of SW must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // 2. Redistributions in binary form of SW must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the distribution.
18 //
19 // 3. User recognizes and acknowledges that it acquires no right,
20 // title or interest in or to any of the names or trademarks used in
21 // connection with the SW ("names") by virtue of this License and waives
22 // any right to or interest in the names. User recognizes and acknowledges
23 // that names of companies or names or products of companies displayed
24 // in the documentation of SW to indicate the interoperability of products
25 // with the SW are the names of their respective owners. The use of such
26 // names in the documentation of SW does not imply any sponsorship,
27 // approval, or endorsement by such companies of this product.
28 //
29 // 4. Modified code versions, i.e. any addition to or deletion from
30 // the substance or structure of the original code of the SW running
31 // the MLPI must be plainly marked as such and must not be misrepresented
32 // as being original SW.
33 //
34 // 5. The SW may only be used in connection with a Bosch Rexroth product.
35 //
36 // THIS INFORMATION IS PROVIDED BY BOSCH REXROTH CORPORATION "AS IS"
37 // AND WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING
38 // (BUT NOTLIMITED TO) ANY IMPLIED WARRANTIES OF MERCHANTABILITY,
39 // FITNESS FOR ANY PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WHILE THE
40 // INFORMATION PROVIDED IS BELIEVED TO BE ACCURATE, IT MAY INCLUDE
41 // ERRORS OR INACCURACIES.
42 // SUBJECT TO COMPULSORY STATUTORY PROVISIONS OF THE GERMAN LAW AS
43 // THE APPLICABLE LAW FOR THIS LICENSE BOSCH REXROTH CORPORATION WILL
44 // NOT BE LIABLE FOR ANY DAMAGES OF ANY KIND ARISING FROM THE USE OF
45 // THE SOFTWARE DISTRIBUTED HEREUNDER, INCLUDING BUT NOT LIMITED TO
46 // DIRECT, INDIRECT, INCIDENTAL, PUNITIVE, AND CONSEQUENTIAL DAMAGES.
47 // -----------------------------------------------------------------------
48 //
58 //
59 // -----------------------------------------------------------------------
60 
61 
62 
63 
79 
80 
81 
82 // -----------------------------------------------------------------------
83 // GLOBAL INCLUDES
84 // -----------------------------------------------------------------------
85 #include <string.h>
86 
87 #include "mlpiGlobal.h"
88 
89 // -----------------------------------------------------------------------
90 // GLOBAL MACROS
91 // -----------------------------------------------------------------------
92 #if defined(TARGET_COMPILER_GCC)
93  // compiling with GNU gcc
94  #define W2A16(strIn) (_wcstombs16((CHAR*)__builtin_alloca(sizeof(CHAR)*(wcslen16(strIn)+1)), strIn, wcslen16(strIn)+1))
95  #define A2W16(strIn) (_mbstowcs16((WCHAR16*)__builtin_alloca(sizeof(WCHAR16)*(strlen(strIn)+1)), strIn, strlen(strIn)+1))
96 #elif defined(TARGET_COMPILER_MSVC)
97  // compiling with Microsoft Visual Studio Compiler
98  #include "malloc.h"
99  #define W2A16(strIn) (_wcstombs16((CHAR*)alloca(sizeof(CHAR)*(wcslen16(strIn)+1)), strIn, wcslen16(strIn)+1))
100  #define A2W16(strIn) (_mbstowcs16((WCHAR16*)alloca(sizeof(WCHAR16)*(strlen(strIn)+1)), strIn, strlen(strIn)+1))
101 #else
102  // unknown compiler
103  #pragma warning "unknown compiler"
104 #endif
105 
106 
107 // -----------------------------------------------------------------------
108 // GLOBAL EXPORTS
109 // -----------------------------------------------------------------------
110 
115 inline size_t wcslen16(const WCHAR16 *src)
116 {
117  const WCHAR16 *s;
118 
119  if(src == NULL)
120  return 0;
121 
122  for (s = src; *s; ++s)
123  continue;
124 
125  return (s - src);
126 }
127 
133 inline WCHAR16 *wcschr16(const WCHAR16 *src, WCHAR16 character)
134 {
135  const WCHAR16 *p;
136 
137  p = NULL;
138  do
139  {
140  if (*src == character)
141  {
142  p = src;
143  }
144  } while (*src++);
145 
146  return const_cast<WCHAR16 *>(p);
147 }
148 
158 inline int wcsncmp16(const WCHAR16 *src1, const WCHAR16 *src2, size_t len)
159 {
160  while(len--)
161  {
162  if(*src1++ != *src2++)
163  {
164  return *(WCHAR16*)(src1 - 1) - *(WCHAR16*)(src2 - 1);
165  }
166  }
167 
168  return 0;
169 }
170 
179 inline int wcscmp16(const WCHAR16 *src1, const WCHAR16 *src2)
180 {
181  while (*src1 == *src2++)
182  {
183  if (!*src1++)
184  {
185  return 0;
186  }
187  }
188 
189  return src1[0] - src2[-1];
190 }
191 
192 inline int wcscmp16_(const WCHAR16 *src1, const WCHAR16 *src2)
193 {
194  while ((~0x0020 & *src1) == (~0x0020 & *src2++)) // ignore case sensitivity...
195  {
196  if (!*src1++)
197  {
198  return 0;
199  }
200  }
201 
202  return src1[0] - src2[-1];
203 }
204 
210 inline WCHAR16* wcscpy16(WCHAR16 *dst, const WCHAR16 *src)
211 {
212  WCHAR16 *run = dst;
213  while ((*run++ = *src++) != L'\0');
214 
215  return dst;
216 }
217 
224 inline WCHAR16* wcsncpy16(WCHAR16 *dst, const WCHAR16 *src, size_t len)
225 {
226  if (len!=0)
227  {
228  int i = 0;
229  while(len != 0) {
230  len--;
231  if ((dst[i++] = *src++) == 0)
232  break;
233  };
234  while(len-- != 0)
235  dst[i++] = 0;
236  }
237  return dst;
238 }
239 
246 inline WCHAR16* wcscat16(WCHAR16 *dst, const WCHAR16 *src)
247 {
248  int i,j;
249  for (i = 0; dst[i] != '\0'; i++)
250  ;
251  for (j = 0; src[j] != '\0'; j++)
252  dst[i+j] = src[j];
253  dst[i+j] = '\0';
254 
255  return dst;
256 }
257 
265 inline WCHAR16* wcsncat16(WCHAR16 *dst, const WCHAR16 *src, size_t len)
266 {
267  int i = 0;
268 
269  while (dst[i++] != 0)
270  ;
271  if (len != 0) {
272  i--;
273  do {
274  if ((dst[i++] = *src++) == 0)
275  break;
276  } while (--len != 0);
277  }
278  return dst;
279 }
280 
285 inline WCHAR16* wcsdup16(const WCHAR16 *src)
286 {
287  WCHAR16 *dst = new WCHAR16[wcslen16(src) + sizeof(WCHAR16)];
288  if(dst != NULL)
289  wcscpy16(dst, src);
290 
291  return dst;
292 }
293 
301 inline int wcsfind16(const WCHAR16 *s, const WCHAR16 *t, const int pos=0)
302 {
303  int i = 0, j;
304  if ( (s==NULL) || (t==NULL) || (pos<0) ) return -2;
305  while (s[pos+i] != 0)
306  {
307  j=0;
308  while ( (s[pos+i+j] != 0) && (s[pos+i+j] == t[j]) )
309  {
310  if (t[j+1] == 0)
311  return (i+j);
312  j++;
313  }
314  i++;
315  if(i < 0) return -3;
316  }
317 
318  return -1;
319 }
320 
328 inline int wcsspn16(const WCHAR16 *src1, const WCHAR16 *src2)
329 {
330  const WCHAR16 *s = src1;
331  const WCHAR16 *p = src2;
332 
333  while (*p)
334  {
335  if (*p++ == *s)
336  {
337  ++s;
338  p = src2;
339  }
340  }
341 
342  return (int)(s - src1);
343 }
344 
350 inline WCHAR16* wcspbrk16(const WCHAR16 *src1, const WCHAR16 *src2)
351 {
352  const WCHAR16 *s;
353  const WCHAR16 *p;
354 
355  for (s=src1; *s; s++)
356  {
357  for (p=src2; *p; p++)
358  {
359  if (*p == *s) return (WCHAR16 *) s;
360  }
361  }
362 
363  return NULL;
364 }
365 
375 inline WCHAR16* wcstok_r16(WCHAR16 *s1, const WCHAR16 *s2, WCHAR16 **context)
376 {
377  WCHAR16 *s;
378  WCHAR16 *p;
379 
380  if (((s = s1) != NULL) || ((s = *context) != NULL))
381  {
382  if (*(s += wcsspn16(s, s2)))
383  {
384  if ((p = wcspbrk16(s, s2)) != NULL)
385  {
386  *p++ = 0;
387  }
388  } else
389  {
390  p = s = NULL;
391  }
392  *context = p;
393  }
394 
395  return s;
396 }
397 
403 inline WCHAR16 *wcsstr16(WCHAR16 *src1, WCHAR16 *src2)
404 {
405  size_t n = wcslen16(src2);
406  while(*src1)
407  if(!memcmp(src1++, src2, sizeof(WCHAR16) * n))
408  return src1 - 1;
409 
410  return 0;
411 }
412 
418 inline const WCHAR16 *wcsstr16(const WCHAR16 *src1, const WCHAR16 *src2)
419 {
420  size_t n = wcslen16(src2);
421  while(*src1)
422  if(!memcmp(src1++, src2, sizeof(WCHAR16) * n))
423  return src1 - 1;
424 
425  return 0;
426 }
427 
432 inline int wtoi16(const WCHAR16 *src)
433 {
434  int result = 0;
435  while (*src >= '0' && *src <= '9')
436  result = result * 10 + *src++ - '0';
437 
438  return result;
439 }
440 
449 inline size_t wcstombs16(char *dst, const WCHAR16 *src, size_t len)
450 {
451  size_t count = 0;
452 
453  if (len != 0)
454  {
455  do
456  {
457  if ((*dst++ = (char)*src++) == 0)
458  break;
459  count++;
460  } while (--len != 0);
461  }
462 
463  return count;
464 }
465 
473 inline size_t mbstowcs16(WCHAR16 *dst, const char *src, size_t len)
474 {
475  size_t count = 0;
476 
477  if (len != 0)
478  {
479  do
480  {
481  if ((*dst++ = (WCHAR16)*src++) == 0)
482  break;
483  count++;
484  } while (--len != 0);
485  }
486 
487  return count;
488 }
489 
497 inline char* _wcstombs16(char *dst, const WCHAR16 *src, size_t len)
498 {
499  // same as wcstombs16, but returns pointer to dst instead of length
500  wcstombs16(dst, src, len);
501  return dst;
502 }
503 
510 inline WCHAR16* _mbstowcs16(WCHAR16 *dst, const char *src, size_t len)
511 {
512  // same as mbstowcs16, but returns pointer to dst instead of length
513  mbstowcs16(dst, src, len);
514  return dst;
515 }
516 
523 template<typename T>
524 WCHAR16* itow16_binary(T src, WCHAR16* dst, const size_t len)
525 {
526  size_t i = 0;
527  const size_t numBits = sizeof(T) * 8;
528 
529  if(dst && len && (len > numBits + 1))
530  {
531  const T mask = 0x1;
532  for(i = 0; i < numBits; i++)
533  {
534  T value = src & mask;
535  dst[numBits - i - 1] = (value) ? '1' : '0';
536  src = src >> 1;
537  }
538  }
539 
540  dst[i] = '\0';
541  return dst;
542 }
543 
544 
545 #endif /* __WCHAR16_H__ */
size_t wcstombs16(char *dst, const WCHAR16 *src, size_t len)
Function translates WCHAR16 characters from the sequence pointed by src to the multibyte equivalent s...
Definition: wchar16.h:449
WCHAR16 * itow16_binary(T src, WCHAR16 *dst, const size_t len)
Function to convert a data type to a binary string. (UCHAR) 0xC => "1010".
Definition: wchar16.h:524
size_t mbstowcs16(WCHAR16 *dst, const char *src, size_t len)
Function converts a sequence of multibyte characters to a corresponding sequence of WCHAR16 character...
Definition: wchar16.h:473
WCHAR16 * wcsncpy16(WCHAR16 *dst, const WCHAR16 *src, size_t len)
Function copies a specified number of WCHAR16 characters of a WCHAR16 string.
Definition: wchar16.h:224
wchar_t WCHAR16
UTF16 string.
Definition: mlpiGlobal.h:193
char * _wcstombs16(char *dst, const WCHAR16 *src, size_t len)
Function translates WCHAR16 characters from the sequence pointed by src to the multibyte equivalent s...
Definition: wchar16.h:497
WCHAR16 * wcsncat16(WCHAR16 *dst, const WCHAR16 *src, size_t len)
Function appends a WCHAR16 string with a certain length to another.
Definition: wchar16.h:265
WCHAR16 * _mbstowcs16(WCHAR16 *dst, const char *src, size_t len)
Function converts a sequence of multibyte characters to a corresponding sequence of WCHAR16 character...
Definition: wchar16.h:510
int wcsfind16(const WCHAR16 *s, const WCHAR16 *t, const int pos=0)
Function to determine the positon of one string in another.
Definition: wchar16.h:301
WCHAR16 * wcstok_r16(WCHAR16 *s1, const WCHAR16 *s2, WCHAR16 **context)
Function to disassemble a WCHAR16 string for certain WCHAR16 characters. A sequence of calls to this ...
Definition: wchar16.h:375
int wcscmp16(const WCHAR16 *src1, const WCHAR16 *src2)
Function compares two WCHAR16 strings with each other.
Definition: wchar16.h:179
WCHAR16 * wcsstr16(WCHAR16 *src1, WCHAR16 *src2)
Function searches for a specific WCHAR16 string sequence in a WCHAR16 string.
Definition: wchar16.h:403
int wcsncmp16(const WCHAR16 *src1, const WCHAR16 *src2, size_t len)
Function compares two WCHAR16 strings up to a certain length with each other.
Definition: wchar16.h:158
WCHAR16 * wcschr16(const WCHAR16 *src, WCHAR16 character)
Function searches a WCHAR16 character in a WCHAR16 string.
Definition: wchar16.h:133
int wtoi16(const WCHAR16 *src)
Function converts a WCHAR16 string to integer.
Definition: wchar16.h:432
WCHAR16 * wcspbrk16(const WCHAR16 *src1, const WCHAR16 *src2)
Function scans WCHAR16 strings for characters in specified character sets.
Definition: wchar16.h:350
WCHAR16 * wcscat16(WCHAR16 *dst, const WCHAR16 *src)
Function appends a WCHAR16 string to another.
Definition: wchar16.h:246
WCHAR16 * wcsdup16(const WCHAR16 *src)
Function appends a WCHAR16 string with a certain length to another.
Definition: wchar16.h:285
WCHAR16 * wcscpy16(WCHAR16 *dst, const WCHAR16 *src)
Function copies a WCHAR16 string.
Definition: wchar16.h:210
int wcsspn16(const WCHAR16 *src1, const WCHAR16 *src2)
Function to determine the length of the matching WCHAR16 string.
Definition: wchar16.h:328
size_t wcslen16(const WCHAR16 *src)
Definition: wchar16.h:115