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

File Contents

# User Rev Content
1 tim 12 /*
2 gezelter 1630 File autogenerated by gengetopt version 2.22.4
3 tim 12 generated with the following command:
4 gezelter 1630 gengetopt -F simpleBuilderCmd -u
5 tim 12
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 gezelter 1067 /* If we use autoconf. */
12     #ifdef HAVE_CONFIG_H
13     #include "config.h"
14     #endif
15 tim 12
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 tim 12
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 gezelter 1067 #include "simpleBuilderCmd.h"
31 tim 12
32 gezelter 1630 const char *gengetopt_args_info_purpose = "";
33    
34     const char *gengetopt_args_info_usage = "Usage: simpleBuilder [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     0
47     };
48    
49     typedef enum {ARG_NO
50     , ARG_STRING
51     , ARG_INT
52     , ARG_DOUBLE
53     } cmdline_parser_arg_type;
54    
55 gezelter 1067 static
56     void clear_given (struct gengetopt_args_info *args_info);
57     static
58     void clear_args (struct gengetopt_args_info *args_info);
59    
60     static int
61 gezelter 1630 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
62     struct cmdline_parser_params *params, const char *additional_error);
63 gezelter 1067
64     static int
65     cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
66    
67     static char *
68     gengetopt_strdup (const char *s);
69    
70     static
71     void clear_given (struct gengetopt_args_info *args_info)
72     {
73     args_info->help_given = 0 ;
74     args_info->version_given = 0 ;
75     args_info->output_given = 0 ;
76     args_info->density_given = 0 ;
77     args_info->nx_given = 0 ;
78     args_info->ny_given = 0 ;
79     args_info->nz_given = 0 ;
80     }
81    
82     static
83     void clear_args (struct gengetopt_args_info *args_info)
84     {
85 gezelter 1630 FIX_UNUSED (args_info);
86 gezelter 1067 args_info->output_arg = NULL;
87     args_info->output_orig = NULL;
88     args_info->density_orig = NULL;
89     args_info->nx_orig = NULL;
90     args_info->ny_orig = NULL;
91     args_info->nz_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    
108     }
109    
110 tim 12 void
111     cmdline_parser_print_version (void)
112     {
113 gezelter 1630 printf ("%s %s\n",
114     (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
115     CMDLINE_PARSER_VERSION);
116 tim 12 }
117    
118 gezelter 1630 static void print_help_common(void) {
119     cmdline_parser_print_version ();
120    
121     if (strlen(gengetopt_args_info_purpose) > 0)
122     printf("\n%s\n", gengetopt_args_info_purpose);
123    
124     if (strlen(gengetopt_args_info_usage) > 0)
125     printf("\n%s\n", gengetopt_args_info_usage);
126    
127     printf("\n");
128    
129     if (strlen(gengetopt_args_info_description) > 0)
130     printf("%s\n\n", gengetopt_args_info_description);
131     }
132    
133 tim 12 void
134     cmdline_parser_print_help (void)
135     {
136 gezelter 1630 int i = 0;
137     print_help_common();
138     while (gengetopt_args_info_help[i])
139     printf("%s\n", gengetopt_args_info_help[i++]);
140 tim 12 }
141    
142 gezelter 1067 void
143     cmdline_parser_init (struct gengetopt_args_info *args_info)
144     {
145     clear_given (args_info);
146     clear_args (args_info);
147 gezelter 1630 init_args_info (args_info);
148 tim 12
149 gezelter 1630 args_info->inputs = 0;
150 gezelter 1067 args_info->inputs_num = 0;
151     }
152 tim 12
153 gezelter 1630 void
154     cmdline_parser_params_init(struct cmdline_parser_params *params)
155     {
156     if (params)
157     {
158     params->override = 0;
159     params->initialize = 1;
160     params->check_required = 1;
161     params->check_ambiguity = 0;
162     params->print_errors = 1;
163     }
164     }
165    
166     struct cmdline_parser_params *
167     cmdline_parser_params_create(void)
168     {
169     struct cmdline_parser_params *params =
170     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
171     cmdline_parser_params_init(params);
172     return params;
173     }
174    
175 gezelter 1067 static void
176 gezelter 1630 free_string_field (char **s)
177     {
178     if (*s)
179     {
180     free (*s);
181     *s = 0;
182     }
183     }
184    
185    
186     static void
187 gezelter 1067 cmdline_parser_release (struct gengetopt_args_info *args_info)
188     {
189     unsigned int i;
190 gezelter 1630 free_string_field (&(args_info->output_arg));
191     free_string_field (&(args_info->output_orig));
192     free_string_field (&(args_info->density_orig));
193     free_string_field (&(args_info->nx_orig));
194     free_string_field (&(args_info->ny_orig));
195     free_string_field (&(args_info->nz_orig));
196 gezelter 1067
197 gezelter 1630
198 gezelter 1067 for (i = 0; i < args_info->inputs_num; ++i)
199     free (args_info->inputs [i]);
200 gezelter 1630
201 gezelter 1067 if (args_info->inputs_num)
202     free (args_info->inputs);
203 gezelter 1630
204 gezelter 1067 clear_given (args_info);
205     }
206    
207 gezelter 1630
208     static void
209     write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
210     {
211     FIX_UNUSED (values);
212     if (arg) {
213     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
214     } else {
215     fprintf(outfile, "%s\n", opt);
216     }
217     }
218    
219    
220 gezelter 1067 int
221 gezelter 1630 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
222     {
223     int i = 0;
224    
225     if (!outfile)
226     {
227     fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
228     return EXIT_FAILURE;
229     }
230    
231     if (args_info->help_given)
232     write_into_file(outfile, "help", 0, 0 );
233     if (args_info->version_given)
234     write_into_file(outfile, "version", 0, 0 );
235     if (args_info->output_given)
236     write_into_file(outfile, "output", args_info->output_orig, 0);
237     if (args_info->density_given)
238     write_into_file(outfile, "density", args_info->density_orig, 0);
239     if (args_info->nx_given)
240     write_into_file(outfile, "nx", args_info->nx_orig, 0);
241     if (args_info->ny_given)
242     write_into_file(outfile, "ny", args_info->ny_orig, 0);
243     if (args_info->nz_given)
244     write_into_file(outfile, "nz", args_info->nz_orig, 0);
245    
246    
247     i = EXIT_SUCCESS;
248     return i;
249     }
250    
251     int
252 gezelter 1067 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
253     {
254     FILE *outfile;
255     int i = 0;
256    
257     outfile = fopen(filename, "w");
258    
259     if (!outfile)
260     {
261     fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
262     return EXIT_FAILURE;
263     }
264    
265 gezelter 1630 i = cmdline_parser_dump(outfile, args_info);
266 gezelter 1067 fclose (outfile);
267    
268     return i;
269     }
270    
271     void
272     cmdline_parser_free (struct gengetopt_args_info *args_info)
273     {
274     cmdline_parser_release (args_info);
275     }
276    
277 gezelter 1630 /** @brief replacement of strdup, which is not standard */
278 tim 12 char *
279     gengetopt_strdup (const char *s)
280     {
281 gezelter 1630 char *result = 0;
282 gezelter 1067 if (!s)
283     return result;
284    
285     result = (char*)malloc(strlen(s) + 1);
286 tim 12 if (result == (char*)0)
287     return (char*)0;
288     strcpy(result, s);
289     return result;
290     }
291    
292     int
293 gezelter 1630 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
294 tim 12 {
295 gezelter 1067 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
296     }
297    
298     int
299 gezelter 1630 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
300     struct cmdline_parser_params *params)
301 gezelter 1067 {
302     int result;
303 gezelter 1630 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
304 gezelter 1067
305 gezelter 1630 if (result == EXIT_FAILURE)
306     {
307     cmdline_parser_free (args_info);
308     exit (EXIT_FAILURE);
309     }
310    
311     return result;
312     }
313 gezelter 1067
314 gezelter 1630 int
315     cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
316     {
317     int result;
318     struct cmdline_parser_params params;
319    
320     params.override = override;
321     params.initialize = initialize;
322     params.check_required = check_required;
323     params.check_ambiguity = 0;
324     params.print_errors = 1;
325    
326     result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
327    
328 gezelter 1067 if (result == EXIT_FAILURE)
329     {
330     cmdline_parser_free (args_info);
331     exit (EXIT_FAILURE);
332     }
333    
334     return result;
335     }
336    
337     int
338     cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
339     {
340     int result = EXIT_SUCCESS;
341    
342 gezelter 1630 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
343 gezelter 1067 result = EXIT_FAILURE;
344    
345     if (result == EXIT_FAILURE)
346     {
347     cmdline_parser_free (args_info);
348     exit (EXIT_FAILURE);
349     }
350    
351     return result;
352     }
353    
354     int
355     cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
356     {
357     int error = 0;
358 gezelter 1630 FIX_UNUSED (additional_error);
359 gezelter 1067
360 gezelter 1630 /* checks for required options */
361 gezelter 1067 if (! args_info->output_given)
362     {
363     fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
364     error = 1;
365     }
366 gezelter 1630
367 gezelter 1067 if (! args_info->density_given)
368     {
369     fprintf (stderr, "%s: '--density' option required%s\n", prog_name, (additional_error ? additional_error : ""));
370     error = 1;
371     }
372 gezelter 1630
373 gezelter 1067 if (! args_info->nx_given)
374     {
375     fprintf (stderr, "%s: '--nx' option required%s\n", prog_name, (additional_error ? additional_error : ""));
376     error = 1;
377     }
378 gezelter 1630
379 gezelter 1067 if (! args_info->ny_given)
380     {
381     fprintf (stderr, "%s: '--ny' option required%s\n", prog_name, (additional_error ? additional_error : ""));
382     error = 1;
383     }
384 gezelter 1630
385 gezelter 1067 if (! args_info->nz_given)
386     {
387     fprintf (stderr, "%s: '--nz' option required%s\n", prog_name, (additional_error ? additional_error : ""));
388     error = 1;
389     }
390 gezelter 1630
391    
392     /* checks for dependences among options */
393 gezelter 1067
394     return error;
395     }
396    
397 gezelter 1630
398     static char *package_name = 0;
399    
400     /**
401     * @brief updates an option
402     * @param field the generic pointer to the field to update
403     * @param orig_field the pointer to the orig field
404     * @param field_given the pointer to the number of occurrence of this option
405     * @param prev_given the pointer to the number of occurrence already seen
406     * @param value the argument for this option (if null no arg was specified)
407     * @param possible_values the possible values for this option (if specified)
408     * @param default_value the default value (in case the option only accepts fixed values)
409     * @param arg_type the type of this option
410     * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
411     * @param override @see cmdline_parser_params.override
412     * @param no_free whether to free a possible previous value
413     * @param multiple_option whether this is a multiple option
414     * @param long_opt the corresponding long option
415     * @param short_opt the corresponding short option (or '-' if none)
416     * @param additional_error possible further error specification
417     */
418     static
419     int update_arg(void *field, char **orig_field,
420     unsigned int *field_given, unsigned int *prev_given,
421     char *value, const char *possible_values[],
422     const char *default_value,
423     cmdline_parser_arg_type arg_type,
424     int check_ambiguity, int override,
425     int no_free, int multiple_option,
426     const char *long_opt, char short_opt,
427     const char *additional_error)
428     {
429     char *stop_char = 0;
430     const char *val = value;
431     int found;
432     char **string_field;
433     FIX_UNUSED (field);
434    
435     stop_char = 0;
436     found = 0;
437    
438     if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
439     {
440     if (short_opt != '-')
441     fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
442     package_name, long_opt, short_opt,
443     (additional_error ? additional_error : ""));
444     else
445     fprintf (stderr, "%s: `--%s' option given more than once%s\n",
446     package_name, long_opt,
447     (additional_error ? additional_error : ""));
448     return 1; /* failure */
449     }
450    
451     FIX_UNUSED (default_value);
452    
453     if (field_given && *field_given && ! override)
454     return 0;
455     if (prev_given)
456     (*prev_given)++;
457     if (field_given)
458     (*field_given)++;
459     if (possible_values)
460     val = possible_values[found];
461    
462     switch(arg_type) {
463     case ARG_INT:
464     if (val) *((int *)field) = strtol (val, &stop_char, 0);
465     break;
466     case ARG_DOUBLE:
467     if (val) *((double *)field) = strtod (val, &stop_char);
468     break;
469     case ARG_STRING:
470     if (val) {
471     string_field = (char **)field;
472     if (!no_free && *string_field)
473     free (*string_field); /* free previous string */
474     *string_field = gengetopt_strdup (val);
475     }
476     break;
477     default:
478     break;
479     };
480    
481     /* check numeric conversion */
482     switch(arg_type) {
483     case ARG_INT:
484     case ARG_DOUBLE:
485     if (val && !(stop_char && *stop_char == '\0')) {
486     fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
487     return 1; /* failure */
488     }
489     break;
490     default:
491     ;
492     };
493    
494     /* store the original value */
495     switch(arg_type) {
496     case ARG_NO:
497     break;
498     default:
499     if (value && orig_field) {
500     if (no_free) {
501     *orig_field = value;
502     } else {
503     if (*orig_field)
504     free (*orig_field); /* free previous string */
505     *orig_field = gengetopt_strdup (value);
506     }
507     }
508     };
509    
510     return 0; /* OK */
511     }
512    
513    
514 gezelter 1067 int
515 gezelter 1630 cmdline_parser_internal (
516     int argc, char **argv, struct gengetopt_args_info *args_info,
517     struct cmdline_parser_params *params, const char *additional_error)
518 gezelter 1067 {
519 tim 12 int c; /* Character of the parsed option. */
520    
521 gezelter 1067 int error = 0;
522     struct gengetopt_args_info local_args_info;
523 gezelter 1630
524     int override;
525     int initialize;
526     int check_required;
527     int check_ambiguity;
528    
529     package_name = argv[0];
530    
531     override = params->override;
532     initialize = params->initialize;
533     check_required = params->check_required;
534     check_ambiguity = params->check_ambiguity;
535 tim 12
536 gezelter 1067 if (initialize)
537     cmdline_parser_init (args_info);
538 tim 12
539 gezelter 1067 cmdline_parser_init (&local_args_info);
540 tim 12
541     optarg = 0;
542 gezelter 1067 optind = 0;
543 gezelter 1630 opterr = params->print_errors;
544 tim 12 optopt = '?';
545    
546     while (1)
547     {
548     int option_index = 0;
549    
550     static struct option long_options[] = {
551     { "help", 0, NULL, 'h' },
552     { "version", 0, NULL, 'V' },
553     { "output", 1, NULL, 'o' },
554     { "density", 1, NULL, 0 },
555     { "nx", 1, NULL, 0 },
556     { "ny", 1, NULL, 0 },
557     { "nz", 1, NULL, 0 },
558 gezelter 1630 { 0, 0, 0, 0 }
559 tim 12 };
560    
561     c = getopt_long (argc, argv, "hVo:", long_options, &option_index);
562    
563     if (c == -1) break; /* Exit from `while (1)' loop. */
564    
565     switch (c)
566     {
567     case 'h': /* Print help and exit. */
568     cmdline_parser_print_help ();
569 gezelter 1067 cmdline_parser_free (&local_args_info);
570 tim 12 exit (EXIT_SUCCESS);
571    
572     case 'V': /* Print version and exit. */
573     cmdline_parser_print_version ();
574 gezelter 1067 cmdline_parser_free (&local_args_info);
575 tim 12 exit (EXIT_SUCCESS);
576    
577     case 'o': /* Output file name. */
578 gezelter 1630
579    
580     if (update_arg( (void *)&(args_info->output_arg),
581     &(args_info->output_orig), &(args_info->output_given),
582     &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
583     check_ambiguity, override, 0, 0,
584     "output", 'o',
585     additional_error))
586     goto failure;
587    
588 tim 12 break;
589    
590     case 0: /* Long option with no short option */
591     /* density (g/cm^3). */
592 gezelter 1067 if (strcmp (long_options[option_index].name, "density") == 0)
593     {
594 gezelter 1630
595    
596     if (update_arg( (void *)&(args_info->density_arg),
597     &(args_info->density_orig), &(args_info->density_given),
598     &(local_args_info.density_given), optarg, 0, 0, ARG_DOUBLE,
599     check_ambiguity, override, 0, 0,
600     "density", '-',
601     additional_error))
602 gezelter 1067 goto failure;
603 gezelter 1630
604 gezelter 1067 }
605 tim 12 /* number of unit cells in x. */
606     else if (strcmp (long_options[option_index].name, "nx") == 0)
607 gezelter 1067 {
608 gezelter 1630
609    
610     if (update_arg( (void *)&(args_info->nx_arg),
611     &(args_info->nx_orig), &(args_info->nx_given),
612     &(local_args_info.nx_given), optarg, 0, 0, ARG_INT,
613     check_ambiguity, override, 0, 0,
614     "nx", '-',
615     additional_error))
616 gezelter 1067 goto failure;
617 gezelter 1630
618 gezelter 1067 }
619 tim 12 /* number of unit cells in y. */
620     else if (strcmp (long_options[option_index].name, "ny") == 0)
621 gezelter 1067 {
622 gezelter 1630
623    
624     if (update_arg( (void *)&(args_info->ny_arg),
625     &(args_info->ny_orig), &(args_info->ny_given),
626     &(local_args_info.ny_given), optarg, 0, 0, ARG_INT,
627     check_ambiguity, override, 0, 0,
628     "ny", '-',
629     additional_error))
630 gezelter 1067 goto failure;
631 gezelter 1630
632 gezelter 1067 }
633 tim 12 /* number of unit cells in z. */
634     else if (strcmp (long_options[option_index].name, "nz") == 0)
635 gezelter 1067 {
636 gezelter 1630
637    
638     if (update_arg( (void *)&(args_info->nz_arg),
639     &(args_info->nz_orig), &(args_info->nz_given),
640     &(local_args_info.nz_given), optarg, 0, 0, ARG_INT,
641     check_ambiguity, override, 0, 0,
642     "nz", '-',
643     additional_error))
644 gezelter 1067 goto failure;
645 gezelter 1630
646 gezelter 1067 }
647 tim 12
648 gezelter 1067 break;
649 tim 12 case '?': /* Invalid option. */
650     /* `getopt_long' already printed an error message. */
651 gezelter 1067 goto failure;
652 tim 12
653     default: /* bug: option not considered. */
654 gezelter 1067 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
655 tim 12 abort ();
656     } /* switch */
657     } /* while */
658    
659    
660 gezelter 1067
661     if (check_required)
662 tim 12 {
663 gezelter 1067 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
664 tim 12 }
665    
666 gezelter 1067 cmdline_parser_release (&local_args_info);
667    
668     if ( error )
669     return (EXIT_FAILURE);
670    
671 tim 12 if (optind < argc)
672     {
673     int i = 0 ;
674 gezelter 1630 int found_prog_name = 0;
675     /* whether program name, i.e., argv[0], is in the remaining args
676     (this may happen with some implementations of getopt,
677     but surely not with the one included by gengetopt) */
678 gezelter 1067
679 gezelter 1630 i = optind;
680     while (i < argc)
681     if (argv[i++] == argv[0]) {
682     found_prog_name = 1;
683     break;
684     }
685     i = 0;
686    
687     args_info->inputs_num = argc - optind - found_prog_name;
688 gezelter 1067 args_info->inputs =
689 tim 12 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
690     while (optind < argc)
691 gezelter 1630 if (argv[optind++] != argv[0])
692     args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
693 tim 12 }
694 gezelter 1067
695     return 0;
696    
697     failure:
698 tim 12
699 gezelter 1067 cmdline_parser_release (&local_args_info);
700     return (EXIT_FAILURE);
701 tim 12 }

Properties

Name Value
svn:keywords Author Id Revision Date