2 File autogenerated by gengetopt version 2.22
3 generated with the following command:
4 /usr/local/bin/gengetopt --output-dir=/home/tbaudier/vv/vv_static/vv/vv_bin/tools --arg-struct-name=args_info_clitkBinarizeImage --func-name=cmdline_parser_clitkBinarizeImage --file-name=clitkBinarizeImage_ggo --unamed-opts --conf-parser --include-getopt
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.
11 /* If we use autoconf. */
21 #include "clitkBinarizeImage_ggo.h"
23 const char *args_info_clitkBinarizeImage_purpose = "";
25 const char *args_info_clitkBinarizeImage_usage = "Usage: clitkBinarizeImage [OPTIONS]... [FILES]...";
27 const char *args_info_clitkBinarizeImage_description = "";
29 const char *args_info_clitkBinarizeImage_help[] = {
30 " -h, --help Print help and exit",
31 " -V, --version Print version and exit",
32 " --config=STRING Config file",
33 " -v, --verbose Verbose (default=off)",
34 " --imagetypes Display allowed image types (default=off)",
35 " -i, --input=STRING Input image filename",
36 " -o, --output=STRING Output image filename",
37 " -l, --lower=DOUBLE Lower intensity (default=min), fg is greater than this \n value",
38 " -u, --upper=DOUBLE Upper intensity (default=max), fg is lower than this \n value",
39 " --fg=DOUBLE Foreground (FG) or 'inside' value (default=`1')",
40 " --bg=DOUBLE Background (BG) or 'ouside' value (default=`0')",
41 " --mode=STRING Use FG and/or BG values (if FG, the BG is replaced by \n the input image values) (possible values=\"FG\", \n \"BG\", \"both\" default=`both')",
49 } cmdline_parser_clitkBinarizeImage_arg_type;
52 void clear_given (struct args_info_clitkBinarizeImage *args_info);
54 void clear_args (struct args_info_clitkBinarizeImage *args_info);
57 cmdline_parser_clitkBinarizeImage_internal (int argc, char * const *argv, struct args_info_clitkBinarizeImage *args_info,
58 struct cmdline_parser_clitkBinarizeImage_params *params, const char *additional_error);
61 cmdline_parser_clitkBinarizeImage_required2 (struct args_info_clitkBinarizeImage *args_info, const char *prog_name, const char *additional_error);
65 struct line_list * next;
68 static struct line_list *cmd_line_list = 0;
69 static struct line_list *cmd_line_list_tmp = 0;
74 /* free the list of a previous call */
77 while (cmd_line_list) {
78 cmd_line_list_tmp = cmd_line_list;
79 cmd_line_list = cmd_line_list->next;
80 free (cmd_line_list_tmp->string_arg);
81 free (cmd_line_list_tmp);
87 char *cmdline_parser_clitkBinarizeImage_mode_values[] = {"FG", "BG", "both", 0} ; /* Possible values for mode. */
90 gengetopt_strdup (const char *s);
93 void clear_given (struct args_info_clitkBinarizeImage *args_info)
95 args_info->help_given = 0 ;
96 args_info->version_given = 0 ;
97 args_info->config_given = 0 ;
98 args_info->verbose_given = 0 ;
99 args_info->imagetypes_given = 0 ;
100 args_info->input_given = 0 ;
101 args_info->output_given = 0 ;
102 args_info->lower_given = 0 ;
103 args_info->upper_given = 0 ;
104 args_info->fg_given = 0 ;
105 args_info->bg_given = 0 ;
106 args_info->mode_given = 0 ;
110 void clear_args (struct args_info_clitkBinarizeImage *args_info)
112 args_info->config_arg = NULL;
113 args_info->config_orig = NULL;
114 args_info->verbose_flag = 0;
115 args_info->imagetypes_flag = 0;
116 args_info->input_arg = NULL;
117 args_info->input_orig = NULL;
118 args_info->output_arg = NULL;
119 args_info->output_orig = NULL;
120 args_info->lower_orig = NULL;
121 args_info->upper_orig = NULL;
122 args_info->fg_arg = 1;
123 args_info->fg_orig = NULL;
124 args_info->bg_arg = 0;
125 args_info->bg_orig = NULL;
126 args_info->mode_arg = gengetopt_strdup ("both");
127 args_info->mode_orig = NULL;
132 void init_args_info(struct args_info_clitkBinarizeImage *args_info)
136 args_info->help_help = args_info_clitkBinarizeImage_help[0] ;
137 args_info->version_help = args_info_clitkBinarizeImage_help[1] ;
138 args_info->config_help = args_info_clitkBinarizeImage_help[2] ;
139 args_info->verbose_help = args_info_clitkBinarizeImage_help[3] ;
140 args_info->imagetypes_help = args_info_clitkBinarizeImage_help[4] ;
141 args_info->input_help = args_info_clitkBinarizeImage_help[5] ;
142 args_info->output_help = args_info_clitkBinarizeImage_help[6] ;
143 args_info->lower_help = args_info_clitkBinarizeImage_help[7] ;
144 args_info->upper_help = args_info_clitkBinarizeImage_help[8] ;
145 args_info->fg_help = args_info_clitkBinarizeImage_help[9] ;
146 args_info->bg_help = args_info_clitkBinarizeImage_help[10] ;
147 args_info->mode_help = args_info_clitkBinarizeImage_help[11] ;
152 cmdline_parser_clitkBinarizeImage_print_version (void)
154 printf ("%s %s\n", CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, CMDLINE_PARSER_CLITKBINARIZEIMAGE_VERSION);
157 static void print_help_common(void) {
158 cmdline_parser_clitkBinarizeImage_print_version ();
160 if (strlen(args_info_clitkBinarizeImage_purpose) > 0)
161 printf("\n%s\n", args_info_clitkBinarizeImage_purpose);
163 if (strlen(args_info_clitkBinarizeImage_usage) > 0)
164 printf("\n%s\n", args_info_clitkBinarizeImage_usage);
168 if (strlen(args_info_clitkBinarizeImage_description) > 0)
169 printf("%s\n", args_info_clitkBinarizeImage_description);
173 cmdline_parser_clitkBinarizeImage_print_help (void)
177 while (args_info_clitkBinarizeImage_help[i])
178 printf("%s\n", args_info_clitkBinarizeImage_help[i++]);
182 cmdline_parser_clitkBinarizeImage_init (struct args_info_clitkBinarizeImage *args_info)
184 clear_given (args_info);
185 clear_args (args_info);
186 init_args_info (args_info);
188 args_info->inputs = NULL;
189 args_info->inputs_num = 0;
193 cmdline_parser_clitkBinarizeImage_params_init(struct cmdline_parser_clitkBinarizeImage_params *params)
197 params->override = 0;
198 params->initialize = 1;
199 params->check_required = 1;
200 params->check_ambiguity = 0;
201 params->print_errors = 1;
205 struct cmdline_parser_clitkBinarizeImage_params *
206 cmdline_parser_clitkBinarizeImage_params_create(void)
208 struct cmdline_parser_clitkBinarizeImage_params *params =
209 (struct cmdline_parser_clitkBinarizeImage_params *)malloc(sizeof(struct cmdline_parser_clitkBinarizeImage_params));
210 cmdline_parser_clitkBinarizeImage_params_init(params);
215 free_string_field (char **s)
226 cmdline_parser_clitkBinarizeImage_release (struct args_info_clitkBinarizeImage *args_info)
229 free_string_field (&(args_info->config_arg));
230 free_string_field (&(args_info->config_orig));
231 free_string_field (&(args_info->input_arg));
232 free_string_field (&(args_info->input_orig));
233 free_string_field (&(args_info->output_arg));
234 free_string_field (&(args_info->output_orig));
235 free_string_field (&(args_info->lower_orig));
236 free_string_field (&(args_info->upper_orig));
237 free_string_field (&(args_info->fg_orig));
238 free_string_field (&(args_info->bg_orig));
239 free_string_field (&(args_info->mode_arg));
240 free_string_field (&(args_info->mode_orig));
243 for (i = 0; i < args_info->inputs_num; ++i)
244 free (args_info->inputs [i]);
246 if (args_info->inputs_num)
247 free (args_info->inputs);
249 clear_given (args_info);
253 * @param val the value to check
254 * @param values the possible values
255 * @return the index of the matched value:
256 * -1 if no value matched,
257 * -2 if more than one value has matched
260 check_possible_values(const char *val, char *values[])
265 if (!val) /* otherwise strlen() crashes below */
266 return -1; /* -1 means no argument for the option */
270 for (i = 0, len = strlen(val); values[i]; ++i)
272 if (strncmp(val, values[i], len) == 0)
276 if (strlen(values[i]) == len)
277 return i; /* exact macth no need to check more */
281 if (found == 1) /* one match: OK */
284 return (found ? -2 : -1); /* return many values or none matched */
289 write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
294 found = check_possible_values(arg, values);
297 fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]);
299 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
301 fprintf(outfile, "%s\n", opt);
307 cmdline_parser_clitkBinarizeImage_dump(FILE *outfile, struct args_info_clitkBinarizeImage *args_info)
313 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE);
317 if (args_info->help_given)
318 write_into_file(outfile, "help", 0, 0 );
319 if (args_info->version_given)
320 write_into_file(outfile, "version", 0, 0 );
321 if (args_info->config_given)
322 write_into_file(outfile, "config", args_info->config_orig, 0);
323 if (args_info->verbose_given)
324 write_into_file(outfile, "verbose", 0, 0 );
325 if (args_info->imagetypes_given)
326 write_into_file(outfile, "imagetypes", 0, 0 );
327 if (args_info->input_given)
328 write_into_file(outfile, "input", args_info->input_orig, 0);
329 if (args_info->output_given)
330 write_into_file(outfile, "output", args_info->output_orig, 0);
331 if (args_info->lower_given)
332 write_into_file(outfile, "lower", args_info->lower_orig, 0);
333 if (args_info->upper_given)
334 write_into_file(outfile, "upper", args_info->upper_orig, 0);
335 if (args_info->fg_given)
336 write_into_file(outfile, "fg", args_info->fg_orig, 0);
337 if (args_info->bg_given)
338 write_into_file(outfile, "bg", args_info->bg_orig, 0);
339 if (args_info->mode_given)
340 write_into_file(outfile, "mode", args_info->mode_orig, cmdline_parser_clitkBinarizeImage_mode_values);
348 cmdline_parser_clitkBinarizeImage_file_save(const char *filename, struct args_info_clitkBinarizeImage *args_info)
353 outfile = fopen(filename, "w");
357 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename);
361 i = cmdline_parser_clitkBinarizeImage_dump(outfile, args_info);
368 cmdline_parser_clitkBinarizeImage_free (struct args_info_clitkBinarizeImage *args_info)
370 cmdline_parser_clitkBinarizeImage_release (args_info);
373 /** @brief replacement of strdup, which is not standard */
375 gengetopt_strdup (const char *s)
381 result = (char*)malloc(strlen(s) + 1);
382 if (result == (char*)0)
389 cmdline_parser_clitkBinarizeImage (int argc, char * const *argv, struct args_info_clitkBinarizeImage *args_info)
391 return cmdline_parser_clitkBinarizeImage2 (argc, argv, args_info, 0, 1, 1);
395 cmdline_parser_clitkBinarizeImage_ext (int argc, char * const *argv, struct args_info_clitkBinarizeImage *args_info,
396 struct cmdline_parser_clitkBinarizeImage_params *params)
399 result = cmdline_parser_clitkBinarizeImage_internal (argc, argv, args_info, params, NULL);
401 if (result == EXIT_FAILURE)
403 cmdline_parser_clitkBinarizeImage_free (args_info);
411 cmdline_parser_clitkBinarizeImage2 (int argc, char * const *argv, struct args_info_clitkBinarizeImage *args_info, int override, int initialize, int check_required)
414 struct cmdline_parser_clitkBinarizeImage_params params;
416 params.override = override;
417 params.initialize = initialize;
418 params.check_required = check_required;
419 params.check_ambiguity = 0;
420 params.print_errors = 1;
422 result = cmdline_parser_clitkBinarizeImage_internal (argc, argv, args_info, ¶ms, NULL);
424 if (result == EXIT_FAILURE)
426 cmdline_parser_clitkBinarizeImage_free (args_info);
434 cmdline_parser_clitkBinarizeImage_required (struct args_info_clitkBinarizeImage *args_info, const char *prog_name)
436 int result = EXIT_SUCCESS;
438 if (cmdline_parser_clitkBinarizeImage_required2(args_info, prog_name, NULL) > 0)
439 result = EXIT_FAILURE;
441 if (result == EXIT_FAILURE)
443 cmdline_parser_clitkBinarizeImage_free (args_info);
451 cmdline_parser_clitkBinarizeImage_required2 (struct args_info_clitkBinarizeImage *args_info, const char *prog_name, const char *additional_error)
455 /* checks for required options */
456 if (! args_info->input_given)
458 fprintf (stderr, "%s: '--input' ('-i') option required%s\n", prog_name, (additional_error ? additional_error : ""));
462 if (! args_info->output_given)
464 fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
469 /* checks for dependences among options */
475 * Extracted from the glibc source tree, version 2.3.6
477 * Licensed under the GPL as per the whole glibc source tree.
479 * This file was modified so that getopt_long can be called
480 * many times without risking previous memory to be spoiled.
482 * Modified by Andre Noll and Lorenzo Bettini for use in
483 * GNU gengetopt generated files.
488 * we must include anything we need since this file is not thought to be
489 * inserted in a file already using getopt.h
497 /* has_arg can't be an enum because some compilers complain about
498 type mismatches in all the code that assumes it is an int. */
504 /* For communication from `getopt' to the caller.
505 When `getopt' finds an option that takes an argument,
506 the argument value is returned here.
507 Also, when `ordering' is RETURN_IN_ORDER,
508 each non-option ARGV-element is returned here. */
512 /* Index in ARGV of the next element to be scanned.
513 This is used for communication to and from the caller
514 and for communication between successive calls to `getopt'.
516 On entry to `getopt', zero means this is the first call; initialize.
518 When `getopt' returns -1, this is the index of the first of the
519 non-option elements that the caller should itself scan.
521 Otherwise, `optind' communicates from one call to the next
522 how much of ARGV has been scanned so far. */
526 /* Callers store zero here to inhibit the error message `getopt' prints
527 for unrecognized options. */
531 /* Set to an option character which was unrecognized. */
535 /* This version of `getopt' appears to the caller like standard Unix `getopt'
536 but it behaves differently for the user, since it allows the user
537 to intersperse the options with the other arguments.
539 As `getopt' works, it permutes the elements of ARGV so that,
540 when it is done, all the options precede everything else. Thus
541 all application programs are extended to handle flexible argument order.
544 If the field `flag' is not NULL, it points to a variable that is set
545 to the value given in the field `val' when the option is found, but
546 left unchanged if the option is not found.
548 To have a long-named option do something other than set an `int' to
549 a compiled-in constant, such as set a value from `custom_optarg', set the
550 option's `flag' field to zero and its `val' field to a nonzero
551 value (the equivalent single-letter option character, if there is
552 one). For long options that have a zero `flag' field, `getopt'
553 returns the contents of the `val' field. */
555 /* Names for the values of the `has_arg' field of `struct option'. */
557 #define no_argument 0
560 #ifndef required_argument
561 #define required_argument 1
564 #ifndef optional_argument
565 #define optional_argument 2
568 struct custom_getopt_data {
570 * These have exactly the same meaning as the corresponding global variables,
571 * except that they are used for the reentrant versions of getopt.
578 /* True if the internal members have been initialized. */
582 * The next char to be scanned in the option-element in which the last option
583 * character we returned was found. This allows us to pick up the scan where
584 * we left off. If this is zero, or a null string, it means resume the scan by
585 * advancing to the next ARGV-element.
590 * Describe the part of ARGV that contains non-options that have been skipped.
591 * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
592 * the index after the last of them.
599 * the variables optarg, optind, opterr and optopt are renamed with
600 * the custom_ prefix so that they don't interfere with getopt ones.
602 * Moreover they're static so they are visible only from within the
603 * file where this very file will be included.
607 * For communication from `custom_getopt' to the caller. When `custom_getopt' finds an
608 * option that takes an argument, the argument value is returned here.
610 static char *custom_optarg;
613 * Index in ARGV of the next element to be scanned. This is used for
614 * communication to and from the caller and for communication between
615 * successive calls to `custom_getopt'.
617 * On entry to `custom_getopt', 1 means this is the first call; initialize.
619 * When `custom_getopt' returns -1, this is the index of the first of the non-option
620 * elements that the caller should itself scan.
622 * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
623 * has been scanned so far.
625 * 1003.2 says this must be 1 before any call.
627 static int custom_optind = 1;
630 * Callers store zero here to inhibit the error message for unrecognized
633 static int custom_opterr = 1;
636 * Set to an option character which was unrecognized. This must be initialized
637 * on some systems to avoid linking in the system's own getopt implementation.
639 static int custom_optopt = '?';
642 * Exchange two adjacent subsequences of ARGV. One subsequence is elements
643 * [first_nonopt,last_nonopt) which contains all the non-options that have been
644 * skipped so far. The other is elements [last_nonopt,custom_optind), which contains
645 * all the options processed since those non-options were skipped.
646 * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
647 * indices of the non-options in ARGV after they are moved.
649 static void exchange(char **argv, struct custom_getopt_data *d)
651 int bottom = d->first_nonopt;
652 int middle = d->last_nonopt;
653 int top = d->custom_optind;
657 * Exchange the shorter segment with the far end of the longer segment.
658 * That puts the shorter segment into the right place. It leaves the
659 * longer segment in the right place overall, but it consists of two
660 * parts that need to be swapped next.
662 while (top > middle && middle > bottom) {
663 if (top - middle > middle - bottom) {
664 /* Bottom segment is the short one. */
665 int len = middle - bottom;
668 /* Swap it with the top part of the top segment. */
669 for (i = 0; i < len; i++) {
670 tem = argv[bottom + i];
672 argv[top - (middle - bottom) + i];
673 argv[top - (middle - bottom) + i] = tem;
675 /* Exclude the moved bottom segment from further swapping. */
678 /* Top segment is the short one. */
679 int len = top - middle;
682 /* Swap it with the bottom part of the bottom segment. */
683 for (i = 0; i < len; i++) {
684 tem = argv[bottom + i];
685 argv[bottom + i] = argv[middle + i];
686 argv[middle + i] = tem;
688 /* Exclude the moved top segment from further swapping. */
692 /* Update records for the slots the non-options now occupy. */
693 d->first_nonopt += (d->custom_optind - d->last_nonopt);
694 d->last_nonopt = d->custom_optind;
697 /* Initialize the internal data when the first call is made. */
698 static void custom_getopt_initialize(struct custom_getopt_data *d)
701 * Start processing options with ARGV-element 1 (since ARGV-element 0
702 * is the program name); the sequence of previously skipped non-option
703 * ARGV-elements is empty.
705 d->first_nonopt = d->last_nonopt = d->custom_optind;
710 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
712 /* return: zero: continue, nonzero: return given value to user */
713 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
714 struct custom_getopt_data *d)
717 * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
718 * moved back by the user (who may also have changed the arguments).
720 if (d->last_nonopt > d->custom_optind)
721 d->last_nonopt = d->custom_optind;
722 if (d->first_nonopt > d->custom_optind)
723 d->first_nonopt = d->custom_optind;
725 * If we have just processed some options following some
726 * non-options, exchange them so that the options come first.
728 if (d->first_nonopt != d->last_nonopt &&
729 d->last_nonopt != d->custom_optind)
730 exchange((char **) argv, d);
731 else if (d->last_nonopt != d->custom_optind)
732 d->first_nonopt = d->custom_optind;
734 * Skip any additional non-options and extend the range of
735 * non-options previously skipped.
737 while (d->custom_optind < argc && NONOPTION_P)
739 d->last_nonopt = d->custom_optind;
741 * The special ARGV-element `--' means premature end of options. Skip
742 * it like a null option, then exchange with previous non-options as if
743 * it were an option, then skip everything else like a non-option.
745 if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
747 if (d->first_nonopt != d->last_nonopt
748 && d->last_nonopt != d->custom_optind)
749 exchange((char **) argv, d);
750 else if (d->first_nonopt == d->last_nonopt)
751 d->first_nonopt = d->custom_optind;
752 d->last_nonopt = argc;
753 d->custom_optind = argc;
756 * If we have done all the ARGV-elements, stop the scan and back over
757 * any non-options that we skipped and permuted.
759 if (d->custom_optind == argc) {
761 * Set the next-arg-index to point at the non-options that we
762 * previously skipped, so the caller will digest them.
764 if (d->first_nonopt != d->last_nonopt)
765 d->custom_optind = d->first_nonopt;
769 * If we have come to a non-option and did not permute it, either stop
770 * the scan or describe it to the caller and pass it by.
773 d->custom_optarg = argv[d->custom_optind++];
777 * We have found another option-ARGV-element. Skip the initial
780 d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
785 * Check whether the ARGV-element is a long option.
787 * If there's a long option "fubar" and the ARGV-element is "-fu", consider
788 * that an abbreviation of the long option, just like "--fu", and not "-f" with
791 * This distinction seems to be the most useful approach.
794 static int check_long_opt(int argc, char *const *argv, const char *optstring,
795 const struct option *longopts, int *longind,
796 int print_errors, struct custom_getopt_data *d)
799 const struct option *p;
800 const struct option *pfound = NULL;
806 for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
809 /* Test all long options for either exact match or abbreviated matches */
810 for (p = longopts, option_index = 0; p->name; p++, option_index++)
811 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
812 if ((unsigned int) (nameend - d->nextchar)
813 == (unsigned int) strlen(p->name)) {
814 /* Exact match found. */
816 indfound = option_index;
819 } else if (pfound == NULL) {
820 /* First nonexact match found. */
822 indfound = option_index;
823 } else if (pfound->has_arg != p->has_arg
824 || pfound->flag != p->flag
825 || pfound->val != p->val)
826 /* Second or later nonexact match found. */
829 if (ambig && !exact) {
832 "%s: option `%s' is ambiguous\n",
833 argv[0], argv[d->custom_optind]);
835 d->nextchar += strlen(d->nextchar);
837 d->custom_optopt = 0;
841 option_index = indfound;
844 if (pfound->has_arg != no_argument)
845 d->custom_optarg = nameend + 1;
848 if (argv[d->custom_optind - 1][1] == '-') {
850 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
851 argv[0], pfound->name);
853 /* +option or -option */
854 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
855 argv[0], argv[d->custom_optind - 1][0], pfound->name);
859 d->nextchar += strlen(d->nextchar);
860 d->custom_optopt = pfound->val;
863 } else if (pfound->has_arg == required_argument) {
864 if (d->custom_optind < argc)
865 d->custom_optarg = argv[d->custom_optind++];
869 "%s: option `%s' requires an argument\n",
871 argv[d->custom_optind - 1]);
873 d->nextchar += strlen(d->nextchar);
874 d->custom_optopt = pfound->val;
875 return optstring[0] == ':' ? ':' : '?';
878 d->nextchar += strlen(d->nextchar);
880 *longind = option_index;
882 *(pfound->flag) = pfound->val;
888 * Can't find it as a long option. If this is not getopt_long_only, or
889 * the option starts with '--' or is not a valid short option, then
890 * it's an error. Otherwise interpret it as a short option.
893 if (argv[d->custom_optind][1] == '-') {
896 "%s: unrecognized option `--%s'\n",
897 argv[0], d->nextchar);
899 /* +option or -option */
901 "%s: unrecognized option `%c%s'\n",
902 argv[0], argv[d->custom_optind][0],
906 d->nextchar = (char *) "";
908 d->custom_optopt = 0;
912 static int check_short_opt(int argc, char *const *argv, const char *optstring,
913 int print_errors, struct custom_getopt_data *d)
915 char c = *d->nextchar++;
916 char *temp = strchr(optstring, c);
918 /* Increment `custom_optind' when we start to process its last character. */
919 if (*d->nextchar == '\0')
921 if (!temp || c == ':') {
923 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
925 d->custom_optopt = c;
928 if (temp[1] == ':') {
929 if (temp[2] == ':') {
930 /* This is an option that accepts an argument optionally. */
931 if (*d->nextchar != '\0') {
932 d->custom_optarg = d->nextchar;
935 d->custom_optarg = NULL;
938 /* This is an option that requires an argument. */
939 if (*d->nextchar != '\0') {
940 d->custom_optarg = d->nextchar;
942 * If we end this ARGV-element by taking the
943 * rest as an arg, we must advance to the next
947 } else if (d->custom_optind == argc) {
950 "%s: option requires an argument -- %c\n",
953 d->custom_optopt = c;
954 if (optstring[0] == ':')
960 * We already incremented `custom_optind' once;
961 * increment it again when taking next ARGV-elt
964 d->custom_optarg = argv[d->custom_optind++];
972 * Scan elements of ARGV for option characters given in OPTSTRING.
974 * If an element of ARGV starts with '-', and is not exactly "-" or "--",
975 * then it is an option element. The characters of this element
976 * (aside from the initial '-') are option characters. If `getopt'
977 * is called repeatedly, it returns successively each of the option characters
978 * from each of the option elements.
980 * If `getopt' finds another option character, it returns that character,
981 * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
982 * resume the scan with the following option character or ARGV-element.
984 * If there are no more option characters, `getopt' returns -1.
985 * Then `custom_optind' is the index in ARGV of the first ARGV-element
986 * that is not an option. (The ARGV-elements have been permuted
987 * so that those that are not options now come last.)
989 * OPTSTRING is a string containing the legitimate option characters.
990 * If an option character is seen that is not listed in OPTSTRING,
991 * return '?' after printing an error message. If you set `custom_opterr' to
992 * zero, the error message is suppressed but we still return '?'.
994 * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
995 * so the following text in the same ARGV-element, or the text of the following
996 * ARGV-element, is returned in `custom_optarg'. Two colons mean an option that
997 * wants an optional arg; if there is text in the current ARGV-element,
998 * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
1000 * If OPTSTRING starts with `-' or `+', it requests different methods of
1001 * handling the non-option ARGV-elements.
1002 * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
1004 * Long-named options begin with `--' instead of `-'.
1005 * Their names may be abbreviated as long as the abbreviation is unique
1006 * or is an exact match for some defined option. If they have an
1007 * argument, it follows the option name in the same ARGV-element, separated
1008 * from the option name by a `=', or else the in next ARGV-element.
1009 * When `getopt' finds a long-named option, it returns 0 if that option's
1010 * `flag' field is nonzero, the value of the option's `val' field
1011 * if the `flag' field is zero.
1013 * The elements of ARGV aren't really const, because we permute them.
1014 * But we pretend they're const in the prototype to be compatible
1015 * with other systems.
1017 * LONGOPTS is a vector of `struct option' terminated by an
1018 * element containing a name which is zero.
1020 * LONGIND returns the index in LONGOPT of the long-named option found.
1021 * It is only valid when a long-named option has been found by the most
1024 * Return the option character from OPTS just read. Return -1 when there are
1025 * no more options. For unrecognized options, or options missing arguments,
1026 * `custom_optopt' is set to the option letter, and '?' is returned.
1028 * The OPTS string is a list of characters which are recognized option letters,
1029 * optionally followed by colons, specifying that that letter takes an
1030 * argument, to be placed in `custom_optarg'.
1032 * If a letter in OPTS is followed by two colons, its argument is optional.
1033 * This behavior is specific to the GNU `getopt'.
1035 * The argument `--' causes premature termination of argument scanning,
1036 * explicitly telling `getopt' that there are no more options. If OPTS begins
1037 * with `--', then non-option arguments are treated as arguments to the option
1038 * '\0'. This behavior is specific to the GNU `getopt'.
1041 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
1042 const struct option *longopts, int *longind,
1043 struct custom_getopt_data *d)
1045 int ret, print_errors = d->custom_opterr;
1047 if (optstring[0] == ':')
1051 d->custom_optarg = NULL;
1054 * This is a big difference with GNU getopt, since optind == 0
1055 * means initialization while here 1 means first call.
1057 if (d->custom_optind == 0 || !d->initialized) {
1058 if (d->custom_optind == 0)
1059 d->custom_optind = 1; /* Don't scan ARGV[0], the program name. */
1060 custom_getopt_initialize(d);
1062 if (d->nextchar == NULL || *d->nextchar == '\0') {
1063 ret = shuffle_argv(argc, argv, longopts, d);
1067 if (longopts && (argv[d->custom_optind][1] == '-' ))
1068 return check_long_opt(argc, argv, optstring, longopts,
1069 longind, print_errors, d);
1070 return check_short_opt(argc, argv, optstring, print_errors, d);
1073 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
1074 const struct option *longopts, int *longind)
1077 /* Keep a global copy of all internal members of d */
1078 static struct custom_getopt_data d;
1080 d.custom_optind = custom_optind;
1081 d.custom_opterr = custom_opterr;
1082 result = getopt_internal_r(argc, argv, optstring, longopts,
1084 custom_optind = d.custom_optind;
1085 custom_optarg = d.custom_optarg;
1086 custom_optopt = d.custom_optopt;
1090 static int custom_getopt_long (int argc, char *const *argv, const char *options,
1091 const struct option *long_options, int *opt_index)
1093 return custom_getopt_internal(argc, argv, options, long_options,
1098 static char *package_name = 0;
1101 * @brief updates an option
1102 * @param field the generic pointer to the field to update
1103 * @param orig_field the pointer to the orig field
1104 * @param field_given the pointer to the number of occurrence of this option
1105 * @param prev_given the pointer to the number of occurrence already seen
1106 * @param value the argument for this option (if null no arg was specified)
1107 * @param possible_values the possible values for this option (if specified)
1108 * @param default_value the default value (in case the option only accepts fixed values)
1109 * @param arg_type the type of this option
1110 * @param check_ambiguity @see cmdline_parser_clitkBinarizeImage_params.check_ambiguity
1111 * @param override @see cmdline_parser_clitkBinarizeImage_params.override
1112 * @param no_free whether to free a possible previous value
1113 * @param multiple_option whether this is a multiple option
1114 * @param long_opt the corresponding long option
1115 * @param short_opt the corresponding short option (or '-' if none)
1116 * @param additional_error possible further error specification
1119 int update_arg(void *field, char **orig_field,
1120 unsigned int *field_given, unsigned int *prev_given,
1121 char *value, char *possible_values[], const char *default_value,
1122 cmdline_parser_clitkBinarizeImage_arg_type arg_type,
1123 int check_ambiguity, int override,
1124 int no_free, int multiple_option,
1125 const char *long_opt, char short_opt,
1126 const char *additional_error)
1128 char *stop_char = 0;
1129 const char *val = value;
1131 char **string_field;
1136 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1138 if (short_opt != '-')
1139 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
1140 package_name, long_opt, short_opt,
1141 (additional_error ? additional_error : ""));
1143 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
1144 package_name, long_opt,
1145 (additional_error ? additional_error : ""));
1146 return 1; /* failure */
1149 if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
1151 if (short_opt != '-')
1152 fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n",
1153 package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt, short_opt,
1154 (additional_error ? additional_error : ""));
1156 fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s'%s\n",
1157 package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt,
1158 (additional_error ? additional_error : ""));
1159 return 1; /* failure */
1162 if (field_given && *field_given && ! override)
1168 if (possible_values)
1169 val = possible_values[found];
1173 *((int *)field) = !*((int *)field);
1176 if (val) *((double *)field) = strtod (val, &stop_char);
1180 string_field = (char **)field;
1181 if (!no_free && *string_field)
1182 free (*string_field); /* free previous string */
1183 *string_field = gengetopt_strdup (val);
1190 /* check numeric conversion */
1193 if (val && !(stop_char && *stop_char == '\0')) {
1194 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1195 return 1; /* failure */
1202 /* store the original value */
1208 if (value && orig_field) {
1210 *orig_field = value;
1213 free (*orig_field); /* free previous string */
1214 *orig_field = gengetopt_strdup (value);
1224 cmdline_parser_clitkBinarizeImage_internal (int argc, char * const *argv, struct args_info_clitkBinarizeImage *args_info,
1225 struct cmdline_parser_clitkBinarizeImage_params *params, const char *additional_error)
1227 int c; /* Character of the parsed option. */
1230 struct args_info_clitkBinarizeImage local_args_info;
1235 int check_ambiguity;
1237 package_name = argv[0];
1239 override = params->override;
1240 initialize = params->initialize;
1241 check_required = params->check_required;
1242 check_ambiguity = params->check_ambiguity;
1245 cmdline_parser_clitkBinarizeImage_init (args_info);
1247 cmdline_parser_clitkBinarizeImage_init (&local_args_info);
1251 opterr = params->print_errors;
1256 int option_index = 0;
1258 static struct option long_options[] = {
1259 { "help", 0, NULL, 'h' },
1260 { "version", 0, NULL, 'V' },
1261 { "config", 1, NULL, 0 },
1262 { "verbose", 0, NULL, 'v' },
1263 { "imagetypes", 0, NULL, 0 },
1264 { "input", 1, NULL, 'i' },
1265 { "output", 1, NULL, 'o' },
1266 { "lower", 1, NULL, 'l' },
1267 { "upper", 1, NULL, 'u' },
1268 { "fg", 1, NULL, 0 },
1269 { "bg", 1, NULL, 0 },
1270 { "mode", 1, NULL, 0 },
1271 { NULL, 0, NULL, 0 }
1274 custom_optarg = optarg;
1275 custom_optind = optind;
1276 custom_opterr = opterr;
1277 custom_optopt = optopt;
1279 c = custom_getopt_long (argc, argv, "hVvi:o:l:u:", long_options, &option_index);
1281 optarg = custom_optarg;
1282 optind = custom_optind;
1283 opterr = custom_opterr;
1284 optopt = custom_optopt;
1286 if (c == -1) break; /* Exit from `while (1)' loop. */
1290 case 'h': /* Print help and exit. */
1291 cmdline_parser_clitkBinarizeImage_print_help ();
1292 cmdline_parser_clitkBinarizeImage_free (&local_args_info);
1293 exit (EXIT_SUCCESS);
1295 case 'V': /* Print version and exit. */
1296 cmdline_parser_clitkBinarizeImage_print_version ();
1297 cmdline_parser_clitkBinarizeImage_free (&local_args_info);
1298 exit (EXIT_SUCCESS);
1300 case 'v': /* Verbose. */
1303 if (update_arg((void *)&(args_info->verbose_flag), 0, &(args_info->verbose_given),
1304 &(local_args_info.verbose_given), optarg, 0, 0, ARG_FLAG,
1305 check_ambiguity, override, 1, 0, "verbose", 'v',
1310 case 'i': /* Input image filename. */
1313 if (update_arg( (void *)&(args_info->input_arg),
1314 &(args_info->input_orig), &(args_info->input_given),
1315 &(local_args_info.input_given), optarg, 0, 0, ARG_STRING,
1316 check_ambiguity, override, 0, 0,
1322 case 'o': /* Output image filename. */
1325 if (update_arg( (void *)&(args_info->output_arg),
1326 &(args_info->output_orig), &(args_info->output_given),
1327 &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1328 check_ambiguity, override, 0, 0,
1334 case 'l': /* Lower intensity (default=min), fg is greater than this value. */
1337 if (update_arg( (void *)&(args_info->lower_arg),
1338 &(args_info->lower_orig), &(args_info->lower_given),
1339 &(local_args_info.lower_given), optarg, 0, 0, ARG_DOUBLE,
1340 check_ambiguity, override, 0, 0,
1346 case 'u': /* Upper intensity (default=max), fg is lower than this value. */
1349 if (update_arg( (void *)&(args_info->upper_arg),
1350 &(args_info->upper_orig), &(args_info->upper_given),
1351 &(local_args_info.upper_given), optarg, 0, 0, ARG_DOUBLE,
1352 check_ambiguity, override, 0, 0,
1359 case 0: /* Long option with no short option */
1361 if (strcmp (long_options[option_index].name, "config") == 0)
1365 if (update_arg( (void *)&(args_info->config_arg),
1366 &(args_info->config_orig), &(args_info->config_given),
1367 &(local_args_info.config_given), optarg, 0, 0, ARG_STRING,
1368 check_ambiguity, override, 0, 0,
1374 /* Display allowed image types. */
1375 else if (strcmp (long_options[option_index].name, "imagetypes") == 0)
1379 if (update_arg((void *)&(args_info->imagetypes_flag), 0, &(args_info->imagetypes_given),
1380 &(local_args_info.imagetypes_given), optarg, 0, 0, ARG_FLAG,
1381 check_ambiguity, override, 1, 0, "imagetypes", '-',
1386 /* Foreground (FG) or 'inside' value. */
1387 else if (strcmp (long_options[option_index].name, "fg") == 0)
1391 if (update_arg( (void *)&(args_info->fg_arg),
1392 &(args_info->fg_orig), &(args_info->fg_given),
1393 &(local_args_info.fg_given), optarg, 0, "1", ARG_DOUBLE,
1394 check_ambiguity, override, 0, 0,
1400 /* Background (BG) or 'ouside' value. */
1401 else if (strcmp (long_options[option_index].name, "bg") == 0)
1405 if (update_arg( (void *)&(args_info->bg_arg),
1406 &(args_info->bg_orig), &(args_info->bg_given),
1407 &(local_args_info.bg_given), optarg, 0, "0", ARG_DOUBLE,
1408 check_ambiguity, override, 0, 0,
1414 /* Use FG and/or BG values (if FG, the BG is replaced by the input image values). */
1415 else if (strcmp (long_options[option_index].name, "mode") == 0)
1419 if (update_arg( (void *)&(args_info->mode_arg),
1420 &(args_info->mode_orig), &(args_info->mode_given),
1421 &(local_args_info.mode_given), optarg, cmdline_parser_clitkBinarizeImage_mode_values, "both", ARG_STRING,
1422 check_ambiguity, override, 0, 0,
1430 case '?': /* Invalid option. */
1431 /* `getopt_long' already printed an error message. */
1434 default: /* bug: option not considered. */
1435 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, c, (additional_error ? additional_error : ""));
1444 error += cmdline_parser_clitkBinarizeImage_required2 (args_info, argv[0], additional_error);
1447 cmdline_parser_clitkBinarizeImage_release (&local_args_info);
1450 return (EXIT_FAILURE);
1455 int found_prog_name = 0;
1456 /* whether program name, i.e., argv[0], is in the remaining args
1457 (this may happen with some implementations of getopt,
1458 but surely not with the one included by gengetopt) */
1461 args_info->inputs_num = argc - optind - found_prog_name;
1463 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1464 while (optind < argc)
1465 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1472 cmdline_parser_clitkBinarizeImage_release (&local_args_info);
1473 return (EXIT_FAILURE);
1476 #ifndef CONFIG_FILE_LINE_SIZE
1477 #define CONFIG_FILE_LINE_SIZE 2048
1479 #define ADDITIONAL_ERROR " in configuration file "
1481 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1482 /* 3 is for "--" and "=" */
1485 _cmdline_parser_clitkBinarizeImage_configfile (char * const filename, int *my_argc)
1488 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
1489 char linebuf[CONFIG_FILE_LINE_SIZE];
1491 int result = 0, equal;
1494 size_t len, next_token;
1497 if ((file = fopen(filename, "r")) == NULL)
1499 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
1500 CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename);
1501 return EXIT_FAILURE;
1504 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
1508 len = strlen(linebuf);
1509 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
1511 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
1512 CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename, line_num);
1513 result = EXIT_FAILURE;
1517 /* find first non-whitespace character in the line */
1518 next_token = strspn (linebuf, " \t\r\n");
1519 str_index = linebuf + next_token;
1521 if ( str_index[0] == '\0' || str_index[0] == '#')
1522 continue; /* empty line or comment line is skipped */
1526 /* truncate fopt at the end of the first non-valid character */
1527 next_token = strcspn (fopt, " \t\r\n=");
1529 if (fopt[next_token] == '\0') /* the line is over */
1536 /* remember if equal sign is present */
1537 equal = (fopt[next_token] == '=');
1538 fopt[next_token++] = '\0';
1540 /* advance pointers to the next token after the end of fopt */
1541 next_token += strspn (fopt + next_token, " \t\r\n");
1543 /* check for the presence of equal sign, and if so, skip it */
1545 if ((equal = (fopt[next_token] == '=')))
1548 next_token += strspn (fopt + next_token, " \t\r\n");
1550 str_index += next_token;
1554 if ( farg[0] == '\"' || farg[0] == '\'' )
1555 { /* quoted argument */
1556 str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1561 "%s:%s:%d: unterminated string in configuration file\n",
1562 CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename, line_num);
1563 result = EXIT_FAILURE;
1568 { /* read up the remaining part up to a delimiter */
1569 next_token = strcspn (farg, " \t\r\n#\'\"");
1570 str_index += next_token;
1573 /* truncate farg at the delimiter and store it for further check */
1574 delimiter = *str_index, *str_index++ = '\0';
1576 /* everything but comment is illegal at the end of line */
1577 if (delimiter != '\0' && delimiter != '#')
1579 str_index += strspn(str_index, " \t\r\n");
1580 if (*str_index != '\0' && *str_index != '#')
1584 "%s:%s:%d: malformed string in configuration file\n",
1585 CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename, line_num);
1586 result = EXIT_FAILURE;
1592 if (!strcmp(fopt,"include")) {
1593 if (farg && *farg) {
1594 result = _cmdline_parser_clitkBinarizeImage_configfile(farg, my_argc);
1596 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1597 CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename, line_num);
1602 strcat (my_argv, len > 1 ? "--" : "-");
1603 strcat (my_argv, fopt);
1604 if (len > 1 && ((farg && *farg) || equal))
1605 strcat (my_argv, "=");
1607 strcat (my_argv, farg);
1610 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1611 cmd_line_list_tmp->next = cmd_line_list;
1612 cmd_line_list = cmd_line_list_tmp;
1613 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1622 cmdline_parser_clitkBinarizeImage_configfile (char * const filename,
1623 struct args_info_clitkBinarizeImage *args_info,
1624 int override, int initialize, int check_required)
1626 struct cmdline_parser_clitkBinarizeImage_params params;
1628 params.override = override;
1629 params.initialize = initialize;
1630 params.check_required = check_required;
1631 params.check_ambiguity = 0;
1632 params.print_errors = 1;
1634 return cmdline_parser_clitkBinarizeImage_config_file (filename, args_info, ¶ms);
1638 cmdline_parser_clitkBinarizeImage_config_file (char * const filename,
1639 struct args_info_clitkBinarizeImage *args_info,
1640 struct cmdline_parser_clitkBinarizeImage_params *params)
1645 char *additional_error;
1647 /* store the program name */
1648 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1649 cmd_line_list_tmp->next = cmd_line_list;
1650 cmd_line_list = cmd_line_list_tmp;
1651 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE);
1653 result = _cmdline_parser_clitkBinarizeImage_configfile(filename, &my_argc);
1655 if (result != EXIT_FAILURE) {
1656 my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1657 cmd_line_list_tmp = cmd_line_list;
1659 for (i = my_argc - 1; i >= 0; --i) {
1660 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1661 cmd_line_list_tmp = cmd_line_list_tmp->next;
1664 my_argv_arg[my_argc] = 0;
1666 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1667 strcpy (additional_error, ADDITIONAL_ERROR);
1668 strcat (additional_error, filename);
1670 cmdline_parser_clitkBinarizeImage_internal (my_argc, my_argv_arg, args_info,
1674 free (additional_error);
1679 if (result == EXIT_FAILURE)
1681 cmdline_parser_clitkBinarizeImage_free (args_info);
1682 exit (EXIT_FAILURE);