Enable proper full C++ exception handling on MSVC
[openal-soft.git] / common / strutils.cpp
blob4bcd41194121effb262f12eb6579042342d63c9b
2 #include "config.h"
4 #include "strutils.h"
6 #include <cstdlib>
8 #ifdef _WIN32
9 #define WIN32_LEAN_AND_MEAN
10 #include <windows.h>
12 #include "alstring.h"
14 std::string wstr_to_utf8(std::wstring_view wstr)
16 std::string ret;
18 const int len{WideCharToMultiByte(CP_UTF8, 0, wstr.data(), al::sizei(wstr), nullptr, 0,
19 nullptr, nullptr)};
20 if(len > 0)
22 ret.resize(static_cast<size_t>(len));
23 WideCharToMultiByte(CP_UTF8, 0, wstr.data(), al::sizei(wstr), ret.data(), len,
24 nullptr, nullptr);
27 return ret;
30 std::wstring utf8_to_wstr(std::string_view str)
32 std::wstring ret;
34 const int len{MultiByteToWideChar(CP_UTF8, 0, str.data(), al::sizei(str), nullptr, 0)};
35 if(len > 0)
37 ret.resize(static_cast<size_t>(len));
38 MultiByteToWideChar(CP_UTF8, 0, str.data(), al::sizei(str), ret.data(), len);
41 return ret;
43 #endif
45 namespace al {
47 std::optional<std::string> getenv(const char *envname)
49 #ifdef _GAMING_XBOX
50 const char *str{::getenv(envname)};
51 #else
52 const char *str{std::getenv(envname)};
53 #endif
54 if(str && *str != '\0')
55 return str;
56 return std::nullopt;
59 #ifdef _WIN32
60 std::optional<std::wstring> getenv(const WCHAR *envname)
62 const WCHAR *str{_wgetenv(envname)};
63 if(str && *str != L'\0')
64 return str;
65 return std::nullopt;
67 #endif
69 } // namespace al