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

# Content
1 /*
2 File autogenerated by gengetopt version 2.22.4
3 generated with the following command:
4 gengetopt -F simpleBuilderCmd -u
5
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 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
23
24 #ifdef WIN32
25 #include "utils/wingetopt.h"
26 #else
27 #include <getopt.h>
28 #endif
29
30 #include "simpleBuilderCmd.h"
31
32 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 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 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
62 struct cmdline_parser_params *params, const char *additional_error);
63
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 FIX_UNUSED (args_info);
86 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 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 void
111 cmdline_parser_print_version (void)
112 {
113 printf ("%s %s\n",
114 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
115 CMDLINE_PARSER_VERSION);
116 }
117
118 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 void
134 cmdline_parser_print_help (void)
135 {
136 int i = 0;
137 print_help_common();
138 while (gengetopt_args_info_help[i])
139 printf("%s\n", gengetopt_args_info_help[i++]);
140 }
141
142 void
143 cmdline_parser_init (struct gengetopt_args_info *args_info)
144 {
145 clear_given (args_info);
146 clear_args (args_info);
147 init_args_info (args_info);
148
149 args_info->inputs = 0;
150 args_info->inputs_num = 0;
151 }
152
153 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 static void
176 free_string_field (char **s)
177 {
178 if (*s)
179 {
180 free (*s);
181 *s = 0;
182 }
183 }
184
185
186 static void
187 cmdline_parser_release (struct gengetopt_args_info *args_info)
188 {
189 unsigned int i;
190 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
197
198 for (i = 0; i < args_info->inputs_num; ++i)
199 free (args_info->inputs [i]);
200
201 if (args_info->inputs_num)
202 free (args_info->inputs);
203
204 clear_given (args_info);
205 }
206
207
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 int
221 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 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 i = cmdline_parser_dump(outfile, args_info);
266 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 /** @brief replacement of strdup, which is not standard */
278 char *
279 gengetopt_strdup (const char *s)
280 {
281 char *result = 0;
282 if (!s)
283 return result;
284
285 result = (char*)malloc(strlen(s) + 1);
286 if (result == (char*)0)
287 return (char*)0;
288 strcpy(result, s);
289 return result;
290 }
291
292 int
293 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
294 {
295 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
296 }
297
298 int
299 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
300 struct cmdline_parser_params *params)
301 {
302 int result;
303 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
304
305 if (result == EXIT_FAILURE)
306 {
307 cmdline_parser_free (args_info);
308 exit (EXIT_FAILURE);
309 }
310
311 return result;
312 }
313
314 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 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 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
343 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 FIX_UNUSED (additional_error);
359
360 /* checks for required options */
361 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
367 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
373 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
379 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
385 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
391
392 /* checks for dependences among options */
393
394 return error;
395 }
396
397
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 int
515 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 {
519 int c; /* Character of the parsed option. */
520
521 int error = 0;
522 struct gengetopt_args_info local_args_info;
523
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
536 if (initialize)
537 cmdline_parser_init (args_info);
538
539 cmdline_parser_init (&local_args_info);
540
541 optarg = 0;
542 optind = 0;
543 opterr = params->print_errors;
544 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 { 0, 0, 0, 0 }
559 };
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 cmdline_parser_free (&local_args_info);
570 exit (EXIT_SUCCESS);
571
572 case 'V': /* Print version and exit. */
573 cmdline_parser_print_version ();
574 cmdline_parser_free (&local_args_info);
575 exit (EXIT_SUCCESS);
576
577 case 'o': /* Output file name. */
578
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 break;
589
590 case 0: /* Long option with no short option */
591 /* density (g/cm^3). */
592 if (strcmp (long_options[option_index].name, "density") == 0)
593 {
594
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 goto failure;
603
604 }
605 /* number of unit cells in x. */
606 else if (strcmp (long_options[option_index].name, "nx") == 0)
607 {
608
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 goto failure;
617
618 }
619 /* number of unit cells in y. */
620 else if (strcmp (long_options[option_index].name, "ny") == 0)
621 {
622
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 goto failure;
631
632 }
633 /* number of unit cells in z. */
634 else if (strcmp (long_options[option_index].name, "nz") == 0)
635 {
636
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 goto failure;
645
646 }
647
648 break;
649 case '?': /* Invalid option. */
650 /* `getopt_long' already printed an error message. */
651 goto failure;
652
653 default: /* bug: option not considered. */
654 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
655 abort ();
656 } /* switch */
657 } /* while */
658
659
660
661 if (check_required)
662 {
663 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
664 }
665
666 cmdline_parser_release (&local_args_info);
667
668 if ( error )
669 return (EXIT_FAILURE);
670
671 if (optind < argc)
672 {
673 int i = 0 ;
674 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
679 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 args_info->inputs =
689 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
690 while (optind < argc)
691 if (argv[optind++] != argv[0])
692 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
693 }
694
695 return 0;
696
697 failure:
698
699 cmdline_parser_release (&local_args_info);
700 return (EXIT_FAILURE);
701 }

Properties

Name Value
svn:keywords Author Id Revision Date