]> Creatis software - clitk.git/blob - vv/tools_ggo/clitkBinarizeImage_ggo.c
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / vv / tools_ggo / clitkBinarizeImage_ggo.c
1 /*
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 
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
21 #include "clitkBinarizeImage_ggo.h"
22
23 const char *args_info_clitkBinarizeImage_purpose = "";
24
25 const char *args_info_clitkBinarizeImage_usage = "Usage: clitkBinarizeImage [OPTIONS]... [FILES]...";
26
27 const char *args_info_clitkBinarizeImage_description = "";
28
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')",
42     0
43 };
44
45 typedef enum {ARG_NO
46   , ARG_FLAG
47   , ARG_STRING
48   , ARG_DOUBLE
49 } cmdline_parser_clitkBinarizeImage_arg_type;
50
51 static
52 void clear_given (struct args_info_clitkBinarizeImage *args_info);
53 static
54 void clear_args (struct args_info_clitkBinarizeImage *args_info);
55
56 static int
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);
59
60 static int
61 cmdline_parser_clitkBinarizeImage_required2 (struct args_info_clitkBinarizeImage *args_info, const char *prog_name, const char *additional_error);
62 struct line_list
63 {
64   char * string_arg;
65   struct line_list * next;
66 };
67
68 static struct line_list *cmd_line_list = 0;
69 static struct line_list *cmd_line_list_tmp = 0;
70
71 static void
72 free_cmd_list(void)
73 {
74   /* free the list of a previous call */
75   if (cmd_line_list)
76     {
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);
82       }
83     }
84 }
85
86
87 char *cmdline_parser_clitkBinarizeImage_mode_values[] = {"FG", "BG", "both", 0} ;       /* Possible values for mode.  */
88
89 static char *
90 gengetopt_strdup (const char *s);
91
92 static
93 void clear_given (struct args_info_clitkBinarizeImage *args_info)
94 {
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 ;
107 }
108
109 static
110 void clear_args (struct args_info_clitkBinarizeImage *args_info)
111 {
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;
128   
129 }
130
131 static
132 void init_args_info(struct args_info_clitkBinarizeImage *args_info)
133 {
134
135
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] ;
148   
149 }
150
151 void
152 cmdline_parser_clitkBinarizeImage_print_version (void)
153 {
154   printf ("%s %s\n", CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, CMDLINE_PARSER_CLITKBINARIZEIMAGE_VERSION);
155 }
156
157 static void print_help_common(void) {
158   cmdline_parser_clitkBinarizeImage_print_version ();
159
160   if (strlen(args_info_clitkBinarizeImage_purpose) > 0)
161     printf("\n%s\n", args_info_clitkBinarizeImage_purpose);
162
163   if (strlen(args_info_clitkBinarizeImage_usage) > 0)
164     printf("\n%s\n", args_info_clitkBinarizeImage_usage);
165
166   printf("\n");
167
168   if (strlen(args_info_clitkBinarizeImage_description) > 0)
169     printf("%s\n", args_info_clitkBinarizeImage_description);
170 }
171
172 void
173 cmdline_parser_clitkBinarizeImage_print_help (void)
174 {
175   int i = 0;
176   print_help_common();
177   while (args_info_clitkBinarizeImage_help[i])
178     printf("%s\n", args_info_clitkBinarizeImage_help[i++]);
179 }
180
181 void
182 cmdline_parser_clitkBinarizeImage_init (struct args_info_clitkBinarizeImage *args_info)
183 {
184   clear_given (args_info);
185   clear_args (args_info);
186   init_args_info (args_info);
187
188   args_info->inputs = NULL;
189   args_info->inputs_num = 0;
190 }
191
192 void
193 cmdline_parser_clitkBinarizeImage_params_init(struct cmdline_parser_clitkBinarizeImage_params *params)
194 {
195   if (params)
196     { 
197       params->override = 0;
198       params->initialize = 1;
199       params->check_required = 1;
200       params->check_ambiguity = 0;
201       params->print_errors = 1;
202     }
203 }
204
205 struct cmdline_parser_clitkBinarizeImage_params *
206 cmdline_parser_clitkBinarizeImage_params_create(void)
207 {
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);  
211   return params;
212 }
213
214 static void
215 free_string_field (char **s)
216 {
217   if (*s)
218     {
219       free (*s);
220       *s = 0;
221     }
222 }
223
224
225 static void
226 cmdline_parser_clitkBinarizeImage_release (struct args_info_clitkBinarizeImage *args_info)
227 {
228   unsigned int i;
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));
241   
242   
243   for (i = 0; i < args_info->inputs_num; ++i)
244     free (args_info->inputs [i]);
245
246   if (args_info->inputs_num)
247     free (args_info->inputs);
248
249   clear_given (args_info);
250 }
251
252 /**
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
258  */
259 static int
260 check_possible_values(const char *val, char *values[])
261 {
262   int i, found, last;
263   size_t len;
264
265   if (!val)   /* otherwise strlen() crashes below */
266     return -1; /* -1 means no argument for the option */
267
268   found = last = 0;
269
270   for (i = 0, len = strlen(val); values[i]; ++i)
271     {
272       if (strncmp(val, values[i], len) == 0)
273         {
274           ++found;
275           last = i;
276           if (strlen(values[i]) == len)
277             return i; /* exact macth no need to check more */
278         }
279     }
280
281   if (found == 1) /* one match: OK */
282     return last;
283
284   return (found ? -2 : -1); /* return many values or none matched */
285 }
286
287
288 static void
289 write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
290 {
291   int found = -1;
292   if (arg) {
293     if (values) {
294       found = check_possible_values(arg, values);      
295     }
296     if (found >= 0)
297       fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]);
298     else
299       fprintf(outfile, "%s=\"%s\"\n", opt, arg);
300   } else {
301     fprintf(outfile, "%s\n", opt);
302   }
303 }
304
305
306 int
307 cmdline_parser_clitkBinarizeImage_dump(FILE *outfile, struct args_info_clitkBinarizeImage *args_info)
308 {
309   int i = 0;
310
311   if (!outfile)
312     {
313       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE);
314       return EXIT_FAILURE;
315     }
316
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);
341   
342
343   i = EXIT_SUCCESS;
344   return i;
345 }
346
347 int
348 cmdline_parser_clitkBinarizeImage_file_save(const char *filename, struct args_info_clitkBinarizeImage *args_info)
349 {
350   FILE *outfile;
351   int i = 0;
352
353   outfile = fopen(filename, "w");
354
355   if (!outfile)
356     {
357       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename);
358       return EXIT_FAILURE;
359     }
360
361   i = cmdline_parser_clitkBinarizeImage_dump(outfile, args_info);
362   fclose (outfile);
363
364   return i;
365 }
366
367 void
368 cmdline_parser_clitkBinarizeImage_free (struct args_info_clitkBinarizeImage *args_info)
369 {
370   cmdline_parser_clitkBinarizeImage_release (args_info);
371 }
372
373 /** @brief replacement of strdup, which is not standard */
374 char *
375 gengetopt_strdup (const char *s)
376 {
377   char *result = NULL;
378   if (!s)
379     return result;
380
381   result = (char*)malloc(strlen(s) + 1);
382   if (result == (char*)0)
383     return (char*)0;
384   strcpy(result, s);
385   return result;
386 }
387
388 int
389 cmdline_parser_clitkBinarizeImage (int argc, char * const *argv, struct args_info_clitkBinarizeImage *args_info)
390 {
391   return cmdline_parser_clitkBinarizeImage2 (argc, argv, args_info, 0, 1, 1);
392 }
393
394 int
395 cmdline_parser_clitkBinarizeImage_ext (int argc, char * const *argv, struct args_info_clitkBinarizeImage *args_info,
396                    struct cmdline_parser_clitkBinarizeImage_params *params)
397 {
398   int result;
399   result = cmdline_parser_clitkBinarizeImage_internal (argc, argv, args_info, params, NULL);
400
401   if (result == EXIT_FAILURE)
402     {
403       cmdline_parser_clitkBinarizeImage_free (args_info);
404       exit (EXIT_FAILURE);
405     }
406   
407   return result;
408 }
409
410 int
411 cmdline_parser_clitkBinarizeImage2 (int argc, char * const *argv, struct args_info_clitkBinarizeImage *args_info, int override, int initialize, int check_required)
412 {
413   int result;
414   struct cmdline_parser_clitkBinarizeImage_params params;
415   
416   params.override = override;
417   params.initialize = initialize;
418   params.check_required = check_required;
419   params.check_ambiguity = 0;
420   params.print_errors = 1;
421
422   result = cmdline_parser_clitkBinarizeImage_internal (argc, argv, args_info, &params, NULL);
423
424   if (result == EXIT_FAILURE)
425     {
426       cmdline_parser_clitkBinarizeImage_free (args_info);
427       exit (EXIT_FAILURE);
428     }
429   
430   return result;
431 }
432
433 int
434 cmdline_parser_clitkBinarizeImage_required (struct args_info_clitkBinarizeImage *args_info, const char *prog_name)
435 {
436   int result = EXIT_SUCCESS;
437
438   if (cmdline_parser_clitkBinarizeImage_required2(args_info, prog_name, NULL) > 0)
439     result = EXIT_FAILURE;
440
441   if (result == EXIT_FAILURE)
442     {
443       cmdline_parser_clitkBinarizeImage_free (args_info);
444       exit (EXIT_FAILURE);
445     }
446   
447   return result;
448 }
449
450 int
451 cmdline_parser_clitkBinarizeImage_required2 (struct args_info_clitkBinarizeImage *args_info, const char *prog_name, const char *additional_error)
452 {
453   int error = 0;
454
455   /* checks for required options */
456   if (! args_info->input_given)
457     {
458       fprintf (stderr, "%s: '--input' ('-i') option required%s\n", prog_name, (additional_error ? additional_error : ""));
459       error = 1;
460     }
461   
462   if (! args_info->output_given)
463     {
464       fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
465       error = 1;
466     }
467   
468   
469   /* checks for dependences among options */
470
471   return error;
472 }
473
474 /*
475  * Extracted from the glibc source tree, version 2.3.6
476  *
477  * Licensed under the GPL as per the whole glibc source tree.
478  *
479  * This file was modified so that getopt_long can be called
480  * many times without risking previous memory to be spoiled.
481  *
482  * Modified by Andre Noll and Lorenzo Bettini for use in
483  * GNU gengetopt generated files.
484  *
485  */
486
487 /* 
488  * we must include anything we need since this file is not thought to be
489  * inserted in a file already using getopt.h
490  *
491  * Lorenzo
492  */
493
494 struct option
495 {
496   const char *name;
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.  */
499   int has_arg;
500   int *flag;
501   int val;
502 };
503
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.  */
509
510 static char *optarg;
511
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'.
515
516    On entry to `getopt', zero means this is the first call; initialize.
517
518    When `getopt' returns -1, this is the index of the first of the
519    non-option elements that the caller should itself scan.
520
521    Otherwise, `optind' communicates from one call to the next
522    how much of ARGV has been scanned so far.  */
523
524 static int optind;
525
526 /* Callers store zero here to inhibit the error message `getopt' prints
527    for unrecognized options.  */
528
529 static int opterr;
530
531 /* Set to an option character which was unrecognized.  */
532
533 static int optopt;
534
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.
538
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.
542 */
543 /*
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.
547
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.  */
554
555 /* Names for the values of the `has_arg' field of `struct option'.  */
556 #ifndef no_argument
557 #define no_argument             0
558 #endif
559
560 #ifndef required_argument
561 #define required_argument       1
562 #endif
563
564 #ifndef optional_argument
565 #define optional_argument       2
566 #endif
567
568 struct custom_getopt_data {
569         /*
570          * These have exactly the same meaning as the corresponding global variables,
571          * except that they are used for the reentrant versions of getopt.
572          */
573         int custom_optind;
574         int custom_opterr;
575         int custom_optopt;
576         char *custom_optarg;
577
578         /* True if the internal members have been initialized.  */
579         int initialized;
580
581         /*
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.
586          */
587         char *nextchar;
588
589         /*
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.
593          */
594         int first_nonopt;
595         int last_nonopt;
596 };
597
598 /*
599  * the variables optarg, optind, opterr and optopt are renamed with
600  * the custom_ prefix so that they don't interfere with getopt ones.
601  *
602  * Moreover they're static so they are visible only from within the
603  * file where this very file will be included.
604  */
605
606 /*
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.
609  */
610 static char *custom_optarg;
611
612 /*
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'.
616  *
617  * On entry to `custom_getopt', 1 means this is the first call; initialize.
618  *
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.
621  *
622  * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
623  * has been scanned so far.
624  *
625  * 1003.2 says this must be 1 before any call.
626  */
627 static int custom_optind = 1;
628
629 /*
630  * Callers store zero here to inhibit the error message for unrecognized
631  * options.
632  */
633 static int custom_opterr = 1;
634
635 /*
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.
638  */
639 static int custom_optopt = '?';
640
641 /*
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.
648  */
649 static void exchange(char **argv, struct custom_getopt_data *d)
650 {
651         int bottom = d->first_nonopt;
652         int middle = d->last_nonopt;
653         int top = d->custom_optind;
654         char *tem;
655
656         /*
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.
661          */
662         while (top > middle && middle > bottom) {
663                 if (top - middle > middle - bottom) {
664                         /* Bottom segment is the short one.  */
665                         int len = middle - bottom;
666                         int i;
667
668                         /* Swap it with the top part of the top segment.  */
669                         for (i = 0; i < len; i++) {
670                                 tem = argv[bottom + i];
671                                 argv[bottom + i] =
672                                         argv[top - (middle - bottom) + i];
673                                 argv[top - (middle - bottom) + i] = tem;
674                         }
675                         /* Exclude the moved bottom segment from further swapping.  */
676                         top -= len;
677                 } else {
678                         /* Top segment is the short one.  */
679                         int len = top - middle;
680                         int i;
681
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;
687                         }
688                         /* Exclude the moved top segment from further swapping.  */
689                         bottom += len;
690                 }
691         }
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;
695 }
696
697 /* Initialize the internal data when the first call is made.  */
698 static void custom_getopt_initialize(struct custom_getopt_data *d)
699 {
700         /*
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.
704          */
705         d->first_nonopt = d->last_nonopt = d->custom_optind;
706         d->nextchar = NULL;
707         d->initialized = 1;
708 }
709
710 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
711
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)
715 {
716         /*
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).
719          */
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;
724         /*
725          * If we have just processed some options following some
726          * non-options, exchange them so that the options come first.
727          */
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;
733         /*
734          * Skip any additional non-options and extend the range of
735          * non-options previously skipped.
736          */
737         while (d->custom_optind < argc && NONOPTION_P)
738                 d->custom_optind++;
739         d->last_nonopt = d->custom_optind;
740         /*
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.
744          */
745         if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
746                 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;
754         }
755         /*
756          * If we have done all the ARGV-elements, stop the scan and back over
757          * any non-options that we skipped and permuted.
758          */
759         if (d->custom_optind == argc) {
760                 /*
761                  * Set the next-arg-index to point at the non-options that we
762                  * previously skipped, so the caller will digest them.
763                  */
764                 if (d->first_nonopt != d->last_nonopt)
765                         d->custom_optind = d->first_nonopt;
766                 return -1;
767         }
768         /*
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.
771          */
772         if (NONOPTION_P) {
773                 d->custom_optarg = argv[d->custom_optind++];
774                 return 1;
775         }
776         /*
777          * We have found another option-ARGV-element. Skip the initial
778          * punctuation.
779          */
780         d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
781         return 0;
782 }
783
784 /*
785  * Check whether the ARGV-element is a long option.
786  *
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
789  * arg "u".
790  *
791  * This distinction seems to be the most useful approach.
792  *
793  */
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)
797 {
798         char *nameend;
799         const struct option *p;
800         const struct option *pfound = NULL;
801         int exact = 0;
802         int ambig = 0;
803         int indfound = -1;
804         int option_index;
805
806         for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
807                 /* Do nothing.  */ ;
808
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.  */
815                                 pfound = p;
816                                 indfound = option_index;
817                                 exact = 1;
818                                 break;
819                         } else if (pfound == NULL) {
820                                 /* First nonexact match found.  */
821                                 pfound = p;
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.  */
827                                 ambig = 1;
828                 }
829         if (ambig && !exact) {
830                 if (print_errors) {
831                         fprintf(stderr,
832                                 "%s: option `%s' is ambiguous\n",
833                                 argv[0], argv[d->custom_optind]);
834                 }
835                 d->nextchar += strlen(d->nextchar);
836                 d->custom_optind++;
837                 d->custom_optopt = 0;
838                 return '?';
839         }
840         if (pfound) {
841                 option_index = indfound;
842                 d->custom_optind++;
843                 if (*nameend) {
844                         if (pfound->has_arg != no_argument)
845                                 d->custom_optarg = nameend + 1;
846                         else {
847                                 if (print_errors) {
848                                         if (argv[d->custom_optind - 1][1] == '-') {
849                                                 /* --option */
850                                                 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
851                                                         argv[0], pfound->name);
852                                         } else {
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);
856                                         }
857
858                                 }
859                                 d->nextchar += strlen(d->nextchar);
860                                 d->custom_optopt = pfound->val;
861                                 return '?';
862                         }
863                 } else if (pfound->has_arg == required_argument) {
864                         if (d->custom_optind < argc)
865                                 d->custom_optarg = argv[d->custom_optind++];
866                         else {
867                                 if (print_errors) {
868                                         fprintf(stderr,
869                                                 "%s: option `%s' requires an argument\n",
870                                                 argv[0],
871                                                 argv[d->custom_optind - 1]);
872                                 }
873                                 d->nextchar += strlen(d->nextchar);
874                                 d->custom_optopt = pfound->val;
875                                 return optstring[0] == ':' ? ':' : '?';
876                         }
877                 }
878                 d->nextchar += strlen(d->nextchar);
879                 if (longind != NULL)
880                         *longind = option_index;
881                 if (pfound->flag) {
882                         *(pfound->flag) = pfound->val;
883                         return 0;
884                 }
885                 return pfound->val;
886         }
887         /*
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.
891          */
892         if (print_errors) {
893                 if (argv[d->custom_optind][1] == '-') {
894                         /* --option */
895                         fprintf(stderr,
896                                 "%s: unrecognized option `--%s'\n",
897                                 argv[0], d->nextchar);
898                 } else {
899                         /* +option or -option */
900                         fprintf(stderr,
901                                 "%s: unrecognized option `%c%s'\n",
902                                 argv[0], argv[d->custom_optind][0],
903                                 d->nextchar);
904                 }
905         }
906         d->nextchar = (char *) "";
907         d->custom_optind++;
908         d->custom_optopt = 0;
909         return '?';
910 }
911
912 static int check_short_opt(int argc, char *const *argv, const char *optstring,
913                 int print_errors, struct custom_getopt_data *d)
914 {
915         char c = *d->nextchar++;
916         char *temp = strchr(optstring, c);
917
918         /* Increment `custom_optind' when we start to process its last character.  */
919         if (*d->nextchar == '\0')
920                 ++d->custom_optind;
921         if (!temp || c == ':') {
922                 if (print_errors)
923                         fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
924
925                 d->custom_optopt = c;
926                 return '?';
927         }
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;
933                                 d->custom_optind++;
934                         } else
935                                 d->custom_optarg = NULL;
936                         d->nextchar = NULL;
937                 } else {
938                         /* This is an option that requires an argument.  */
939                         if (*d->nextchar != '\0') {
940                                 d->custom_optarg = d->nextchar;
941                                 /*
942                                  * If we end this ARGV-element by taking the
943                                  * rest as an arg, we must advance to the next
944                                  * element now.
945                                  */
946                                 d->custom_optind++;
947                         } else if (d->custom_optind == argc) {
948                                 if (print_errors) {
949                                         fprintf(stderr,
950                                                 "%s: option requires an argument -- %c\n",
951                                                 argv[0], c);
952                                 }
953                                 d->custom_optopt = c;
954                                 if (optstring[0] == ':')
955                                         c = ':';
956                                 else
957                                         c = '?';
958                         } else
959                                 /*
960                                  * We already incremented `custom_optind' once;
961                                  * increment it again when taking next ARGV-elt
962                                  * as argument.
963                                  */
964                                 d->custom_optarg = argv[d->custom_optind++];
965                         d->nextchar = NULL;
966                 }
967         }
968         return c;
969 }
970
971 /*
972  * Scan elements of ARGV for option characters given in OPTSTRING.
973  *
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.
979  *
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.
983  *
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.)
988  *
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 '?'.
993  *
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.
999  *
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.
1003  *
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.
1012  *
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.
1016  *
1017  * LONGOPTS is a vector of `struct option' terminated by an
1018  * element containing a name which is zero.
1019  *
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
1022  * recent call.
1023  *
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.
1027  *
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'.
1031  *
1032  * If a letter in OPTS is followed by two colons, its argument is optional.
1033  * This behavior is specific to the GNU `getopt'.
1034  *
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'.
1039  */
1040
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)
1044 {
1045         int ret, print_errors = d->custom_opterr;
1046
1047         if (optstring[0] == ':')
1048                 print_errors = 0;
1049         if (argc < 1)
1050                 return -1;
1051         d->custom_optarg = NULL;
1052
1053         /* 
1054          * This is a big difference with GNU getopt, since optind == 0
1055          * means initialization while here 1 means first call.
1056          */
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);
1061         }
1062         if (d->nextchar == NULL || *d->nextchar == '\0') {
1063                 ret = shuffle_argv(argc, argv, longopts, d);
1064                 if (ret)
1065                         return ret;
1066         }
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);
1071 }
1072
1073 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
1074         const struct option *longopts, int *longind)
1075 {
1076         int result;
1077         /* Keep a global copy of all internal members of d */
1078         static struct custom_getopt_data d;
1079
1080         d.custom_optind = custom_optind;
1081         d.custom_opterr = custom_opterr;
1082         result = getopt_internal_r(argc, argv, optstring, longopts,
1083                 longind, &d);
1084         custom_optind = d.custom_optind;
1085         custom_optarg = d.custom_optarg;
1086         custom_optopt = d.custom_optopt;
1087         return result;
1088 }
1089
1090 static int custom_getopt_long (int argc, char *const *argv, const char *options,
1091         const struct option *long_options, int *opt_index)
1092 {
1093         return custom_getopt_internal(argc, argv, options, long_options,
1094                 opt_index);
1095 }
1096
1097
1098 static char *package_name = 0;
1099
1100 /**
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
1117  */
1118 static
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)
1127 {
1128   char *stop_char = 0;
1129   const char *val = value;
1130   int found;
1131   char **string_field;
1132
1133   stop_char = 0;
1134   found = 0;
1135
1136   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1137     {
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 : ""));
1142       else
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 */
1147     }
1148
1149   if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
1150     {
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 : ""));
1155       else
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 */
1160     }
1161     
1162   if (field_given && *field_given && ! override)
1163     return 0;
1164   if (prev_given)
1165     (*prev_given)++;
1166   if (field_given)
1167     (*field_given)++;
1168   if (possible_values)
1169     val = possible_values[found];
1170
1171   switch(arg_type) {
1172   case ARG_FLAG:
1173     *((int *)field) = !*((int *)field);
1174     break;
1175   case ARG_DOUBLE:
1176     if (val) *((double *)field) = strtod (val, &stop_char);
1177     break;
1178   case ARG_STRING:
1179     if (val) {
1180       string_field = (char **)field;
1181       if (!no_free && *string_field)
1182         free (*string_field); /* free previous string */
1183       *string_field = gengetopt_strdup (val);
1184     }
1185     break;
1186   default:
1187     break;
1188   };
1189
1190   /* check numeric conversion */
1191   switch(arg_type) {
1192   case ARG_DOUBLE:
1193     if (val && !(stop_char && *stop_char == '\0')) {
1194       fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1195       return 1; /* failure */
1196     }
1197     break;
1198   default:
1199     ;
1200   };
1201
1202   /* store the original value */
1203   switch(arg_type) {
1204   case ARG_NO:
1205   case ARG_FLAG:
1206     break;
1207   default:
1208     if (value && orig_field) {
1209       if (no_free) {
1210         *orig_field = value;
1211       } else {
1212         if (*orig_field)
1213           free (*orig_field); /* free previous string */
1214         *orig_field = gengetopt_strdup (value);
1215       }
1216     }
1217   };
1218
1219   return 0; /* OK */
1220 }
1221
1222
1223 int
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)
1226 {
1227   int c;        /* Character of the parsed option.  */
1228
1229   int error = 0;
1230   struct args_info_clitkBinarizeImage local_args_info;
1231   
1232   int override;
1233   int initialize;
1234   int check_required;
1235   int check_ambiguity;
1236   
1237   package_name = argv[0];
1238   
1239   override = params->override;
1240   initialize = params->initialize;
1241   check_required = params->check_required;
1242   check_ambiguity = params->check_ambiguity;
1243
1244   if (initialize)
1245     cmdline_parser_clitkBinarizeImage_init (args_info);
1246
1247   cmdline_parser_clitkBinarizeImage_init (&local_args_info);
1248
1249   optarg = 0;
1250   optind = 0;
1251   opterr = params->print_errors;
1252   optopt = '?';
1253
1254   while (1)
1255     {
1256       int option_index = 0;
1257
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 }
1272       };
1273
1274       custom_optarg = optarg;
1275       custom_optind = optind;
1276       custom_opterr = opterr;
1277       custom_optopt = optopt;
1278
1279       c = custom_getopt_long (argc, argv, "hVvi:o:l:u:", long_options, &option_index);
1280
1281       optarg = custom_optarg;
1282       optind = custom_optind;
1283       opterr = custom_opterr;
1284       optopt = custom_optopt;
1285
1286       if (c == -1) break;       /* Exit from `while (1)' loop.  */
1287
1288       switch (c)
1289         {
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);
1294
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);
1299
1300         case 'v':       /* Verbose.  */
1301         
1302         
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',
1306               additional_error))
1307             goto failure;
1308         
1309           break;
1310         case 'i':       /* Input image filename.  */
1311         
1312         
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,
1317               "input", 'i',
1318               additional_error))
1319             goto failure;
1320         
1321           break;
1322         case 'o':       /* Output image filename.  */
1323         
1324         
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,
1329               "output", 'o',
1330               additional_error))
1331             goto failure;
1332         
1333           break;
1334         case 'l':       /* Lower intensity (default=min), fg is greater than this value.  */
1335         
1336         
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,
1341               "lower", 'l',
1342               additional_error))
1343             goto failure;
1344         
1345           break;
1346         case 'u':       /* Upper intensity (default=max), fg is lower than this value.  */
1347         
1348         
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,
1353               "upper", 'u',
1354               additional_error))
1355             goto failure;
1356         
1357           break;
1358
1359         case 0: /* Long option with no short option */
1360           /* Config file.  */
1361           if (strcmp (long_options[option_index].name, "config") == 0)
1362           {
1363           
1364           
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,
1369                 "config", '-',
1370                 additional_error))
1371               goto failure;
1372           
1373           }
1374           /* Display allowed image types.  */
1375           else if (strcmp (long_options[option_index].name, "imagetypes") == 0)
1376           {
1377           
1378           
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", '-',
1382                 additional_error))
1383               goto failure;
1384           
1385           }
1386           /* Foreground (FG) or 'inside' value.  */
1387           else if (strcmp (long_options[option_index].name, "fg") == 0)
1388           {
1389           
1390           
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,
1395                 "fg", '-',
1396                 additional_error))
1397               goto failure;
1398           
1399           }
1400           /* Background (BG) or 'ouside' value.  */
1401           else if (strcmp (long_options[option_index].name, "bg") == 0)
1402           {
1403           
1404           
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,
1409                 "bg", '-',
1410                 additional_error))
1411               goto failure;
1412           
1413           }
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)
1416           {
1417           
1418           
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,
1423                 "mode", '-',
1424                 additional_error))
1425               goto failure;
1426           
1427           }
1428           
1429           break;
1430         case '?':       /* Invalid option.  */
1431           /* `getopt_long' already printed an error message.  */
1432           goto failure;
1433
1434         default:        /* bug: option not considered.  */
1435           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, c, (additional_error ? additional_error : ""));
1436           abort ();
1437         } /* switch */
1438     } /* while */
1439
1440
1441
1442   if (check_required)
1443     {
1444       error += cmdline_parser_clitkBinarizeImage_required2 (args_info, argv[0], additional_error);
1445     }
1446
1447   cmdline_parser_clitkBinarizeImage_release (&local_args_info);
1448
1449   if ( error )
1450     return (EXIT_FAILURE);
1451
1452   if (optind < argc)
1453     {
1454       int i = 0 ;
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) */
1459
1460
1461       args_info->inputs_num = argc - optind - found_prog_name;
1462       args_info->inputs =
1463         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1464       while (optind < argc)
1465         args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1466     }
1467
1468   return 0;
1469
1470 failure:
1471   
1472   cmdline_parser_clitkBinarizeImage_release (&local_args_info);
1473   return (EXIT_FAILURE);
1474 }
1475
1476 #ifndef CONFIG_FILE_LINE_SIZE
1477 #define CONFIG_FILE_LINE_SIZE 2048
1478 #endif
1479 #define ADDITIONAL_ERROR " in configuration file "
1480
1481 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1482 /* 3 is for "--" and "=" */
1483
1484 static int
1485 _cmdline_parser_clitkBinarizeImage_configfile (char * const filename, int *my_argc)
1486 {
1487   FILE* file;
1488   char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
1489   char linebuf[CONFIG_FILE_LINE_SIZE];
1490   int line_num = 0;
1491   int result = 0, equal;
1492   char *fopt, *farg;
1493   char *str_index;
1494   size_t len, next_token;
1495   char delimiter;
1496
1497   if ((file = fopen(filename, "r")) == NULL)
1498     {
1499       fprintf (stderr, "%s: Error opening configuration file '%s'\n",
1500                CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename);
1501       return EXIT_FAILURE;
1502     }
1503
1504   while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
1505     {
1506       ++line_num;
1507       my_argv[0] = '\0';
1508       len = strlen(linebuf);
1509       if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
1510         {
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;
1514           break;
1515         }
1516
1517       /* find first non-whitespace character in the line */
1518       next_token = strspn (linebuf, " \t\r\n");
1519       str_index  = linebuf + next_token;
1520
1521       if ( str_index[0] == '\0' || str_index[0] == '#')
1522         continue; /* empty line or comment line is skipped */
1523
1524       fopt = str_index;
1525
1526       /* truncate fopt at the end of the first non-valid character */
1527       next_token = strcspn (fopt, " \t\r\n=");
1528
1529       if (fopt[next_token] == '\0') /* the line is over */
1530         {
1531           farg  = NULL;
1532           equal = 0;
1533           goto noarg;
1534         }
1535
1536       /* remember if equal sign is present */
1537       equal = (fopt[next_token] == '=');
1538       fopt[next_token++] = '\0';
1539
1540       /* advance pointers to the next token after the end of fopt */
1541       next_token += strspn (fopt + next_token, " \t\r\n");
1542
1543       /* check for the presence of equal sign, and if so, skip it */
1544       if ( !equal )
1545         if ((equal = (fopt[next_token] == '=')))
1546           {
1547             next_token++;
1548             next_token += strspn (fopt + next_token, " \t\r\n");
1549           }
1550       str_index  += next_token;
1551
1552       /* find argument */
1553       farg = str_index;
1554       if ( farg[0] == '\"' || farg[0] == '\'' )
1555         { /* quoted argument */
1556           str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1557           if (! str_index)
1558             {
1559               fprintf
1560                 (stderr,
1561                  "%s:%s:%d: unterminated string in configuration file\n",
1562                  CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename, line_num);
1563               result = EXIT_FAILURE;
1564               break;
1565             }
1566         }
1567       else
1568         { /* read up the remaining part up to a delimiter */
1569           next_token = strcspn (farg, " \t\r\n#\'\"");
1570           str_index += next_token;
1571         }
1572
1573       /* truncate farg at the delimiter and store it for further check */
1574       delimiter = *str_index, *str_index++ = '\0';
1575
1576       /* everything but comment is illegal at the end of line */
1577       if (delimiter != '\0' && delimiter != '#')
1578         {
1579           str_index += strspn(str_index, " \t\r\n");
1580           if (*str_index != '\0' && *str_index != '#')
1581             {
1582               fprintf
1583                 (stderr,
1584                  "%s:%s:%d: malformed string in configuration file\n",
1585                  CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename, line_num);
1586               result = EXIT_FAILURE;
1587               break;
1588             }
1589         }
1590
1591     noarg:
1592       if (!strcmp(fopt,"include")) {
1593         if (farg && *farg) {
1594           result = _cmdline_parser_clitkBinarizeImage_configfile(farg, my_argc);
1595         } else {
1596           fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1597                   CMDLINE_PARSER_CLITKBINARIZEIMAGE_PACKAGE, filename, line_num);
1598         }
1599         continue;
1600       }
1601       len = strlen(fopt);
1602       strcat (my_argv, len > 1 ? "--" : "-");
1603       strcat (my_argv, fopt);
1604       if (len > 1 && ((farg && *farg) || equal))
1605         strcat (my_argv, "=");
1606       if (farg && *farg)
1607         strcat (my_argv, farg);
1608       ++(*my_argc);
1609
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);
1614     } /* while */
1615
1616   if (file)
1617     fclose(file);
1618   return result;
1619 }
1620
1621 int
1622 cmdline_parser_clitkBinarizeImage_configfile (char * const filename,
1623                            struct args_info_clitkBinarizeImage *args_info,
1624                            int override, int initialize, int check_required)
1625 {
1626   struct cmdline_parser_clitkBinarizeImage_params params;
1627
1628   params.override = override;
1629   params.initialize = initialize;
1630   params.check_required = check_required;
1631   params.check_ambiguity = 0;
1632   params.print_errors = 1;
1633   
1634   return cmdline_parser_clitkBinarizeImage_config_file (filename, args_info, &params);
1635 }
1636
1637 int
1638 cmdline_parser_clitkBinarizeImage_config_file (char * const filename,
1639                            struct args_info_clitkBinarizeImage *args_info,
1640                            struct cmdline_parser_clitkBinarizeImage_params *params)
1641 {
1642   int i, result;
1643   int my_argc = 1;
1644   char **my_argv_arg;
1645   char *additional_error;
1646
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);
1652
1653   result = _cmdline_parser_clitkBinarizeImage_configfile(filename, &my_argc);
1654
1655   if (result != EXIT_FAILURE) {
1656     my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1657     cmd_line_list_tmp = cmd_line_list;
1658
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;
1662     }
1663
1664     my_argv_arg[my_argc] = 0;
1665
1666     additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1667     strcpy (additional_error, ADDITIONAL_ERROR);
1668     strcat (additional_error, filename);
1669     result =
1670       cmdline_parser_clitkBinarizeImage_internal (my_argc, my_argv_arg, args_info,
1671                               params,
1672                               additional_error);
1673
1674     free (additional_error);
1675     free (my_argv_arg);
1676   }
1677
1678   free_cmd_list();
1679   if (result == EXIT_FAILURE)
1680     {
1681       cmdline_parser_clitkBinarizeImage_free (args_info);
1682       exit (EXIT_FAILURE);
1683     }
1684   
1685   return result;
1686 }