diff options
-rw-r--r-- | platform/win32/libmupdf.vcproj | 4 | ||||
-rw-r--r-- | platform/x11/win_main.c | 38 | ||||
-rw-r--r-- | source/fitz/getoptw.c | 72 |
3 files changed, 89 insertions, 25 deletions
diff --git a/platform/win32/libmupdf.vcproj b/platform/win32/libmupdf.vcproj index a1df42fd..b47540ba 100644 --- a/platform/win32/libmupdf.vcproj +++ b/platform/win32/libmupdf.vcproj @@ -870,6 +870,10 @@ > </File> <File + RelativePath="..\..\source\fitz\getoptw.c" + > + </File> + <File RelativePath="..\..\source\fitz\glyph.c" > </File> diff --git a/platform/x11/win_main.c b/platform/x11/win_main.c index 0ef15919..538f02f5 100644 --- a/platform/x11/win_main.c +++ b/platform/x11/win_main.c @@ -1209,8 +1209,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { int argc; - LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc); - char **argv; + LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); char argv0[256]; MSG msg; int code; @@ -1218,9 +1217,8 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow int bps = 0; int displayRes = get_system_dpi(); int c; - int i; - char *password = NULL; - char *layout_css = NULL; + wchar_t *password = NULL; + wchar_t *layout_css = NULL; ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) @@ -1228,33 +1226,27 @@ 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_getopt(argc, argv, L"p:r:A:C:W:H:S:U:b:")) != -1) + while ((c = fz_getoptw(argc, argv, L"p:r:A:C:W:H:S:U:b:")) != -1) { switch (c) { case 'C': - c = strtol(fz_optarg, NULL, 16); + c = wcstol(fz_optargw, 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_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; + 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; default: usage(); } } @@ -1299,10 +1291,6 @@ 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 new file mode 100644 index 00000000..55b8d015 --- /dev/null +++ b/source/fitz/getoptw.c @@ -0,0 +1,72 @@ +/* + * 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 <stdio.h> +#include <string.h> +#include <windows.h> + +#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 |