ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/simpleBuilder/simpleBuilderCmd.cpp
Revision: 1630
Committed: Wed Sep 14 21:40:12 2011 UTC (13 years, 7 months ago) by gezelter
Content type: text/plain
Original Path: branches/development/src/applications/simpleBuilder/simpleBuilderCmd.c
File size: 18512 byte(s)
Log Message:
updating gengetopt runs

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

Properties

Name Value
svn:keywords Author Id Revision Date