Combine two function calls into one
[openal-soft.git] / common / strutils.h
blobdb9b07c64b63e803b4eb12caed0533586072498d
1 #ifndef AL_STRUTILS_H
2 #define AL_STRUTILS_H
4 #include <string>
6 #include "aloptional.h"
8 #ifdef _WIN32
9 #define WIN32_LEAN_AND_MEAN
10 #include <windows.h>
13 inline 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 inline 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;
43 #endif
45 namespace al {
47 al::optional<std::string> getenv(const char *envname);
48 #ifdef _WIN32
49 al::optional<std::wstring> getenv(const WCHAR *envname);
50 #endif
52 } // namespace al
54 #endif /* AL_STRUTILS_H */