diff options
author | Chris Robinson <[email protected]> | 2019-08-11 18:50:07 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2019-08-11 18:50:07 -0700 |
commit | e200569cd319448d12099f27a2713c69da382d26 (patch) | |
tree | 5475e5aa20505bd403b46053321dd2d6a019ac5d /common/strutils.h | |
parent | 7118733458bc388a900677da6e0d4e4244d0f536 (diff) |
Move the wstr converters to a separate header
Diffstat (limited to 'common/strutils.h')
-rw-r--r-- | common/strutils.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/common/strutils.h b/common/strutils.h new file mode 100644 index 00000000..2bfd15fc --- /dev/null +++ b/common/strutils.h @@ -0,0 +1,43 @@ +#ifndef AL_STRUTILS_H +#define AL_STRUTILS_H + +#ifdef _WIN32 + +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +#include <string> + +inline std::string wstr_to_utf8(const WCHAR *wstr) +{ + std::string ret; + + int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr); + if(len > 0) + { + ret.resize(len); + WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &ret[0], len, nullptr, nullptr); + ret.pop_back(); + } + + return ret; +} + +inline std::wstring utf8_to_wstr(const char *str) +{ + std::wstring ret; + + int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0); + if(len > 0) + { + ret.resize(len); + MultiByteToWideChar(CP_UTF8, 0, str, -1, &ret[0], len); + ret.pop_back(); + } + + return ret; +} + +#endif + +#endif /* AL_STRUTILS_H */ |