oculus1
diff libovr/Src/Kernel/OVR_Std.h @ 1:e2f9e4603129
added LibOVR and started a simple vr wrapper.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 14 Sep 2013 16:14:59 +0300 |
parents | |
children | b069a5c27388 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libovr/Src/Kernel/OVR_Std.h Sat Sep 14 16:14:59 2013 +0300 1.3 @@ -0,0 +1,1 @@ 1.4 +/************************************************************************************ 1.5 1.6 PublicHeader: OVR.h 1.7 Filename : OVR_Std.h 1.8 Content : Standard C function interface 1.9 Created : September 19, 2012 1.10 Notes : 1.11 1.12 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.13 1.14 Use of this software is subject to the terms of the Oculus license 1.15 agreement provided at the time of installation or download, or which 1.16 otherwise accompanies this software in either electronic or hard copy form. 1.17 1.18 ************************************************************************************/ 1.19 1.20 #ifndef OVR_Std_h 1.21 #define OVR_Std_h 1.22 1.23 #include "OVR_Types.h" 1.24 #include <stdarg.h> // for va_list args 1.25 #include <string.h> 1.26 #include <stdio.h> 1.27 #include <stdlib.h> 1.28 #include <ctype.h> 1.29 1.30 #if !defined(OVR_OS_WINCE) && defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400) 1.31 #define OVR_MSVC_SAFESTRING 1.32 #include <errno.h> 1.33 #endif 1.34 1.35 // Wide-char funcs 1.36 #include <wchar.h> 1.37 #include <wctype.h> 1.38 1.39 namespace OVR { 1.40 1.41 #if defined(OVR_OS_WIN32) 1.42 inline char* OVR_CDECL OVR_itoa(int val, char *dest, UPInt destsize, int radix) 1.43 { 1.44 #if defined(OVR_MSVC_SAFESTRING) 1.45 _itoa_s(val, dest, destsize, radix); 1.46 return dest; 1.47 #else 1.48 OVR_UNUSED(destsize); 1.49 return itoa(val, dest, radix); 1.50 #endif 1.51 } 1.52 #else // OVR_OS_WIN32 1.53 inline char* OVR_itoa(int val, char* dest, unsigned int len, int radix) 1.54 { 1.55 if (val == 0) 1.56 { 1.57 if (len > 1) 1.58 { 1.59 dest[0] = '0'; 1.60 dest[1] = '\0'; 1.61 } 1.62 return dest; 1.63 } 1.64 1.65 int cur = val; 1.66 unsigned int i = 0; 1.67 unsigned int sign = 0; 1.68 1.69 if (val < 0) 1.70 { 1.71 val = -val; 1.72 sign = 1; 1.73 } 1.74 1.75 while ((val != 0) && (i < (len - 1 - sign))) 1.76 { 1.77 cur = val % radix; 1.78 val /= radix; 1.79 1.80 if (radix == 16) 1.81 { 1.82 switch(cur) 1.83 { 1.84 case 10: 1.85 dest[i] = 'a'; 1.86 break; 1.87 case 11: 1.88 dest[i] = 'b'; 1.89 break; 1.90 case 12: 1.91 dest[i] = 'c'; 1.92 break; 1.93 case 13: 1.94 dest[i] = 'd'; 1.95 break; 1.96 case 14: 1.97 dest[i] = 'e'; 1.98 break; 1.99 case 15: 1.100 dest[i] = 'f'; 1.101 break; 1.102 default: 1.103 dest[i] = (char)('0' + cur); 1.104 break; 1.105 } 1.106 } 1.107 else 1.108 { 1.109 dest[i] = (char)('0' + cur); 1.110 } 1.111 ++i; 1.112 } 1.113 1.114 if (sign) 1.115 { 1.116 dest[i++] = '-'; 1.117 } 1.118 1.119 for (unsigned int j = 0; j < i / 2; ++j) 1.120 { 1.121 char tmp = dest[j]; 1.122 dest[j] = dest[i - 1 - j]; 1.123 dest[i - 1 - j] = tmp; 1.124 } 1.125 dest[i] = '\0'; 1.126 1.127 return dest; 1.128 } 1.129 1.130 #endif 1.131 1.132 1.133 // String functions 1.134 1.135 inline UPInt OVR_CDECL OVR_strlen(const char* str) 1.136 { 1.137 return strlen(str); 1.138 } 1.139 1.140 inline char* OVR_CDECL OVR_strcpy(char* dest, UPInt destsize, const char* src) 1.141 { 1.142 #if defined(OVR_MSVC_SAFESTRING) 1.143 strcpy_s(dest, destsize, src); 1.144 return dest; 1.145 #else 1.146 OVR_UNUSED(destsize); 1.147 return strcpy(dest, src); 1.148 #endif 1.149 } 1.150 1.151 inline char* OVR_CDECL OVR_strncpy(char* dest, UPInt destsize, const char* src, UPInt count) 1.152 { 1.153 #if defined(OVR_MSVC_SAFESTRING) 1.154 strncpy_s(dest, destsize, src, count); 1.155 return dest; 1.156 #else 1.157 OVR_UNUSED(destsize); 1.158 return strncpy(dest, src, count); 1.159 #endif 1.160 } 1.161 1.162 inline char * OVR_CDECL OVR_strcat(char* dest, UPInt destsize, const char* src) 1.163 { 1.164 #if defined(OVR_MSVC_SAFESTRING) 1.165 strcat_s(dest, destsize, src); 1.166 return dest; 1.167 #else 1.168 OVR_UNUSED(destsize); 1.169 return strcat(dest, src); 1.170 #endif 1.171 } 1.172 1.173 inline int OVR_CDECL OVR_strcmp(const char* dest, const char* src) 1.174 { 1.175 return strcmp(dest, src); 1.176 } 1.177 1.178 inline const char* OVR_CDECL OVR_strchr(const char* str, char c) 1.179 { 1.180 return strchr(str, c); 1.181 } 1.182 1.183 inline char* OVR_CDECL OVR_strchr(char* str, char c) 1.184 { 1.185 return strchr(str, c); 1.186 } 1.187 1.188 inline const char* OVR_strrchr(const char* str, char c) 1.189 { 1.190 UPInt len = OVR_strlen(str); 1.191 for (UPInt i=len; i>0; i--) 1.192 if (str[i]==c) 1.193 return str+i; 1.194 return 0; 1.195 } 1.196 1.197 inline const UByte* OVR_CDECL OVR_memrchr(const UByte* str, UPInt size, UByte c) 1.198 { 1.199 for (SPInt i = (SPInt)size - 1; i >= 0; i--) 1.200 { 1.201 if (str[i] == c) 1.202 return str + i; 1.203 } 1.204 return 0; 1.205 } 1.206 1.207 inline char* OVR_CDECL OVR_strrchr(char* str, char c) 1.208 { 1.209 UPInt len = OVR_strlen(str); 1.210 for (UPInt i=len; i>0; i--) 1.211 if (str[i]==c) 1.212 return str+i; 1.213 return 0; 1.214 } 1.215 1.216 1.217 double OVR_CDECL OVR_strtod(const char* string, char** tailptr); 1.218 1.219 inline long OVR_CDECL OVR_strtol(const char* string, char** tailptr, int radix) 1.220 { 1.221 return strtol(string, tailptr, radix); 1.222 } 1.223 1.224 inline long OVR_CDECL OVR_strtoul(const char* string, char** tailptr, int radix) 1.225 { 1.226 return strtoul(string, tailptr, radix); 1.227 } 1.228 1.229 inline int OVR_CDECL OVR_strncmp(const char* ws1, const char* ws2, UPInt size) 1.230 { 1.231 return strncmp(ws1, ws2, size); 1.232 } 1.233 1.234 inline UInt64 OVR_CDECL OVR_strtouq(const char *nptr, char **endptr, int base) 1.235 { 1.236 #if defined(OVR_CC_MSVC) && !defined(OVR_OS_WINCE) 1.237 return _strtoui64(nptr, endptr, base); 1.238 #else 1.239 return strtoull(nptr, endptr, base); 1.240 #endif 1.241 } 1.242 1.243 inline SInt64 OVR_CDECL OVR_strtoq(const char *nptr, char **endptr, int base) 1.244 { 1.245 #if defined(OVR_CC_MSVC) && !defined(OVR_OS_WINCE) 1.246 return _strtoi64(nptr, endptr, base); 1.247 #else 1.248 return strtoll(nptr, endptr, base); 1.249 #endif 1.250 } 1.251 1.252 1.253 inline SInt64 OVR_CDECL OVR_atoq(const char* string) 1.254 { 1.255 #if defined(OVR_CC_MSVC) && !defined(OVR_OS_WINCE) 1.256 return _atoi64(string); 1.257 #else 1.258 return atoll(string); 1.259 #endif 1.260 } 1.261 1.262 inline UInt64 OVR_CDECL OVR_atouq(const char* string) 1.263 { 1.264 return OVR_strtouq(string, NULL, 10); 1.265 } 1.266 1.267 1.268 // Implemented in GStd.cpp in platform-specific manner. 1.269 int OVR_CDECL OVR_stricmp(const char* dest, const char* src); 1.270 int OVR_CDECL OVR_strnicmp(const char* dest, const char* src, UPInt count); 1.271 1.272 inline UPInt OVR_CDECL OVR_sprintf(char *dest, UPInt destsize, const char* format, ...) 1.273 { 1.274 va_list argList; 1.275 va_start(argList,format); 1.276 UPInt ret; 1.277 #if defined(OVR_CC_MSVC) 1.278 #if defined(OVR_MSVC_SAFESTRING) 1.279 ret = _vsnprintf_s(dest, destsize, _TRUNCATE, format, argList); 1.280 OVR_ASSERT(ret != -1); 1.281 #else 1.282 OVR_UNUSED(destsize); 1.283 ret = _vsnprintf(dest, destsize - 1, format, argList); // -1 for space for the null character 1.284 OVR_ASSERT(ret != -1); 1.285 dest[destsize-1] = 0; 1.286 #endif 1.287 #else 1.288 OVR_UNUSED(destsize); 1.289 ret = vsprintf(dest, format, argList); 1.290 OVR_ASSERT(ret < destsize); 1.291 #endif 1.292 va_end(argList); 1.293 return ret; 1.294 } 1.295 1.296 inline UPInt OVR_CDECL OVR_vsprintf(char *dest, UPInt destsize, const char * format, va_list argList) 1.297 { 1.298 UPInt ret; 1.299 #if defined(OVR_CC_MSVC) 1.300 #if defined(OVR_MSVC_SAFESTRING) 1.301 dest[0] = '\0'; 1.302 int rv = vsnprintf_s(dest, destsize, _TRUNCATE, format, argList); 1.303 if (rv == -1) 1.304 { 1.305 dest[destsize - 1] = '\0'; 1.306 ret = destsize - 1; 1.307 } 1.308 else 1.309 ret = (UPInt)rv; 1.310 #else 1.311 OVR_UNUSED(destsize); 1.312 int rv = _vsnprintf(dest, destsize - 1, format, argList); 1.313 OVR_ASSERT(rv != -1); 1.314 ret = (UPInt)rv; 1.315 dest[destsize-1] = 0; 1.316 #endif 1.317 #else 1.318 OVR_UNUSED(destsize); 1.319 ret = (UPInt)vsprintf(dest, format, argList); 1.320 OVR_ASSERT(ret < destsize); 1.321 #endif 1.322 return ret; 1.323 } 1.324 1.325 // Returns the number of characters in the formatted string. 1.326 inline UPInt OVR_CDECL OVR_vscprintf(const char * format, va_list argList) 1.327 { 1.328 UPInt ret; 1.329 #if defined(OVR_CC_MSVC) 1.330 ret = (UPInt) _vscprintf(format, argList); 1.331 #else 1.332 ret = (UPInt) vsnprintf(NULL, 0, format, argList); 1.333 #endif 1.334 return ret; 1.335 } 1.336 1.337 1.338 wchar_t* OVR_CDECL OVR_wcscpy(wchar_t* dest, UPInt destsize, const wchar_t* src); 1.339 wchar_t* OVR_CDECL OVR_wcsncpy(wchar_t* dest, UPInt destsize, const wchar_t* src, UPInt count); 1.340 wchar_t* OVR_CDECL OVR_wcscat(wchar_t* dest, UPInt destsize, const wchar_t* src); 1.341 UPInt OVR_CDECL OVR_wcslen(const wchar_t* str); 1.342 int OVR_CDECL OVR_wcscmp(const wchar_t* a, const wchar_t* b); 1.343 int OVR_CDECL OVR_wcsicmp(const wchar_t* a, const wchar_t* b); 1.344 1.345 inline int OVR_CDECL OVR_wcsicoll(const wchar_t* a, const wchar_t* b) 1.346 { 1.347 #if defined(OVR_OS_WIN32) 1.348 #if defined(OVR_CC_MSVC) && (OVR_CC_MSVC >= 1400) 1.349 return ::_wcsicoll(a, b); 1.350 #else 1.351 return ::wcsicoll(a, b); 1.352 #endif 1.353 #else 1.354 // not supported, use regular wcsicmp 1.355 return OVR_wcsicmp(a, b); 1.356 #endif 1.357 } 1.358 1.359 inline int OVR_CDECL OVR_wcscoll(const wchar_t* a, const wchar_t* b) 1.360 { 1.361 #if defined(OVR_OS_WIN32) || defined(OVR_OS_LINUX) 1.362 return wcscoll(a, b); 1.363 #else 1.364 // not supported, use regular wcscmp 1.365 return OVR_wcscmp(a, b); 1.366 #endif 1.367 } 1.368 1.369 #ifndef OVR_NO_WCTYPE 1.370 1.371 inline int OVR_CDECL UnicodeCharIs(const UInt16* table, wchar_t charCode) 1.372 { 1.373 unsigned offset = table[charCode >> 8]; 1.374 if (offset == 0) return 0; 1.375 if (offset == 1) return 1; 1.376 return (table[offset + ((charCode >> 4) & 15)] & (1 << (charCode & 15))) != 0; 1.377 } 1.378 1.379 extern const UInt16 UnicodeAlnumBits[]; 1.380 extern const UInt16 UnicodeAlphaBits[]; 1.381 extern const UInt16 UnicodeDigitBits[]; 1.382 extern const UInt16 UnicodeSpaceBits[]; 1.383 extern const UInt16 UnicodeXDigitBits[]; 1.384 1.385 // Uncomment if necessary 1.386 //extern const UInt16 UnicodeCntrlBits[]; 1.387 //extern const UInt16 UnicodeGraphBits[]; 1.388 //extern const UInt16 UnicodeLowerBits[]; 1.389 //extern const UInt16 UnicodePrintBits[]; 1.390 //extern const UInt16 UnicodePunctBits[]; 1.391 //extern const UInt16 UnicodeUpperBits[]; 1.392 1.393 inline int OVR_CDECL OVR_iswalnum (wchar_t charCode) { return UnicodeCharIs(UnicodeAlnumBits, charCode); } 1.394 inline int OVR_CDECL OVR_iswalpha (wchar_t charCode) { return UnicodeCharIs(UnicodeAlphaBits, charCode); } 1.395 inline int OVR_CDECL OVR_iswdigit (wchar_t charCode) { return UnicodeCharIs(UnicodeDigitBits, charCode); } 1.396 inline int OVR_CDECL OVR_iswspace (wchar_t charCode) { return UnicodeCharIs(UnicodeSpaceBits, charCode); } 1.397 inline int OVR_CDECL OVR_iswxdigit(wchar_t charCode) { return UnicodeCharIs(UnicodeXDigitBits, charCode); } 1.398 1.399 // Uncomment if necessary 1.400 //inline int OVR_CDECL OVR_iswcntrl (wchar_t charCode) { return UnicodeCharIs(UnicodeCntrlBits, charCode); } 1.401 //inline int OVR_CDECL OVR_iswgraph (wchar_t charCode) { return UnicodeCharIs(UnicodeGraphBits, charCode); } 1.402 //inline int OVR_CDECL OVR_iswlower (wchar_t charCode) { return UnicodeCharIs(UnicodeLowerBits, charCode); } 1.403 //inline int OVR_CDECL OVR_iswprint (wchar_t charCode) { return UnicodeCharIs(UnicodePrintBits, charCode); } 1.404 //inline int OVR_CDECL OVR_iswpunct (wchar_t charCode) { return UnicodeCharIs(UnicodePunctBits, charCode); } 1.405 //inline int OVR_CDECL OVR_iswupper (wchar_t charCode) { return UnicodeCharIs(UnicodeUpperBits, charCode); } 1.406 1.407 int OVR_CDECL OVR_towupper(wchar_t charCode); 1.408 int OVR_CDECL OVR_towlower(wchar_t charCode); 1.409 1.410 #else // OVR_NO_WCTYPE 1.411 1.412 inline int OVR_CDECL OVR_iswspace(wchar_t c) 1.413 { 1.414 return iswspace(c); 1.415 } 1.416 1.417 inline int OVR_CDECL OVR_iswdigit(wchar_t c) 1.418 { 1.419 return iswdigit(c); 1.420 } 1.421 1.422 inline int OVR_CDECL OVR_iswxdigit(wchar_t c) 1.423 { 1.424 return iswxdigit(c); 1.425 } 1.426 1.427 inline int OVR_CDECL OVR_iswalpha(wchar_t c) 1.428 { 1.429 return iswalpha(c); 1.430 } 1.431 1.432 inline int OVR_CDECL OVR_iswalnum(wchar_t c) 1.433 { 1.434 return iswalnum(c); 1.435 } 1.436 1.437 inline wchar_t OVR_CDECL OVR_towlower(wchar_t c) 1.438 { 1.439 return (wchar_t)towlower(c); 1.440 } 1.441 1.442 inline wchar_t OVR_towupper(wchar_t c) 1.443 { 1.444 return (wchar_t)towupper(c); 1.445 } 1.446 1.447 #endif // OVR_NO_WCTYPE 1.448 1.449 // ASCII versions of tolower and toupper. Don't use "char" 1.450 inline int OVR_CDECL OVR_tolower(int c) 1.451 { 1.452 return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; 1.453 } 1.454 1.455 inline int OVR_CDECL OVR_toupper(int c) 1.456 { 1.457 return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; 1.458 } 1.459 1.460 1.461 1.462 inline double OVR_CDECL OVR_wcstod(const wchar_t* string, wchar_t** tailptr) 1.463 { 1.464 #if defined(OVR_OS_OTHER) 1.465 OVR_UNUSED(tailptr); 1.466 char buffer[64]; 1.467 char* tp = NULL; 1.468 UPInt max = OVR_wcslen(string); 1.469 if (max > 63) max = 63; 1.470 unsigned char c = 0; 1.471 for (UPInt i=0; i < max; i++) 1.472 { 1.473 c = (unsigned char)string[i]; 1.474 buffer[i] = ((c) < 128 ? (char)c : '!'); 1.475 } 1.476 buffer[max] = 0; 1.477 return OVR_strtod(buffer, &tp); 1.478 #else 1.479 return wcstod(string, tailptr); 1.480 #endif 1.481 } 1.482 1.483 inline long OVR_CDECL OVR_wcstol(const wchar_t* string, wchar_t** tailptr, int radix) 1.484 { 1.485 #if defined(OVR_OS_OTHER) 1.486 OVR_UNUSED(tailptr); 1.487 char buffer[64]; 1.488 char* tp = NULL; 1.489 UPInt max = OVR_wcslen(string); 1.490 if (max > 63) max = 63; 1.491 unsigned char c = 0; 1.492 for (UPInt i=0; i < max; i++) 1.493 { 1.494 c = (unsigned char)string[i]; 1.495 buffer[i] = ((c) < 128 ? (char)c : '!'); 1.496 } 1.497 buffer[max] = 0; 1.498 return strtol(buffer, &tp, radix); 1.499 #else 1.500 return wcstol(string, tailptr, radix); 1.501 #endif 1.502 } 1.503 1.504 } // OVR 1.505 1.506 #endif // OVR_Std_h 1.507 \ No newline at end of file