gwnavruntime/kernel/SF_Std.h Source File

SF_Std.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: Kernel
10 Filename : KY_Std.h
11 Content : Standard C function interface
12 Created : 2/25/2007
13 Authors : Ankur Mohan, Michael Antonov, Artem Bolgar, Maxim Shemanarev
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_Std_H
18 #define INC_KY_Kernel_Std_H
19 
21 #include <stdarg.h> // for va_list args
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 
27 #if !defined(KY_OS_WINCE) && defined(KY_CC_MSVC) && (KY_CC_MSVC >= 1400)
28 #define KY_MSVC_SAFESTRING
29 #include <errno.h>
30 #endif
31 
32 // Wide-char funcs
33 #if !defined(KY_OS_SYMBIAN) && !defined(KY_CC_RENESAS) && !defined(KY_OS_PS2)
34 #include <wchar.h>
35 #endif
36 
37 #if !defined(KY_OS_SYMBIAN) && !defined(KY_CC_RENESAS) && !defined(KY_OS_PS2) && !defined(KY_CC_SNC)
38 #include <wctype.h>
39 #endif
40 
41 namespace Kaim {
42 
43 #if defined(KY_OS_WIN32)
44 inline char* KY_CDECL SFitoa(int val, char *dest, UPInt destsize, int radix)
45 {
46 #if defined(KY_MSVC_SAFESTRING)
47  _itoa_s(val, dest, destsize, radix);
48  return dest;
49 #else
50  KY_UNUSED(destsize);
51  return itoa(val, dest, radix);
52 #endif
53 }
54 #else // KY_OS_WIN32
55 inline char* SFitoa(int val, char* dest, unsigned int len, int radix)
56 {
57  if (val == 0)
58  {
59  if (len > 1)
60  {
61  dest[0] = '0';
62  dest[1] = '\0';
63  }
64  return dest;
65  }
66 
67  int cur = val;
68  unsigned int i = 0;
69  unsigned int sign = 0;
70 
71  if (val < 0)
72  {
73  val = -val;
74  sign = 1;
75  }
76 
77  while ((val != 0) && (i < (len - 1 - sign)))
78  {
79  cur = val % radix;
80  val /= radix;
81 
82  if (radix == 16)
83  {
84  switch(cur)
85  {
86  case 10:
87  dest[i] = 'a';
88  break;
89  case 11:
90  dest[i] = 'b';
91  break;
92  case 12:
93  dest[i] = 'c';
94  break;
95  case 13:
96  dest[i] = 'd';
97  break;
98  case 14:
99  dest[i] = 'e';
100  break;
101  case 15:
102  dest[i] = 'f';
103  break;
104  default:
105  dest[i] = (char)('0' + cur);
106  break;
107  }
108  }
109  else
110  {
111  dest[i] = (char)('0' + cur);
112  }
113  ++i;
114  }
115 
116  if (sign)
117  {
118  dest[i++] = '-';
119  }
120 
121  for (unsigned int j = 0; j < i / 2; ++j)
122  {
123  char tmp = dest[j];
124  dest[j] = dest[i - 1 - j];
125  dest[i - 1 - j] = tmp;
126  }
127  dest[i] = '\0';
128 
129  return dest;
130 }
131 
132 #endif
133 
134 
135 // String functions
136 
137 inline UPInt KY_CDECL SFstrlen(const char* str)
138 {
139  return strlen(str);
140 }
141 
142 inline char* KY_CDECL SFstrcpy(char* dest, UPInt destsize, const char* src)
143 {
144 #if defined(KY_MSVC_SAFESTRING)
145  strcpy_s(dest, destsize, src);
146  return dest;
147 #else
148  KY_UNUSED(destsize);
149  return strcpy(dest, src);
150 #endif
151 }
152 
153 // Note that when using strncpy no null-character is implicitly appended at the end of destination if source is longer than num
154 // (thus, in this case, destination may not be a null terminated C string).
155 inline char* KY_CDECL SFstrncpy(char* dest, UPInt destsize, const char* src, UPInt count)
156 {
157 #if defined(KY_MSVC_SAFESTRING)
158  strncpy_s(dest, destsize, src, count);
159  return dest;
160 #else
161  KY_UNUSED(destsize);
162  return strncpy(dest, src, count);
163 #endif
164 }
165 
166 inline char * KY_CDECL SFstrcat(char* dest, UPInt destsize, const char* src)
167 {
168 #if defined(KY_MSVC_SAFESTRING)
169  strcat_s(dest, destsize, src);
170  return dest;
171 #else
172  KY_UNUSED(destsize);
173  return strcat(dest, src);
174 #endif
175 }
176 
177 inline int KY_CDECL SFstrcmp(const char* dest, const char* src)
178 {
179  return strcmp(dest, src);
180 }
181 
182 inline const char* KY_CDECL SFstrchr(const char* str, char c)
183 {
184  return strchr(str, c);
185 }
186 
187 inline char* KY_CDECL SFstrchr(char* str, char c)
188 {
189  return strchr(str, c);
190 }
191 
192 inline const char* SFstrrchr(const char* str, char c)
193 {
194  UPInt len = SFstrlen(str);
195  for (UPInt i=len; i>0; i--)
196  if (str[i]==c)
197  return str+i;
198  return 0;
199 }
200 
201 inline const UByte* KY_CDECL SFmemrchr(const UByte* str, UPInt size, UByte c)
202 {
203  for (SPInt i = (SPInt)size - 1; i >= 0; i--)
204  {
205  if (str[i] == c)
206  return str + i;
207  }
208  return 0;
209 }
210 
211 inline char* KY_CDECL SFstrrchr(char* str, char c)
212 {
213  UPInt len = SFstrlen(str);
214  for (UPInt i=len; i>0; i--)
215  if (str[i]==c)
216  return str+i;
217  return 0;
218 }
219 
220 
221 double KY_CDECL SFstrtod(const char* string, char** tailptr);
222 
223 inline long KY_CDECL SFstrtol(const char* string, char** tailptr, int radix)
224 {
225  return strtol(string, tailptr, radix);
226 }
227 
228 inline long KY_CDECL SFstrtoul(const char* string, char** tailptr, int radix)
229 {
230  return strtoul(string, tailptr, radix);
231 }
232 
233 inline int KY_CDECL SFstrncmp(const char* ws1, const char* ws2, UPInt size)
234 {
235  return strncmp(ws1, ws2, size);
236 }
237 
238 inline UInt64 KY_CDECL SFstrtouq(const char *nptr, char **endptr, int base)
239 {
240 #if defined(KY_CC_MSVC) && !defined(KY_OS_WINCE)
241  return _strtoui64(nptr, endptr, base);
242 #else
243  return strtoull(nptr, endptr, base);
244 #endif
245 }
246 
247 inline SInt64 KY_CDECL SFstrtoq(const char *nptr, char **endptr, int base)
248 {
249 #if defined(KY_CC_MSVC) && !defined(KY_OS_WINCE)
250  return _strtoi64(nptr, endptr, base);
251 #else
252  return strtoll(nptr, endptr, base);
253 #endif
254 }
255 
256 
257 inline SInt64 KY_CDECL SFatoq(const char* string)
258 {
259 #if defined(KY_CC_MSVC) && !defined(KY_OS_WINCE)
260  return _atoi64(string);
261 #else
262  return atoll(string);
263 #endif
264 }
265 
266 inline UInt64 KY_CDECL SFatouq(const char* string)
267 {
268  return SFstrtouq(string, NULL, 10);
269 }
270 
271 
272 // Implemented in GStd.cpp in platform-specific manner.
273 int KY_CDECL SFstricmp(const char* dest, const char* src);
274 int KY_CDECL SFstrnicmp(const char* dest, const char* src, UPInt count);
275 
276 inline UPInt KY_CDECL SFsprintf(char *dest, UPInt destsize, const char* format, ...)
277 {
278  va_list argList;
279  va_start(argList,format);
280  UPInt ret;
281 #if defined(KY_CC_MSVC)
282  #if defined(KY_MSVC_SAFESTRING)
283  ret = _vsnprintf_s(dest, destsize, _TRUNCATE, format, argList);
284  KY_ASSERT(ret != -1);
285  #else
286  KY_UNUSED(destsize);
287  ret = _vsnprintf(dest, destsize - 1, format, argList); // -1 for space for the null character
288  KY_ASSERT(ret != -1);
289  dest[destsize-1] = 0;
290  #endif
291 #else
292  KY_UNUSED(destsize);
293  ret = vsprintf(dest, format, argList);
294  KY_ASSERT(ret < destsize);
295 #endif
296  va_end(argList);
297  return ret;
298 }
299 
300 inline UPInt KY_CDECL SFvsprintf(char *dest, UPInt destsize, const char * format, va_list argList)
301 {
302  UPInt ret;
303 #if defined(KY_CC_MSVC)
304  #if defined(KY_MSVC_SAFESTRING)
305  dest[0] = '\0';
306  int rv = vsnprintf_s(dest, destsize, _TRUNCATE, format, argList);
307  if (rv == -1)
308  {
309  dest[destsize - 1] = '\0';
310  ret = destsize - 1;
311  }
312  else
313  ret = (UPInt)rv;
314  #else
315  KY_UNUSED(destsize);
316  int rv = _vsnprintf(dest, destsize - 1, format, argList);
317  KY_ASSERT(rv != -1);
318  ret = (UPInt)rv;
319  dest[destsize-1] = 0;
320  #endif
321 #else
322  KY_UNUSED(destsize);
323  ret = (UPInt)vsprintf(dest, format, argList);
324  KY_ASSERT(ret < destsize);
325 #endif
326  return ret;
327 }
328 
329 wchar_t* KY_CDECL SFwcscpy(wchar_t* dest, UPInt destsize, const wchar_t* src);
330 wchar_t* KY_CDECL SFwcsncpy(wchar_t* dest, UPInt destsize, const wchar_t* src, UPInt count);
331 wchar_t* KY_CDECL SFwcscat(wchar_t* dest, UPInt destsize, const wchar_t* src);
332 UPInt KY_CDECL SFwcslen(const wchar_t* str);
333 int KY_CDECL SFwcscmp(const wchar_t* a, const wchar_t* b);
334 int KY_CDECL SFwcsicmp(const wchar_t* a, const wchar_t* b);
335 
336 inline int KY_CDECL SFwcsicoll(const wchar_t* a, const wchar_t* b)
337 {
338 #if defined(KY_OS_WIN32) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360) || defined(KY_OS_WII)
339 #if defined(KY_CC_MSVC) && (KY_CC_MSVC >= 1400)
340  return ::_wcsicoll(a, b);
341 #else
342  return ::wcsicoll(a, b);
343 #endif
344 #else
345  // not supported, use regular wcsicmp
346  return SFwcsicmp(a, b);
347 #endif
348 }
349 
350 inline int KY_CDECL SFwcscoll(const wchar_t* a, const wchar_t* b)
351 {
352 #if defined(KY_OS_WIN32) || defined(KY_OS_XBOX) || defined(KY_OS_XBOX360) || defined(KY_OS_PS3) || defined(KY_OS_WII) || defined(KY_OS_LINUX)
353  return wcscoll(a, b);
354 #else
355  // not supported, use regular wcscmp
356  return SFwcscmp(a, b);
357 #endif
358 }
359 
360 #ifndef KY_NO_WCTYPE
361 
362 inline int KY_CDECL UnicodeCharIs(const UInt16* table, wchar_t charCode)
363 {
364  unsigned offset = table[charCode >> 8];
365  if (offset == 0) return 0;
366  if (offset == 1) return 1;
367  return (table[offset + ((charCode >> 4) & 15)] & (1 << (charCode & 15))) != 0;
368 }
369 
370 extern const UInt16 UnicodeAlnumBits[];
371 extern const UInt16 UnicodeAlphaBits[];
372 extern const UInt16 UnicodeDigitBits[];
373 extern const UInt16 UnicodeSpaceBits[];
374 extern const UInt16 UnicodeXDigitBits[];
375 
376 // Uncomment if necessary
377 //extern const UInt16 UnicodeCntrlBits[];
378 //extern const UInt16 UnicodeGraphBits[];
379 //extern const UInt16 UnicodeLowerBits[];
380 //extern const UInt16 UnicodePrintBits[];
381 //extern const UInt16 UnicodePunctBits[];
382 //extern const UInt16 UnicodeUpperBits[];
383 
384 inline int KY_CDECL SFiswalnum (wchar_t charCode) { return UnicodeCharIs(UnicodeAlnumBits, charCode); }
385 inline int KY_CDECL SFiswalpha (wchar_t charCode) { return UnicodeCharIs(UnicodeAlphaBits, charCode); }
386 inline int KY_CDECL SFiswdigit (wchar_t charCode) { return UnicodeCharIs(UnicodeDigitBits, charCode); }
387 inline int KY_CDECL SFiswspace (wchar_t charCode) { return UnicodeCharIs(UnicodeSpaceBits, charCode); }
388 inline int KY_CDECL SFiswxdigit(wchar_t charCode) { return UnicodeCharIs(UnicodeXDigitBits, charCode); }
389 
390 // Uncomment if necessary
391 //inline int KY_CDECL SFiswcntrl (wchar_t charCode) { return UnicodeCharIs(UnicodeCntrlBits, charCode); }
392 //inline int KY_CDECL SFiswgraph (wchar_t charCode) { return UnicodeCharIs(UnicodeGraphBits, charCode); }
393 //inline int KY_CDECL SFiswlower (wchar_t charCode) { return UnicodeCharIs(UnicodeLowerBits, charCode); }
394 //inline int KY_CDECL SFiswprint (wchar_t charCode) { return UnicodeCharIs(UnicodePrintBits, charCode); }
395 //inline int KY_CDECL SFiswpunct (wchar_t charCode) { return UnicodeCharIs(UnicodePunctBits, charCode); }
396 //inline int KY_CDECL SFiswupper (wchar_t charCode) { return UnicodeCharIs(UnicodeUpperBits, charCode); }
397 
398 int KY_CDECL SFtowupper(wchar_t charCode);
399 int KY_CDECL SFtowlower(wchar_t charCode);
400 
401 #else // KY_NO_WCTYPE
402 
403 inline int KY_CDECL SFiswspace(wchar_t c)
404 {
405 #if defined(KY_OS_WII) || defined(KY_CC_SNC)
406  return ((c) < 128 ? isspace((char)c) : 0);
407 #else
408  return iswspace(c);
409 #endif
410 }
411 
412 inline int KY_CDECL SFiswdigit(wchar_t c)
413 {
414 #if defined(KY_OS_WII) || defined(KY_CC_SNC)
415  return ((c) < 128 ? isdigit((char)c) : 0);
416 #else
417  return iswdigit(c);
418 #endif
419 }
420 
421 inline int KY_CDECL SFiswxdigit(wchar_t c)
422 {
423 #if defined(KY_OS_WII) || defined(KY_CC_SNC)
424  return ((c) < 128 ? isxdigit((char)c) : 0);
425 #else
426  return iswxdigit(c);
427 #endif
428 }
429 
430 inline int KY_CDECL SFiswalpha(wchar_t c)
431 {
432 #if defined(KY_OS_WII) || defined(KY_CC_SNC)
433  return ((c) < 128 ? isalpha((char)c) : 0);
434 #else
435  return iswalpha(c);
436 #endif
437 }
438 
439 inline int KY_CDECL SFiswalnum(wchar_t c)
440 {
441 #if defined(KY_OS_WII) || defined(KY_CC_SNC)
442  return ((c) < 128 ? isalnum((char)c) : 0);
443 #else
444  return iswalnum(c);
445 #endif
446 }
447 
448 inline wchar_t KY_CDECL SFtowlower(wchar_t c)
449 {
450 #if (defined(KY_OS_PS3) && defined(KY_CC_SNC))
451  return (wchar_t)tolower((char)c);
452 #else
453  return (wchar_t)towlower(c);
454 #endif
455 }
456 
457 inline wchar_t SFtowupper(wchar_t c)
458 {
459 #if (defined(KY_OS_PS3) && defined(KY_CC_SNC))
460  return (wchar_t)toupper((char)c);
461 #else
462  return (wchar_t)towupper(c);
463 #endif
464 }
465 
466 #endif // KY_NO_WCTYPE
467 
468 // ASCII versions of tolower and toupper. Don't use "char"
469 inline int KY_CDECL SFtolower(int c)
470 {
471  return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c;
472 }
473 
474 inline int KY_CDECL SFtoupper(int c)
475 {
476  return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
477 }
478 
479 
480 
481 inline double KY_CDECL SFwcstod(const wchar_t* string, wchar_t** tailptr)
482 {
483 #if defined(KY_OS_OTHER)
484  KY_UNUSED(tailptr);
485  char buffer[64];
486  char* tp = NULL;
487  UPInt max = SFwcslen(string);
488  if (max > 63) max = 63;
489  unsigned char c = 0;
490  for (UPInt i=0; i < max; i++)
491  {
492  c = (unsigned char)string[i];
493  buffer[i] = ((c) < 128 ? (char)c : '!');
494  }
495  buffer[max] = 0;
496  return SFstrtod(buffer, &tp);
497 #else
498  return wcstod(string, tailptr);
499 #endif
500 }
501 
502 inline long KY_CDECL SFwcstol(const wchar_t* string, wchar_t** tailptr, int radix)
503 {
504 #if defined(KY_OS_OTHER)
505  KY_UNUSED(tailptr);
506  char buffer[64];
507  char* tp = NULL;
508  UPInt max = SFwcslen(string);
509  if (max > 63) max = 63;
510  unsigned char c = 0;
511  for (UPInt i=0; i < max; i++)
512  {
513  c = (unsigned char)string[i];
514  buffer[i] = ((c) < 128 ? (char)c : '!');
515  }
516  buffer[max] = 0;
517  return strtol(buffer, &tp, radix);
518 #else
519  return wcstol(string, tailptr, radix);
520 #endif
521 }
522 
523 } // Scaleform
524 
525 #endif // INC_GSTD_H
Definition: gamekitcrowddispersion.h:20