Fix compiler warnings related to win utf8 stuff.
[flac.git] / src / share / win_utf8_io / win_utf8_io.c
blobb32db3c7de54b1d5884a63579c9c624a8b721ec0
2 #if HAVE_CONFIG_H
3 # include <config.h>
4 #endif
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 */
15 #include "share/compat.h"
16 #include "share/win_utf8_io.h"
17 #include "share/compat.h"
19 static UINT win_utf8_io_codepage = CP_ACP;
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)
25 char *utf8str;
26 int len;
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;
36 return utf8str;
39 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
40 static
41 wchar_t *wchar_from_utf8(const char *str)
43 wchar_t *widestr;
44 int len;
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;
57 return widestr;
60 /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */
61 int get_utf8_argv(int *argc, char ***argv)
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;
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;
79 for (i=0; i<wargc; i++) {
80 if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) {
81 ret = 1;
82 break;
84 if (ret != 0) break;
87 FreeLibrary(handle);
89 if (ret == 0) {
90 win_utf8_io_codepage = CP_UTF8;
91 *argc = wargc;
92 *argv = utf8argv;
93 } else {
94 free(utf8argv);
97 return ret;
100 /* return number of characters in the UTF-8 string */
101 size_t strlen_utf8(const char *str)
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;
109 /* get the console width in characters */
110 int win_get_console_width(void)
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;
119 /* print functions */
121 int print_console(FILE *stream, const wchar_t *text, uint32_t len)
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;
141 int printf_utf8(const char *format, ...)
143 char *utmp = NULL;
144 wchar_t *wout = NULL;
145 int ret = -1;
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;
158 ret = print_console(stdout, wout, wcslen(wout));
159 break;
161 if (utmp) free(utmp);
162 if (wout) free(wout);
164 return ret;
167 int fprintf_utf8(FILE *stream, const char *format, ...)
169 char *utmp = NULL;
170 wchar_t *wout = NULL;
171 int ret = -1;
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;
184 ret = print_console(stream, wout, wcslen(wout));
185 break;
187 if (utmp) free(utmp);
188 if (wout) free(wout);
190 return ret;
193 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
195 char *utmp = NULL;
196 wchar_t *wout = NULL;
197 int ret = -1;
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;
206 ret = print_console(stream, wout, wcslen(wout));
207 break;
209 if (utmp) free(utmp);
210 if (wout) free(wout);
212 return ret;
215 /* file functions */
217 FILE *fopen_utf8(const char *filename, const char *mode)
219 wchar_t *wname = NULL;
220 wchar_t *wmode = NULL;
221 FILE *f = NULL;
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;
229 if (wname) free(wname);
230 if (wmode) free(wmode);
232 return f;
235 int _stat64_utf8(const char *path, struct __stat64 *buffer)
237 wchar_t *wpath;
238 int ret;
240 if (!(wpath = wchar_from_utf8(path))) return -1;
241 ret = _wstat64(wpath, buffer);
242 free(wpath);
244 return ret;
247 int chmod_utf8(const char *filename, int pmode)
249 wchar_t *wname;
250 int ret;
252 if (!(wname = wchar_from_utf8(filename))) return -1;
253 ret = _wchmod(wname, pmode);
254 free(wname);
256 return ret;
259 int utime_utf8(const char *filename, struct utimbuf *times)
261 wchar_t *wname;
262 struct _utimbuf ut;
263 int ret;
265 if (!(wname = wchar_from_utf8(filename))) return -1;
266 ret = _wutime(wname, &ut);
267 free(wname);
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;
278 return ret;
281 int unlink_utf8(const char *filename)
283 wchar_t *wname;
284 int ret;
286 if (!(wname = wchar_from_utf8(filename))) return -1;
287 ret = _wunlink(wname);
288 free(wname);
290 return ret;
293 int rename_utf8(const char *oldname, const char *newname)
295 wchar_t *wold = NULL;
296 wchar_t *wnew = NULL;
297 int ret = -1;
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;
305 if (wold) free(wold);
306 if (wnew) free(wnew);
308 return ret;