1 #ifdef FLAC__STRINGS_IN_UTF8
10 #include <windows.h> /* for WideCharToMultiByte and MultiByteToWideChar */
12 #include "share/win_utf8_io.h"
14 /* 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 */
32 wchar_t *wchar_from_utf8(const char *str)
37 if (!str) return NULL;
38 len=(int)strlen(str)+1;
39 if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) != NULL) {
40 if (MultiByteToWideChar(CP_UTF8, 0, str, len, widestr, len) == 0) {
41 if (MultiByteToWideChar(CP_ACP, 0, str, len, widestr, len) == 0) { /* try conversion from Ansi in case the initial UTF-8 conversion had failed */
51 /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */
52 int get_utf8_argv(int *argc, char ***argv)
54 typedef int (__cdecl *__wgetmainargs_)(int*, wchar_t***, wchar_t***, int, int*);
55 __wgetmainargs_ __wgetmainargs;
63 if ((handle = LoadLibrary("msvcrt.dll")) == NULL) return 1;
64 if ((__wgetmainargs = (__wgetmainargs_)GetProcAddress(handle, "__wgetmainargs")) == NULL) return 1;
66 if (__wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) return 1;
67 if ((utf8argv = (char **)malloc(wargc*sizeof(char*))) == NULL) return 1;
70 for (i=0; i<wargc; i++) {
71 if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) {
90 int printf_utf8(const char *format, ...)
98 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
99 va_start(argptr, format);
100 ret = vsprintf(utmp, format, argptr);
103 if (!(wout = wchar_from_utf8(utmp))) {
107 ret = wprintf(L"%s", wout);
110 if (utmp) free(utmp);
111 if (wout) free(wout);
116 int fprintf_utf8(FILE *stream, const char *format, ...)
119 wchar_t *wout = NULL;
124 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
125 va_start(argptr, format);
126 ret = vsprintf(utmp, format, argptr);
129 if (!(wout = wchar_from_utf8(utmp))) {
133 ret = fwprintf(stream, L"%s", wout);
136 if (utmp) free(utmp);
137 if (wout) free(wout);
142 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
145 wchar_t *wout = NULL;
149 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
150 if ((ret = vsprintf(utmp, format, argptr)) < 0) break;
151 if (!(wout = wchar_from_utf8(utmp))) {
155 ret = fwprintf(stream, L"%s", wout);
158 if (utmp) free(utmp);
159 if (wout) free(wout);
166 FILE *fopen_utf8(const char *filename, const char *mode)
168 wchar_t *wname = NULL;
169 wchar_t *wmode = NULL;
173 if (!(wname = wchar_from_utf8(filename))) break;
174 if (!(wmode = wchar_from_utf8(mode))) break;
175 f = _wfopen(wname, wmode);
178 if (wname) free(wname);
179 if (wmode) free(wmode);
184 int _stat64_utf8(const char *path, struct _stat64 *buffer)
189 if (!(wpath = wchar_from_utf8(path))) return -1;
190 ret = _wstat64(wpath, buffer);
196 int chmod_utf8(const char *filename, int pmode)
201 if (!(wname = wchar_from_utf8(filename))) return -1;
202 ret = _wchmod(wname, pmode);
208 int utime_utf8(const char *filename, struct utimbuf *times)
214 if (!(wname = wchar_from_utf8(filename))) return -1;
215 ret = _wutime(wname, &ut);
219 if (sizeof(*times) == sizeof(ut)) {
220 memcpy(times, &ut, sizeof(ut));
222 times->actime = ut.actime;
223 times->modtime = ut.modtime;
230 int unlink_utf8(const char *filename)
235 if (!(wname = wchar_from_utf8(filename))) return -1;
236 ret = _wunlink(wname);
242 int rename_utf8(const char *oldname, const char *newname)
244 wchar_t *wold = NULL;
245 wchar_t *wnew = NULL;
249 if (!(wold = wchar_from_utf8(oldname))) break;
250 if (!(wnew = wchar_from_utf8(newname))) break;
251 ret = _wrename(wold, wnew);
254 if (wold) free(wold);
255 if (wnew) free(wnew);