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

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date