forked from NewLunarFire/png2snes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparser.c
144 lines (118 loc) · 3.4 KB
/
argparser.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdlib.h>
#include <argp.h>
#include "argparser.h"
#define BINARY 1
#define BASENAME 2
/* Version and bugs address */
const char *argp_program_version = "png2snes beta";
const char *argp_program_bug_address = "<[email protected]>";
/* Program documentation. */
static char doc[] = "png2snes -- Create SNES Graphics from PNG files";
/* A description of the arguments we accept. */
static char args_doc[] = "INPUT_FILE";
/* The options we understand. */
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce more verbose output" },
{"quiet", 'q', 0, 0, "Don't produce any output" },
{"output", 'o', "BASENAME", 0, "Output to file instead of standard output" },
{"bitplanes", 'b', "PLANES", 0, "Number of bitplanes per tile (2,4 or 8)"},
{"tilesize", 't', "SIZE", 0, "Size of tiles (8x8 or 16x16)"},
{"binary", BINARY, 0, 0, "Output to binary format"},
{ 0 }
};
int parse_number(char* number)
{
char* endptr;
int val = strtol(number, &endptr, 0);
/* Check for various possible errors */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0))
return 0;
if (endptr == number)
return 0;
/* Return valid conversion result */
return (int)val;
}
int parse_tilesize(char* arg)
{
int ts = parse_number(arg);
/* Check for invalid values */
if(ts != 8 && ts != 16)
return 0;
return ts;
}
int parse_bitplanes(char* arg)
{
int bp = parse_number(arg);
/* Check for invalid values */
if(bp != 2 && bp != 4 && bp != 8)
return 0;
return bp;
}
/* Parse a single option. */
error_t parse_opt (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
switch (key)
{
case 'q': case 's':
arguments->verbose = 0;
break;
case 'v':
arguments->verbose = 1;
break;
case BINARY:
arguments->binary = 1;
break;
case 'o':
arguments->output_file = arg;
break;
case 'b':
arguments->bitplanes = parse_bitplanes(arg);
if(!arguments->bitplanes)
{
fprintf(stderr, "Invalid value for bitplanes: %s (Possible values are 2, 4 and 8)\n", arg);
argp_usage(state);
}
break;
case 't':
arguments->tilesize = parse_tilesize(arg);
if(!arguments->tilesize)
{
fprintf(stderr, "Invalid value for bitplanes: %s (Possible values are 8 and 16)\n", arg);
argp_usage(state);
}
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 1)
/* Too many arguments. */
argp_usage (state);
arguments->input_file = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 1)
/* Not enough arguments. */
argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
static struct argp argp = { options, parse_opt, args_doc, doc };
struct arguments parse_arguments(int argc, char **argv)
{
struct arguments arguments;
/* Default values. */
arguments.verbose = 1;
arguments.binary = 0;
arguments.output_file = "-";
arguments.bitplanes = 0;
arguments.tilesize = 0;
/* Parse our arguments; every option seen by parse_opt will
be reflected in arguments. */
argp_parse (&argp, argc, argv, 0, 0, &arguments);
return arguments;
}