ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/randomBuilder/randomBuilderCmd.cpp
Revision: 1655
Committed: Mon Oct 3 20:32:45 2011 UTC (13 years, 7 months ago) by gezelter
File size: 28681 byte(s)
Log Message:
Replacing most of the C-code with C++ code, and migrating the config.h
file into the PROJECT_BINARY_DIR so that we can have multiple builds
on the same architecture.

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

Properties

Name Value
svn:keywords Author Id Revision Date