Sha256: a3b14cf39c4eb18b806b883ed7d284c2b747067aed1abf69d1cf12a7be0c1d7e

Contents?: true

Size: 1.73 KB

Versions: 25

Compression:

Stored size: 1.73 KB

Contents

#include <Gosu/Platform.hpp>
#if defined(GOSU_IS_WIN)

#include "WinUtility.hpp"
#include <Gosu/Utility.hpp>
#include <stdexcept>
#include <windows.h>
using namespace std;

wstring Gosu::utf8_to_utf16(const string& utf8)
{
    wstring utf16(utf8.size(), '\0');
    auto len = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), utf8.size(),
                                   const_cast<wchar_t*>(utf16.data()), utf16.size());
    utf16.resize(len);
    return utf16;
}

string Gosu::utf16_to_utf8(const wstring& utf16)
{
    auto len = WideCharToMultiByte(CP_UTF8, 0, utf16.c_str(), utf16.size(),
                                   nullptr, 0, nullptr, nullptr);
    string utf8(len, '\0');
    WideCharToMultiByte(CP_UTF8, 0, utf16.c_str(), utf16.size(),
                        const_cast<char*>(utf8.data()), utf8.size(), nullptr, nullptr);
    return utf8;
}

void Gosu::throw_last_winapi_error(const string& action)
{
    // Obtain error message from Windows.
    wchar_t* buffer;

    if (!FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                        FORMAT_MESSAGE_FROM_SYSTEM |
                        FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, GetLastError(),
                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR) &buffer, 0, nullptr)
            || buffer == nullptr) {
        throw runtime_error("Unknown error");
    }

    // Safely move the message into a string.
    string message;
    try {
        message = utf16_to_utf8(buffer);
    }
    catch (...) {
        LocalFree(buffer);
        throw;
    }
    LocalFree(buffer);
    
    // Optionally prepend the action.
    if (!action.empty()) {
        message = "While " + action + ", the following error occured: " + message;
    }

    throw runtime_error(message);
}

#endif

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
gosu-1.4.6 src/WinUtility.cpp
gosu-1.4.5 src/WinUtility.cpp
gosu-1.4.5.pre1 src/WinUtility.cpp
gosu-1.4.4 src/WinUtility.cpp
gosu-1.4.3 src/WinUtility.cpp
gosu-1.4.1 src/WinUtility.cpp
gosu-1.4.0 src/WinUtility.cpp
gosu-1.3.0 src/WinUtility.cpp
gosu-1.2.0 src/WinUtility.cpp
gosu-1.1.1.1 src/WinUtility.cpp
gosu-1.1.0 src/WinUtility.cpp
gosu-1.1.0.pre2 src/WinUtility.cpp
gosu-1.1.0.pre1 src/WinUtility.cpp
gosu-1.0.0 src/WinUtility.cpp
gosu-1.0.0.pre2 src/WinUtility.cpp
gosu-1.0.0.pre1 src/WinUtility.cpp
gosu-0.15.2 src/WinUtility.cpp
gosu-0.15.1 src/WinUtility.cpp
gosu-0.15.0 src/WinUtility.cpp
gosu-0.14.6.pre1 src/WinUtility.cpp