From efb5a38ca0bac3537ceaf3383681a518df133143 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 29 Jul 2015 13:51:16 +0200 Subject: win32: Convert argv to utf-8 and use regular getopt. Easier than duplicating getopt for wchar_t, since we already have windows specific functions to convert wchar_t strings. --- platform/win32/libmupdf.vcproj | 4 --- platform/x11/win_main.c | 38 ++++++++++++++-------- source/fitz/getoptw.c | 72 ------------------------------------------ 3 files changed, 25 insertions(+), 89 deletions(-) delete mode 100644 source/fitz/getoptw.c diff --git a/platform/win32/libmupdf.vcproj b/platform/win32/libmupdf.vcproj index b47540ba..a1df42fd 100644 --- a/platform/win32/libmupdf.vcproj +++ b/platform/win32/libmupdf.vcproj @@ -869,10 +869,6 @@ RelativePath="..\..\source\fitz\getopt.c" > - - diff --git a/platform/x11/win_main.c b/platform/x11/win_main.c index 538f02f5..0ef15919 100644 --- a/platform/x11/win_main.c +++ b/platform/x11/win_main.c @@ -1209,7 +1209,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { int argc; - LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); + LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc); + char **argv; char argv0[256]; MSG msg; int code; @@ -1217,8 +1218,9 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow int bps = 0; int displayRes = get_system_dpi(); int c; - wchar_t *password = NULL; - wchar_t *layout_css = NULL; + int i; + char *password = NULL; + char *layout_css = NULL; ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) @@ -1226,27 +1228,33 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow fprintf(stderr, "cannot initialise context\n"); exit(1); } + + /* Convert wchar_t argv to utf-8 */ + argv = fz_malloc_array(ctx, argc, sizeof (char*)); + for (i = 0; i < argc; ++i) + argv[i] = fz_utf8_from_wchar(wargv[i]); + pdfapp_init(ctx, &gapp); - while ((c = fz_getoptw(argc, argv, L"p:r:A:C:W:H:S:U:b:")) != -1) + while ((c = fz_getopt(argc, argv, L"p:r:A:C:W:H:S:U:b:")) != -1) { switch (c) { case 'C': - c = wcstol(fz_optargw, NULL, 16); + c = strtol(fz_optarg, NULL, 16); gapp.tint = 1; gapp.tint_r = (c >> 16) & 255; gapp.tint_g = (c >> 8) & 255; gapp.tint_b = (c) & 255; break; - case 'p': password = fz_optargw; break; - case 'r': displayRes = _wtoi(fz_optargw); break; - case 'A': fz_set_aa_level(ctx, _wtoi(fz_optargw)); break; - case 'W': gapp.layout_w = _wtoi(fz_optargw); break; - case 'H': gapp.layout_h = _wtoi(fz_optargw); break; - case 'S': gapp.layout_em = _wtoi(fz_optargw); break; - case 'b': bps = (fz_optargw && *fz_optargw) ? _wtoi(fz_optargw) : 4096; break; - case 'U': layout_css = fz_optargw; break; + case 'p': password = fz_optarg; break; + case 'r': displayRes = atoi(fz_optarg); break; + case 'A': fz_set_aa_level(ctx, atoi(fz_optarg)); break; + case 'W': gapp.layout_w = fz_atof(fz_optarg); break; + case 'H': gapp.layout_h = fz_atof(fz_optarg); break; + case 'S': gapp.layout_em = fz_atof(fz_optarg); break; + case 'b': bps = (fz_optarg && *fz_optarg) ? atoi(fz_optarg) : 4096; break; + case 'U': layout_css = fz_optarg; break; default: usage(); } } @@ -1291,6 +1299,10 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow DispatchMessage(&msg); } + for (i = 0; i < argc; ++i) + free(argv[i]); + free(argv); + do_close(&gapp); return 0; diff --git a/source/fitz/getoptw.c b/source/fitz/getoptw.c deleted file mode 100644 index 55b8d015..00000000 --- a/source/fitz/getoptw.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * This is a version of the public domain getopt implementation by - * Henry Spencer originally posted to net.sources. Adapted to - * windows wchar's. - * - * This file is in the public domain. - */ - -#if defined(_WIN64) || defined(_WIN32) - -#include -#include -#include - -#define getoptw fz_getoptw -#define optargw fz_optargw -#define optindw fz_optindw - -wchar_t *optargw; /* Global argument pointer. */ -int optindw = 0; /* Global argv index. */ - -static wchar_t *scan = NULL; /* Private scan pointer. */ - -int -getoptw(wchar_t argc, wchar_t *argv[], wchar_t *optstring) -{ - wchar_t c; - wchar_t *place; - - optargw = NULL; - - if (!scan || *scan == '\0') { - if (optindw == 0) - optindw++; - - if (optindw >= argc || argv[optindw][0] != '-' || argv[optindw][1] == '\0') - return EOF; - if (argv[optindw][1] == '-' && argv[optindw][2] == '\0') { - optindw++; - return EOF; - } - - scan = argv[optindw]+1; - optindw++; - } - - c = *scan++; - place = wcschr(optstring, c); - - if (!place || c == ':') { - fprintf(stderr, "%s: unknown option -%C\n", argv[0], c); - return '?'; - } - - place++; - if (*place == ':') { - if (*scan != '\0') { - optargw = scan; - scan = NULL; - } else if( optindw < argc ) { - optargw = argv[optindw]; - optindw++; - } else { - fprintf(stderr, "%s: option requires argument -%C\n", argv[0], c); - return ':'; - } - } - - return c; -} - -#endif -- cgit v1.2.3