diff options
| author | Pietro Gagliardi <[email protected]> | 2015-04-09 11:12:01 -0400 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2015-04-09 11:12:01 -0400 |
| commit | 79a7e18b8da55b4bcec72cc66f9b4e07878e7eee (patch) | |
| tree | 1c3b3873576e80ee73cc8a6281cef5e0ef03b1be /new/text_windows.c | |
| parent | 1f18d88f565844436fb4487b596175ba48138c05 (diff) | |
Implemented the text functions on Windows.
Diffstat (limited to 'new/text_windows.c')
| -rw-r--r-- | new/text_windows.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/new/text_windows.c b/new/text_windows.c index e483a95..4f5b741 100644 --- a/new/text_windows.c +++ b/new/text_windows.c @@ -16,3 +16,46 @@ WCHAR *toUTF16(const char *str) logLastError("error converting from UTF-8 to UTF-16 in toUTF16()"); return wstr; } + +#define WCTMB(wstr, str, bufsiz) WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, wstr, -1, str, bufsiz, NULL, FALSE) + +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()"); + // TODO does n include the null terminator? + str = (char *) uiAlloc((n + 1) * sizeof (char), "char[]"); + if (WCTMB(wstr, str, n + 1) != n) + logLastError("error converting from UTF-16 to UTF-8 in toUTFF8()"); + return str; +} + +WCHAR *windowText(HWND hwnd) +{ + int n; + WCHAR *text; + DWORD le; + + SetLastError(0); + n = GetWindowTextLengthW(hwnd); + if (n == 0) { + le = GetLastError(); + SetLastError(le); // just in case + if (le != 0) + logLastError("error getting window text length in windowText()"); + } + // TODO null terminator? + text = (WCHAR *) uiAlloc((n + 1) * sizeof (WCHAR), "WCHAR[]"); + if (GetWindowTextW(hwnd, text, n + 1) != n) + logLastError("error getting window text in windowText()"); + return text; +} + +void uiFreeText(char *text) +{ + uiFree(text); +} |
