ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanoparticleBuilderCmd.cpp
Revision: 1465
Committed: Fri Jul 9 23:08:25 2010 UTC (14 years, 9 months ago) by chuckv
Content type: text/plain
Original Path: branches/development/src/applications/nanoparticleBuilder/nanoparticleBuilderCmd.c
File size: 31396 byte(s)
Log Message:
Creating busticated version of OpenMD

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date