• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (yguyon@google.com)
15 
16 #ifndef WEBP_EXAMPLES_UNICODE_H_
17 #define WEBP_EXAMPLES_UNICODE_H_
18 
19 #if defined(_WIN32) && defined(_UNICODE)
20 
21 // wchar_t is used instead of TCHAR because we only perform additional work when
22 // Unicode is enabled and because the output of CommandLineToArgvW() is wchar_t.
23 
24 #include <wchar.h>
25 #include <windows.h>
26 #include <shellapi.h>
27 
28 // Create a wchar_t array containing Unicode parameters.
29 #define INIT_WARGV(ARGC, ARGV)                                                \
30   int wargc;                                                                  \
31   const W_CHAR** const wargv =                                                \
32       (const W_CHAR**)CommandLineToArgvW(GetCommandLineW(), &wargc);          \
33   do {                                                                        \
34     if (wargv == NULL || wargc != (ARGC)) {                                   \
35       fprintf(stderr, "Error: Unable to get Unicode arguments.\n");           \
36       FREE_WARGV_AND_RETURN(-1);                                              \
37     }                                                                         \
38   } while (0)
39 
40 // Use this to get a Unicode argument (e.g. file path).
41 #define GET_WARGV(UNUSED, C) wargv[C]
42 // For cases where argv is shifted by one compared to wargv.
43 #define GET_WARGV_SHIFTED(UNUSED, C) wargv[(C) + 1]
44 #define GET_WARGV_OR_NULL() wargv
45 
46 // Release resources. LocalFree() is needed after CommandLineToArgvW().
47 #define FREE_WARGV() LOCAL_FREE((W_CHAR** const)wargv)
48 #define LOCAL_FREE(WARGV)                  \
49   do {                                     \
50     if ((WARGV) != NULL) LocalFree(WARGV); \
51   } while (0)
52 
53 #define W_CHAR wchar_t  // WCHAR without underscore might already be defined.
54 #define TO_W_CHAR(STR) (L##STR)
55 
56 #define WFOPEN(ARG, OPT) _wfopen((const W_CHAR*)ARG, TO_W_CHAR(OPT))
57 
58 #define WPRINTF(STR, ...) wprintf(TO_W_CHAR(STR), __VA_ARGS__)
59 #define WFPRINTF(STDERR, STR, ...) fwprintf(STDERR, TO_W_CHAR(STR), __VA_ARGS__)
60 
61 #define WSTRLEN(FILENAME) wcslen((const W_CHAR*)FILENAME)
62 #define WSTRCMP(FILENAME, STR) wcscmp((const W_CHAR*)FILENAME, TO_W_CHAR(STR))
63 #define WSTRRCHR(FILENAME, STR) wcsrchr((const W_CHAR*)FILENAME, TO_W_CHAR(STR))
64 #define WSNPRINTF(A, B, STR, ...) _snwprintf(A, B, TO_W_CHAR(STR), __VA_ARGS__)
65 
66 #else
67 
68 // Unicode file paths work as is on Unix platforms, and no extra work is done on
69 // Windows either if Unicode is disabled.
70 
71 #define INIT_WARGV(ARGC, ARGV)
72 
73 #define GET_WARGV(ARGV, C) (ARGV)[C]
74 #define GET_WARGV_SHIFTED(ARGV, C) (ARGV)[C]
75 #define GET_WARGV_OR_NULL() NULL
76 
77 #define FREE_WARGV()
78 #define LOCAL_FREE(WARGV)
79 
80 #define W_CHAR char
81 #define TO_W_CHAR(STR) (STR)
82 
83 #define WFOPEN(ARG, OPT) fopen(ARG, OPT)
84 
85 #define WPRINTF(STR, ...) printf(STR, __VA_ARGS__)
86 #define WFPRINTF(STDERR, STR, ...) fprintf(STDERR, STR, __VA_ARGS__)
87 
88 #define WSTRLEN(FILENAME) strlen(FILENAME)
89 #define WSTRCMP(FILENAME, STR) strcmp(FILENAME, STR)
90 #define WSTRRCHR(FILENAME, STR) strrchr(FILENAME, STR)
91 #define WSNPRINTF(A, B, STR, ...) snprintf(A, B, STR, __VA_ARGS__)
92 
93 #endif  // defined(_WIN32) && defined(_UNICODE)
94 
95 // Don't forget to free wargv before returning (e.g. from main).
96 #define FREE_WARGV_AND_RETURN(VALUE) \
97   do {                               \
98     FREE_WARGV();                    \
99     return (VALUE);                  \
100   } while (0)
101 
102 #endif  // WEBP_EXAMPLES_UNICODE_H_
103