OVR_Std.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 PublicHeader:   OVR.h
00004 Filename    :   OVR_Std.h
00005 Content     :   Standard C function interface
00006 Created     :   September 19, 2012
00007 Notes       : 
00008 
00009 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
00010 
00011 Use of this software is subject to the terms of the Oculus license
00012 agreement provided at the time of installation or download, or which
00013 otherwise accompanies this software in either electronic or hard copy form.
00014 
00015 ************************************************************************************/
00016 
00017 #ifndef OVR_Std_h
00018 #define OVR_Std_h
00019 
00020 #include "OVR_Types.h"
00021 #include <stdarg.h> // for va_list args
00022 #include <string.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <ctype.h>
00026 
00027 #if !defined(OVR_OS_WINCE) && defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400)
00028 #define OVR_MSVC_SAFESTRING
00029 #include <errno.h>
00030 #endif
00031 
00032 // Wide-char funcs
00033 #include <wchar.h>
00034 #include <wctype.h>
00035 
00036 namespace OVR {
00037 
00038 #if defined(OVR_OS_WIN32)
00039 inline char* OVR_CDECL OVR_itoa(int val, char *dest, UPInt destsize, int radix)
00040 {
00041 #if defined(OVR_MSVC_SAFESTRING)
00042     _itoa_s(val, dest, destsize, radix);
00043     return dest;
00044 #else
00045     OVR_UNUSED(destsize);
00046     return itoa(val, dest, radix);
00047 #endif
00048 }
00049 #else // OVR_OS_WIN32
00050 inline char* OVR_itoa(int val, char* dest, unsigned int len, int radix)
00051 {
00052     if (val == 0)
00053     {
00054         if (len > 1)
00055         {
00056             dest[0] = '0';
00057             dest[1] = '\0';  
00058         }
00059         return dest;
00060     }
00061 
00062     int cur = val;
00063     unsigned int i    = 0; 
00064     unsigned int sign = 0;
00065 
00066     if (val < 0)
00067     {
00068         val = -val;
00069         sign = 1;
00070     }
00071 
00072     while ((val != 0) && (i < (len - 1 - sign)))        
00073     {
00074         cur    = val % radix;
00075         val   /= radix;
00076 
00077         if (radix == 16)
00078         {
00079             switch(cur)
00080             {
00081             case 10:
00082                 dest[i] = 'a';
00083                 break;
00084             case 11:
00085                 dest[i] = 'b';
00086                 break;
00087             case 12:
00088                 dest[i] = 'c';
00089                 break;
00090             case 13:
00091                 dest[i] = 'd';
00092                 break;
00093             case 14:
00094                 dest[i] = 'e';
00095                 break;
00096             case 15:
00097                 dest[i] = 'f';
00098                 break;
00099             default:
00100                 dest[i] = (char)('0' + cur);
00101                 break;
00102             }
00103         } 
00104         else
00105         {
00106             dest[i] = (char)('0' + cur);
00107         }
00108         ++i;
00109     }
00110 
00111     if (sign)
00112     {
00113         dest[i++] = '-';
00114     }
00115 
00116     for (unsigned int j = 0; j < i / 2; ++j)
00117     {
00118         char tmp        = dest[j];
00119         dest[j]         = dest[i - 1 - j];
00120         dest[i - 1 - j] = tmp;
00121     }
00122     dest[i] = '\0';
00123 
00124     return dest;
00125 }
00126 
00127 #endif
00128 
00129 
00130 // String functions
00131 
00132 inline UPInt OVR_CDECL OVR_strlen(const char* str)
00133 {
00134     return strlen(str);
00135 }
00136 
00137 inline char* OVR_CDECL OVR_strcpy(char* dest, UPInt destsize, const char* src)
00138 {
00139 #if defined(OVR_MSVC_SAFESTRING)
00140     strcpy_s(dest, destsize, src);
00141     return dest;
00142 #else
00143     OVR_UNUSED(destsize);
00144     return strcpy(dest, src);
00145 #endif
00146 }
00147 
00148 inline char* OVR_CDECL OVR_strncpy(char* dest, UPInt destsize, const char* src, UPInt count)
00149 {
00150 #if defined(OVR_MSVC_SAFESTRING)
00151     strncpy_s(dest, destsize, src, count);
00152     return dest;
00153 #else
00154     OVR_UNUSED(destsize);
00155     return strncpy(dest, src, count);
00156 #endif
00157 }
00158 
00159 inline char * OVR_CDECL OVR_strcat(char* dest, UPInt destsize, const char* src)
00160 {
00161 #if defined(OVR_MSVC_SAFESTRING)
00162     strcat_s(dest, destsize, src);
00163     return dest;
00164 #else
00165     OVR_UNUSED(destsize);
00166     return strcat(dest, src);
00167 #endif
00168 }
00169 
00170 inline int OVR_CDECL OVR_strcmp(const char* dest, const char* src)
00171 {
00172     return strcmp(dest, src);
00173 }
00174 
00175 inline const char* OVR_CDECL OVR_strchr(const char* str, char c)
00176 {
00177     return strchr(str, c);
00178 }
00179 
00180 inline char* OVR_CDECL OVR_strchr(char* str, char c)
00181 {
00182     return strchr(str, c);
00183 }
00184 
00185 inline const char* OVR_strrchr(const char* str, char c)
00186 {
00187     UPInt len = OVR_strlen(str);
00188     for (UPInt i=len; i>0; i--)     
00189         if (str[i]==c) 
00190             return str+i;
00191     return 0;
00192 }
00193 
00194 inline const UByte* OVR_CDECL OVR_memrchr(const UByte* str, UPInt size, UByte c)
00195 {
00196     for (SPInt i = (SPInt)size - 1; i >= 0; i--)     
00197     {
00198         if (str[i] == c) 
00199             return str + i;
00200     }
00201     return 0;
00202 }
00203 
00204 inline char* OVR_CDECL OVR_strrchr(char* str, char c)
00205 {
00206     UPInt len = OVR_strlen(str);
00207     for (UPInt i=len; i>0; i--)     
00208         if (str[i]==c) 
00209             return str+i;
00210     return 0;
00211 }
00212 
00213 
00214 double OVR_CDECL OVR_strtod(const char* string, char** tailptr);
00215 
00216 inline long OVR_CDECL OVR_strtol(const char* string, char** tailptr, int radix)
00217 {
00218     return strtol(string, tailptr, radix);
00219 }
00220 
00221 inline long OVR_CDECL OVR_strtoul(const char* string, char** tailptr, int radix)
00222 {
00223     return strtoul(string, tailptr, radix);
00224 }
00225 
00226 inline int OVR_CDECL OVR_strncmp(const char* ws1, const char* ws2, UPInt size)
00227 {
00228     return strncmp(ws1, ws2, size);
00229 }
00230 
00231 inline UInt64 OVR_CDECL OVR_strtouq(const char *nptr, char **endptr, int base)
00232 {
00233 #if defined(OVR_CC_MSVC) && !defined(OVR_OS_WINCE)
00234     return _strtoui64(nptr, endptr, base);
00235 #else
00236     return strtoull(nptr, endptr, base);
00237 #endif
00238 }
00239 
00240 inline SInt64 OVR_CDECL OVR_strtoq(const char *nptr, char **endptr, int base)
00241 {
00242 #if defined(OVR_CC_MSVC) && !defined(OVR_OS_WINCE)
00243     return _strtoi64(nptr, endptr, base);
00244 #else
00245     return strtoll(nptr, endptr, base);
00246 #endif
00247 }
00248 
00249 
00250 inline SInt64 OVR_CDECL OVR_atoq(const char* string)
00251 {
00252 #if defined(OVR_CC_MSVC) && !defined(OVR_OS_WINCE)
00253     return _atoi64(string);
00254 #else
00255     return atoll(string);
00256 #endif
00257 }
00258 
00259 inline UInt64 OVR_CDECL OVR_atouq(const char* string)
00260 {
00261   return OVR_strtouq(string, NULL, 10);
00262 }
00263 
00264 
00265 // Implemented in GStd.cpp in platform-specific manner.
00266 int OVR_CDECL OVR_stricmp(const char* dest, const char* src);
00267 int OVR_CDECL OVR_strnicmp(const char* dest, const char* src, UPInt count);
00268 
00269 inline UPInt OVR_CDECL OVR_sprintf(char *dest, UPInt destsize, const char* format, ...)
00270 {
00271     va_list argList;
00272     va_start(argList,format);
00273     UPInt ret;
00274 #if defined(OVR_CC_MSVC)
00275     #if defined(OVR_MSVC_SAFESTRING)
00276         ret = _vsnprintf_s(dest, destsize, _TRUNCATE, format, argList);
00277         OVR_ASSERT(ret != -1);
00278     #else
00279         OVR_UNUSED(destsize);
00280         ret = _vsnprintf(dest, destsize - 1, format, argList); // -1 for space for the null character
00281         OVR_ASSERT(ret != -1);
00282         dest[destsize-1] = 0;
00283     #endif
00284 #else
00285     OVR_UNUSED(destsize);
00286     ret = vsprintf(dest, format, argList);
00287     OVR_ASSERT(ret < destsize);
00288 #endif
00289     va_end(argList);
00290     return ret;
00291 }
00292 
00293 inline UPInt OVR_CDECL OVR_vsprintf(char *dest, UPInt destsize, const char * format, va_list argList)
00294 {
00295     UPInt ret;
00296 #if defined(OVR_CC_MSVC)
00297     #if defined(OVR_MSVC_SAFESTRING)
00298         dest[0] = '\0';
00299         int rv = vsnprintf_s(dest, destsize, _TRUNCATE, format, argList);
00300         if (rv == -1)
00301         {
00302             dest[destsize - 1] = '\0';
00303             ret = destsize - 1;
00304         }
00305         else
00306             ret = (UPInt)rv;
00307     #else
00308         OVR_UNUSED(destsize);
00309         int rv = _vsnprintf(dest, destsize - 1, format, argList);
00310         OVR_ASSERT(rv != -1);
00311         ret = (UPInt)rv;
00312         dest[destsize-1] = 0;
00313     #endif
00314 #else
00315     OVR_UNUSED(destsize);
00316     ret = (UPInt)vsprintf(dest, format, argList);
00317     OVR_ASSERT(ret < destsize);
00318 #endif
00319     return ret;
00320 }
00321 
00322 // Returns the number of characters in the formatted string.
00323 inline UPInt OVR_CDECL OVR_vscprintf(const char * format, va_list argList)
00324 {
00325     UPInt ret;
00326 #if defined(OVR_CC_MSVC)
00327     ret = (UPInt) _vscprintf(format, argList);
00328 #else    
00329     ret = (UPInt) vsnprintf(NULL, 0, format, argList);
00330 #endif
00331     return ret;       
00332 }
00333 
00334 
00335 wchar_t* OVR_CDECL OVR_wcscpy(wchar_t* dest, UPInt destsize, const wchar_t* src);
00336 wchar_t* OVR_CDECL OVR_wcsncpy(wchar_t* dest, UPInt destsize, const wchar_t* src, UPInt count);
00337 wchar_t* OVR_CDECL OVR_wcscat(wchar_t* dest, UPInt destsize, const wchar_t* src);
00338 UPInt    OVR_CDECL OVR_wcslen(const wchar_t* str);
00339 int      OVR_CDECL OVR_wcscmp(const wchar_t* a, const wchar_t* b);
00340 int      OVR_CDECL OVR_wcsicmp(const wchar_t* a, const wchar_t* b);
00341 
00342 inline int OVR_CDECL OVR_wcsicoll(const wchar_t* a, const wchar_t* b)
00343 {
00344 #if defined(OVR_OS_WIN32)
00345 #if defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400)
00346     return ::_wcsicoll(a, b);
00347 #else
00348     return ::wcsicoll(a, b);
00349 #endif
00350 #else
00351     // not supported, use regular wcsicmp
00352     return OVR_wcsicmp(a, b);
00353 #endif
00354 }
00355 
00356 inline int OVR_CDECL OVR_wcscoll(const wchar_t* a, const wchar_t* b)
00357 {
00358 #if defined(OVR_OS_WIN32) || defined(OVR_OS_LINUX)
00359     return wcscoll(a, b);
00360 #else
00361     // not supported, use regular wcscmp
00362     return OVR_wcscmp(a, b);
00363 #endif
00364 }
00365 
00366 #ifndef OVR_NO_WCTYPE
00367 
00368 inline int OVR_CDECL UnicodeCharIs(const UInt16* table, wchar_t charCode)
00369 {
00370     unsigned offset = table[charCode >> 8];
00371     if (offset == 0) return 0;
00372     if (offset == 1) return 1;
00373     return (table[offset + ((charCode >> 4) & 15)] & (1 << (charCode & 15))) != 0;
00374 }
00375 
00376 extern const UInt16 UnicodeAlnumBits[];
00377 extern const UInt16 UnicodeAlphaBits[];
00378 extern const UInt16 UnicodeDigitBits[];
00379 extern const UInt16 UnicodeSpaceBits[];
00380 extern const UInt16 UnicodeXDigitBits[];
00381 
00382 // Uncomment if necessary
00383 //extern const UInt16 UnicodeCntrlBits[];
00384 //extern const UInt16 UnicodeGraphBits[];
00385 //extern const UInt16 UnicodeLowerBits[];
00386 //extern const UInt16 UnicodePrintBits[];
00387 //extern const UInt16 UnicodePunctBits[];
00388 //extern const UInt16 UnicodeUpperBits[];
00389 
00390 inline int OVR_CDECL OVR_iswalnum (wchar_t charCode) { return UnicodeCharIs(UnicodeAlnumBits,  charCode); }
00391 inline int OVR_CDECL OVR_iswalpha (wchar_t charCode) { return UnicodeCharIs(UnicodeAlphaBits,  charCode); }
00392 inline int OVR_CDECL OVR_iswdigit (wchar_t charCode) { return UnicodeCharIs(UnicodeDigitBits,  charCode); }
00393 inline int OVR_CDECL OVR_iswspace (wchar_t charCode) { return UnicodeCharIs(UnicodeSpaceBits,  charCode); }
00394 inline int OVR_CDECL OVR_iswxdigit(wchar_t charCode) { return UnicodeCharIs(UnicodeXDigitBits, charCode); }
00395 
00396 // Uncomment if necessary
00397 //inline int OVR_CDECL OVR_iswcntrl (wchar_t charCode) { return UnicodeCharIs(UnicodeCntrlBits,  charCode); }
00398 //inline int OVR_CDECL OVR_iswgraph (wchar_t charCode) { return UnicodeCharIs(UnicodeGraphBits,  charCode); }
00399 //inline int OVR_CDECL OVR_iswlower (wchar_t charCode) { return UnicodeCharIs(UnicodeLowerBits,  charCode); }
00400 //inline int OVR_CDECL OVR_iswprint (wchar_t charCode) { return UnicodeCharIs(UnicodePrintBits,  charCode); }
00401 //inline int OVR_CDECL OVR_iswpunct (wchar_t charCode) { return UnicodeCharIs(UnicodePunctBits,  charCode); }
00402 //inline int OVR_CDECL OVR_iswupper (wchar_t charCode) { return UnicodeCharIs(UnicodeUpperBits,  charCode); }
00403 
00404 int OVR_CDECL OVR_towupper(wchar_t charCode);
00405 int OVR_CDECL OVR_towlower(wchar_t charCode);
00406 
00407 #else // OVR_NO_WCTYPE
00408 
00409 inline int OVR_CDECL OVR_iswspace(wchar_t c)
00410 {
00411     return iswspace(c);
00412 }
00413 
00414 inline int OVR_CDECL OVR_iswdigit(wchar_t c)
00415 {
00416     return iswdigit(c);
00417 }
00418 
00419 inline int OVR_CDECL OVR_iswxdigit(wchar_t c)
00420 {
00421     return iswxdigit(c);
00422 }
00423 
00424 inline int OVR_CDECL OVR_iswalpha(wchar_t c)
00425 {
00426     return iswalpha(c);
00427 }
00428 
00429 inline int OVR_CDECL OVR_iswalnum(wchar_t c)
00430 {
00431     return iswalnum(c);
00432 }
00433 
00434 inline wchar_t OVR_CDECL OVR_towlower(wchar_t c)
00435 {
00436     return (wchar_t)towlower(c);
00437 }
00438 
00439 inline wchar_t OVR_towupper(wchar_t c)
00440 {
00441     return (wchar_t)towupper(c);
00442 }
00443 
00444 #endif // OVR_NO_WCTYPE
00445 
00446 // ASCII versions of tolower and toupper. Don't use "char"
00447 inline int OVR_CDECL OVR_tolower(int c)
00448 {
00449     return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c;
00450 }
00451 
00452 inline int OVR_CDECL OVR_toupper(int c)
00453 {
00454     return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
00455 }
00456 
00457 
00458 
00459 inline double OVR_CDECL OVR_wcstod(const wchar_t* string, wchar_t** tailptr)
00460 {
00461 #if defined(OVR_OS_OTHER)
00462     OVR_UNUSED(tailptr);
00463     char buffer[64];
00464     char* tp = NULL;
00465     UPInt max = OVR_wcslen(string);
00466     if (max > 63) max = 63;
00467     unsigned char c = 0;
00468     for (UPInt i=0; i < max; i++)
00469     {
00470         c = (unsigned char)string[i];
00471         buffer[i] = ((c) < 128 ? (char)c : '!');
00472     }
00473     buffer[max] = 0;
00474     return OVR_strtod(buffer, &tp);
00475 #else
00476     return wcstod(string, tailptr);
00477 #endif
00478 }
00479 
00480 inline long OVR_CDECL OVR_wcstol(const wchar_t* string, wchar_t** tailptr, int radix)
00481 {
00482 #if defined(OVR_OS_OTHER)
00483     OVR_UNUSED(tailptr);
00484     char buffer[64];
00485     char* tp = NULL;
00486     UPInt max = OVR_wcslen(string);
00487     if (max > 63) max = 63;
00488     unsigned char c = 0;
00489     for (UPInt i=0; i < max; i++)
00490     {
00491         c = (unsigned char)string[i];
00492         buffer[i] = ((c) < 128 ? (char)c : '!');
00493     }
00494     buffer[max] = 0;
00495     return strtol(buffer, &tp, radix);
00496 #else
00497     return wcstol(string, tailptr, radix);
00498 #endif
00499 }
00500 
00501 } // OVR
00502 
00503 #endif // OVR_Std_h


oculus_sdk
Author(s):
autogenerated on Mon Oct 6 2014 03:01:19