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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
bool debug = false;
// process_flag_noarg( name, argc, argv );
// Process the -d flag, and check that there are no
// remaining arguments.
void process_flag_noarg( char *name, int argc, char **argv )
{
int arg=1;
if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
{
debug = true;
arg++;
}
int left = argc-arg;
if( left != 0 )
{
fprintf( stderr, "Usage: %s [-d]\n", name );
exit(1);
}
}
// int argno = process_flag_n_args( name, argc, argv, n, argmsg );
// Process the -d flag, and check that there are exactly
// n remaining arguments, return the index position of the first
// argument. If not, generate a fatal Usage error using the argmsg.
//
int process_flag_n_args( char *name, int argc, char **argv, int n, char *argmsg )
{
int arg=1;
if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
{
debug = true;
arg++;
}
int left = argc-arg;
if( left != n )
{
fprintf( stderr, "Usage: %s [-d] %s\n Exactly %d "
"arguments needed\n", name, argmsg, n );
exit(1);
}
return arg;
}
// int argno = process_flag_n_m_args( name, argc, argv, min, max, argmsg );
// Process the -d flag, and check that there are between
// min and max remaining arguments, return the index position of the first
// argument. If not, generate a fatal Usage error using the argmsg.
//
int process_flag_n_m_args( char *name, int argc, char **argv, int min, int max, char *argmsg )
{
int arg=1;
if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
{
debug = true;
arg++;
}
int left = argc-arg;
if( left < min || left > max )
{
fprintf( stderr, "Usage: %s [-d] %s\n Between %d and %d "
"arguments needed\n", name, argmsg, min, max );
exit(1);
}
return arg;
}
// process_onenumarg_default( name, argc, argv, defvalue, &n );
// Process the -d flag, and check that there is a single
// remaining numeric argument (or no arguments, in which case
// we use the defvalue), putting it into n
void process_onenumarg_default( char *name, int argc, char **argv, int defvalue, int *n )
{
char argmsg[100];
sprintf( argmsg, "[int default %d]", defvalue );
int arg = process_flag_n_m_args( name, argc, argv, 0, 1, argmsg );
*n = arg == argc ? defvalue : atoi( argv[arg] );
}
// process_onenumarg( name, argc, argv, &n );
// Process the -d flag, and check that there is a single
// remaining numeric argument, putting it into n
void process_onenumarg( char *name, int argc, char **argv, int *n )
{
int arg = process_flag_n_args( name, argc, argv, 1, "int" );
// argument is in argv[arg]
*n = atoi( argv[arg] );
}
// process_twonumargs( name, argc, argv, &m, &n );
// Process the -d flag, and check that there are 2
// remaining numeric arguments, putting them into m and n
void process_twonumargs( char *name, int argc, char **argv, int *m, int *n )
{
int arg = process_flag_n_args( name, argc, argv, 2, "int" );
// arguments are in argv[arg] and argv[arg+1]
*m = atoi( argv[arg++] );
*n = atoi( argv[arg] );
}
// process_twostrargs() IS DEPRECATED: use process_flag_n_m_args() instead
// int arr[100];
// int nel = process_listnumargs( name, argc, argv, arr, 100 );
// Process the -d flag, and check that there are >= 2
// remaining numeric arguments, putting them into arr[0..nel-1]
// and returning nel.
int process_listnumargs( char *name, int argc, char **argv, int *arr, int maxel )
{
int arg=1;
if( argc>1 && strcmp( argv[arg], "-d" ) == 0 )
{
debug = true;
arg++;
}
int left = argc-arg;
if( left < 2 )
{
fprintf( stderr, "Usage: %s [-d] list_of_numeric_args\n", name );
exit(1);
}
if( left > maxel )
{
fprintf( stderr, "%s: more than %d args\n", name, maxel );
exit(1);
}
// elements are in argv[arg], argv[arg+1]...
if( debug )
{
printf( "debug: remaining arguments are in arg=%d, "
"firstn=%s, secondn=%s..\n",
arg, argv[arg], argv[arg+1] );
}
int nel = 0;
for( int i=arg; i<argc; i++ )
{
arr[nel++] = atoi( argv[i] );
}
arr[nel] = -1;
return nel;
}
//
// bool isint = check_unsigned_int( char *val, int *n );
// Given an string val, check that there's an unsigned integer
// in it (after optional whitespace). If there is a valid
// unsigned integer value, store that integer value in *n and
// return true; otherwise return false (and don't alter *n).
bool check_unsigned_int( char *val, int *n )
{
// skip whitespace in val
char *p;
for( p=val; isspace(*p); p++ )
{
/*EMPTY*/
}
if( ! isdigit(*p) ) return false;
*n = atoi(p);
return true;
}
//
// bool ok = check_unsigned_real( char *val, double *n );
// Given an string val, check that there's an unsigned real
// in it (after optional whitespace). If there is a valid
// unsigned real value, store that value in *n and
// return true; otherwise return false (and don't alter *n).
bool check_unsigned_real( char *val, double *n )
{
// skip whitespace in val
char *p;
for( p=val; isspace(*p); p++ )
{
/*EMPTY*/
}
if( ! isdigit(*p) ) return false;
*n = atof(p);
return true;
}
|