From 547cbe578dcdb5aa5cfdac1cdb327f92bad21219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 26 Jul 2022 19:27:35 +0200 Subject: Remove ANSI compatibility. Even though it is a fun challange in many ways, I think that, realistically, it is probably not worth the complexity. The Prolog backend isn't ANSI-compatible either. --- c/common.cpp | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'c/common.cpp') diff --git a/c/common.cpp b/c/common.cpp index c4624c0..ca8dfac 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -15,11 +15,37 @@ Win32Error::~Win32Error() HeapFree(GetProcessHeap(), 0, m_wszMsg); } +const char* Win32Error::what() const noexcept +{ + if (!m_szMsg) + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dwErr, + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), + (char*)&m_szMsg, + 0, NULL); + return m_szMsg; +} + +const wchar_t* Win32Error::WhatW() const noexcept +{ + if (!m_wszMsg) + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dwErr, + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), + (wchar_t*)&m_wszMsg, + 0, NULL); + return m_wszMsg; +} + /* Library: Wrapper for loading and freeing dynamically linked libraries. */ -Library::Library(const TCHAR* const tszLibrary) +Library::Library(const wchar_t* const wszLibrary) { - m_hModule = LoadLibrary(tszLibrary); + m_hModule = LoadLibrary(wszLibrary); if (!m_hModule) throw Win32Error(); } @@ -29,15 +55,8 @@ Library::~Library() FreeLibrary(m_hModule); } -/* Convert narrow unmanaged string to narrow/wide basic string. */ -template <> -std::basic_string BstrFromSz(const char* sz, int) -{ - return std::string(sz); -} - -template <> -std::basic_string BstrFromSz(const char* sz, int iCp) +/* Convert narrow unmanaged string to wide managed string. */ +std::wstring WstrFromSz(const char* sz, int iCp) { int cbMultiByte = strlen(sz)+1; int cchWideChar = MultiByteToWideChar(iCp, 0, sz, cbMultiByte, NULL, 0); @@ -71,11 +90,11 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR } /* Show message box owned by and centered in the main window. */ -int EBMessageBox(const TCHAR* const tszText, const TCHAR* const tszCaption, const unsigned uType) +int EBMessageBox(const wchar_t* const wszText, const wchar_t* const wszCaption, const unsigned uType) { extern HWND g_hWnd; HHOOK hHook = require(SetWindowsHookEx(WH_CBT, CBTProc, (HINSTANCE)NULL, GetCurrentThreadId())); - int r = MessageBox(g_hWnd, tszText, tszCaption, uType); + int r = MessageBox(g_hWnd, wszText, wszCaption, uType); require(UnhookWindowsHookEx(hHook)); return r; } -- cgit v1.2.3