1 // Copyright 2018 Google Inc. All Rights Reserved. 2 // 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the COPYING file in the root of the source 5 // tree. An additional intellectual property rights grant can be found 6 // in the file PATENTS. All contributing project authors may 7 // be found in the AUTHORS file in the root of the source tree. 8 // ----------------------------------------------------------------------------- 9 // 10 // Unicode support for Windows. The main idea is to maintain an array of Unicode 11 // arguments (wargv) and use it only for file paths. The regular argv is used 12 // for everything else. 13 // 14 // Author: Yannis Guyon ([email protected]) 15 16 #ifndef WEBP_EXAMPLES_UNICODE_H_ 17 #define WEBP_EXAMPLES_UNICODE_H_ 18 19 #include <stdio.h> 20 21 #if defined(_WIN32) && defined(_UNICODE) 22 23 // wchar_t is used instead of TCHAR because we only perform additional work when 24 // Unicode is enabled and because the output of CommandLineToArgvW() is wchar_t. 25 26 #include <fcntl.h> 27 #include <io.h> 28 #include <wchar.h> 29 #include <windows.h> 30 #include <shellapi.h> 31 32 // Create a wchar_t array containing Unicode parameters. 33 #define INIT_WARGV(ARGC, ARGV) \ 34 int wargc; \ 35 const W_CHAR** const wargv = \ 36 (const W_CHAR**)CommandLineToArgvW(GetCommandLineW(), &wargc); \ 37 do { \ 38 if (wargv == NULL || wargc != (ARGC)) { \ 39 fprintf(stderr, "Error: Unable to get Unicode arguments.\n"); \ 40 FREE_WARGV_AND_RETURN(-1); \ 41 } \ 42 } while (0) 43 44 // Use this to get a Unicode argument (e.g. file path). 45 #define GET_WARGV(UNUSED, C) wargv[C] 46 // For cases where argv is shifted by one compared to wargv. 47 #define GET_WARGV_SHIFTED(UNUSED, C) wargv[(C) + 1] 48 #define GET_WARGV_OR_NULL() wargv 49 50 // Release resources. LocalFree() is needed after CommandLineToArgvW(). 51 #define FREE_WARGV() LOCAL_FREE((W_CHAR** const)wargv) 52 #define LOCAL_FREE(WARGV) \ 53 do { \ 54 if ((WARGV) != NULL) LocalFree(WARGV); \ 55 } while (0) 56 57 #define W_CHAR wchar_t // WCHAR without underscore might already be defined. 58 #define TO_W_CHAR(STR) (L##STR) 59 60 #define WFOPEN(ARG, OPT) _wfopen((const W_CHAR*)ARG, TO_W_CHAR(OPT)) 61 62 #define WFPRINTF(STREAM, STR, ...) \ 63 do { \ 64 int prev_mode; \ 65 fflush(STREAM); \ 66 prev_mode = _setmode(_fileno(STREAM), _O_U8TEXT); \ 67 fwprintf(STREAM, TO_W_CHAR(STR), __VA_ARGS__); \ 68 fflush(STREAM); \ 69 (void)_setmode(_fileno(STREAM), prev_mode); \ 70 } while (0) 71 #define WPRINTF(STR, ...) WFPRINTF(stdout, STR, __VA_ARGS__) 72 73 #define WSTRLEN(FILENAME) wcslen((const W_CHAR*)FILENAME) 74 #define WSTRCMP(FILENAME, STR) wcscmp((const W_CHAR*)FILENAME, TO_W_CHAR(STR)) 75 #define WSTRRCHR(FILENAME, STR) wcsrchr((const W_CHAR*)FILENAME, TO_W_CHAR(STR)) 76 #define WSNPRINTF(A, B, STR, ...) _snwprintf(A, B, TO_W_CHAR(STR), __VA_ARGS__) 77 78 #else 79 80 #include <string.h> 81 82 // Unicode file paths work as is on Unix platforms, and no extra work is done on 83 // Windows either if Unicode is disabled. 84 85 #define INIT_WARGV(ARGC, ARGV) 86 87 #define GET_WARGV(ARGV, C) (ARGV)[C] 88 #define GET_WARGV_SHIFTED(ARGV, C) (ARGV)[C] 89 #define GET_WARGV_OR_NULL() NULL 90 91 #define FREE_WARGV() 92 #define LOCAL_FREE(WARGV) 93 94 #define W_CHAR char 95 #define TO_W_CHAR(STR) (STR) 96 97 #define WFOPEN(ARG, OPT) fopen(ARG, OPT) 98 99 #define WPRINTF(STR, ...) printf(STR, __VA_ARGS__) 100 #define WFPRINTF(STREAM, STR, ...) fprintf(STREAM, STR, __VA_ARGS__) 101 102 #define WSTRLEN(FILENAME) strlen(FILENAME) 103 #define WSTRCMP(FILENAME, STR) strcmp(FILENAME, STR) 104 #define WSTRRCHR(FILENAME, STR) strrchr(FILENAME, STR) 105 #define WSNPRINTF(A, B, STR, ...) snprintf(A, B, STR, __VA_ARGS__) 106 107 #endif // defined(_WIN32) && defined(_UNICODE) 108 109 // Don't forget to free wargv before returning (e.g. from main). 110 #define FREE_WARGV_AND_RETURN(VALUE) \ 111 do { \ 112 FREE_WARGV(); \ 113 return (VALUE); \ 114 } while (0) 115 116 #endif // WEBP_EXAMPLES_UNICODE_H_ 117