ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/randomBuilder/randomBuilderCmd.cpp
Revision: 1798
Committed: Thu Sep 13 14:10:11 2012 UTC (12 years, 7 months ago) by gezelter
File size: 28736 byte(s)
Log Message:
Merged trunk changes into the development branch

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date