Make a couple more functions private
[openal-soft.git] / common / strutils.cpp
blob870a0ed3bd0586a0169699263c1c2855b6927cb5
2 #include "config.h"
4 #include "strutils.h"
6 #include <cstdlib>
9 #ifdef _WIN32
10 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h>
13 std::string wstr_to_utf8(const WCHAR *wstr)
15 std::string ret;
17 int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
18 if(len > 0)
20 ret.resize(len);
21 WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &ret[0], len, nullptr, nullptr);
22 ret.pop_back();
25 return ret;
28 std::wstring utf8_to_wstr(const char *str)
30 std::wstring ret;
32 int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
33 if(len > 0)
35 ret.resize(len);
36 MultiByteToWideChar(CP_UTF8, 0, str, -1, &ret[0], len);
37 ret.pop_back();
40 return ret;
42 #endif
44 namespace al {
46 al::optional<std::string> getenv(const char *envname)
48 const char *str{std::getenv(envname)};
49 if(str && str[0] != '\0') return str;
50 return al::nullopt;
53 #ifdef _WIN32
54 al::optional<std::wstring> getenv(const WCHAR *envname)
56 const WCHAR *str{_wgetenv(envname)};
57 if(str && str[0] != L'\0') return str;
58 return al::nullopt;
60 #endif
62 } // namespace al