ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/recenter/recenterCmd.cpp
Revision: 1876
Committed: Fri May 17 17:10:11 2013 UTC (11 years, 11 months ago) by gezelter
File size: 14070 byte(s)
Log Message:
Compilation and portability fixes

File Contents

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