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

File Contents

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

Properties

Name Value
svn:keywords Author Id Revision Date