--- trunk/src/applications/simpleBuilder/simpleBuilderCmd.c 2004/09/28 23:24:25 12 +++ branches/development/src/applications/simpleBuilder/simpleBuilderCmd.c 2010/07/09 23:08:25 1465 @@ -1,27 +1,64 @@ /* - File autogenerated by gengetopt version 2.11 + File autogenerated by gengetopt version 2.16 generated with the following command: - /home/maul/gezelter/tim/program/gengetopt-2.11/src/gengetopt -F simpleBuilderCmd -u + gengetopt -F simpleBuilderCmd --unamed-opts The developers of gengetopt consider the fixed text that goes in all gengetopt output files to be in the public domain: we make no copyright claims on it. */ - -#include -#include -#include - /* If we use autoconf. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include +#include +#include + #include "getopt.h" -#include "applications/simpleBuilder/simpleBuilderCmd.h" +#include "simpleBuilderCmd.h" +static +void clear_given (struct gengetopt_args_info *args_info); +static +void clear_args (struct gengetopt_args_info *args_info); + +static int +cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error); + +static int +cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error); + +static char * +gengetopt_strdup (const char *s); + +static +void clear_given (struct gengetopt_args_info *args_info) +{ + args_info->help_given = 0 ; + args_info->version_given = 0 ; + args_info->output_given = 0 ; + args_info->density_given = 0 ; + args_info->nx_given = 0 ; + args_info->ny_given = 0 ; + args_info->nz_given = 0 ; +} + +static +void clear_args (struct gengetopt_args_info *args_info) +{ + args_info->output_arg = NULL; + args_info->output_orig = NULL; + args_info->density_orig = NULL; + args_info->nx_orig = NULL; + args_info->ny_orig = NULL; + args_info->nz_orig = NULL; + +} + void cmdline_parser_print_version (void) { @@ -32,28 +69,151 @@ cmdline_parser_print_help (void) cmdline_parser_print_help (void) { cmdline_parser_print_version (); - printf("\n" - "Usage: %s [OPTIONS]... [FILES]...\n", CMDLINE_PARSER_PACKAGE); - printf("\n"); - printf(" -h, --help Print help and exit\n"); - printf(" -V, --version Print version and exit\n"); - printf(" -o, --output=STRING Output file name\n"); - printf(" --latticetype=STRING Lattice type string. Valid types are fcc,hcp,bcc \n and hcp-water. (default=`fcc')\n"); - printf(" --density=DOUBLE density (g/cm^3)\n"); - printf(" --nx=INT number of unit cells in x\n"); - printf(" --ny=INT number of unit cells in y\n"); - printf(" --nz=INT number of unit cells in z\n"); + printf("\nUsage: simpleBuilder [OPTIONS]... [FILES]...\n\n"); + printf("%s\n"," -h, --help Print help and exit"); + printf("%s\n"," -V, --version Print version and exit"); + printf("%s\n"," -o, --output=STRING Output file name"); + printf("%s\n"," --density=DOUBLE density (g/cm^3)"); + printf("%s\n"," --nx=INT number of unit cells in x"); + printf("%s\n"," --ny=INT number of unit cells in y"); + printf("%s\n"," --nz=INT number of unit cells in z"); + } +void +cmdline_parser_init (struct gengetopt_args_info *args_info) +{ + clear_given (args_info); + clear_args (args_info); -static char *gengetopt_strdup (const char *s); + args_info->inputs = NULL; + args_info->inputs_num = 0; +} +static void +cmdline_parser_release (struct gengetopt_args_info *args_info) +{ + + unsigned int i; + if (args_info->output_arg) + { + free (args_info->output_arg); /* free previous argument */ + args_info->output_arg = 0; + } + if (args_info->output_orig) + { + free (args_info->output_orig); /* free previous argument */ + args_info->output_orig = 0; + } + if (args_info->density_orig) + { + free (args_info->density_orig); /* free previous argument */ + args_info->density_orig = 0; + } + if (args_info->nx_orig) + { + free (args_info->nx_orig); /* free previous argument */ + args_info->nx_orig = 0; + } + if (args_info->ny_orig) + { + free (args_info->ny_orig); /* free previous argument */ + args_info->ny_orig = 0; + } + if (args_info->nz_orig) + { + free (args_info->nz_orig); /* free previous argument */ + args_info->nz_orig = 0; + } + + for (i = 0; i < args_info->inputs_num; ++i) + free (args_info->inputs [i]); + + if (args_info->inputs_num) + free (args_info->inputs); + + clear_given (args_info); +} + +int +cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) +{ + FILE *outfile; + int i = 0; + + outfile = fopen(filename, "w"); + + if (!outfile) + { + fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); + return EXIT_FAILURE; + } + + if (args_info->help_given) { + fprintf(outfile, "%s\n", "help"); + } + if (args_info->version_given) { + fprintf(outfile, "%s\n", "version"); + } + if (args_info->output_given) { + if (args_info->output_orig) { + fprintf(outfile, "%s=\"%s\"\n", "output", args_info->output_orig); + } else { + fprintf(outfile, "%s\n", "output"); + } + } + if (args_info->density_given) { + if (args_info->density_orig) { + fprintf(outfile, "%s=\"%s\"\n", "density", args_info->density_orig); + } else { + fprintf(outfile, "%s\n", "density"); + } + } + if (args_info->nx_given) { + if (args_info->nx_orig) { + fprintf(outfile, "%s=\"%s\"\n", "nx", args_info->nx_orig); + } else { + fprintf(outfile, "%s\n", "nx"); + } + } + if (args_info->ny_given) { + if (args_info->ny_orig) { + fprintf(outfile, "%s=\"%s\"\n", "ny", args_info->ny_orig); + } else { + fprintf(outfile, "%s\n", "ny"); + } + } + if (args_info->nz_given) { + if (args_info->nz_orig) { + fprintf(outfile, "%s=\"%s\"\n", "nz", args_info->nz_orig); + } else { + fprintf(outfile, "%s\n", "nz"); + } + } + + fclose (outfile); + + i = EXIT_SUCCESS; + return i; +} + +void +cmdline_parser_free (struct gengetopt_args_info *args_info) +{ + cmdline_parser_release (args_info); +} + + /* gengetopt_strdup() */ /* strdup.c replacement of strdup, which is not standard */ char * gengetopt_strdup (const char *s) { - char *result = (char*)malloc(strlen(s) + 1); + char *result = NULL; + if (!s) + return result; + + result = (char*)malloc(strlen(s) + 1); if (result == (char*)0) return (char*)0; strcpy(result, s); @@ -63,29 +223,91 @@ cmdline_parser (int argc, char * const *argv, struct g int cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info) { - int c; /* Character of the parsed option. */ - int missing_required_options = 0; + return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); +} - args_info->help_given = 0 ; - args_info->version_given = 0 ; - args_info->output_given = 0 ; - args_info->latticetype_given = 0 ; - args_info->density_given = 0 ; - args_info->nx_given = 0 ; - args_info->ny_given = 0 ; - args_info->nz_given = 0 ; -#define clear_args() { \ - args_info->output_arg = NULL; \ - args_info->latticetype_arg = gengetopt_strdup("fcc") ;\ +int +cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) +{ + int result; + + result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL); + + if (result == EXIT_FAILURE) + { + cmdline_parser_free (args_info); + exit (EXIT_FAILURE); + } + + return result; } - clear_args(); +int +cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) +{ + int result = EXIT_SUCCESS; - args_info->inputs = NULL; - args_info->inputs_num = 0; + if (cmdline_parser_required2(args_info, prog_name, NULL) > 0) + result = EXIT_FAILURE; + + if (result == EXIT_FAILURE) + { + cmdline_parser_free (args_info); + exit (EXIT_FAILURE); + } + + return result; +} +int +cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error) +{ + int error = 0; + + if (! args_info->output_given) + { + fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : "")); + error = 1; + } + if (! args_info->density_given) + { + fprintf (stderr, "%s: '--density' option required%s\n", prog_name, (additional_error ? additional_error : "")); + error = 1; + } + if (! args_info->nx_given) + { + fprintf (stderr, "%s: '--nx' option required%s\n", prog_name, (additional_error ? additional_error : "")); + error = 1; + } + if (! args_info->ny_given) + { + fprintf (stderr, "%s: '--ny' option required%s\n", prog_name, (additional_error ? additional_error : "")); + error = 1; + } + if (! args_info->nz_given) + { + fprintf (stderr, "%s: '--nz' option required%s\n", prog_name, (additional_error ? additional_error : "")); + error = 1; + } + + return error; +} + +int +cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error) +{ + int c; /* Character of the parsed option. */ + + int error = 0; + struct gengetopt_args_info local_args_info; + + if (initialize) + cmdline_parser_init (args_info); + + cmdline_parser_init (&local_args_info); + optarg = 0; - optind = 1; + optind = 0; opterr = 1; optopt = '?'; @@ -98,7 +320,6 @@ cmdline_parser (int argc, char * const *argv, struct g { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'V' }, { "output", 1, NULL, 'o' }, - { "latticetype", 1, NULL, 0 }, { "density", 1, NULL, 0 }, { "nx", 1, NULL, 0 }, { "ny", 1, NULL, 0 }, @@ -114,145 +335,158 @@ cmdline_parser (int argc, char * const *argv, struct g switch (c) { case 'h': /* Print help and exit. */ - clear_args (); cmdline_parser_print_help (); + cmdline_parser_free (&local_args_info); exit (EXIT_SUCCESS); case 'V': /* Print version and exit. */ - clear_args (); cmdline_parser_print_version (); + cmdline_parser_free (&local_args_info); exit (EXIT_SUCCESS); case 'o': /* Output file name. */ - if (args_info->output_given) + if (local_args_info.output_given) { - fprintf (stderr, "%s: `--output' (`-o') option given more than once\n", CMDLINE_PARSER_PACKAGE); - clear_args (); - exit (EXIT_FAILURE); + fprintf (stderr, "%s: `--output' (`-o') option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); + goto failure; } + if (args_info->output_given && ! override) + continue; + local_args_info.output_given = 1; args_info->output_given = 1; + if (args_info->output_arg) + free (args_info->output_arg); /* free previous string */ args_info->output_arg = gengetopt_strdup (optarg); + if (args_info->output_orig) + free (args_info->output_orig); /* free previous string */ + args_info->output_orig = gengetopt_strdup (optarg); break; case 0: /* Long option with no short option */ - /* Lattice type string. Valid types are fcc,hcp,bcc and hcp-water.. */ - if (strcmp (long_options[option_index].name, "latticetype") == 0) - { - if (args_info->latticetype_given) - { - fprintf (stderr, "%s: `--latticetype' option given more than once\n", CMDLINE_PARSER_PACKAGE); - clear_args (); - exit (EXIT_FAILURE); - } - args_info->latticetype_given = 1; - if (args_info->latticetype_arg) - free (args_info->latticetype_arg); /* free default string */ - args_info->latticetype_arg = gengetopt_strdup (optarg); - break; - } - /* density (g/cm^3). */ - else if (strcmp (long_options[option_index].name, "density") == 0) + if (strcmp (long_options[option_index].name, "density") == 0) { - if (args_info->density_given) + if (local_args_info.density_given) { - fprintf (stderr, "%s: `--density' option given more than once\n", CMDLINE_PARSER_PACKAGE); - clear_args (); - exit (EXIT_FAILURE); + fprintf (stderr, "%s: `--density' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); + goto failure; } + if (args_info->density_given && ! override) + continue; + local_args_info.density_given = 1; args_info->density_given = 1; - args_info->density_arg = strtod (optarg, NULL); - break; + args_info->density_arg = strtod (optarg, &stop_char); + if (!(stop_char && *stop_char == '\0')) { + fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); + goto failure; + } + if (args_info->density_orig) + free (args_info->density_orig); /* free previous string */ + args_info->density_orig = gengetopt_strdup (optarg); } - /* number of unit cells in x. */ else if (strcmp (long_options[option_index].name, "nx") == 0) { - if (args_info->nx_given) + if (local_args_info.nx_given) { - fprintf (stderr, "%s: `--nx' option given more than once\n", CMDLINE_PARSER_PACKAGE); - clear_args (); - exit (EXIT_FAILURE); + fprintf (stderr, "%s: `--nx' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); + goto failure; } + if (args_info->nx_given && ! override) + continue; + local_args_info.nx_given = 1; args_info->nx_given = 1; - args_info->nx_arg = strtol (optarg,&stop_char,0); - break; + args_info->nx_arg = strtol (optarg, &stop_char, 0); + if (!(stop_char && *stop_char == '\0')) { + fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); + goto failure; + } + if (args_info->nx_orig) + free (args_info->nx_orig); /* free previous string */ + args_info->nx_orig = gengetopt_strdup (optarg); } - /* number of unit cells in y. */ else if (strcmp (long_options[option_index].name, "ny") == 0) { - if (args_info->ny_given) + if (local_args_info.ny_given) { - fprintf (stderr, "%s: `--ny' option given more than once\n", CMDLINE_PARSER_PACKAGE); - clear_args (); - exit (EXIT_FAILURE); + fprintf (stderr, "%s: `--ny' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); + goto failure; } + if (args_info->ny_given && ! override) + continue; + local_args_info.ny_given = 1; args_info->ny_given = 1; - args_info->ny_arg = strtol (optarg,&stop_char,0); - break; + args_info->ny_arg = strtol (optarg, &stop_char, 0); + if (!(stop_char && *stop_char == '\0')) { + fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); + goto failure; + } + if (args_info->ny_orig) + free (args_info->ny_orig); /* free previous string */ + args_info->ny_orig = gengetopt_strdup (optarg); } - /* number of unit cells in z. */ else if (strcmp (long_options[option_index].name, "nz") == 0) { - if (args_info->nz_given) + if (local_args_info.nz_given) { - fprintf (stderr, "%s: `--nz' option given more than once\n", CMDLINE_PARSER_PACKAGE); - clear_args (); - exit (EXIT_FAILURE); + fprintf (stderr, "%s: `--nz' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); + goto failure; } + if (args_info->nz_given && ! override) + continue; + local_args_info.nz_given = 1; args_info->nz_given = 1; - args_info->nz_arg = strtol (optarg,&stop_char,0); - break; + args_info->nz_arg = strtol (optarg, &stop_char, 0); + if (!(stop_char && *stop_char == '\0')) { + fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg); + goto failure; + } + if (args_info->nz_orig) + free (args_info->nz_orig); /* free previous string */ + args_info->nz_orig = gengetopt_strdup (optarg); } - + break; case '?': /* Invalid option. */ /* `getopt_long' already printed an error message. */ - exit (EXIT_FAILURE); + goto failure; default: /* bug: option not considered. */ - fprintf (stderr, "%s: option unknown: %c\n", CMDLINE_PARSER_PACKAGE, c); + fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); abort (); } /* switch */ } /* while */ - if (! args_info->density_given) + + if (check_required) { - fprintf (stderr, "%s: '--density' option required\n", CMDLINE_PARSER_PACKAGE); - missing_required_options = 1; + error += cmdline_parser_required2 (args_info, argv[0], additional_error); } - if (! args_info->nx_given) - { - fprintf (stderr, "%s: '--nx' option required\n", CMDLINE_PARSER_PACKAGE); - missing_required_options = 1; - } - if (! args_info->ny_given) - { - fprintf (stderr, "%s: '--ny' option required\n", CMDLINE_PARSER_PACKAGE); - missing_required_options = 1; - } - if (! args_info->nz_given) - { - fprintf (stderr, "%s: '--nz' option required\n", CMDLINE_PARSER_PACKAGE); - missing_required_options = 1; - } - if ( missing_required_options ) - exit (EXIT_FAILURE); + cmdline_parser_release (&local_args_info); + + if ( error ) + return (EXIT_FAILURE); + if (optind < argc) { int i = 0 ; - + args_info->inputs_num = argc - optind ; - args_info->inputs = + args_info->inputs = (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ; while (optind < argc) - args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ; + args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ; } - + return 0; + +failure: + + cmdline_parser_release (&local_args_info); + return (EXIT_FAILURE); }