Win utf8 treatment for CreateFile.
[platform/upstream/flac.git] / src / share / win_utf8_io / win_utf8_io.c
1
2 #if HAVE_CONFIG_H
3 #  include <config.h>
4 #endif
5
6 #include <stdio.h>
7 #include <sys/stat.h>
8 #include <sys/utime.h>
9 #include <io.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdarg.h>
13 #include <windows.h> /* for WideCharToMultiByte and MultiByteToWideChar */
14
15 #include "share/compat.h"
16 #include "share/win_utf8_io.h"
17 #include "share/compat.h"
18
19 static UINT win_utf8_io_codepage = CP_ACP;
20
21 /* convert WCHAR stored Unicode string to UTF-8. Caller is responsible for freeing memory */
22 static
23 char *utf8_from_wchar(const wchar_t *wstr)
24 {
25         char *utf8str;
26         int len;
27
28         if (!wstr) return NULL;
29         if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0) return NULL;
30         if ((utf8str = (char *)malloc(++len)) == NULL) return NULL;
31         if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) {
32                 free(utf8str);
33                 utf8str = NULL;
34         }
35
36         return utf8str;
37 }
38
39 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
40 static
41 wchar_t *wchar_from_utf8(const char *str)
42 {
43         wchar_t *widestr;
44         int len;
45
46         if (!str) return NULL;
47         len=(int)strlen(str)+1;
48         if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) != NULL) {
49                 if (MultiByteToWideChar(win_utf8_io_codepage, 0, str, len, widestr, len) == 0) {
50                         if (MultiByteToWideChar(CP_ACP, 0, str, len, widestr, len) == 0) { /* try conversion from Ansi in case the initial UTF-8 conversion had failed */
51                                 free(widestr);
52                                 widestr = NULL;
53                         }
54                 }
55         }
56
57         return widestr;
58 }
59
60 /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */
61 int get_utf8_argv(int *argc, char ***argv)
62 {
63         typedef int (__cdecl *__wgetmainargs_)(int*, wchar_t***, wchar_t***, int, int*);
64         __wgetmainargs_ __wgetmainargs;
65         HMODULE handle;
66         int wargc;
67         wchar_t **wargv;
68         wchar_t **wenv;
69         char **utf8argv;
70         int ret, i;
71
72         if ((handle = LoadLibrary("msvcrt.dll")) == NULL) return 1;
73         if ((__wgetmainargs = (__wgetmainargs_)GetProcAddress(handle, "__wgetmainargs")) == NULL) return 1;
74         i = 0;
75         if (__wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) return 1;
76         if ((utf8argv = (char **)malloc(wargc*sizeof(char*))) == NULL) return 1;
77         ret = 0;
78
79         for (i=0; i<wargc; i++) {
80                 if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) {
81                         ret = 1;
82                         break;
83                 }
84                 if (ret != 0) break;
85         }
86
87         FreeLibrary(handle);
88
89         if (ret == 0) {
90                 win_utf8_io_codepage = CP_UTF8;
91                 *argc = wargc;
92                 *argv = utf8argv;
93         } else {
94                 free(utf8argv);
95         }
96
97         return ret;
98 }
99
100 /* return number of characters in the UTF-8 string */
101 size_t strlen_utf8(const char *str)
102 {
103         size_t len;
104         if ((len = MultiByteToWideChar(win_utf8_io_codepage, 0, str, -1, NULL, 0)) == 0)
105                 len = strlen(str);
106         return len;
107 }
108
109 /* get the console width in characters */
110 int win_get_console_width(void)
111 {
112         int width = 80;
113         CONSOLE_SCREEN_BUFFER_INFO csbi;
114         HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
115         if (GetConsoleScreenBufferInfo(hOut, &csbi) != 0) width = csbi.dwSize.X;
116         return width;
117 }
118
119 /* print functions */
120
121 int print_console(FILE *stream, const wchar_t *text, uint32_t len)
122 {
123         static HANDLE hOut;
124         static HANDLE hErr;
125         DWORD out;
126         hOut = GetStdHandle(STD_OUTPUT_HANDLE);
127         hErr = GetStdHandle(STD_ERROR_HANDLE);
128         if (stream == stdout && hOut != INVALID_HANDLE_VALUE && GetFileType(hOut) == FILE_TYPE_CHAR) {
129                 if (WriteConsoleW(hOut, text, len, &out, NULL) == 0) return -1;
130                 return out;
131         } else if (stream == stderr && hErr != INVALID_HANDLE_VALUE && GetFileType(hErr) == FILE_TYPE_CHAR) {
132                 if (WriteConsoleW(hErr, text, len, &out, NULL) == 0) return -1;
133                 return out;
134         } else {
135                 int ret = fwprintf(stream, L"%s", text);
136                 if (ret < 0) return ret;
137                 return len;
138         }
139 }
140
141 int printf_utf8(const char *format, ...)
142 {
143         char *utmp = NULL;
144         wchar_t *wout = NULL;
145         int ret = -1;
146
147         while (1) {
148                 va_list argptr;
149                 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
150                 va_start(argptr, format);
151                 ret = vsprintf(utmp, format, argptr);
152                 va_end(argptr);
153                 if (ret < 0) break;
154                 if (!(wout = wchar_from_utf8(utmp))) {
155                         ret = -1;
156                         break;
157                 }
158                 ret = print_console(stdout, wout, wcslen(wout));
159                 break;
160         }
161         if (utmp) free(utmp);
162         if (wout) free(wout);
163
164         return ret;
165 }
166
167 int fprintf_utf8(FILE *stream, const char *format, ...)
168 {
169         char *utmp = NULL;
170         wchar_t *wout = NULL;
171         int ret = -1;
172
173         while (1) {
174                 va_list argptr;
175                 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
176                 va_start(argptr, format);
177                 ret = vsprintf(utmp, format, argptr);
178                 va_end(argptr);
179                 if (ret < 0) break;
180                 if (!(wout = wchar_from_utf8(utmp))) {
181                         ret = -1;
182                         break;
183                 }
184                 ret = print_console(stream, wout, wcslen(wout));
185                 break;
186         }
187         if (utmp) free(utmp);
188         if (wout) free(wout);
189
190         return ret;
191 }
192
193 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
194 {
195         char *utmp = NULL;
196         wchar_t *wout = NULL;
197         int ret = -1;
198
199         while (1) {
200                 if (!(utmp = (char *)malloc(32768*sizeof(char)))) break;
201                 if ((ret = vsprintf(utmp, format, argptr)) < 0) break;
202                 if (!(wout = wchar_from_utf8(utmp))) {
203                         ret = -1;
204                         break;
205                 }
206                 ret = print_console(stream, wout, wcslen(wout));
207                 break;
208         }
209         if (utmp) free(utmp);
210         if (wout) free(wout);
211
212         return ret;
213 }
214
215 /* file functions */
216
217 FILE *fopen_utf8(const char *filename, const char *mode)
218 {
219         wchar_t *wname = NULL;
220         wchar_t *wmode = NULL;
221         FILE *f = NULL;
222
223         while (1) {
224                 if (!(wname = wchar_from_utf8(filename))) break;
225                 if (!(wmode = wchar_from_utf8(mode))) break;
226                 f = _wfopen(wname, wmode);
227                 break;
228         }
229         if (wname) free(wname);
230         if (wmode) free(wmode);
231
232         return f;
233 }
234
235 int _stat64_utf8(const char *path, struct __stat64 *buffer)
236 {
237         wchar_t *wpath;
238         int ret;
239
240         if (!(wpath = wchar_from_utf8(path))) return -1;
241         ret = _wstat64(wpath, buffer);
242         free(wpath);
243
244         return ret;
245 }
246
247 int chmod_utf8(const char *filename, int pmode)
248 {
249         wchar_t *wname;
250         int ret;
251
252         if (!(wname = wchar_from_utf8(filename))) return -1;
253         ret = _wchmod(wname, pmode);
254         free(wname);
255
256         return ret;
257 }
258
259 int utime_utf8(const char *filename, struct utimbuf *times)
260 {
261         wchar_t *wname;
262         struct _utimbuf ut;
263         int ret;
264
265         if (!(wname = wchar_from_utf8(filename))) return -1;
266         ret = _wutime(wname, &ut);
267         free(wname);
268
269         if (ret != -1) {
270                 if (sizeof(*times) == sizeof(ut)) {
271                         memcpy(times, &ut, sizeof(ut));
272                 } else {
273                         times->actime = ut.actime;
274                         times->modtime = ut.modtime;
275                 }
276         }
277
278         return ret;
279 }
280
281 int unlink_utf8(const char *filename)
282 {
283         wchar_t *wname;
284         int ret;
285
286         if (!(wname = wchar_from_utf8(filename))) return -1;
287         ret = _wunlink(wname);
288         free(wname);
289
290         return ret;
291 }
292
293 int rename_utf8(const char *oldname, const char *newname)
294 {
295         wchar_t *wold = NULL;
296         wchar_t *wnew = NULL;
297         int ret = -1;
298
299         while (1) {
300                 if (!(wold = wchar_from_utf8(oldname))) break;
301                 if (!(wnew = wchar_from_utf8(newname))) break;
302                 ret = _wrename(wold, wnew);
303                 break;
304         }
305         if (wold) free(wold);
306         if (wnew) free(wnew);
307
308         return ret;
309 }
310
311 HANDLE WINAPI CreateFile_utf8(const char *lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
312 {
313         wchar_t *wname;
314         HANDLE handle = INVALID_HANDLE_VALUE;
315
316         if ((wname = wchar_from_utf8(lpFileName)) != NULL) {
317                 handle = CreateFileW(wname, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
318                 free(wname);
319         }
320
321         return handle;
322 }