-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.c
459 lines (429 loc) · 14.8 KB
/
args.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// src/aksl/args.c 2017-10-25 Alan U. Kennington.
/*-----------------------------------------------------------------------------
Copyright (C) 1989-2018, Alan U. Kennington.
You may distribute this software under the terms of Alan U. Kennington's
modified Artistic Licence, as specified in the accompanying LICENCE file.
-----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
Command line interpretation software.
Note: should also provide a generic function which takes a valuelist as a
parameter, and fills in the values from the command line.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Functions in this file:
get_arg0
get_arg0
get_arg
get_arg3
get_arg
get_arg
get_arg
get_arg
get_arg
get_arg
------------------------------------------------------------------------------*/
// AKSL header files:
#include "aksl/args.h"
using namespace std;
// System header files:
#ifndef AKSL_X_STRING_H
#define AKSL_X_STRING_H
#include <string.h>
#endif
#ifndef AKSL_X_IOSTREAM_H
#define AKSL_X_IOSTREAM_H
#include <iostream>
#endif
#ifndef AKSL_X_STDLIB_H
#define AKSL_X_STDLIB_H
#include <stdlib.h>
#endif
/*------------------------------------------------------------------------------
This version just reads a single argument. No parameter indicator is assumed.
------------------------------------------------------------------------------*/
//----------------------//
// get_arg0 //
//----------------------//
bool_enum get_arg0(int argc, char** argv, long& result) {
if (argc <= 0 || !argv) { // Ran out of arguments.
cout << flush;
cerr << "\nNo arguments.\n";
return false;
}
char* arg = *argv;
char* ptr = arg;
long ltmp = strtol(arg, &ptr, (int)0);
if (ptr == arg || *ptr) {
cout << flush;
cerr << "\nError in parameter.\n";
return false;
}
result = ltmp;
return true;
} // End of function get_arg0.
/*------------------------------------------------------------------------------
This version just reads a single argument. No parameter indicator is assumed.
------------------------------------------------------------------------------*/
//----------------------//
// get_arg0 //
//----------------------//
bool_enum get_arg0(int argc, char** argv, char*& result) {
if (argc <= 0 || !argv) { // Ran out of arguments.
cout << flush;
cerr << "\nNo arguments.\n";
return false;
}
char* arg = *argv;
int len = strlen(arg);
char* ptr = new char[len + 1];
strcpy(ptr, arg);
result = ptr;
return true;
} // End of function get_arg0.
//----------------------//
// get_arg //
//----------------------//
bool_enum get_arg(int& argc, char**& argv, long& result) {
// Repeat things which have probably already been done:
char* arg = *argv;
int len = strlen(arg);
char c0 = arg[0];
char c1 = arg[1];
if (len <= 2) {
if (--argc > 0)
arg = *++argv;
else { // Ran out of arguments.
// help(cerr);
cerr << "\nNo parameter given for option " << c0 << c1 << ".\n";
return false;
}
}
else { // No space between option and parameter.
arg += 2;
}
char* ptr = arg;
long ltmp = strtol(arg, &ptr, (int)0);
if (ptr == arg || *ptr) {
// help(cerr);
cerr << "\nError in parameter to option " << c0 << c1 << ".\n";
return false;
}
result = ltmp;
return true;
} // End of function get_arg.
//----------------------//
// get_arg3 //
//----------------------//
bool_enum get_arg3(int& argc, char**& argv, long& result) {
// Repeat things which have probably already been done:
char* arg = *argv;
char c0 = arg[0];
char c1 = arg[1];
char c2 = arg[2];
int len = strlen(arg);
if (len <= 3) {
if (--argc > 0)
arg = *++argv;
else { // Ran out of arguments.
cerr << "\nNo parameter given for option "
<< c0 << c1 << c2 << ".\n";
return false;
}
}
else { // No space between option and parameter.
arg += 3;
}
char* ptr = arg;
long ltmp = strtol(arg, &ptr, (int)0);
if (ptr == arg || *ptr) {
cerr << "\nError in parameter to option " << c0 << c1 << c2 << ".\n";
return false;
}
result = ltmp;
return true;
} // End of function get_arg3.
/*------------------------------------------------------------------------------
Interpret a command line portion of the form
-x <arg1> <arg2>
or
-x<arg1> <arg2>
where arg1 is a long integer and arg2 is a long integer.
This functions expects to be called with argv pointing to the command line
argument in which the "-x" was found, and argc equal to the number of command
line arguments left in the array argv. Both argc and argv are updated as the
arguments are read, so that they point to the second argument if the reading
is successful, otherwise they are unaltered or else point to the argument
containing the first character after the "-x".
Any two-character string may be used in place of "-x".
The return value is "true" if two arguments are successfully read. Otherwise the
return value is "false", and the values of "res1" and "res2" are left unaltered.
------------------------------------------------------------------------------*/
//----------------------//
// get_arg //
//----------------------//
bool_enum get_arg(int& argc, char**& argv, long& res1, long& res2) {
if (argc < 2 || !argv || !argv[0] || !argv[1])
return false;
// Repeat things which have probably already been done:
char* arg = argv[0];
if (!arg[0] || !arg[1])
return false;
char c0 = arg[0];
char c1 = arg[1];
if (!arg[2]) {
if (argc >= 3 && argv[2]) {
--argc;
arg = *++argv;
}
else { // Ran out of arguments.
cerr << "\nInsufficient parameters for option "
<< c0 << c1 << ".\n";
return false;
}
}
else { // No space between option and parameter.
arg += 2;
}
char* ptr = arg;
long ltmp1 = strtol(arg, &ptr, (int)0);
if (ptr == arg || *ptr) {
cerr << "\nError in first parameter to option " << c0 << c1 << ".\n";
return false;
}
arg = argv[1];
ptr = arg;
long ltmp2 = strtol(arg, &ptr, (int)0);
if (ptr == arg || *ptr) {
cerr << "\nError in second parameter to option " << c0 << c1 << ".\n";
return false;
}
res1 = ltmp1;
res2 = ltmp2;
--argc;
++argv;
return true;
} // End of function get_arg.
/*------------------------------------------------------------------------------
Interpret a command line portion of the form
-x <arg1> <arg2>
or
-x<arg1> <arg2>
where arg1 is a long integer and arg2 is a double float.
This functions expects to be called with argv pointing to the command line
argument in which the "-x" was found, and argc equal to the number of command
line arguments left in the array argv. Both argc and argv are updated as the
arguments are read, so that they point to the second argument if the reading
is successful, otherwise they are unaltered or else point to the argument
containing the first character after the "-x".
Any two-character string may be used in place of "-x".
The return value is "true" if two arguments are successfully read. Otherwise the
return value is "false", and the values of "res1" and "res2" are left unaltered.
------------------------------------------------------------------------------*/
//----------------------//
// get_arg //
//----------------------//
bool_enum get_arg(int& argc, char**& argv, long& res1, double& res2) {
if (argc < 2 || !argv || !argv[0] || !argv[1])
return false;
// Repeat things which have probably already been done:
char* arg = argv[0];
if (!arg[0] || !arg[1])
return false;
char c0 = arg[0];
char c1 = arg[1];
if (!arg[2]) {
if (argc >= 3 && argv[2]) {
--argc;
arg = *++argv;
}
else { // Ran out of arguments.
cerr << "\nInsufficient parameters for option "
<< c0 << c1 << ".\n";
return false;
}
}
else { // No space between option and parameter.
arg += 2;
}
char* ptr = arg;
long tmp1 = strtol(arg, &ptr, (int)0);
if (ptr == arg || *ptr) {
cerr << "\nError in first parameter to option " << c0 << c1 << ".\n";
return false;
}
arg = argv[1];
ptr = arg;
double tmp2 = strtod(arg, &ptr);
if (ptr == arg || *ptr) {
cerr << "\nError in second parameter to option " << c0 << c1 << ".\n";
return false;
}
res1 = tmp1;
res2 = tmp2;
--argc;
++argv;
return true;
} // End of function get_arg.
/*------------------------------------------------------------------------------
This function gets a double parameter to the option *argv[0] *argv[1]. That is,
it is assumed that the first two characters of *argv are of the form "-c", or
some such option specifier. And then a double parameter for this option is to be
found. If all is okay, d is set to the parameter, and "true" is returned.
------------------------------------------------------------------------------*/
//----------------------//
// get_arg //
//----------------------//
bool_enum get_arg(int& argc, char**& argv, double& result) {
// Repeat things which have probably already been done:
char* arg = *argv;
int len = strlen(arg);
char c0 = arg[0];
char c1 = arg[1];
if (len <= 2) {
if (--argc > 0)
arg = *++argv;
else { // Ran out of arguments.
// help(cerr);
cerr << "\nNo parameter given for option " << c0 << c1 << ".\n";
return false;
}
}
else { // No space between option and parameter.
arg += 2;
}
char* ptr = arg;
double dtmp = strtod(arg, &ptr);
if (ptr == arg || *ptr) {
// help(cerr);
cerr << "\nError in parameter to option " << c0 << c1 << ".\n";
return false;
}
result = dtmp;
return true;
} // End of function get_arg.
/*------------------------------------------------------------------------------
This function gets a string parameter to the option *argv[0] *argv[1]. That is,
it is assumed that the first two characters of *argv are of the form "-c", or
some such option specifier. And then a string parameter for this option is to be
found. If all is okay, "result" is set to the parameter, and "true" is returned.
Otherwise "result" is not altered.
The result string is heap-allocated.
------------------------------------------------------------------------------*/
//----------------------//
// get_arg //
//----------------------//
bool_enum get_arg(int& argc, char**& argv, char*& result) {
// Repeat things which have probably already been done:
char* arg = *argv;
int len = strlen(arg);
char c0 = arg[0];
char c1 = arg[1];
if (len <= 2) {
if (--argc <= 0) {
// Ran out of arguments.
cerr << "\nNo parameter given for option " << c0 << c1 << ".\n";
return false;
}
arg = *++argv;
len = strlen(arg);
}
else { // No space between option and parameter.
arg += 2;
len -= 2;
}
char* ptr = new char[len + 1];
strcpy(ptr, arg);
result = ptr;
return true;
} // End of function get_arg.
/*------------------------------------------------------------------------------
This function gets a string argument and an integer argument to the option
*argv[0] *argv[1]. That is, it is assumed that the first two characters of *argv
are of the form "-c", or some such option specifier. etc. etc.
The result1 string is heap-allocated.
------------------------------------------------------------------------------*/
//----------------------//
// get_arg //
//----------------------//
bool_enum get_arg(int& argc, char**& argv, char*& result1, long& result2) {
if (argc < 2 || !argv || !argv[0] || !argv[1])
return false;
// Repeat things which have probably already been done:
char* arg = argv[0];
if (!arg[0] || !arg[1])
return false;
char c0 = arg[0];
char c1 = arg[1];
if (!arg[2]) {
if (argc >= 3 && argv[2]) {
--argc;
arg = *++argv;
}
else { // Ran out of arguments.
cerr << "\nInsufficient parameters for option "
<< c0 << c1 << ".\n";
return false;
}
}
else { // No space between option and parameter.
arg += 2;
}
char* ptr = new char[strlen(arg) + 1];
strcpy(ptr, arg);
result1 = ptr;
arg = argv[1];
ptr = arg;
long ltmp = strtol(arg, &ptr, (int)0);
if (ptr == arg || *ptr) {
cerr << "\nError in parameter to option " << c0 << c1 << ".\n";
return false;
}
result2 = ltmp;
--argc;
++argv;
return true;
} // End of function get_arg.
/*------------------------------------------------------------------------------
This function gets two string arguments to the option *argv[0] *argv[1]. That
is, it is assumed that the first two characters of *argv are of the form "-c",
or some such option specifier. And then two string arguments for this option is
to be found. If all is okay, "result1" and "result2" are set to the arguments,
and "true" is returned. Otherwise "result1" and "result2" are not altered.
The result strings are heap-allocated.
------------------------------------------------------------------------------*/
//----------------------//
// get_arg //
//----------------------//
bool_enum get_arg(int& argc, char**& argv, char*& result1, char*& result2) {
if (argc < 2 || !argv || !argv[0] || !argv[1])
return false;
// Repeat things which have probably already been done:
char* arg = argv[0];
if (!arg[0] || !arg[1])
return false;
char c0 = arg[0];
char c1 = arg[1];
if (!arg[2]) {
if (argc >= 3 && argv[2]) {
--argc;
arg = *++argv;
}
else { // Ran out of arguments.
cerr << "\nInsufficient parameters for option "
<< c0 << c1 << ".\n";
return false;
}
}
else { // No space between option and parameter.
arg += 2;
}
char* ptr = new char[strlen(arg) + 1];
strcpy(ptr, arg);
result1 = ptr;
arg = argv[1];
ptr = new char[strlen(arg) + 1];
strcpy(ptr, arg);
result2 = ptr;
--argc;
++argv;
return true;
} // End of function get_arg.