aboutsummaryrefslogtreecommitdiffstats
path: root/common/strutils.cpp
blob: f7868e2e147b4b7030d93e01cad49844ac56765b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include "config.h"

#include "strutils.h"

#include <cstdlib>


#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

std::string wstr_to_utf8(std::wstring_view wstr)
{
    std::string ret;

    int len{WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast<int>(wstr.length()), nullptr,
        0, nullptr, nullptr)};
    if(len > 0)
    {
        ret.resize(len);
        WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast<int>(wstr.length()), &ret[0], len,
            nullptr, nullptr);
    }

    return ret;
}

std::wstring utf8_to_wstr(std::string_view str)
{
    std::wstring ret;

    int len{MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.length()), nullptr,
        0)};
    if(len > 0)
    {
        ret.resize(len);
        MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.length()), &ret[0], len);
    }

    return ret;
}
#endif

namespace al {

std::optional<std::string> getenv(const char *envname)
{
#ifdef _GAMING_XBOX
    const char *str{::getenv(envname)};
#else
    const char *str{std::getenv(envname)};
#endif
    if(str && str[0] != '\0')
        return str;
    return std::nullopt;
}

#ifdef _WIN32
std::optional<std::wstring> getenv(const WCHAR *envname)
{
    const WCHAR *str{_wgetenv(envname)};
    if(str && str[0] != L'\0')
        return str;
    return std::nullopt;
}
#endif

} // namespace al