1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2013-2016 Xiph.Org Foundation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "share/win_utf8_io.h"
38 #include "share/windows_unicode_filenames.h"
40 #define UTF8_BUFFER_SIZE 32768
42 static int local_vsnprintf(char *str, size_t size, const char *fmt, va_list va)
49 rc = vsnprintf_s(str, size, _TRUNCATE, fmt, va);
52 #elif defined __MINGW32__
53 rc = __mingw_vsnprintf(str, size, fmt, va);
55 rc = vsnprintf(str, size, fmt, va);
61 /* convert WCHAR stored Unicode string to UTF-8. Caller is responsible for freeing memory */
62 static char *utf8_from_wchar(const wchar_t *wstr)
69 if ((len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL)) == 0)
71 if ((utf8str = (char *)malloc(len)) == NULL)
73 if (WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8str, len, NULL, NULL) == 0) {
81 /* convert UTF-8 back to WCHAR. Caller is responsible for freeing memory */
82 static wchar_t *wchar_from_utf8(const char *str)
89 if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) == 0)
91 if ((widestr = (wchar_t *)malloc(len*sizeof(wchar_t))) == NULL)
93 if (MultiByteToWideChar(CP_UTF8, 0, str, -1, widestr, len) == 0) {
101 /* retrieve WCHAR commandline, expand wildcards and convert everything to UTF-8 */
102 int get_utf8_argv(int *argc, char ***argv)
104 typedef int (__cdecl *wgetmainargs_t)(int*, wchar_t***, wchar_t***, int, int*);
105 wgetmainargs_t wgetmainargs;
113 if ((handle = LoadLibrary("msvcrt.dll")) == NULL) return 1;
114 if ((wgetmainargs = (wgetmainargs_t)GetProcAddress(handle, "__wgetmainargs")) == NULL) {
119 /* when the 4th argument is 1, __wgetmainargs expands wildcards but also erroneously converts \\?\c:\path\to\file.flac to \\file.flac */
120 if (wgetmainargs(&wargc, &wargv, &wenv, 1, &i) != 0) {
124 if ((utf8argv = (char **)calloc(wargc, sizeof(char*))) == NULL) {
130 for (i=0; i<wargc; i++) {
131 if ((utf8argv[i] = utf8_from_wchar(wargv[i])) == NULL) {
137 FreeLibrary(handle); /* do not free it when wargv or wenv are still in use */
140 flac_set_utf8_filenames(true);
144 for (i=0; i<wargc; i++)
152 /* return number of characters in the UTF-8 string */
153 size_t strlen_utf8(const char *str)
156 len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); /* includes terminating null */
163 /* get the console width in characters */
164 int win_get_console_width(void)
167 CONSOLE_SCREEN_BUFFER_INFO csbi;
168 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
169 if(hOut != INVALID_HANDLE_VALUE && hOut != NULL)
170 if (GetConsoleScreenBufferInfo(hOut, &csbi) != 0)
171 width = csbi.dwSize.X;
175 /* print functions */
177 static int wprint_console(FILE *stream, const wchar_t *text, size_t len)
183 if (stream == stdout) {
184 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
185 if (hOut == INVALID_HANDLE_VALUE || hOut == NULL || GetFileType(hOut) != FILE_TYPE_CHAR)
187 if (WriteConsoleW(hOut, text, len, &out, NULL) == 0)
191 if (stream == stderr) {
192 HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE);
193 if (hErr == INVALID_HANDLE_VALUE || hErr == NULL || GetFileType(hErr) != FILE_TYPE_CHAR)
195 if (WriteConsoleW(hErr, text, len, &out, NULL) == 0)
201 ret = fputws(text, stream);
207 int printf_utf8(const char *format, ...)
211 va_start(argptr, format);
213 ret = vfprintf_utf8(stdout, format, argptr);
220 int fprintf_utf8(FILE *stream, const char *format, ...)
224 va_start(argptr, format);
226 ret = vfprintf_utf8(stream, format, argptr);
233 int vfprintf_utf8(FILE *stream, const char *format, va_list argptr)
236 wchar_t *wout = NULL;
240 if (!(utmp = (char *)malloc(UTF8_BUFFER_SIZE))) break;
241 if ((ret = local_vsnprintf(utmp, UTF8_BUFFER_SIZE, format, argptr)) <= 0) break;
242 if (!(wout = wchar_from_utf8(utmp))) {
246 ret = wprint_console(stream, wout, wcslen(wout));