#ifdef _WIN32
+wchar_t *
+babl_convert_utf8_to_utf16 (const char *str)
+{
+ int wchar_count = 0;
+ wchar_t *wstr = NULL;
+
+ if (!str)
+ return NULL;
+
+ wchar_count = MultiByteToWideChar (CP_UTF8,
+ MB_ERR_INVALID_CHARS,
+ str, -1,
+ NULL, 0);
+ if (wchar_count <= 0)
+ return NULL;
+
+ wstr = babl_malloc (wchar_count * sizeof (wchar_t));
+ if (!wstr)
+ return NULL;
+
+ wchar_count = MultiByteToWideChar (CP_UTF8,
+ MB_ERR_INVALID_CHARS,
+ str, -1,
+ wstr, wchar_count);
+ if (wchar_count <= 0)
+ {
+ babl_free (wstr);
+ return NULL;
+ }
+
+ return wstr;
+}
+
+char *
+babl_convert_utf16_to_utf8 (const wchar_t *wstr)
+{
+ int char_count = 0;
+ char *str = NULL;
+
+ if (!wstr)
+ return NULL;
+
+ char_count = WideCharToMultiByte (CP_UTF8,
+ WC_ERR_INVALID_CHARS,
+ wstr, -1,
+ NULL, 0,
+ NULL, NULL);
+ if (char_count <= 0)
+ return NULL;
+
+ str = babl_malloc (char_count);
+ if (!str)
+ return NULL;
+
+ char_count = WideCharToMultiByte (CP_UTF8,
+ WC_ERR_INVALID_CHARS,
+ wstr, -1,
+ str, char_count,
+ NULL, NULL);
+ if (char_count <= 0)
+ {
+ babl_free (str);
+ return NULL;
+ }
+
+ return str;
+}
+
extern IMAGE_DOS_HEADER __ImageBase;
void *
#ifndef _BABL_UTIL_H
#define _BABL_UTIL_H
+#include <stddef.h>
+
long
babl_ticks (void);
#ifdef _WIN32
+wchar_t *
+babl_convert_utf8_to_utf16 (const char *str);
+
+char *
+babl_convert_utf16_to_utf8 (const wchar_t *wstr);
+
void *
get_libbabl_module (void);
{
#ifdef _WIN32
/* Figure it out from the location of this DLL */
- char *filename;
- int filename_size;
- char *sep1, *sep2;
-
wchar_t w_filename[MAX_PATH];
+ char *filename = NULL;
+ char *sep1, *sep2;
DWORD nSize = sizeof (w_filename) / sizeof ((w_filename)[0]);
if (GetModuleFileNameW (get_libbabl_module (), w_filename, nSize) == 0)
babl_fatal ("GetModuleFilenameW failed");
- filename_size = WideCharToMultiByte (CP_UTF8, 0, w_filename, -1, NULL, 0,
- NULL, NULL);
- filename = babl_malloc (sizeof (char) * filename_size);
- if (!WideCharToMultiByte (CP_UTF8, 0, w_filename, -1,
- filename, filename_size, NULL, NULL))
+ filename = babl_convert_utf16_to_utf8 (w_filename);
+ if (!filename)
babl_fatal ("Converting module filename to UTF-8 failed");
/* If the DLL file name is of the format