ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanorodBuilderCmd.cpp
Revision: 1876
Committed: Fri May 17 17:10:11 2013 UTC (11 years, 11 months ago) by gezelter
File size: 33968 byte(s)
Log Message:
Compilation and portability fixes

File Contents

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