Wrote up a utf16 with conversion and allocating-sprintf() functions. Changed the way deubgging will work.

This commit is contained in:
Pietro Gagliardi 2016-04-21 18:03:52 -04:00
parent 35b2b490bb
commit eb48bc1732
5 changed files with 150 additions and 44 deletions

View File

@ -13,8 +13,7 @@ This README is being written.<br>
* All platforms:
* GNU make 3.81 or newer (Xcode comes with this; on Windows you will need to get it yourself)
* Windows: Microsoft Visual Studio 2013 or newer
* This may be reduced to 2010 if necessary.
* Windows: Microsoft Visual Studio 2013 or newer (2013 is needed for `va_copy()`)
* MinGW is currently unsupported. MinGW-w64 support will be re-added once the following features come in:
* [Isolation awareness](https://msdn.microsoft.com/en-us/library/aa375197%28v=vs.85%29.aspx)
* Linker symbols for some functions such as `TaskDialog()` (which I thought I submitted...)

View File

@ -22,7 +22,7 @@ static std::map<HWND, struct handler> handlers;
void uiWindowsRegisterWM_COMMANDHandler(HWND hwnd, BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *), uiControl *c)
{
if (handlers[hwnd].commandHandler != NULL)
implbug("already registered a WM_COMMAND handler to window handle %p in uiWindowsRegisterWM_COMMANDHandler()", hwnd);
implbug(L"already registered a WM_COMMAND handler to window handle %p", hwnd);
handlers[hwnd].commandHandler = handler;
handlers[hwnd].c = c;
}
@ -30,7 +30,7 @@ void uiWindowsRegisterWM_COMMANDHandler(HWND hwnd, BOOL (*handler)(uiControl *,
void uiWindowsRegisterWM_NOTIFYHandler(HWND hwnd, BOOL (*handler)(uiControl *, HWND, NMHDR *, LRESULT *), uiControl *c)
{
if (handlers[hwnd].notifyHandler != NULL)
implbug("already registered a WM_NOTIFY handler to window handle %p in uiWindowsRegisterWM_NOTIFYHandler()", hwnd);
implbug(L"already registered a WM_NOTIFY handler to window handle %p", hwnd);
handlers[hwnd].notifyHandler = handler;
handlers[hwnd].c = c;
}
@ -38,7 +38,7 @@ void uiWindowsRegisterWM_NOTIFYHandler(HWND hwnd, BOOL (*handler)(uiControl *, H
void uiWindowsRegisterWM_HSCROLLHandler(HWND hwnd, BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *), uiControl *c)
{
if (handlers[hwnd].hscrollHandler != NULL)
implbug("already registered a WM_HSCROLL handler to window handle %p in uiWindowsRegisterWM_HSCROLLHandler()", hwnd);
implbug(L"already registered a WM_HSCROLL handler to window handle %p", hwnd);
handlers[hwnd].hscrollHandler = handler;
handlers[hwnd].c = c;
}
@ -46,21 +46,21 @@ void uiWindowsRegisterWM_HSCROLLHandler(HWND hwnd, BOOL (*handler)(uiControl *,
void uiWindowsUnregisterWM_COMMANDHandler(HWND hwnd)
{
if (handlers[hwnd].commandHandler == NULL)
implbug("window handle %p not registered to receive WM_COMMAND events in uiWindowsUnregisterWM_COMMANDHandler()", hwnd);
implbug(L"window handle %p not registered to receive WM_COMMAND events", hwnd);
handlers[hwnd].commandHandler = NULL;
}
void uiWindowsUnregisterWM_NOTIFYHandler(HWND hwnd)
{
if (handlers[hwnd].notifyHandler == NULL)
implbug("window handle %p not registered to receive WM_NOTIFY events in uiWindowsUnregisterWM_NOTIFYHandler()", hwnd);
implbug(L"window handle %p not registered to receive WM_NOTIFY events", hwnd);
handlers[hwnd].notifyHandler = NULL;
}
void uiWindowsUnregisterWM_HSCROLLHandler(HWND hwnd)
{
if (handlers[hwnd].hscrollHandler == NULL)
implbug("window handle %p not registered to receive WM_HSCROLL events in uiWindowsUnregisterWM_HSCROLLHandler()", hwnd);
implbug(L"window handle %p not registered to receive WM_HSCROLL events", hwnd);
handlers[hwnd].hscrollHandler = NULL;
}
@ -130,15 +130,14 @@ static std::map<HWND, bool> wininichanges;
void uiWindowsRegisterReceiveWM_WININICHANGE(HWND hwnd)
{
if (wininichanges[hwnd])
implbug("window handle %p already subscribed to receive WM_WINICHANGEs in uiWindowsRegisterReceiveWM_WININICHANGE()", hwnd);
implbug(L"window handle %p already subscribed to receive WM_WINICHANGEs", hwnd);
wininichanges[hwnd] = true;
}
void uiWindowsUnregisterReceiveWM_WININICHANGE(HWND hwnd)
{
if (!wininichanges[hwnd])
implbug("window handle %p not registered to receive WM_WININICHANGEs in uiWindowsUnregisterReceiveWM_WININICHANGE
()", hwnd);
implbug(L"window handle %p not registered to receive WM_WININICHANGEs", hwnd);
wininichanges[hwnd] = false;
}

View File

@ -1,39 +1,6 @@
// 9 april 2015
#include "uipriv_windows.h"
// see http://stackoverflow.com/a/29556509/3408572
#define MBTWC(str, wstr, bufsiz) MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, bufsiz)
WCHAR *toUTF16(const char *str)
{
WCHAR *wstr;
int n;
n = MBTWC(str, NULL, 0);
if (n == 0)
logLastError("error figuring out number of characters to convert to in toUTF16()");
wstr = (WCHAR *) uiAlloc(n * sizeof (WCHAR), "WCHAR[]");
if (MBTWC(str, wstr, n) != n)
logLastError("error converting from UTF-8 to UTF-16 in toUTF16()");
return wstr;
}
#define WCTMB(wstr, str, bufsiz) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, bufsiz, NULL, NULL)
char *toUTF8(const WCHAR *wstr)
{
char *str;
int n;
n = WCTMB(wstr, NULL, 0);
if (n == 0)
logLastError("error figuring out number of characters to convert to in toUTF8()");
str = (char *) uiAlloc(n * sizeof (char), "char[]");
if (WCTMB(wstr, str, n) != n)
logLastError("error converting from UTF-16 to UTF-8 in toUTFF8()");
return str;
}
WCHAR *windowText(HWND hwnd)
{

View File

@ -5,3 +5,20 @@ extern BOOL runWM_COMMAND(WPARAM wParam, LPARAM lParam, LRESULT *lResult);
extern BOOL runWM_NOTIFY(WPARAM wParam, LPARAM lParam, LRESULT *lResult);
extern BOOL runWM_HSCROLL(WPARAM wParam, LPARAM lParam, LRESULT *lResult);
extern void issueWM_WININICHANGE(WPARAM wParam, LPARAM lParam);
// utf16.cpp
extern WCHAR *toUTF16(const char *str);
extern char *toUTF8(const WCHAR *wstr);
extern WCHAR *strf(const WCHAR *format, ...);
extern WCHAR *vstrf(const WCHAR *format, va_list ap);
extern WCHAR *debugstrf(const WCHAR *format, ..);
extern WCHAR *debugvstrf(const WCHAR *format, va_list ap);
// debug.cpp
#define debugargs const WCHAR *file, uintmax_t line, const WCHAR *file
extern HRESULT _logLastError(debugargs, const WCHAR *func, const WCHAR *s);
#define logLastError(s) _logLastError(L ## __FILE__, __LINE__, L ## __func__, s)
extern HRESULT _logHRESULT(debugargs, const WCHAR *s, HRESULT hr);
#define logHRESULT(s, hr) _logHRESULT(L ## __FILE__, __LINE__, L ## __func__, s, hr)
extern void _implbug(debugargs, const WCHAR *format, ...);
#define implbug(...) _implbug(L ## __FILE__, __LINE__, L ## __func__, __VA_LIST__)

124
windows/utf16.cpp Normal file
View File

@ -0,0 +1,124 @@
// 21 april 2016
#include "uipriv_windows.hpp"
// see http://stackoverflow.com/a/29556509/3408572
#define MBTWC(str, wstr, bufsiz) MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, bufsiz)
#define emptyUTF16() ((WCHAR *) uiAlloc(1 * sizeof (WCHAR), "WCHAR[]"))
WCHAR *toUTF16(const char *str)
{
WCHAR *wstr;
int n;
if (*str == '\0') // empty string
return emptyUTF16();
n = MBTWC(str, NULL, 0);
if (n == 0) {
logLastError(L"error figuring out number of characters to convert to");
return emptyUTF16();
}
wstr = (WCHAR *) uiAlloc(n * sizeof (WCHAR), "WCHAR[]");
if (MBTWC(str, wstr, n) != n) {
logLastError(L"error converting from UTF-8 to UTF-16");
// and return an empty string
*wstr = L'\0';
}
return wstr;
}
#define WCTMB(wstr, str, bufsiz) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, bufsiz, NULL, NULL)
#define emptyUTF8() ((char *) uiAlloc(1 * sizeof (char), "char[]"))
char *toUTF8(const WCHAR *wstr)
{
char *str;
int n;
if (*wstr == L'\0') // empty string
return emptyUTF8();
n = WCTMB(wstr, NULL, 0);
if (n == 0) {
logLastError(L"error figuring out number of characters to convert to");
return emptyUTF8();
}
str = (char *) uiAlloc(n * sizeof (char), "char[]");
if (WCTMB(wstr, str, n) != n) {
logLastError(L"error converting from UTF-16 to UTF-8");
// and return an empty string
*str = '\0';
}
return str;
}
// if recursing is TRUE, do NOT recursively call wstrf() in logHRESULT()
static WCHAR *strfcore(BOOL recursing, WCHAR *format, va_list ap)
{
va_list ap2;
WCHAR *buf;
size_t n;
HRESULT hr;
if (*format == L'\0')
return emptyUTF16();
va_copy(ap2, ap);
hr = SafeCchVPrintfEx(NULL, 0,
NULL, &n,
STRSAFE_IGNORE_NULLS,
format, ap2);
va_end(ap2);
if (hr != S_OK && hr != STRSAFE_E_INSUFFICIENT_BUFFER) {
if (!failing)
logHRESULT(L"error determining needed buffer size", hr);
return emptyUTF16();
}
// n includes the terminating L'\0'
buf = (WCHAR *) uiAlloc(n * sizeof (WCHAR), "WCHAR[]");
hr = SafeCchVPrintfEx(buf, n, // TODO what about this?
NULL, NULL,
0,
format, ap);
if (hr != S_OK) {
if (!failing)
logLastError(L"error formatting string", hr);
// and return an empty string
*buf = L'\0';
}
return buf;
}
WCHAR *strf(const WCHAR *format, ...)
{
va_list ap;
WCHAR *str;
va_start(ap, format);
str = vstrf(format, ap);
va_end(ap);
return str;
}
WCHAR *vstrf(const WCHAR *format, va_list ap)
{
return strfcore(FALSE, format, ap);
}
WCHAR *debugstrf(const WCHAR *format, ..)
{
va_list ap;
WCHAR *str;
va_start(ap, format);
str = debugvstrf(format, ap);
va_end(ap);
return str;
}
WCHAR *debugvstrf(const WCHAR *format, va_list ap)
{
return strfcore(TRUE, format, ap);
}