]> git.tdb.fi Git - ext/openal.git/blob - common/strutils.cpp
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / common / strutils.cpp
1
2 #include "config.h"
3
4 #include "strutils.h"
5
6 #include <cstdlib>
7
8
9 #ifdef _WIN32
10 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h>
12
13 std::string wstr_to_utf8(const WCHAR *wstr)
14 {
15     std::string ret;
16
17     int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
18     if(len > 0)
19     {
20         ret.resize(len);
21         WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &ret[0], len, nullptr, nullptr);
22         ret.pop_back();
23     }
24
25     return ret;
26 }
27
28 std::wstring utf8_to_wstr(const char *str)
29 {
30     std::wstring ret;
31
32     int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
33     if(len > 0)
34     {
35         ret.resize(len);
36         MultiByteToWideChar(CP_UTF8, 0, str, -1, &ret[0], len);
37         ret.pop_back();
38     }
39
40     return ret;
41 }
42 #endif
43
44 namespace al {
45
46 al::optional<std::string> getenv(const char *envname)
47 {
48     const char *str{std::getenv(envname)};
49     if(str && str[0] != '\0')
50         return str;
51     return al::nullopt;
52 }
53
54 #ifdef _WIN32
55 al::optional<std::wstring> getenv(const WCHAR *envname)
56 {
57     const WCHAR *str{_wgetenv(envname)};
58     if(str && str[0] != L'\0')
59         return str;
60     return al::nullopt;
61 }
62 #endif
63
64 } // namespace al