diff --git a/include/bx/os.h b/include/bx/os.h index 7258970..92edacd 100644 --- a/include/bx/os.h +++ b/include/bx/os.h @@ -38,13 +38,13 @@ namespace bx void dlclose(void* _handle); /// - void* dlsym(void* _handle, const char* _symbol); + void* dlsym(void* _handle, const StringView& _symbol); /// - bool getEnv(const char* _name, char* _out, uint32_t* _inOutSize); + bool getEnv(char* _out, uint32_t* _inOutSize, const StringView& _name); /// - void setEnv(const char* _name, const char* _value); + void setEnv(const StringView& _name, const StringView& _value); /// int chdir(const char* _path); diff --git a/src/filepath.cpp b/src/filepath.cpp index f3a5431..888da2f 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -153,12 +153,12 @@ namespace bx return size; } - static bool getEnv(const char* _name, FileInfo::Enum _type, char* _out, uint32_t* _inOutSize) + static bool getEnv(char* _out, uint32_t* _inOutSize, const StringView& _name, FileInfo::Enum _type) { uint32_t len = *_inOutSize; *_out = '\0'; - if (getEnv(_name, _out, &len) ) + if (getEnv(_out, &len, _name) ) { FileInfo fi; if (stat(_out, fi) @@ -203,9 +203,9 @@ namespace bx { return false #if BX_PLATFORM_WINDOWS - || getEnv("USERPROFILE", FileInfo::Directory, _out, _inOutSize) + || getEnv(_out, _inOutSize, "USERPROFILE", FileInfo::Directory) #endif // BX_PLATFORM_WINDOWS - || getEnv("HOME", FileInfo::Directory, _out, _inOutSize) + || getEnv(_out, _inOutSize, "HOME", FileInfo::Directory) ; } @@ -217,21 +217,21 @@ namespace bx *_inOutSize = len; return result; #else - static const char* s_tmp[] = + static const StringView s_tmp[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", - NULL + "" }; - for (const char** tmp = s_tmp; *tmp != NULL; ++tmp) + for (const StringView* tmp = s_tmp; !tmp->isEmpty(); ++tmp) { uint32_t len = *_inOutSize; *_out = '\0'; - bool ok = getEnv(*tmp, FileInfo::Directory, _out, &len); + bool ok = getEnv(_out, &len, *tmp, FileInfo::Directory); if (ok && len != 0 diff --git a/src/os.cpp b/src/os.cpp index 0051d69..0e925bb 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -202,26 +202,34 @@ namespace bx #endif // BX_PLATFORM_ } - void* dlsym(void* _handle, const char* _symbol) + void* dlsym(void* _handle, const StringView& _symbol) { + const int32_t symbolMax = _symbol.getLength()+1; + char* symbol = (char*)alloca(symbolMax); + bx::strCopy(symbol, symbolMax, _symbol); + #if BX_PLATFORM_WINDOWS - return (void*)::GetProcAddress( (HMODULE)_handle, _symbol); + return (void*)::GetProcAddress( (HMODULE)_handle, symbol); #elif BX_PLATFORM_EMSCRIPTEN \ || BX_PLATFORM_PS4 \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT \ || BX_CRT_NONE - BX_UNUSED(_handle, _symbol); + BX_UNUSED(_handle, symbol); return NULL; #else - return ::dlsym(_handle, _symbol); + return ::dlsym(_handle, symbol); #endif // BX_PLATFORM_ } - bool getEnv(const char* _name, char* _out, uint32_t* _inOutSize) + bool getEnv(char* _out, uint32_t* _inOutSize, const StringView& _name) { + const int32_t nameMax = _name.getLength()+1; + char* name = (char*)alloca(nameMax); + bx::strCopy(name, nameMax, _name); + #if BX_PLATFORM_WINDOWS - DWORD len = ::GetEnvironmentVariableA(_name, _out, *_inOutSize); + DWORD len = ::GetEnvironmentVariableA(name, _out, *_inOutSize); bool result = len != 0 && len < *_inOutSize; *_inOutSize = len; return result; @@ -229,10 +237,10 @@ namespace bx || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT \ || BX_CRT_NONE - BX_UNUSED(_name, _out, _inOutSize); + BX_UNUSED(name, _out, _inOutSize); return false; #else - const char* ptr = ::getenv(_name); + const char* ptr = ::getenv(name); uint32_t len = 0; bool result = false; if (NULL != ptr) @@ -251,23 +259,35 @@ namespace bx #endif // BX_PLATFORM_ } - void setEnv(const char* _name, const char* _value) + void setEnv(const StringView& _name, const StringView& _value) { + const int32_t nameMax = _name.getLength()+1; + char* name = (char*)alloca(nameMax); + bx::strCopy(name, nameMax, _name); + + char* value = NULL; + if (!_value.isEmpty() ) + { + int32_t valueMax = _value.getLength()+1; + value = (char*)alloca(valueMax); + bx::strCopy(value, valueMax, _value); + } + #if BX_PLATFORM_WINDOWS - ::SetEnvironmentVariableA(_name, _value); + ::SetEnvironmentVariableA(name, value); #elif BX_PLATFORM_PS4 \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT \ || BX_CRT_NONE - BX_UNUSED(_name, _value); + BX_UNUSED(name, value); #else - if (NULL != _value) + if (NULL != value) { - ::setenv(_name, _value, 1); + ::setenv(name, value, 1); } else { - ::unsetenv(_name); + ::unsetenv(name); } #endif // BX_PLATFORM_ }