1 |
/* |
2 |
File autogenerated by gengetopt version 2.22.4 |
3 |
generated with the following command: |
4 |
gengetopt -F nanoparticleBuilderCmd -u |
5 |
|
6 |
The developers of gengetopt consider the fixed text that goes in all |
7 |
gengetopt output files to be in the public domain: |
8 |
we make no copyright claims on it. |
9 |
*/ |
10 |
|
11 |
/* If we use autoconf. */ |
12 |
#ifdef HAVE_CONFIG_H |
13 |
#include "config.h" |
14 |
#endif |
15 |
|
16 |
#include <stdio.h> |
17 |
#include <stdlib.h> |
18 |
#include <string.h> |
19 |
|
20 |
#ifndef FIX_UNUSED |
21 |
#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */ |
22 |
#endif |
23 |
|
24 |
#ifdef WIN32 |
25 |
#include "utils/wingetopt.h" |
26 |
#else |
27 |
#include <getopt.h> |
28 |
#endif |
29 |
|
30 |
#include "nanoparticleBuilderCmd.h" |
31 |
|
32 |
const char *gengetopt_args_info_purpose = "Builds spherical random or core-shell nanoparticles and outputs an OpenMD \nstartup file"; |
33 |
|
34 |
const char *gengetopt_args_info_usage = "Usage: nanoparticleBuilder [OPTIONS]... [FILES]..."; |
35 |
|
36 |
const char *gengetopt_args_info_description = ""; |
37 |
|
38 |
const char *gengetopt_args_info_help[] = { |
39 |
" -h, --help Print help and exit", |
40 |
" -V, --version Print version and exit", |
41 |
" -o, --output=STRING Output file name", |
42 |
" --latticeConstant=DOUBLE Lattice spacing in Angstroms for cubic lattice.", |
43 |
" --radius=DOUBLE Nanoparticle radius in Angstroms", |
44 |
" --shellRadius=DOUBLE Radius containing within it only molecules of a \n specific component. Specified for each \n component > 1 in the template file.", |
45 |
" --molFraction=DOUBLE Builds a multi-component random alloy \n nanoparticle. A mole Fraction must be \n specified for each component > 1 in the \n template file.", |
46 |
" --vacancyPercent=DOUBLE Percentage of atoms to remove from within \n vacancy range", |
47 |
" --vacancyInnerRadius=DOUBLE\n Radius arround core-shell where vacancies \n should be located.", |
48 |
" --vacancyOuterRadius=DOUBLE\n Radius arround core-shell where vacancies \n should be located.", |
49 |
0 |
50 |
}; |
51 |
|
52 |
typedef enum {ARG_NO |
53 |
, ARG_STRING |
54 |
, ARG_DOUBLE |
55 |
} cmdline_parser_arg_type; |
56 |
|
57 |
static |
58 |
void clear_given (struct gengetopt_args_info *args_info); |
59 |
static |
60 |
void clear_args (struct gengetopt_args_info *args_info); |
61 |
|
62 |
static int |
63 |
cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info, |
64 |
struct cmdline_parser_params *params, const char *additional_error); |
65 |
|
66 |
static int |
67 |
cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error); |
68 |
|
69 |
static char * |
70 |
gengetopt_strdup (const char *s); |
71 |
|
72 |
static |
73 |
void clear_given (struct gengetopt_args_info *args_info) |
74 |
{ |
75 |
args_info->help_given = 0 ; |
76 |
args_info->version_given = 0 ; |
77 |
args_info->output_given = 0 ; |
78 |
args_info->latticeConstant_given = 0 ; |
79 |
args_info->radius_given = 0 ; |
80 |
args_info->shellRadius_given = 0 ; |
81 |
args_info->molFraction_given = 0 ; |
82 |
args_info->vacancyPercent_given = 0 ; |
83 |
args_info->vacancyInnerRadius_given = 0 ; |
84 |
args_info->vacancyOuterRadius_given = 0 ; |
85 |
} |
86 |
|
87 |
static |
88 |
void clear_args (struct gengetopt_args_info *args_info) |
89 |
{ |
90 |
FIX_UNUSED (args_info); |
91 |
args_info->output_arg = NULL; |
92 |
args_info->output_orig = NULL; |
93 |
args_info->latticeConstant_orig = NULL; |
94 |
args_info->radius_orig = NULL; |
95 |
args_info->shellRadius_arg = NULL; |
96 |
args_info->shellRadius_orig = NULL; |
97 |
args_info->molFraction_arg = NULL; |
98 |
args_info->molFraction_orig = NULL; |
99 |
args_info->vacancyPercent_orig = NULL; |
100 |
args_info->vacancyInnerRadius_orig = NULL; |
101 |
args_info->vacancyOuterRadius_orig = NULL; |
102 |
|
103 |
} |
104 |
|
105 |
static |
106 |
void init_args_info(struct gengetopt_args_info *args_info) |
107 |
{ |
108 |
|
109 |
|
110 |
args_info->help_help = gengetopt_args_info_help[0] ; |
111 |
args_info->version_help = gengetopt_args_info_help[1] ; |
112 |
args_info->output_help = gengetopt_args_info_help[2] ; |
113 |
args_info->latticeConstant_help = gengetopt_args_info_help[3] ; |
114 |
args_info->radius_help = gengetopt_args_info_help[4] ; |
115 |
args_info->shellRadius_help = gengetopt_args_info_help[5] ; |
116 |
args_info->shellRadius_min = 0; |
117 |
args_info->shellRadius_max = 0; |
118 |
args_info->molFraction_help = gengetopt_args_info_help[6] ; |
119 |
args_info->molFraction_min = 0; |
120 |
args_info->molFraction_max = 0; |
121 |
args_info->vacancyPercent_help = gengetopt_args_info_help[7] ; |
122 |
args_info->vacancyInnerRadius_help = gengetopt_args_info_help[8] ; |
123 |
args_info->vacancyOuterRadius_help = gengetopt_args_info_help[9] ; |
124 |
|
125 |
} |
126 |
|
127 |
void |
128 |
cmdline_parser_print_version (void) |
129 |
{ |
130 |
printf ("%s %s\n", |
131 |
(strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), |
132 |
CMDLINE_PARSER_VERSION); |
133 |
} |
134 |
|
135 |
static void print_help_common(void) { |
136 |
cmdline_parser_print_version (); |
137 |
|
138 |
if (strlen(gengetopt_args_info_purpose) > 0) |
139 |
printf("\n%s\n", gengetopt_args_info_purpose); |
140 |
|
141 |
if (strlen(gengetopt_args_info_usage) > 0) |
142 |
printf("\n%s\n", gengetopt_args_info_usage); |
143 |
|
144 |
printf("\n"); |
145 |
|
146 |
if (strlen(gengetopt_args_info_description) > 0) |
147 |
printf("%s\n\n", gengetopt_args_info_description); |
148 |
} |
149 |
|
150 |
void |
151 |
cmdline_parser_print_help (void) |
152 |
{ |
153 |
int i = 0; |
154 |
print_help_common(); |
155 |
while (gengetopt_args_info_help[i]) |
156 |
printf("%s\n", gengetopt_args_info_help[i++]); |
157 |
} |
158 |
|
159 |
void |
160 |
cmdline_parser_init (struct gengetopt_args_info *args_info) |
161 |
{ |
162 |
clear_given (args_info); |
163 |
clear_args (args_info); |
164 |
init_args_info (args_info); |
165 |
|
166 |
args_info->inputs = 0; |
167 |
args_info->inputs_num = 0; |
168 |
} |
169 |
|
170 |
void |
171 |
cmdline_parser_params_init(struct cmdline_parser_params *params) |
172 |
{ |
173 |
if (params) |
174 |
{ |
175 |
params->override = 0; |
176 |
params->initialize = 1; |
177 |
params->check_required = 1; |
178 |
params->check_ambiguity = 0; |
179 |
params->print_errors = 1; |
180 |
} |
181 |
} |
182 |
|
183 |
struct cmdline_parser_params * |
184 |
cmdline_parser_params_create(void) |
185 |
{ |
186 |
struct cmdline_parser_params *params = |
187 |
(struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params)); |
188 |
cmdline_parser_params_init(params); |
189 |
return params; |
190 |
} |
191 |
|
192 |
static void |
193 |
free_string_field (char **s) |
194 |
{ |
195 |
if (*s) |
196 |
{ |
197 |
free (*s); |
198 |
*s = 0; |
199 |
} |
200 |
} |
201 |
|
202 |
/** @brief generic value variable */ |
203 |
union generic_value { |
204 |
double double_arg; |
205 |
char *string_arg; |
206 |
const char *default_string_arg; |
207 |
}; |
208 |
|
209 |
/** @brief holds temporary values for multiple options */ |
210 |
struct generic_list |
211 |
{ |
212 |
union generic_value arg; |
213 |
char *orig; |
214 |
struct generic_list *next; |
215 |
}; |
216 |
|
217 |
/** |
218 |
* @brief add a node at the head of the list |
219 |
*/ |
220 |
static void add_node(struct generic_list **list) { |
221 |
struct generic_list *new_node = (struct generic_list *) malloc (sizeof (struct generic_list)); |
222 |
new_node->next = *list; |
223 |
*list = new_node; |
224 |
new_node->arg.string_arg = 0; |
225 |
new_node->orig = 0; |
226 |
} |
227 |
|
228 |
/** |
229 |
* The passed arg parameter is NOT set to 0 from this function |
230 |
*/ |
231 |
static void |
232 |
free_multiple_field(unsigned int len, void *arg, char ***orig) |
233 |
{ |
234 |
unsigned int i; |
235 |
if (arg) { |
236 |
for (i = 0; i < len; ++i) |
237 |
{ |
238 |
free_string_field(&((*orig)[i])); |
239 |
} |
240 |
|
241 |
free (arg); |
242 |
free (*orig); |
243 |
*orig = 0; |
244 |
} |
245 |
} |
246 |
|
247 |
|
248 |
static void |
249 |
cmdline_parser_release (struct gengetopt_args_info *args_info) |
250 |
{ |
251 |
unsigned int i; |
252 |
free_string_field (&(args_info->output_arg)); |
253 |
free_string_field (&(args_info->output_orig)); |
254 |
free_string_field (&(args_info->latticeConstant_orig)); |
255 |
free_string_field (&(args_info->radius_orig)); |
256 |
free_multiple_field (args_info->shellRadius_given, (void *)(args_info->shellRadius_arg), &(args_info->shellRadius_orig)); |
257 |
args_info->shellRadius_arg = 0; |
258 |
free_multiple_field (args_info->molFraction_given, (void *)(args_info->molFraction_arg), &(args_info->molFraction_orig)); |
259 |
args_info->molFraction_arg = 0; |
260 |
free_string_field (&(args_info->vacancyPercent_orig)); |
261 |
free_string_field (&(args_info->vacancyInnerRadius_orig)); |
262 |
free_string_field (&(args_info->vacancyOuterRadius_orig)); |
263 |
|
264 |
|
265 |
for (i = 0; i < args_info->inputs_num; ++i) |
266 |
free (args_info->inputs [i]); |
267 |
|
268 |
if (args_info->inputs_num) |
269 |
free (args_info->inputs); |
270 |
|
271 |
clear_given (args_info); |
272 |
} |
273 |
|
274 |
|
275 |
static void |
276 |
write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[]) |
277 |
{ |
278 |
FIX_UNUSED (values); |
279 |
if (arg) { |
280 |
fprintf(outfile, "%s=\"%s\"\n", opt, arg); |
281 |
} else { |
282 |
fprintf(outfile, "%s\n", opt); |
283 |
} |
284 |
} |
285 |
|
286 |
static void |
287 |
write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[]) |
288 |
{ |
289 |
int i; |
290 |
|
291 |
for (i = 0; i < len; ++i) |
292 |
write_into_file(outfile, opt, (arg ? arg[i] : 0), values); |
293 |
} |
294 |
|
295 |
int |
296 |
cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info) |
297 |
{ |
298 |
int i = 0; |
299 |
|
300 |
if (!outfile) |
301 |
{ |
302 |
fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE); |
303 |
return EXIT_FAILURE; |
304 |
} |
305 |
|
306 |
if (args_info->help_given) |
307 |
write_into_file(outfile, "help", 0, 0 ); |
308 |
if (args_info->version_given) |
309 |
write_into_file(outfile, "version", 0, 0 ); |
310 |
if (args_info->output_given) |
311 |
write_into_file(outfile, "output", args_info->output_orig, 0); |
312 |
if (args_info->latticeConstant_given) |
313 |
write_into_file(outfile, "latticeConstant", args_info->latticeConstant_orig, 0); |
314 |
if (args_info->radius_given) |
315 |
write_into_file(outfile, "radius", args_info->radius_orig, 0); |
316 |
write_multiple_into_file(outfile, args_info->shellRadius_given, "shellRadius", args_info->shellRadius_orig, 0); |
317 |
write_multiple_into_file(outfile, args_info->molFraction_given, "molFraction", args_info->molFraction_orig, 0); |
318 |
if (args_info->vacancyPercent_given) |
319 |
write_into_file(outfile, "vacancyPercent", args_info->vacancyPercent_orig, 0); |
320 |
if (args_info->vacancyInnerRadius_given) |
321 |
write_into_file(outfile, "vacancyInnerRadius", args_info->vacancyInnerRadius_orig, 0); |
322 |
if (args_info->vacancyOuterRadius_given) |
323 |
write_into_file(outfile, "vacancyOuterRadius", args_info->vacancyOuterRadius_orig, 0); |
324 |
|
325 |
|
326 |
i = EXIT_SUCCESS; |
327 |
return i; |
328 |
} |
329 |
|
330 |
int |
331 |
cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) |
332 |
{ |
333 |
FILE *outfile; |
334 |
int i = 0; |
335 |
|
336 |
outfile = fopen(filename, "w"); |
337 |
|
338 |
if (!outfile) |
339 |
{ |
340 |
fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); |
341 |
return EXIT_FAILURE; |
342 |
} |
343 |
|
344 |
i = cmdline_parser_dump(outfile, args_info); |
345 |
fclose (outfile); |
346 |
|
347 |
return i; |
348 |
} |
349 |
|
350 |
void |
351 |
cmdline_parser_free (struct gengetopt_args_info *args_info) |
352 |
{ |
353 |
cmdline_parser_release (args_info); |
354 |
} |
355 |
|
356 |
/** @brief replacement of strdup, which is not standard */ |
357 |
char * |
358 |
gengetopt_strdup (const char *s) |
359 |
{ |
360 |
char *result = 0; |
361 |
if (!s) |
362 |
return result; |
363 |
|
364 |
result = (char*)malloc(strlen(s) + 1); |
365 |
if (result == (char*)0) |
366 |
return (char*)0; |
367 |
strcpy(result, s); |
368 |
return result; |
369 |
} |
370 |
|
371 |
static char * |
372 |
get_multiple_arg_token(const char *arg) |
373 |
{ |
374 |
const char *tok; |
375 |
char *ret; |
376 |
size_t len, num_of_escape, i, j; |
377 |
|
378 |
if (!arg) |
379 |
return 0; |
380 |
|
381 |
tok = strchr (arg, ','); |
382 |
num_of_escape = 0; |
383 |
|
384 |
/* make sure it is not escaped */ |
385 |
while (tok) |
386 |
{ |
387 |
if (*(tok-1) == '\\') |
388 |
{ |
389 |
/* find the next one */ |
390 |
tok = strchr (tok+1, ','); |
391 |
++num_of_escape; |
392 |
} |
393 |
else |
394 |
break; |
395 |
} |
396 |
|
397 |
if (tok) |
398 |
len = (size_t)(tok - arg + 1); |
399 |
else |
400 |
len = strlen (arg) + 1; |
401 |
|
402 |
len -= num_of_escape; |
403 |
|
404 |
ret = (char *) malloc (len); |
405 |
|
406 |
i = 0; |
407 |
j = 0; |
408 |
while (arg[i] && (j < len-1)) |
409 |
{ |
410 |
if (arg[i] == '\\' && |
411 |
arg[ i + 1 ] && |
412 |
arg[ i + 1 ] == ',') |
413 |
++i; |
414 |
|
415 |
ret[j++] = arg[i++]; |
416 |
} |
417 |
|
418 |
ret[len-1] = '\0'; |
419 |
|
420 |
return ret; |
421 |
} |
422 |
|
423 |
static const char * |
424 |
get_multiple_arg_token_next(const char *arg) |
425 |
{ |
426 |
const char *tok; |
427 |
|
428 |
if (!arg) |
429 |
return 0; |
430 |
|
431 |
tok = strchr (arg, ','); |
432 |
|
433 |
/* make sure it is not escaped */ |
434 |
while (tok) |
435 |
{ |
436 |
if (*(tok-1) == '\\') |
437 |
{ |
438 |
/* find the next one */ |
439 |
tok = strchr (tok+1, ','); |
440 |
} |
441 |
else |
442 |
break; |
443 |
} |
444 |
|
445 |
if (! tok || strlen(tok) == 1) |
446 |
return 0; |
447 |
|
448 |
return tok+1; |
449 |
} |
450 |
|
451 |
static int |
452 |
check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc); |
453 |
|
454 |
int |
455 |
check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc) |
456 |
{ |
457 |
int error = 0; |
458 |
|
459 |
if (option_given && (min > 0 || max > 0)) |
460 |
{ |
461 |
if (min > 0 && max > 0) |
462 |
{ |
463 |
if (min == max) |
464 |
{ |
465 |
/* specific occurrences */ |
466 |
if (option_given != (unsigned int) min) |
467 |
{ |
468 |
fprintf (stderr, "%s: %s option occurrences must be %d\n", |
469 |
prog_name, option_desc, min); |
470 |
error = 1; |
471 |
} |
472 |
} |
473 |
else if (option_given < (unsigned int) min |
474 |
|| option_given > (unsigned int) max) |
475 |
{ |
476 |
/* range occurrences */ |
477 |
fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n", |
478 |
prog_name, option_desc, min, max); |
479 |
error = 1; |
480 |
} |
481 |
} |
482 |
else if (min > 0) |
483 |
{ |
484 |
/* at least check */ |
485 |
if (option_given < min) |
486 |
{ |
487 |
fprintf (stderr, "%s: %s option occurrences must be at least %d\n", |
488 |
prog_name, option_desc, min); |
489 |
error = 1; |
490 |
} |
491 |
} |
492 |
else if (max > 0) |
493 |
{ |
494 |
/* at most check */ |
495 |
if (option_given > max) |
496 |
{ |
497 |
fprintf (stderr, "%s: %s option occurrences must be at most %d\n", |
498 |
prog_name, option_desc, max); |
499 |
error = 1; |
500 |
} |
501 |
} |
502 |
} |
503 |
|
504 |
return error; |
505 |
} |
506 |
int |
507 |
cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info) |
508 |
{ |
509 |
return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); |
510 |
} |
511 |
|
512 |
int |
513 |
cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info, |
514 |
struct cmdline_parser_params *params) |
515 |
{ |
516 |
int result; |
517 |
result = cmdline_parser_internal (argc, argv, args_info, params, 0); |
518 |
|
519 |
if (result == EXIT_FAILURE) |
520 |
{ |
521 |
cmdline_parser_free (args_info); |
522 |
exit (EXIT_FAILURE); |
523 |
} |
524 |
|
525 |
return result; |
526 |
} |
527 |
|
528 |
int |
529 |
cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) |
530 |
{ |
531 |
int result; |
532 |
struct cmdline_parser_params params; |
533 |
|
534 |
params.override = override; |
535 |
params.initialize = initialize; |
536 |
params.check_required = check_required; |
537 |
params.check_ambiguity = 0; |
538 |
params.print_errors = 1; |
539 |
|
540 |
result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0); |
541 |
|
542 |
if (result == EXIT_FAILURE) |
543 |
{ |
544 |
cmdline_parser_free (args_info); |
545 |
exit (EXIT_FAILURE); |
546 |
} |
547 |
|
548 |
return result; |
549 |
} |
550 |
|
551 |
int |
552 |
cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) |
553 |
{ |
554 |
int result = EXIT_SUCCESS; |
555 |
|
556 |
if (cmdline_parser_required2(args_info, prog_name, 0) > 0) |
557 |
result = EXIT_FAILURE; |
558 |
|
559 |
if (result == EXIT_FAILURE) |
560 |
{ |
561 |
cmdline_parser_free (args_info); |
562 |
exit (EXIT_FAILURE); |
563 |
} |
564 |
|
565 |
return result; |
566 |
} |
567 |
|
568 |
int |
569 |
cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error) |
570 |
{ |
571 |
int error = 0; |
572 |
FIX_UNUSED (additional_error); |
573 |
|
574 |
/* checks for required options */ |
575 |
if (! args_info->output_given) |
576 |
{ |
577 |
fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : "")); |
578 |
error = 1; |
579 |
} |
580 |
|
581 |
if (! args_info->latticeConstant_given) |
582 |
{ |
583 |
fprintf (stderr, "%s: '--latticeConstant' option required%s\n", prog_name, (additional_error ? additional_error : "")); |
584 |
error = 1; |
585 |
} |
586 |
|
587 |
if (! args_info->radius_given) |
588 |
{ |
589 |
fprintf (stderr, "%s: '--radius' option required%s\n", prog_name, (additional_error ? additional_error : "")); |
590 |
error = 1; |
591 |
} |
592 |
|
593 |
if (check_multiple_option_occurrences(prog_name, args_info->shellRadius_given, args_info->shellRadius_min, args_info->shellRadius_max, "'--shellRadius'")) |
594 |
error = 1; |
595 |
|
596 |
if (check_multiple_option_occurrences(prog_name, args_info->molFraction_given, args_info->molFraction_min, args_info->molFraction_max, "'--molFraction'")) |
597 |
error = 1; |
598 |
|
599 |
|
600 |
/* checks for dependences among options */ |
601 |
|
602 |
return error; |
603 |
} |
604 |
|
605 |
|
606 |
static char *package_name = 0; |
607 |
|
608 |
/** |
609 |
* @brief updates an option |
610 |
* @param field the generic pointer to the field to update |
611 |
* @param orig_field the pointer to the orig field |
612 |
* @param field_given the pointer to the number of occurrence of this option |
613 |
* @param prev_given the pointer to the number of occurrence already seen |
614 |
* @param value the argument for this option (if null no arg was specified) |
615 |
* @param possible_values the possible values for this option (if specified) |
616 |
* @param default_value the default value (in case the option only accepts fixed values) |
617 |
* @param arg_type the type of this option |
618 |
* @param check_ambiguity @see cmdline_parser_params.check_ambiguity |
619 |
* @param override @see cmdline_parser_params.override |
620 |
* @param no_free whether to free a possible previous value |
621 |
* @param multiple_option whether this is a multiple option |
622 |
* @param long_opt the corresponding long option |
623 |
* @param short_opt the corresponding short option (or '-' if none) |
624 |
* @param additional_error possible further error specification |
625 |
*/ |
626 |
static |
627 |
int update_arg(void *field, char **orig_field, |
628 |
unsigned int *field_given, unsigned int *prev_given, |
629 |
char *value, const char *possible_values[], |
630 |
const char *default_value, |
631 |
cmdline_parser_arg_type arg_type, |
632 |
int check_ambiguity, int override, |
633 |
int no_free, int multiple_option, |
634 |
const char *long_opt, char short_opt, |
635 |
const char *additional_error) |
636 |
{ |
637 |
char *stop_char = 0; |
638 |
const char *val = value; |
639 |
int found; |
640 |
char **string_field; |
641 |
FIX_UNUSED (field); |
642 |
|
643 |
stop_char = 0; |
644 |
found = 0; |
645 |
|
646 |
if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given))) |
647 |
{ |
648 |
if (short_opt != '-') |
649 |
fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", |
650 |
package_name, long_opt, short_opt, |
651 |
(additional_error ? additional_error : "")); |
652 |
else |
653 |
fprintf (stderr, "%s: `--%s' option given more than once%s\n", |
654 |
package_name, long_opt, |
655 |
(additional_error ? additional_error : "")); |
656 |
return 1; /* failure */ |
657 |
} |
658 |
|
659 |
FIX_UNUSED (default_value); |
660 |
|
661 |
if (field_given && *field_given && ! override) |
662 |
return 0; |
663 |
if (prev_given) |
664 |
(*prev_given)++; |
665 |
if (field_given) |
666 |
(*field_given)++; |
667 |
if (possible_values) |
668 |
val = possible_values[found]; |
669 |
|
670 |
switch(arg_type) { |
671 |
case ARG_DOUBLE: |
672 |
if (val) *((double *)field) = strtod (val, &stop_char); |
673 |
break; |
674 |
case ARG_STRING: |
675 |
if (val) { |
676 |
string_field = (char **)field; |
677 |
if (!no_free && *string_field) |
678 |
free (*string_field); /* free previous string */ |
679 |
*string_field = gengetopt_strdup (val); |
680 |
} |
681 |
break; |
682 |
default: |
683 |
break; |
684 |
}; |
685 |
|
686 |
/* check numeric conversion */ |
687 |
switch(arg_type) { |
688 |
case ARG_DOUBLE: |
689 |
if (val && !(stop_char && *stop_char == '\0')) { |
690 |
fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val); |
691 |
return 1; /* failure */ |
692 |
} |
693 |
break; |
694 |
default: |
695 |
; |
696 |
}; |
697 |
|
698 |
/* store the original value */ |
699 |
switch(arg_type) { |
700 |
case ARG_NO: |
701 |
break; |
702 |
default: |
703 |
if (value && orig_field) { |
704 |
if (no_free) { |
705 |
*orig_field = value; |
706 |
} else { |
707 |
if (*orig_field) |
708 |
free (*orig_field); /* free previous string */ |
709 |
*orig_field = gengetopt_strdup (value); |
710 |
} |
711 |
} |
712 |
}; |
713 |
|
714 |
return 0; /* OK */ |
715 |
} |
716 |
|
717 |
/** |
718 |
* @brief store information about a multiple option in a temporary list |
719 |
* @param list where to (temporarily) store multiple options |
720 |
*/ |
721 |
static |
722 |
int update_multiple_arg_temp(struct generic_list **list, |
723 |
unsigned int *prev_given, const char *val, |
724 |
const char *possible_values[], const char *default_value, |
725 |
cmdline_parser_arg_type arg_type, |
726 |
const char *long_opt, char short_opt, |
727 |
const char *additional_error) |
728 |
{ |
729 |
/* store single arguments */ |
730 |
char *multi_token; |
731 |
const char *multi_next; |
732 |
|
733 |
if (arg_type == ARG_NO) { |
734 |
(*prev_given)++; |
735 |
return 0; /* OK */ |
736 |
} |
737 |
|
738 |
multi_token = get_multiple_arg_token(val); |
739 |
multi_next = get_multiple_arg_token_next (val); |
740 |
|
741 |
while (1) |
742 |
{ |
743 |
add_node (list); |
744 |
if (update_arg((void *)&((*list)->arg), &((*list)->orig), 0, |
745 |
prev_given, multi_token, possible_values, default_value, |
746 |
arg_type, 0, 1, 1, 1, long_opt, short_opt, additional_error)) { |
747 |
if (multi_token) free(multi_token); |
748 |
return 1; /* failure */ |
749 |
} |
750 |
|
751 |
if (multi_next) |
752 |
{ |
753 |
multi_token = get_multiple_arg_token(multi_next); |
754 |
multi_next = get_multiple_arg_token_next (multi_next); |
755 |
} |
756 |
else |
757 |
break; |
758 |
} |
759 |
|
760 |
return 0; /* OK */ |
761 |
} |
762 |
|
763 |
/** |
764 |
* @brief free the passed list (including possible string argument) |
765 |
*/ |
766 |
static |
767 |
void free_list(struct generic_list *list, short string_arg) |
768 |
{ |
769 |
if (list) { |
770 |
struct generic_list *tmp; |
771 |
while (list) |
772 |
{ |
773 |
tmp = list; |
774 |
if (string_arg && list->arg.string_arg) |
775 |
free (list->arg.string_arg); |
776 |
if (list->orig) |
777 |
free (list->orig); |
778 |
list = list->next; |
779 |
free (tmp); |
780 |
} |
781 |
} |
782 |
} |
783 |
|
784 |
/** |
785 |
* @brief updates a multiple option starting from the passed list |
786 |
*/ |
787 |
static |
788 |
void update_multiple_arg(void *field, char ***orig_field, |
789 |
unsigned int field_given, unsigned int prev_given, union generic_value *default_value, |
790 |
cmdline_parser_arg_type arg_type, |
791 |
struct generic_list *list) |
792 |
{ |
793 |
int i; |
794 |
struct generic_list *tmp; |
795 |
|
796 |
if (prev_given && list) { |
797 |
*orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *)); |
798 |
|
799 |
switch(arg_type) { |
800 |
case ARG_DOUBLE: |
801 |
*((double **)field) = (double *)realloc (*((double **)field), (field_given + prev_given) * sizeof (double)); break; |
802 |
case ARG_STRING: |
803 |
*((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break; |
804 |
default: |
805 |
break; |
806 |
}; |
807 |
|
808 |
for (i = (prev_given - 1); i >= 0; --i) |
809 |
{ |
810 |
tmp = list; |
811 |
|
812 |
switch(arg_type) { |
813 |
case ARG_DOUBLE: |
814 |
(*((double **)field))[i + field_given] = tmp->arg.double_arg; break; |
815 |
case ARG_STRING: |
816 |
(*((char ***)field))[i + field_given] = tmp->arg.string_arg; break; |
817 |
default: |
818 |
break; |
819 |
} |
820 |
(*orig_field) [i + field_given] = list->orig; |
821 |
list = list->next; |
822 |
free (tmp); |
823 |
} |
824 |
} else { /* set the default value */ |
825 |
if (default_value && ! field_given) { |
826 |
switch(arg_type) { |
827 |
case ARG_DOUBLE: |
828 |
if (! *((double **)field)) { |
829 |
*((double **)field) = (double *)malloc (sizeof (double)); |
830 |
(*((double **)field))[0] = default_value->double_arg; |
831 |
} |
832 |
break; |
833 |
case ARG_STRING: |
834 |
if (! *((char ***)field)) { |
835 |
*((char ***)field) = (char **)malloc (sizeof (char *)); |
836 |
(*((char ***)field))[0] = gengetopt_strdup(default_value->string_arg); |
837 |
} |
838 |
break; |
839 |
default: break; |
840 |
} |
841 |
if (!(*orig_field)) { |
842 |
*orig_field = (char **) malloc (sizeof (char *)); |
843 |
(*orig_field)[0] = 0; |
844 |
} |
845 |
} |
846 |
} |
847 |
} |
848 |
|
849 |
int |
850 |
cmdline_parser_internal ( |
851 |
int argc, char **argv, struct gengetopt_args_info *args_info, |
852 |
struct cmdline_parser_params *params, const char *additional_error) |
853 |
{ |
854 |
int c; /* Character of the parsed option. */ |
855 |
|
856 |
struct generic_list * shellRadius_list = NULL; |
857 |
struct generic_list * molFraction_list = NULL; |
858 |
int error = 0; |
859 |
struct gengetopt_args_info local_args_info; |
860 |
|
861 |
int override; |
862 |
int initialize; |
863 |
int check_required; |
864 |
int check_ambiguity; |
865 |
|
866 |
package_name = argv[0]; |
867 |
|
868 |
override = params->override; |
869 |
initialize = params->initialize; |
870 |
check_required = params->check_required; |
871 |
check_ambiguity = params->check_ambiguity; |
872 |
|
873 |
if (initialize) |
874 |
cmdline_parser_init (args_info); |
875 |
|
876 |
cmdline_parser_init (&local_args_info); |
877 |
|
878 |
optarg = 0; |
879 |
optind = 0; |
880 |
opterr = params->print_errors; |
881 |
optopt = '?'; |
882 |
|
883 |
while (1) |
884 |
{ |
885 |
int option_index = 0; |
886 |
|
887 |
static struct option long_options[] = { |
888 |
{ "help", 0, NULL, 'h' }, |
889 |
{ "version", 0, NULL, 'V' }, |
890 |
{ "output", 1, NULL, 'o' }, |
891 |
{ "latticeConstant", 1, NULL, 0 }, |
892 |
{ "radius", 1, NULL, 0 }, |
893 |
{ "shellRadius", 1, NULL, 0 }, |
894 |
{ "molFraction", 1, NULL, 0 }, |
895 |
{ "vacancyPercent", 1, NULL, 0 }, |
896 |
{ "vacancyInnerRadius", 1, NULL, 0 }, |
897 |
{ "vacancyOuterRadius", 1, NULL, 0 }, |
898 |
{ 0, 0, 0, 0 } |
899 |
}; |
900 |
|
901 |
c = getopt_long (argc, argv, "hVo:", long_options, &option_index); |
902 |
|
903 |
if (c == -1) break; /* Exit from `while (1)' loop. */ |
904 |
|
905 |
switch (c) |
906 |
{ |
907 |
case 'h': /* Print help and exit. */ |
908 |
cmdline_parser_print_help (); |
909 |
cmdline_parser_free (&local_args_info); |
910 |
exit (EXIT_SUCCESS); |
911 |
|
912 |
case 'V': /* Print version and exit. */ |
913 |
cmdline_parser_print_version (); |
914 |
cmdline_parser_free (&local_args_info); |
915 |
exit (EXIT_SUCCESS); |
916 |
|
917 |
case 'o': /* Output file name. */ |
918 |
|
919 |
|
920 |
if (update_arg( (void *)&(args_info->output_arg), |
921 |
&(args_info->output_orig), &(args_info->output_given), |
922 |
&(local_args_info.output_given), optarg, 0, 0, ARG_STRING, |
923 |
check_ambiguity, override, 0, 0, |
924 |
"output", 'o', |
925 |
additional_error)) |
926 |
goto failure; |
927 |
|
928 |
break; |
929 |
|
930 |
case 0: /* Long option with no short option */ |
931 |
/* Lattice spacing in Angstroms for cubic lattice.. */ |
932 |
if (strcmp (long_options[option_index].name, "latticeConstant") == 0) |
933 |
{ |
934 |
|
935 |
|
936 |
if (update_arg( (void *)&(args_info->latticeConstant_arg), |
937 |
&(args_info->latticeConstant_orig), &(args_info->latticeConstant_given), |
938 |
&(local_args_info.latticeConstant_given), optarg, 0, 0, ARG_DOUBLE, |
939 |
check_ambiguity, override, 0, 0, |
940 |
"latticeConstant", '-', |
941 |
additional_error)) |
942 |
goto failure; |
943 |
|
944 |
} |
945 |
/* Nanoparticle radius in Angstroms. */ |
946 |
else if (strcmp (long_options[option_index].name, "radius") == 0) |
947 |
{ |
948 |
|
949 |
|
950 |
if (update_arg( (void *)&(args_info->radius_arg), |
951 |
&(args_info->radius_orig), &(args_info->radius_given), |
952 |
&(local_args_info.radius_given), optarg, 0, 0, ARG_DOUBLE, |
953 |
check_ambiguity, override, 0, 0, |
954 |
"radius", '-', |
955 |
additional_error)) |
956 |
goto failure; |
957 |
|
958 |
} |
959 |
/* Radius containing within it only molecules of a specific component. Specified for each component > 1 in the template file.. */ |
960 |
else if (strcmp (long_options[option_index].name, "shellRadius") == 0) |
961 |
{ |
962 |
|
963 |
if (update_multiple_arg_temp(&shellRadius_list, |
964 |
&(local_args_info.shellRadius_given), optarg, 0, 0, ARG_DOUBLE, |
965 |
"shellRadius", '-', |
966 |
additional_error)) |
967 |
goto failure; |
968 |
|
969 |
} |
970 |
/* Builds a multi-component random alloy nanoparticle. A mole Fraction must be specified for each component > 1 in the template file.. */ |
971 |
else if (strcmp (long_options[option_index].name, "molFraction") == 0) |
972 |
{ |
973 |
|
974 |
if (update_multiple_arg_temp(&molFraction_list, |
975 |
&(local_args_info.molFraction_given), optarg, 0, 0, ARG_DOUBLE, |
976 |
"molFraction", '-', |
977 |
additional_error)) |
978 |
goto failure; |
979 |
|
980 |
} |
981 |
/* Percentage of atoms to remove from within vacancy range. */ |
982 |
else if (strcmp (long_options[option_index].name, "vacancyPercent") == 0) |
983 |
{ |
984 |
|
985 |
|
986 |
if (update_arg( (void *)&(args_info->vacancyPercent_arg), |
987 |
&(args_info->vacancyPercent_orig), &(args_info->vacancyPercent_given), |
988 |
&(local_args_info.vacancyPercent_given), optarg, 0, 0, ARG_DOUBLE, |
989 |
check_ambiguity, override, 0, 0, |
990 |
"vacancyPercent", '-', |
991 |
additional_error)) |
992 |
goto failure; |
993 |
|
994 |
} |
995 |
/* Radius arround core-shell where vacancies should be located.. */ |
996 |
else if (strcmp (long_options[option_index].name, "vacancyInnerRadius") == 0) |
997 |
{ |
998 |
|
999 |
|
1000 |
if (update_arg( (void *)&(args_info->vacancyInnerRadius_arg), |
1001 |
&(args_info->vacancyInnerRadius_orig), &(args_info->vacancyInnerRadius_given), |
1002 |
&(local_args_info.vacancyInnerRadius_given), optarg, 0, 0, ARG_DOUBLE, |
1003 |
check_ambiguity, override, 0, 0, |
1004 |
"vacancyInnerRadius", '-', |
1005 |
additional_error)) |
1006 |
goto failure; |
1007 |
|
1008 |
} |
1009 |
/* Radius arround core-shell where vacancies should be located.. */ |
1010 |
else if (strcmp (long_options[option_index].name, "vacancyOuterRadius") == 0) |
1011 |
{ |
1012 |
|
1013 |
|
1014 |
if (update_arg( (void *)&(args_info->vacancyOuterRadius_arg), |
1015 |
&(args_info->vacancyOuterRadius_orig), &(args_info->vacancyOuterRadius_given), |
1016 |
&(local_args_info.vacancyOuterRadius_given), optarg, 0, 0, ARG_DOUBLE, |
1017 |
check_ambiguity, override, 0, 0, |
1018 |
"vacancyOuterRadius", '-', |
1019 |
additional_error)) |
1020 |
goto failure; |
1021 |
|
1022 |
} |
1023 |
|
1024 |
break; |
1025 |
case '?': /* Invalid option. */ |
1026 |
/* `getopt_long' already printed an error message. */ |
1027 |
goto failure; |
1028 |
|
1029 |
default: /* bug: option not considered. */ |
1030 |
fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); |
1031 |
abort (); |
1032 |
} /* switch */ |
1033 |
} /* while */ |
1034 |
|
1035 |
|
1036 |
update_multiple_arg((void *)&(args_info->shellRadius_arg), |
1037 |
&(args_info->shellRadius_orig), args_info->shellRadius_given, |
1038 |
local_args_info.shellRadius_given, 0, |
1039 |
ARG_DOUBLE, shellRadius_list); |
1040 |
update_multiple_arg((void *)&(args_info->molFraction_arg), |
1041 |
&(args_info->molFraction_orig), args_info->molFraction_given, |
1042 |
local_args_info.molFraction_given, 0, |
1043 |
ARG_DOUBLE, molFraction_list); |
1044 |
|
1045 |
args_info->shellRadius_given += local_args_info.shellRadius_given; |
1046 |
local_args_info.shellRadius_given = 0; |
1047 |
args_info->molFraction_given += local_args_info.molFraction_given; |
1048 |
local_args_info.molFraction_given = 0; |
1049 |
|
1050 |
if (check_required) |
1051 |
{ |
1052 |
error += cmdline_parser_required2 (args_info, argv[0], additional_error); |
1053 |
} |
1054 |
|
1055 |
cmdline_parser_release (&local_args_info); |
1056 |
|
1057 |
if ( error ) |
1058 |
return (EXIT_FAILURE); |
1059 |
|
1060 |
if (optind < argc) |
1061 |
{ |
1062 |
int i = 0 ; |
1063 |
int found_prog_name = 0; |
1064 |
/* whether program name, i.e., argv[0], is in the remaining args |
1065 |
(this may happen with some implementations of getopt, |
1066 |
but surely not with the one included by gengetopt) */ |
1067 |
|
1068 |
i = optind; |
1069 |
while (i < argc) |
1070 |
if (argv[i++] == argv[0]) { |
1071 |
found_prog_name = 1; |
1072 |
break; |
1073 |
} |
1074 |
i = 0; |
1075 |
|
1076 |
args_info->inputs_num = argc - optind - found_prog_name; |
1077 |
args_info->inputs = |
1078 |
(char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; |
1079 |
while (optind < argc) |
1080 |
if (argv[optind++] != argv[0]) |
1081 |
args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ; |
1082 |
} |
1083 |
|
1084 |
return 0; |
1085 |
|
1086 |
failure: |
1087 |
free_list (shellRadius_list, 0 ); |
1088 |
free_list (molFraction_list, 0 ); |
1089 |
|
1090 |
cmdline_parser_release (&local_args_info); |
1091 |
return (EXIT_FAILURE); |
1092 |
} |