aboutsummaryrefslogtreecommitdiff
path: root/utils.h
blob: effe318b21e750c20c2b387b84cb33ce056a3096 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once

// ------------------------------------------------------------------------------------------
// Structure and operators to insert a zero-filled hex-formatted number into a std::ostream.
struct HEX
{
    HEX(unsigned long num, unsigned long fieldwidth = 8, bool bUpcase = false)
        : m_num(num), m_width(fieldwidth), m_upcase(bUpcase)
        {}

    unsigned long m_num;
    unsigned long m_width;
    bool m_upcase;
};

inline std::ostream& operator << ( std::ostream& os, const HEX & h )
{
    int fmt = os.flags();
    char fillchar = os.fill('0');
    os << "0x" << std::hex << (h.m_upcase ? std::uppercase : std::nouppercase) << std::setw(h.m_width) << h.m_num ;
    os.fill(fillchar);
    os.flags(fmt);
    return os;
}

inline std::wostream& operator << ( std::wostream& os, const HEX & h )
{
    int fmt = os.flags();
    wchar_t fillchar = os.fill(L'0');
    os << L"0x" << std::hex << (h.m_upcase ? std::uppercase : std::nouppercase) << std::setw(h.m_width) << h.m_num ;
    os.fill(fillchar);
    os.flags(fmt);
    return os;
}


// ------------------------------------------------------------------------------------------

// Convert an error code to corresponding text, returning it as a std::wstring.

inline std::wstring SysErrorMessageWithCode(DWORD dwErrCode /*= GetLastError()*/)
{
    LPWSTR pszErrMsg = NULL;
    std::wstringstream sRetval;
    DWORD flags =
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_IGNORE_INSERTS |
        FORMAT_MESSAGE_FROM_SYSTEM ;

    if ( FormatMessageW(
        flags,
        NULL,
        dwErrCode,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPWSTR) &pszErrMsg,
        0,
        NULL ) )
    {
        sRetval << pszErrMsg << L" (Error # " << dwErrCode << L" = " << HEX(dwErrCode) << L")";
        LocalFree(pszErrMsg);
    }
    else
    {
        sRetval << L"Error # " << dwErrCode << L" (" << HEX(dwErrCode) << L")";
    }
    return sRetval.str();
}

// Convert std::string to std::wstring
std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

// Convert std::wstring to std::string
std::string ws2s(const std::wstring& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), slength, 0, 0, 0, 0);
    char* buf = new char[len];
    WideCharToMultiByte(CP_UTF8, 0, s.c_str(), slength, buf, len, 0, 0);
    std::string r(buf);
    delete[] buf;
    return r;
}