9 #include <windows.h> /* for WideCharToMultiByte and MultiByteToWideChar */
11 #include "share/win_utf8_io.h"
13 static UINT win_utf8_io_codepage = CP_ACP;
15 /* convert WCHAR stored Unicode string to UTF-8. Caller is responsible for freeing memory */
17 char *utf8_from_wchar(const wchar_t *wstr)
22 if (!wstr) return NULL;
23 if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0) return NULL;
24 if ((utf8str = (char *)malloc(++len)) == NULL) return NULL;
25 if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) {
33 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
35 wchar_t *wchar_from_utf8(const char *str)
40 if (!str) return NULL;
41 len=(int)strlen(str)+1;
42 if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) != NULL) {
43 if (MultiByteToWideChar(win_utf8_io_codepage, 0, str, len, widestr, len) == 0) {
44 if (MultiByteToWideChar(CP_ACP, 0, str, len, widestr, len) == 0) { /* try conversion from Ansi in case the initial UTF-8 conversion had failed */
54 /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */
55 int get_utf8_argv(int *argc, char ***argv)
57 typedef int (__cdecl *__wgetmainargs_)(int*, wchar_t***, wchar_t***, int, int*);
58 __wgetmainargs_ __wgetmainargs;
66 if ((handle = LoadLibrary("msvcrt.dll")) == NULL) return 1;
67 if ((__wgetmainargs = (__wgetmainargs_)GetProcAddress(handle, "__wgetmainargs")) == NULL) return 1;
69 if (__wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) return 1;
70 if ((utf8argv = (char **)malloc(wargc*sizeof(char*))) == NULL) return 1;
73 for (i=0; i<wargc; i++) {
74 if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) {
84 win_utf8_io_codepage = CP_UTF8;
96 int printf_utf8(const char *format, ...)
104 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
105 va_start(argptr, format);
106 ret = vsprintf(utmp, format, argptr);
109 if (!(wout = wchar_from_utf8(utmp))) {
113 ret = wprintf(L"%s", wout);
116 if (utmp) free(utmp);
117 if (wout) free(wout);
122 int fprintf_utf8(FILE *stream, const char *format, ...)
125 wchar_t *wout = NULL;
130 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
131 va_start(argptr, format);
132 ret = vsprintf(utmp, format, argptr);
135 if (!(wout = wchar_from_utf8(utmp))) {
139 ret = fwprintf(stream, L"%s", wout);
142 if (utmp) free(utmp);
143 if (wout) free(wout);
148 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
151 wchar_t *wout = NULL;
155 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
156 if ((ret = vsprintf(utmp, format, argptr)) < 0) break;
157 if (!(wout = wchar_from_utf8(utmp))) {
161 ret = fwprintf(stream, L"%s", wout);
164 if (utmp) free(utmp);
165 if (wout) free(wout);
172 FILE *fopen_utf8(const char *filename, const char *mode)
174 wchar_t *wname = NULL;
175 wchar_t *wmode = NULL;
179 if (!(wname = wchar_from_utf8(filename))) break;
180 if (!(wmode = wchar_from_utf8(mode))) break;
181 f = _wfopen(wname, wmode);
184 if (wname) free(wname);
185 if (wmode) free(wmode);
190 int _stat64_utf8(const char *path, struct __stat64 *buffer)
195 if (!(wpath = wchar_from_utf8(path))) return -1;
196 ret = _wstat64(wpath, buffer);
202 int chmod_utf8(const char *filename, int pmode)
207 if (!(wname = wchar_from_utf8(filename))) return -1;
208 ret = _wchmod(wname, pmode);
214 int utime_utf8(const char *filename, struct utimbuf *times)
220 if (!(wname = wchar_from_utf8(filename))) return -1;
221 ret = _wutime(wname, &ut);
225 if (sizeof(*times) == sizeof(ut)) {
226 memcpy(times, &ut, sizeof(ut));
228 times->actime = ut.actime;
229 times->modtime = ut.modtime;
236 int unlink_utf8(const char *filename)
241 if (!(wname = wchar_from_utf8(filename))) return -1;
242 ret = _wunlink(wname);
248 int rename_utf8(const char *oldname, const char *newname)
250 wchar_t *wold = NULL;
251 wchar_t *wnew = NULL;
255 if (!(wold = wchar_from_utf8(oldname))) break;
256 if (!(wnew = wchar_from_utf8(newname))) break;
257 ret = _wrename(wold, wnew);
260 if (wold) free(wold);
261 if (wnew) free(wnew);