diff options
Diffstat (limited to 'LibOVR/Src/Kernel/OVR_Std.cpp')
-rw-r--r-- | LibOVR/Src/Kernel/OVR_Std.cpp | 187 |
1 files changed, 124 insertions, 63 deletions
diff --git a/LibOVR/Src/Kernel/OVR_Std.cpp b/LibOVR/Src/Kernel/OVR_Std.cpp index 6b5be18..fc5ad04 100644 --- a/LibOVR/Src/Kernel/OVR_Std.cpp +++ b/LibOVR/Src/Kernel/OVR_Std.cpp @@ -5,16 +5,16 @@ Content : Standard C function implementation Created : September 19, 2012 Notes : -Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved. +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. -Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License"); +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); you may not use the Oculus VR Rift SDK except in compliance with the License, which is provided at the time of installation or download, or which otherwise accompanies this software in either electronic or hard copy form. You may obtain a copy of the License at -http://www.oculusvr.com/licenses/LICENSE-3.1 +http://www.oculusvr.com/licenses/LICENSE-3.2 Unless required by applicable law or agreed to in writing, the Oculus VR SDK distributed under the License is distributed on an "AS IS" BASIS, @@ -34,10 +34,58 @@ namespace OVR { // Source for functions not available on all platforms is included here. +size_t OVR_CDECL OVR_strlcpy(char* dest, const char* src, size_t destsize) +{ + const char* s = src; + size_t n = destsize; + + if(n && --n) + { + do{ + if((*dest++ = *s++) == 0) + break; + } while(--n); + } + + if(!n) + { + if(destsize) + *dest = 0; + while(*s++) + { } + } + + return (size_t)((s - src) - 1); +} + + +size_t OVR_CDECL OVR_strlcat(char* dest, const char* src, size_t destsize) +{ + const size_t d = destsize ? OVR_strlen(dest) : 0; + const size_t s = OVR_strlen(src); + const size_t t = s + d; + + OVR_ASSERT((destsize == 0) || (d < destsize)); + + if(t < destsize) + memcpy(dest + d, src, (s + 1) * sizeof(*src)); + else + { + if(destsize) + { + memcpy(dest + d, src, ((destsize - d) - 1) * sizeof(*src)); + dest[destsize - 1] = 0; + } + } + + return t; +} + + // Case insensitive compare implemented in platform-specific way. int OVR_CDECL OVR_stricmp(const char* a, const char* b) { -#if defined(OVR_OS_WIN32) +#if defined(OVR_OS_MS) #if defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400) return ::_stricmp(a, b); #else @@ -49,50 +97,50 @@ int OVR_CDECL OVR_stricmp(const char* a, const char* b) #endif } -int OVR_CDECL OVR_strnicmp(const char* a, const char* b, UPInt count) +int OVR_CDECL OVR_strnicmp(const char* a, const char* b, size_t count) { -#if defined(OVR_OS_WIN32) -#if defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400) - return ::_strnicmp(a, b, count); -#else - return ::strnicmp(a, b, count); -#endif +#if defined(OVR_OS_MS) + #if defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400) + return ::_strnicmp(a, b, count); + #else + return ::strnicmp(a, b, count); + #endif #else return strncasecmp(a, b, count); #endif } -wchar_t* OVR_CDECL OVR_wcscpy(wchar_t* dest, UPInt destsize, const wchar_t* src) +wchar_t* OVR_CDECL OVR_wcscpy(wchar_t* dest, size_t destsize, const wchar_t* src) { #if defined(OVR_MSVC_SAFESTRING) wcscpy_s(dest, destsize, src); return dest; -#elif defined(OVR_OS_WIN32) +#elif defined(OVR_OS_MS) OVR_UNUSED(destsize); wcscpy(dest, src); return dest; #else - UPInt l = OVR_wcslen(src) + 1; // incl term null + size_t l = OVR_wcslen(src) + 1; // incl term null l = (l < destsize) ? l : destsize; memcpy(dest, src, l * sizeof(wchar_t)); return dest; #endif } -wchar_t* OVR_CDECL OVR_wcsncpy(wchar_t* dest, UPInt destsize, const wchar_t* src, UPInt count) +wchar_t* OVR_CDECL OVR_wcsncpy(wchar_t* dest, size_t destsize, const wchar_t* src, size_t count) { #if defined(OVR_MSVC_SAFESTRING) wcsncpy_s(dest, destsize, src, count); return dest; #else - UPInt srclen = OVR_wcslen(src); - UPInt l = Alg::Min(srclen, count); + size_t srclen = OVR_wcslen(src); + size_t l = Alg::Min(srclen, count); l = (l < destsize) ? l : destsize; memcpy(dest, src, l * sizeof(wchar_t)); if (count > srclen) { - UPInt remLen = Alg::Min(destsize - l, (count - srclen)); + size_t remLen = Alg::Min(destsize - l, (count - srclen)); memset(&dest[l], 0, sizeof(wchar_t)*remLen); } else if (l < destsize) @@ -102,30 +150,30 @@ wchar_t* OVR_CDECL OVR_wcsncpy(wchar_t* dest, UPInt destsize, const wchar_t* src } -wchar_t* OVR_CDECL OVR_wcscat(wchar_t* dest, UPInt destsize, const wchar_t* src) +wchar_t* OVR_CDECL OVR_wcscat(wchar_t* dest, size_t destsize, const wchar_t* src) { #if defined(OVR_MSVC_SAFESTRING) wcscat_s(dest, destsize, src); return dest; -#elif defined(OVR_OS_WIN32) +#elif defined(OVR_OS_MS) OVR_UNUSED(destsize); wcscat(dest, src); return dest; #else - UPInt dstlen = OVR_wcslen(dest); // do not incl term null - UPInt srclen = OVR_wcslen(src) + 1; // incl term null - UPInt copylen = (dstlen + srclen < destsize) ? srclen : destsize - dstlen; + size_t dstlen = OVR_wcslen(dest); // do not incl term null + size_t srclen = OVR_wcslen(src) + 1; // incl term null + size_t copylen = (dstlen + srclen < destsize) ? srclen : destsize - dstlen; memcpy(dest + dstlen, src, copylen * sizeof(wchar_t)); return dest; #endif } -UPInt OVR_CDECL OVR_wcslen(const wchar_t* str) +size_t OVR_CDECL OVR_wcslen(const wchar_t* str) { -#if defined(OVR_OS_WIN32) +#if defined(OVR_OS_MS) return wcslen(str); #else - UPInt i = 0; + size_t i = 0; while(str[i] != '\0') ++i; return i; @@ -134,7 +182,7 @@ UPInt OVR_CDECL OVR_wcslen(const wchar_t* str) int OVR_CDECL OVR_wcscmp(const wchar_t* a, const wchar_t* b) { -#if defined(OVR_OS_WIN32) || defined(OVR_OS_LINUX) +#if defined(OVR_OS_MS) || defined(OVR_OS_LINUX) return wcscmp(a, b); #else // not supported, use custom implementation @@ -161,12 +209,12 @@ int OVR_CDECL OVR_wcscmp(const wchar_t* a, const wchar_t* b) int OVR_CDECL OVR_wcsicmp(const wchar_t* a, const wchar_t* b) { -#if defined(OVR_OS_WIN32) -#if defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400) - return ::_wcsicmp(a, b); -#else - return ::wcsicmp(a, b); -#endif +#if defined(OVR_OS_MS) + #if defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400) + return ::_wcsicmp(a, b); + #else + return ::wcsicmp(a, b); + #endif #elif defined(OVR_OS_MAC) || defined(__CYGWIN__) || defined(OVR_OS_ANDROID) || defined(OVR_OS_IPHONE) // not supported, use custom implementation const wchar_t *pa = a, *pb = b; @@ -193,16 +241,19 @@ int OVR_CDECL OVR_wcsicmp(const wchar_t* a, const wchar_t* b) } // This function is not inline because of dependency on <locale.h> -double OVR_CDECL OVR_strtod(const char* string, char** tailptr) +double OVR_CDECL OVR_strtod(const char* str, char** tailptr) { -#if !defined(OVR_OS_ANDROID) +#if !defined(OVR_OS_ANDROID) // The Android C library doesn't have localeconv. const char s = *localeconv()->decimal_point; - if (s != '.') + if (s != '.') // If the C library is using a locale that is not using '.' as a decimal point, we convert the input str's '.' chars to the char that the C library expects (e.g. ',' or ' '). { char buffer[347 + 1]; - OVR_strcpy(buffer, sizeof(buffer), string); + OVR_strcpy(buffer, sizeof(buffer), str); + + // Ensure null-termination of string + buffer[sizeof(buffer)-1] = '\0'; for (char* c = buffer; *c != '\0'; ++c) { @@ -213,11 +264,21 @@ double OVR_CDECL OVR_strtod(const char* string, char** tailptr) } } - return strtod(buffer, tailptr); + char *nextPtr = NULL; + double retval = strtod(buffer, &nextPtr); + + // If a tail pointer is requested, + if (tailptr) + { + // Return a tail pointer that points to the same offset as nextPtr, in the orig string + *tailptr = !nextPtr ? NULL : (char*)str + (int)(nextPtr - buffer); + } + + return retval; } #endif - return strtod(string, tailptr); + return strtod(str, tailptr); } @@ -254,7 +315,7 @@ double OVR_CDECL OVR_strtod(const char* string, char** tailptr) // if (isNull(i)) Offsets[i] = 0; // else // if (isFull(i)) Offsets[i] = 1; -// else Offsets[i] = UInt16(offsetCount++ * 16 + 256); +// else Offsets[i] = uint16_t(offsetCount++ * 16 + 256); // } // for(i = 0; i < 16; ++i) // { @@ -280,7 +341,7 @@ double OVR_CDECL OVR_strtod(const char* string, char** tailptr) //private: // bool isNull(unsigned n) const // { -// const UInt16* p = Bits[n]; +// const uint16_t* p = Bits[n]; // for(unsigned i = 0; i < 16; ++i) // if (p[i] != 0) return false; // return true; @@ -288,18 +349,18 @@ double OVR_CDECL OVR_strtod(const char* string, char** tailptr) // // bool isFull(unsigned n) const // { -// const UInt16* p = Bits[n]; +// const uint16_t* p = Bits[n]; // for(unsigned i = 0; i < 16; ++i) // if (p[i] != 0xFFFF) return false; // return true; // } // -// UInt16 Offsets[256]; -// UInt16 Bits[256][16]; +// uint16_t Offsets[256]; +// uint16_t Bits[256][16]; //}; -const UInt16 UnicodeAlnumBits[] = { +const uint16_t UnicodeAlnumBits[] = { 256, 1, 272, 288, 304, 320, 336, 352, 0, 368, 384, 400, 416, 432, 448, 464, 480, 496, 512, 528, 544, 1, 560, 576, 592, 0, 0, 0, 0, 0, 608, 624, 640, 656, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -354,7 +415,7 @@ const UInt16 UnicodeAlnumBits[] = { 0, 0, 0, 0, 0, 0, 0,65495,65535,65535,65535,65535,65535,65535,65535, 8191, 0, 1023,65534, 2047,65534, 2047,65472,65534,65535,16383,65535,32767,64764, 7420, 0, 0}; -const UInt16 UnicodeAlphaBits[] = { +const uint16_t UnicodeAlphaBits[] = { 256, 1, 272, 288, 304, 320, 336, 352, 0, 368, 384, 400, 416, 432, 448, 464, 480, 496, 512, 528, 544, 1, 560, 576, 592, 0, 0, 0, 0, 0, 608, 624, 640, 656, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -409,7 +470,7 @@ const UInt16 UnicodeAlphaBits[] = { 0, 0, 0, 0, 0, 0, 0,65495,65535,65535,65535,65535,65535,65535,65535, 8191, 0, 0,65534, 2047,65534, 2047,65472,65534,65535,16383,65535,32767,64764, 7420, 0, 0}; -const UInt16 UnicodeDigitBits[] = { +const uint16_t UnicodeDigitBits[] = { 256, 0, 0, 0, 0, 0, 272, 0, 0, 288, 304, 320, 336, 352, 368, 384, 400, 0, 0, 416, 0, 0, 0, 432, 448, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -441,7 +502,7 @@ const UInt16 UnicodeDigitBits[] = { 0, 1023, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1023, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -const UInt16 UnicodeSpaceBits[] = { +const uint16_t UnicodeSpaceBits[] = { 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 272, 0, 0, 0, 0, 0, 0, 0, 0, 0, 288, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -463,7 +524,7 @@ const UInt16 UnicodeSpaceBits[] = { 4095, 0,33536, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -const UInt16 UnicodeXDigitBits[] = { +const uint16_t UnicodeXDigitBits[] = { 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -484,7 +545,7 @@ const UInt16 UnicodeXDigitBits[] = { 0, 1023, 126, 0, 126, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Uncomment if necessary -//const UInt16 UnicodeCntrlBits[] = { +//const uint16_t UnicodeCntrlBits[] = { // 256, 0, 0, 0, 0, 0, 0, 272, 0, 0, 0, 0, 0, 0, 0, 0, // 0, 0, 0, 0, 0, 0, 0, 0, 288, 0, 0, 0, 0, 0, 0, 0, // 304, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -508,7 +569,7 @@ const UInt16 UnicodeXDigitBits[] = { // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,32768, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3584}; // -//const UInt16 UnicodeGraphBits[] = { +//const uint16_t UnicodeGraphBits[] = { // 256, 1, 272, 288, 304, 320, 336, 352, 0, 368, 384, 400, 416, 432, 448, 464, // 480, 496, 512, 528, 544, 1, 560, 576, 592, 0, 0, 0, 0, 0, 608, 624, // 640, 656, 0, 672, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -564,7 +625,7 @@ const UInt16 UnicodeXDigitBits[] = { // 0, 0, 0,65535,65055,65527, 3339,65495,65535,65535,65535,65535,65535,65535,65535, 8191, //63470,36863,65535,49151,65534,12287,65534,65534,65535,16383,65535,32767,64764, 7420, 0, 0}; // -//const UInt16 UnicodePrintBits[] = { +//const uint16_t UnicodePrintBits[] = { // 256, 1, 272, 288, 304, 320, 336, 352, 0, 368, 384, 400, 416, 432, 448, 464, // 480, 496, 512, 528, 544, 1, 560, 576, 592, 0, 0, 0, 0, 0, 608, 624, // 640, 656, 0, 672, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -620,7 +681,7 @@ const UInt16 UnicodeXDigitBits[] = { // 0, 0, 0,65535,65055,65527, 3339,65495,65535,65535,65535,65535,65535,65535,65535,40959, //63470,36863,65535,49151,65534,12287,65534,65534,65535,16383,65535,32767,64764, 7420, 0, 0}; // -//const UInt16 UnicodePunctBits[] = { +//const uint16_t UnicodePunctBits[] = { // 256, 0, 0, 272, 0, 288, 304, 320, 0, 336, 0, 0, 0, 352, 368, 384, // 400, 0, 0, 416, 0, 0, 432, 448, 464, 0, 0, 0, 0, 0, 0, 0, // 480, 0, 0, 496, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -658,7 +719,7 @@ const UInt16 UnicodeXDigitBits[] = { // 0, 0, 0,65535,65055,65527, 3339, 0, 0, 0, 0, 0, 0, 0, 0, 0, //63470,35840, 1,47104, 0,10240, 62, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // -//const UInt16 UnicodeLowerBits[] = { +//const uint16_t UnicodeLowerBits[] = { // 256, 272, 288, 304, 320, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 352, 368, // 384, 400, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -688,7 +749,7 @@ const UInt16 UnicodeXDigitBits[] = { // 127, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0, 0, 0, 0,65534, 2047, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // -//const UInt16 UnicodeUpperBits[] = { +//const uint16_t UnicodeUpperBits[] = { // 256, 272, 288, 304, 320, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 352, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 368, 384, // 0, 400, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -734,7 +795,7 @@ const UInt16 UnicodeXDigitBits[] = { // they match AS3. -static const UInt16 UnicodeToUpperBits[] = { +static const uint16_t UnicodeToUpperBits[] = { 256, 272, 288, 304, 320, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 352, 368, 0, 384, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -763,7 +824,7 @@ static const UInt16 UnicodeToUpperBits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,65535, 1023, 0, 0, 0, 0, 0,65534, 2047, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -static const UInt16 UnicodeToLowerBits[] = { +static const uint16_t UnicodeToLowerBits[] = { 256, 272, 288, 304, 320, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 352, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 368, 384, 0, 400, 0, 0, 416, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -795,10 +856,10 @@ static const UInt16 UnicodeToLowerBits[] = { struct GUnicodePairType { - UInt16 Key, Value; + uint16_t Key, Value; }; -static inline bool CmpUnicodeKey(const GUnicodePairType& a, UInt16 key) +static inline bool CmpUnicodeKey(const GUnicodePairType& a, uint16_t key) { return a.Key < key; } @@ -1002,11 +1063,11 @@ int OVR_CDECL OVR_towupper(wchar_t charCode) { // To protect from memory overrun in case the character is not found // we use one extra fake element in the table {65536, 0}. - UPInt idx = Alg::LowerBoundSliced( + size_t idx = Alg::LowerBoundSliced( UnicodeToUpperTable, 0, sizeof(UnicodeToUpperTable) / sizeof(UnicodeToUpperTable[0]) - 1, - (UInt16)charCode, + (uint16_t)charCode, CmpUnicodeKey); return UnicodeToUpperTable[idx].Value; } @@ -1020,11 +1081,11 @@ int OVR_CDECL OVR_towlower(wchar_t charCode) { // To protect from memory overrun in case the character is not found // we use one extra fake element in the table {65536, 0}. - UPInt idx = Alg::LowerBoundSliced( + size_t idx = Alg::LowerBoundSliced( UnicodeToLowerTable, 0, sizeof(UnicodeToLowerTable) / sizeof(UnicodeToLowerTable[0]) - 1, - (UInt16)charCode, + (uint16_t)charCode, CmpUnicodeKey); return UnicodeToLowerTable[idx].Value; } |