ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanoparticleBuilderCmd.cpp
Revision: 1630
Committed: Wed Sep 14 21:40:12 2011 UTC (13 years, 7 months ago) by gezelter
Content type: text/plain
Original Path: branches/development/src/applications/nanoparticleBuilder/nanoparticleBuilderCmd.c
File size: 31948 byte(s)
Log Message:
updating gengetopt runs

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date