summaryrefslogtreecommitdiff
path: root/source/fitz/getoptw.c
blob: 55b8d015280c88e61330230511fc54082b1a99e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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