2019-08-12 03:59:52 -07:00
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include "strutils.h"
|
|
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
2019-10-01 23:33:00 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
2024-01-27 22:08:58 -08:00
|
|
|
#include "alstring.h"
|
|
|
|
|
|
2023-08-06 18:49:42 -07:00
|
|
|
std::string wstr_to_utf8(std::wstring_view wstr)
|
2019-10-01 23:33:00 -07:00
|
|
|
{
|
|
|
|
|
std::string ret;
|
|
|
|
|
|
2024-01-27 22:08:58 -08:00
|
|
|
const int len{WideCharToMultiByte(CP_UTF8, 0, wstr.data(), al::sizei(wstr), nullptr, 0,
|
|
|
|
|
nullptr, nullptr)};
|
2019-10-01 23:33:00 -07:00
|
|
|
if(len > 0)
|
|
|
|
|
{
|
2024-01-27 22:08:58 -08:00
|
|
|
ret.resize(static_cast<size_t>(len));
|
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), al::sizei(wstr), ret.data(), len,
|
2023-08-06 18:49:42 -07:00
|
|
|
nullptr, nullptr);
|
2019-10-01 23:33:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-06 18:49:42 -07:00
|
|
|
std::wstring utf8_to_wstr(std::string_view str)
|
2019-10-01 23:33:00 -07:00
|
|
|
{
|
|
|
|
|
std::wstring ret;
|
|
|
|
|
|
2024-01-27 22:08:58 -08:00
|
|
|
const int len{MultiByteToWideChar(CP_UTF8, 0, str.data(), al::sizei(str), nullptr, 0)};
|
2019-10-01 23:33:00 -07:00
|
|
|
if(len > 0)
|
|
|
|
|
{
|
2024-01-27 22:08:58 -08:00
|
|
|
ret.resize(static_cast<size_t>(len));
|
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, str.data(), al::sizei(str), ret.data(), len);
|
2019-10-01 23:33:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-08-12 03:59:52 -07:00
|
|
|
namespace al {
|
|
|
|
|
|
2023-05-04 08:03:40 -07:00
|
|
|
std::optional<std::string> getenv(const char *envname)
|
2019-08-12 03:59:52 -07:00
|
|
|
{
|
2023-07-26 21:57:53 +02:00
|
|
|
#ifdef _GAMING_XBOX
|
|
|
|
|
const char *str{::getenv(envname)};
|
|
|
|
|
#else
|
2019-08-12 03:59:52 -07:00
|
|
|
const char *str{std::getenv(envname)};
|
2023-07-26 21:57:53 +02:00
|
|
|
#endif
|
2024-03-12 08:12:37 -07:00
|
|
|
if(str && *str != '\0')
|
2023-01-13 01:25:20 -08:00
|
|
|
return str;
|
2023-05-04 08:03:40 -07:00
|
|
|
return std::nullopt;
|
2019-08-12 03:59:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2023-05-04 08:03:40 -07:00
|
|
|
std::optional<std::wstring> getenv(const WCHAR *envname)
|
2019-08-12 03:59:52 -07:00
|
|
|
{
|
|
|
|
|
const WCHAR *str{_wgetenv(envname)};
|
2024-03-12 08:12:37 -07:00
|
|
|
if(str && *str != L'\0')
|
2023-01-13 01:25:20 -08:00
|
|
|
return str;
|
2023-05-04 08:03:40 -07:00
|
|
|
return std::nullopt;
|
2019-08-12 03:59:52 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
} // namespace al
|