9 #include <windows.h> /* for WideCharToMultiByte and MultiByteToWideChar */
11 #include "share/win_utf8_io.h"
13 /* convert WCHAR stored Unicode string to UTF-8. Caller is responsible for freeing memory */
15 char *utf8_from_wchar(const wchar_t *wstr)
20 if (!wstr) return NULL;
21 if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0) return NULL;
22 if ((utf8str = (char *)malloc(++len)) == NULL) return NULL;
23 if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) {
31 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
33 wchar_t *wchar_from_utf8(const char *str)
38 if (!str) return NULL;
39 len=(int)strlen(str)+1;
40 if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) != NULL) {
41 if (MultiByteToWideChar(CP_UTF8, 0, str, len, widestr, len) == 0) {
42 if (MultiByteToWideChar(CP_ACP, 0, str, len, widestr, len) == 0) { /* try conversion from Ansi in case the initial UTF-8 conversion had failed */
52 /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */
53 int get_utf8_argv(int *argc, char ***argv)
55 typedef int (__cdecl *__wgetmainargs_)(int*, wchar_t***, wchar_t***, int, int*);
56 __wgetmainargs_ __wgetmainargs;
64 if ((handle = LoadLibrary("msvcrt.dll")) == NULL) return 1;
65 if ((__wgetmainargs = (__wgetmainargs_)GetProcAddress(handle, "__wgetmainargs")) == NULL) return 1;
67 if (__wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) return 1;
68 if ((utf8argv = (char **)malloc(wargc*sizeof(char*))) == NULL) return 1;
71 for (i=0; i<wargc; i++) {
72 if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) {
91 int printf_utf8(const char *format, ...)
99 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
100 va_start(argptr, format);
101 ret = vsprintf(utmp, format, argptr);
104 if (!(wout = wchar_from_utf8(utmp))) {
108 ret = wprintf(L"%s", wout);
111 if (utmp) free(utmp);
112 if (wout) free(wout);
117 int fprintf_utf8(FILE *stream, const char *format, ...)
120 wchar_t *wout = NULL;
125 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
126 va_start(argptr, format);
127 ret = vsprintf(utmp, format, argptr);
130 if (!(wout = wchar_from_utf8(utmp))) {
134 ret = fwprintf(stream, L"%s", wout);
137 if (utmp) free(utmp);
138 if (wout) free(wout);
143 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
146 wchar_t *wout = NULL;
150 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
151 if ((ret = vsprintf(utmp, format, argptr)) < 0) break;
152 if (!(wout = wchar_from_utf8(utmp))) {
156 ret = fwprintf(stream, L"%s", wout);
159 if (utmp) free(utmp);
160 if (wout) free(wout);
167 FILE *fopen_utf8(const char *filename, const char *mode)
169 wchar_t *wname = NULL;
170 wchar_t *wmode = NULL;
174 if (!(wname = wchar_from_utf8(filename))) break;
175 if (!(wmode = wchar_from_utf8(mode))) break;
176 f = _wfopen(wname, wmode);
179 if (wname) free(wname);
180 if (wmode) free(wmode);
185 int _stat64_utf8(const char *path, struct _stat64 *buffer)
190 if (!(wpath = wchar_from_utf8(path))) return -1;
191 ret = _wstat64(wpath, buffer);
197 int chmod_utf8(const char *filename, int pmode)
202 if (!(wname = wchar_from_utf8(filename))) return -1;
203 ret = _wchmod(wname, pmode);
209 int utime_utf8(const char *filename, struct utimbuf *times)
215 if (!(wname = wchar_from_utf8(filename))) return -1;
216 ret = _wutime(wname, &ut);
220 if (sizeof(*times) == sizeof(ut)) {
221 memcpy(times, &ut, sizeof(ut));
223 times->actime = ut.actime;
224 times->modtime = ut.modtime;
231 int unlink_utf8(const char *filename)
236 if (!(wname = wchar_from_utf8(filename))) return -1;
237 ret = _wunlink(wname);
243 int rename_utf8(const char *oldname, const char *newname)
245 wchar_t *wold = NULL;
246 wchar_t *wnew = NULL;
250 if (!(wold = wchar_from_utf8(oldname))) break;
251 if (!(wnew = wchar_from_utf8(newname))) break;
252 ret = _wrename(wold, wnew);
255 if (wold) free(wold);
256 if (wnew) free(wnew);