--- trunk/src/OEMisc.c 2006/03/27 12:47:50 10 +++ trunk/src/OEMisc.c 2006/04/07 10:46:41 12 @@ -30,6 +30,42 @@ #include "GPGOE.h" +char* +native_to_utf8 (const char *string) +{ + wchar_t *result; + char *native; + int n; + + n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0); + if (n < 0) + return NULL; + + result = (wchar_t*)xcalloc (1, (n+1) * sizeof *result); + + n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n); + if (n < 0) { + free (result); + return NULL; + } + + n = WideCharToMultiByte (CP_UTF8, 0, result, -1, NULL, 0, NULL, NULL); + if (n < 0) + return NULL; + + native = (char*)xcalloc (1, n+1); + + n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL); + if (n < 0) { + free (result); + return NULL; + } + + free (result); + return native; +} + + /* Convert the UTF8 string @string into the native charset. */ char* utf8_to_native (const char *string) @@ -108,7 +144,6 @@ set_clip_text (HWND hwnd, const char *text, int nbytes) { HANDLE clipmem; - int rc = 0; char *p; if (OpenClipboard (hwnd) == FALSE) @@ -120,18 +155,18 @@ abort (); p = (char *) GlobalLock (clipmem); if (p == NULL) { - rc = -1; - goto leave; + CloseClipboard (); + GlobalFree (clipmem); + return -1; } memcpy (p, text, nbytes); p[nbytes] = '\0'; - GlobalUnlock (clipmem); SetClipboardData (CF_TEXT, clipmem); + GlobalUnlock (clipmem); + CloseClipboard (); GlobalFree (clipmem); -leave: - CloseClipboard (); return 0; } @@ -158,6 +193,14 @@ } +void +xfree (void *p) +{ + if (p != NULL) + free (p); +} + + /* printf style message box. */ void show_error (HWND hwnd, const char *caption, UINT type, const char *fmt, ...)