]> Creatis software - clitk.git/blob - vv/tools_ggo/clitkConnectedComponentLabeling_ggo.c
2ad535d2cb1d475e5b720806add241416a9e3766
[clitk.git] / vv / tools_ggo / clitkConnectedComponentLabeling_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/segmentation --arg-struct-name=args_info_clitkConnectedComponentLabeling --func-name=cmdline_parser_clitkConnectedComponentLabeling --file-name=clitkConnectedComponentLabeling_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 "clitkConnectedComponentLabeling_ggo.h"
22
23 const char *args_info_clitkConnectedComponentLabeling_purpose = "Basic segmentation : connected component labeling ";
24
25 const char *args_info_clitkConnectedComponentLabeling_usage = "Usage: clitkConnectedComponentLabeling [OPTIONS]... [FILES]...";
26
27 const char *args_info_clitkConnectedComponentLabeling_description = "";
28
29 const char *args_info_clitkConnectedComponentLabeling_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   "      --verboseOption  Display options values  (default=off)",
36   "\nI/O:",
37   "  -i, --input=STRING   Input image filename",
38   "      --inputBG=INT    Input Background  (default=`0')",
39   "  -o, --output=STRING  Output filename",
40   "\nLabelize:",
41   "      --minSize=INT    Minimum component size in voxels  (default=`100')",
42   "      --full           Full connecticity  (default=off)",
43     0
44 };
45
46 typedef enum {ARG_NO
47   , ARG_FLAG
48   , ARG_STRING
49   , ARG_INT
50 } cmdline_parser_clitkConnectedComponentLabeling_arg_type;
51
52 static
53 void clear_given (struct args_info_clitkConnectedComponentLabeling *args_info);
54 static
55 void clear_args (struct args_info_clitkConnectedComponentLabeling *args_info);
56
57 static int
58 cmdline_parser_clitkConnectedComponentLabeling_internal (int argc, char * const *argv, struct args_info_clitkConnectedComponentLabeling *args_info,
59                         struct cmdline_parser_clitkConnectedComponentLabeling_params *params, const char *additional_error);
60
61 static int
62 cmdline_parser_clitkConnectedComponentLabeling_required2 (struct args_info_clitkConnectedComponentLabeling *args_info, const char *prog_name, const char *additional_error);
63 struct line_list
64 {
65   char * string_arg;
66   struct line_list * next;
67 };
68
69 static struct line_list *cmd_line_list = 0;
70 static struct line_list *cmd_line_list_tmp = 0;
71
72 static void
73 free_cmd_list(void)
74 {
75   /* free the list of a previous call */
76   if (cmd_line_list)
77     {
78       while (cmd_line_list) {
79         cmd_line_list_tmp = cmd_line_list;
80         cmd_line_list = cmd_line_list->next;
81         free (cmd_line_list_tmp->string_arg);
82         free (cmd_line_list_tmp);
83       }
84     }
85 }
86
87
88 static char *
89 gengetopt_strdup (const char *s);
90
91 static
92 void clear_given (struct args_info_clitkConnectedComponentLabeling *args_info)
93 {
94   args_info->help_given = 0 ;
95   args_info->version_given = 0 ;
96   args_info->config_given = 0 ;
97   args_info->verbose_given = 0 ;
98   args_info->imagetypes_given = 0 ;
99   args_info->verboseOption_given = 0 ;
100   args_info->input_given = 0 ;
101   args_info->inputBG_given = 0 ;
102   args_info->output_given = 0 ;
103   args_info->minSize_given = 0 ;
104   args_info->full_given = 0 ;
105 }
106
107 static
108 void clear_args (struct args_info_clitkConnectedComponentLabeling *args_info)
109 {
110   args_info->config_arg = NULL;
111   args_info->config_orig = NULL;
112   args_info->verbose_flag = 0;
113   args_info->imagetypes_flag = 0;
114   args_info->verboseOption_flag = 0;
115   args_info->input_arg = NULL;
116   args_info->input_orig = NULL;
117   args_info->inputBG_arg = 0;
118   args_info->inputBG_orig = NULL;
119   args_info->output_arg = NULL;
120   args_info->output_orig = NULL;
121   args_info->minSize_arg = 100;
122   args_info->minSize_orig = NULL;
123   args_info->full_flag = 0;
124   
125 }
126
127 static
128 void init_args_info(struct args_info_clitkConnectedComponentLabeling *args_info)
129 {
130
131
132   args_info->help_help = args_info_clitkConnectedComponentLabeling_help[0] ;
133   args_info->version_help = args_info_clitkConnectedComponentLabeling_help[1] ;
134   args_info->config_help = args_info_clitkConnectedComponentLabeling_help[2] ;
135   args_info->verbose_help = args_info_clitkConnectedComponentLabeling_help[3] ;
136   args_info->imagetypes_help = args_info_clitkConnectedComponentLabeling_help[4] ;
137   args_info->verboseOption_help = args_info_clitkConnectedComponentLabeling_help[5] ;
138   args_info->input_help = args_info_clitkConnectedComponentLabeling_help[7] ;
139   args_info->inputBG_help = args_info_clitkConnectedComponentLabeling_help[8] ;
140   args_info->output_help = args_info_clitkConnectedComponentLabeling_help[9] ;
141   args_info->minSize_help = args_info_clitkConnectedComponentLabeling_help[11] ;
142   args_info->full_help = args_info_clitkConnectedComponentLabeling_help[12] ;
143   
144 }
145
146 void
147 cmdline_parser_clitkConnectedComponentLabeling_print_version (void)
148 {
149   printf ("%s %s\n", CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE, CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_VERSION);
150 }
151
152 static void print_help_common(void) {
153   cmdline_parser_clitkConnectedComponentLabeling_print_version ();
154
155   if (strlen(args_info_clitkConnectedComponentLabeling_purpose) > 0)
156     printf("\n%s\n", args_info_clitkConnectedComponentLabeling_purpose);
157
158   if (strlen(args_info_clitkConnectedComponentLabeling_usage) > 0)
159     printf("\n%s\n", args_info_clitkConnectedComponentLabeling_usage);
160
161   printf("\n");
162
163   if (strlen(args_info_clitkConnectedComponentLabeling_description) > 0)
164     printf("%s\n", args_info_clitkConnectedComponentLabeling_description);
165 }
166
167 void
168 cmdline_parser_clitkConnectedComponentLabeling_print_help (void)
169 {
170   int i = 0;
171   print_help_common();
172   while (args_info_clitkConnectedComponentLabeling_help[i])
173     printf("%s\n", args_info_clitkConnectedComponentLabeling_help[i++]);
174 }
175
176 void
177 cmdline_parser_clitkConnectedComponentLabeling_init (struct args_info_clitkConnectedComponentLabeling *args_info)
178 {
179   clear_given (args_info);
180   clear_args (args_info);
181   init_args_info (args_info);
182
183   args_info->inputs = NULL;
184   args_info->inputs_num = 0;
185 }
186
187 void
188 cmdline_parser_clitkConnectedComponentLabeling_params_init(struct cmdline_parser_clitkConnectedComponentLabeling_params *params)
189 {
190   if (params)
191     { 
192       params->override = 0;
193       params->initialize = 1;
194       params->check_required = 1;
195       params->check_ambiguity = 0;
196       params->print_errors = 1;
197     }
198 }
199
200 struct cmdline_parser_clitkConnectedComponentLabeling_params *
201 cmdline_parser_clitkConnectedComponentLabeling_params_create(void)
202 {
203   struct cmdline_parser_clitkConnectedComponentLabeling_params *params = 
204     (struct cmdline_parser_clitkConnectedComponentLabeling_params *)malloc(sizeof(struct cmdline_parser_clitkConnectedComponentLabeling_params));
205   cmdline_parser_clitkConnectedComponentLabeling_params_init(params);  
206   return params;
207 }
208
209 static void
210 free_string_field (char **s)
211 {
212   if (*s)
213     {
214       free (*s);
215       *s = 0;
216     }
217 }
218
219
220 static void
221 cmdline_parser_clitkConnectedComponentLabeling_release (struct args_info_clitkConnectedComponentLabeling *args_info)
222 {
223   unsigned int i;
224   free_string_field (&(args_info->config_arg));
225   free_string_field (&(args_info->config_orig));
226   free_string_field (&(args_info->input_arg));
227   free_string_field (&(args_info->input_orig));
228   free_string_field (&(args_info->inputBG_orig));
229   free_string_field (&(args_info->output_arg));
230   free_string_field (&(args_info->output_orig));
231   free_string_field (&(args_info->minSize_orig));
232   
233   
234   for (i = 0; i < args_info->inputs_num; ++i)
235     free (args_info->inputs [i]);
236
237   if (args_info->inputs_num)
238     free (args_info->inputs);
239
240   clear_given (args_info);
241 }
242
243
244 static void
245 write_into_file(FILE *outfile, const char *opt, const char *arg, char *values[])
246 {
247   if (arg) {
248     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
249   } else {
250     fprintf(outfile, "%s\n", opt);
251   }
252 }
253
254
255 int
256 cmdline_parser_clitkConnectedComponentLabeling_dump(FILE *outfile, struct args_info_clitkConnectedComponentLabeling *args_info)
257 {
258   int i = 0;
259
260   if (!outfile)
261     {
262       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE);
263       return EXIT_FAILURE;
264     }
265
266   if (args_info->help_given)
267     write_into_file(outfile, "help", 0, 0 );
268   if (args_info->version_given)
269     write_into_file(outfile, "version", 0, 0 );
270   if (args_info->config_given)
271     write_into_file(outfile, "config", args_info->config_orig, 0);
272   if (args_info->verbose_given)
273     write_into_file(outfile, "verbose", 0, 0 );
274   if (args_info->imagetypes_given)
275     write_into_file(outfile, "imagetypes", 0, 0 );
276   if (args_info->verboseOption_given)
277     write_into_file(outfile, "verboseOption", 0, 0 );
278   if (args_info->input_given)
279     write_into_file(outfile, "input", args_info->input_orig, 0);
280   if (args_info->inputBG_given)
281     write_into_file(outfile, "inputBG", args_info->inputBG_orig, 0);
282   if (args_info->output_given)
283     write_into_file(outfile, "output", args_info->output_orig, 0);
284   if (args_info->minSize_given)
285     write_into_file(outfile, "minSize", args_info->minSize_orig, 0);
286   if (args_info->full_given)
287     write_into_file(outfile, "full", 0, 0 );
288   
289
290   i = EXIT_SUCCESS;
291   return i;
292 }
293
294 int
295 cmdline_parser_clitkConnectedComponentLabeling_file_save(const char *filename, struct args_info_clitkConnectedComponentLabeling *args_info)
296 {
297   FILE *outfile;
298   int i = 0;
299
300   outfile = fopen(filename, "w");
301
302   if (!outfile)
303     {
304       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE, filename);
305       return EXIT_FAILURE;
306     }
307
308   i = cmdline_parser_clitkConnectedComponentLabeling_dump(outfile, args_info);
309   fclose (outfile);
310
311   return i;
312 }
313
314 void
315 cmdline_parser_clitkConnectedComponentLabeling_free (struct args_info_clitkConnectedComponentLabeling *args_info)
316 {
317   cmdline_parser_clitkConnectedComponentLabeling_release (args_info);
318 }
319
320 /** @brief replacement of strdup, which is not standard */
321 char *
322 gengetopt_strdup (const char *s)
323 {
324   char *result = NULL;
325   if (!s)
326     return result;
327
328   result = (char*)malloc(strlen(s) + 1);
329   if (result == (char*)0)
330     return (char*)0;
331   strcpy(result, s);
332   return result;
333 }
334
335 int
336 cmdline_parser_clitkConnectedComponentLabeling (int argc, char * const *argv, struct args_info_clitkConnectedComponentLabeling *args_info)
337 {
338   return cmdline_parser_clitkConnectedComponentLabeling2 (argc, argv, args_info, 0, 1, 1);
339 }
340
341 int
342 cmdline_parser_clitkConnectedComponentLabeling_ext (int argc, char * const *argv, struct args_info_clitkConnectedComponentLabeling *args_info,
343                    struct cmdline_parser_clitkConnectedComponentLabeling_params *params)
344 {
345   int result;
346   result = cmdline_parser_clitkConnectedComponentLabeling_internal (argc, argv, args_info, params, NULL);
347
348   if (result == EXIT_FAILURE)
349     {
350       cmdline_parser_clitkConnectedComponentLabeling_free (args_info);
351       exit (EXIT_FAILURE);
352     }
353   
354   return result;
355 }
356
357 int
358 cmdline_parser_clitkConnectedComponentLabeling2 (int argc, char * const *argv, struct args_info_clitkConnectedComponentLabeling *args_info, int override, int initialize, int check_required)
359 {
360   int result;
361   struct cmdline_parser_clitkConnectedComponentLabeling_params params;
362   
363   params.override = override;
364   params.initialize = initialize;
365   params.check_required = check_required;
366   params.check_ambiguity = 0;
367   params.print_errors = 1;
368
369   result = cmdline_parser_clitkConnectedComponentLabeling_internal (argc, argv, args_info, &params, NULL);
370
371   if (result == EXIT_FAILURE)
372     {
373       cmdline_parser_clitkConnectedComponentLabeling_free (args_info);
374       exit (EXIT_FAILURE);
375     }
376   
377   return result;
378 }
379
380 int
381 cmdline_parser_clitkConnectedComponentLabeling_required (struct args_info_clitkConnectedComponentLabeling *args_info, const char *prog_name)
382 {
383   int result = EXIT_SUCCESS;
384
385   if (cmdline_parser_clitkConnectedComponentLabeling_required2(args_info, prog_name, NULL) > 0)
386     result = EXIT_FAILURE;
387
388   if (result == EXIT_FAILURE)
389     {
390       cmdline_parser_clitkConnectedComponentLabeling_free (args_info);
391       exit (EXIT_FAILURE);
392     }
393   
394   return result;
395 }
396
397 int
398 cmdline_parser_clitkConnectedComponentLabeling_required2 (struct args_info_clitkConnectedComponentLabeling *args_info, const char *prog_name, const char *additional_error)
399 {
400   int error = 0;
401
402   /* checks for required options */
403   if (! args_info->input_given)
404     {
405       fprintf (stderr, "%s: '--input' ('-i') option required%s\n", prog_name, (additional_error ? additional_error : ""));
406       error = 1;
407     }
408   
409   if (! args_info->output_given)
410     {
411       fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
412       error = 1;
413     }
414   
415   
416   /* checks for dependences among options */
417
418   return error;
419 }
420
421 /*
422  * Extracted from the glibc source tree, version 2.3.6
423  *
424  * Licensed under the GPL as per the whole glibc source tree.
425  *
426  * This file was modified so that getopt_long can be called
427  * many times without risking previous memory to be spoiled.
428  *
429  * Modified by Andre Noll and Lorenzo Bettini for use in
430  * GNU gengetopt generated files.
431  *
432  */
433
434 /* 
435  * we must include anything we need since this file is not thought to be
436  * inserted in a file already using getopt.h
437  *
438  * Lorenzo
439  */
440
441 struct option
442 {
443   const char *name;
444   /* has_arg can't be an enum because some compilers complain about
445      type mismatches in all the code that assumes it is an int.  */
446   int has_arg;
447   int *flag;
448   int val;
449 };
450
451 /* For communication from `getopt' to the caller.
452    When `getopt' finds an option that takes an argument,
453    the argument value is returned here.
454    Also, when `ordering' is RETURN_IN_ORDER,
455    each non-option ARGV-element is returned here.  */
456
457 static char *optarg;
458
459 /* Index in ARGV of the next element to be scanned.
460    This is used for communication to and from the caller
461    and for communication between successive calls to `getopt'.
462
463    On entry to `getopt', zero means this is the first call; initialize.
464
465    When `getopt' returns -1, this is the index of the first of the
466    non-option elements that the caller should itself scan.
467
468    Otherwise, `optind' communicates from one call to the next
469    how much of ARGV has been scanned so far.  */
470
471 static int optind;
472
473 /* Callers store zero here to inhibit the error message `getopt' prints
474    for unrecognized options.  */
475
476 static int opterr;
477
478 /* Set to an option character which was unrecognized.  */
479
480 static int optopt;
481
482 /* This version of `getopt' appears to the caller like standard Unix `getopt'
483    but it behaves differently for the user, since it allows the user
484    to intersperse the options with the other arguments.
485
486    As `getopt' works, it permutes the elements of ARGV so that,
487    when it is done, all the options precede everything else.  Thus
488    all application programs are extended to handle flexible argument order.
489 */
490 /*
491    If the field `flag' is not NULL, it points to a variable that is set
492    to the value given in the field `val' when the option is found, but
493    left unchanged if the option is not found.
494
495    To have a long-named option do something other than set an `int' to
496    a compiled-in constant, such as set a value from `custom_optarg', set the
497    option's `flag' field to zero and its `val' field to a nonzero
498    value (the equivalent single-letter option character, if there is
499    one).  For long options that have a zero `flag' field, `getopt'
500    returns the contents of the `val' field.  */
501
502 /* Names for the values of the `has_arg' field of `struct option'.  */
503 #ifndef no_argument
504 #define no_argument             0
505 #endif
506
507 #ifndef required_argument
508 #define required_argument       1
509 #endif
510
511 #ifndef optional_argument
512 #define optional_argument       2
513 #endif
514
515 struct custom_getopt_data {
516         /*
517          * These have exactly the same meaning as the corresponding global variables,
518          * except that they are used for the reentrant versions of getopt.
519          */
520         int custom_optind;
521         int custom_opterr;
522         int custom_optopt;
523         char *custom_optarg;
524
525         /* True if the internal members have been initialized.  */
526         int initialized;
527
528         /*
529          * The next char to be scanned in the option-element in which the last option
530          * character we returned was found.  This allows us to pick up the scan where
531          * we left off.  If this is zero, or a null string, it means resume the scan by
532          * advancing to the next ARGV-element.
533          */
534         char *nextchar;
535
536         /*
537          * Describe the part of ARGV that contains non-options that have been skipped.
538          * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
539          * the index after the last of them.
540          */
541         int first_nonopt;
542         int last_nonopt;
543 };
544
545 /*
546  * the variables optarg, optind, opterr and optopt are renamed with
547  * the custom_ prefix so that they don't interfere with getopt ones.
548  *
549  * Moreover they're static so they are visible only from within the
550  * file where this very file will be included.
551  */
552
553 /*
554  * For communication from `custom_getopt' to the caller.  When `custom_getopt' finds an
555  * option that takes an argument, the argument value is returned here.
556  */
557 static char *custom_optarg;
558
559 /*
560  * Index in ARGV of the next element to be scanned.  This is used for
561  * communication to and from the caller and for communication between
562  * successive calls to `custom_getopt'.
563  *
564  * On entry to `custom_getopt', 1 means this is the first call; initialize.
565  *
566  * When `custom_getopt' returns -1, this is the index of the first of the non-option
567  * elements that the caller should itself scan.
568  *
569  * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
570  * has been scanned so far.
571  *
572  * 1003.2 says this must be 1 before any call.
573  */
574 static int custom_optind = 1;
575
576 /*
577  * Callers store zero here to inhibit the error message for unrecognized
578  * options.
579  */
580 static int custom_opterr = 1;
581
582 /*
583  * Set to an option character which was unrecognized.  This must be initialized
584  * on some systems to avoid linking in the system's own getopt implementation.
585  */
586 static int custom_optopt = '?';
587
588 /*
589  * Exchange two adjacent subsequences of ARGV.  One subsequence is elements
590  * [first_nonopt,last_nonopt) which contains all the non-options that have been
591  * skipped so far.  The other is elements [last_nonopt,custom_optind), which contains
592  * all the options processed since those non-options were skipped.
593  * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
594  * indices of the non-options in ARGV after they are moved.
595  */
596 static void exchange(char **argv, struct custom_getopt_data *d)
597 {
598         int bottom = d->first_nonopt;
599         int middle = d->last_nonopt;
600         int top = d->custom_optind;
601         char *tem;
602
603         /*
604          * Exchange the shorter segment with the far end of the longer segment.
605          * That puts the shorter segment into the right place.  It leaves the
606          * longer segment in the right place overall, but it consists of two
607          * parts that need to be swapped next.
608          */
609         while (top > middle && middle > bottom) {
610                 if (top - middle > middle - bottom) {
611                         /* Bottom segment is the short one.  */
612                         int len = middle - bottom;
613                         int i;
614
615                         /* Swap it with the top part of the top segment.  */
616                         for (i = 0; i < len; i++) {
617                                 tem = argv[bottom + i];
618                                 argv[bottom + i] =
619                                         argv[top - (middle - bottom) + i];
620                                 argv[top - (middle - bottom) + i] = tem;
621                         }
622                         /* Exclude the moved bottom segment from further swapping.  */
623                         top -= len;
624                 } else {
625                         /* Top segment is the short one.  */
626                         int len = top - middle;
627                         int i;
628
629                         /* Swap it with the bottom part of the bottom segment.  */
630                         for (i = 0; i < len; i++) {
631                                 tem = argv[bottom + i];
632                                 argv[bottom + i] = argv[middle + i];
633                                 argv[middle + i] = tem;
634                         }
635                         /* Exclude the moved top segment from further swapping.  */
636                         bottom += len;
637                 }
638         }
639         /* Update records for the slots the non-options now occupy.  */
640         d->first_nonopt += (d->custom_optind - d->last_nonopt);
641         d->last_nonopt = d->custom_optind;
642 }
643
644 /* Initialize the internal data when the first call is made.  */
645 static void custom_getopt_initialize(struct custom_getopt_data *d)
646 {
647         /*
648          * Start processing options with ARGV-element 1 (since ARGV-element 0
649          * is the program name); the sequence of previously skipped non-option
650          * ARGV-elements is empty.
651          */
652         d->first_nonopt = d->last_nonopt = d->custom_optind;
653         d->nextchar = NULL;
654         d->initialized = 1;
655 }
656
657 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
658
659 /* return: zero: continue, nonzero: return given value to user */
660 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
661         struct custom_getopt_data *d)
662 {
663         /*
664          * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
665          * moved back by the user (who may also have changed the arguments).
666          */
667         if (d->last_nonopt > d->custom_optind)
668                 d->last_nonopt = d->custom_optind;
669         if (d->first_nonopt > d->custom_optind)
670                 d->first_nonopt = d->custom_optind;
671         /*
672          * If we have just processed some options following some
673          * non-options, exchange them so that the options come first.
674          */
675         if (d->first_nonopt != d->last_nonopt &&
676                         d->last_nonopt != d->custom_optind)
677                 exchange((char **) argv, d);
678         else if (d->last_nonopt != d->custom_optind)
679                 d->first_nonopt = d->custom_optind;
680         /*
681          * Skip any additional non-options and extend the range of
682          * non-options previously skipped.
683          */
684         while (d->custom_optind < argc && NONOPTION_P)
685                 d->custom_optind++;
686         d->last_nonopt = d->custom_optind;
687         /*
688          * The special ARGV-element `--' means premature end of options.  Skip
689          * it like a null option, then exchange with previous non-options as if
690          * it were an option, then skip everything else like a non-option.
691          */
692         if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
693                 d->custom_optind++;
694                 if (d->first_nonopt != d->last_nonopt
695                                 && d->last_nonopt != d->custom_optind)
696                         exchange((char **) argv, d);
697                 else if (d->first_nonopt == d->last_nonopt)
698                         d->first_nonopt = d->custom_optind;
699                 d->last_nonopt = argc;
700                 d->custom_optind = argc;
701         }
702         /*
703          * If we have done all the ARGV-elements, stop the scan and back over
704          * any non-options that we skipped and permuted.
705          */
706         if (d->custom_optind == argc) {
707                 /*
708                  * Set the next-arg-index to point at the non-options that we
709                  * previously skipped, so the caller will digest them.
710                  */
711                 if (d->first_nonopt != d->last_nonopt)
712                         d->custom_optind = d->first_nonopt;
713                 return -1;
714         }
715         /*
716          * If we have come to a non-option and did not permute it, either stop
717          * the scan or describe it to the caller and pass it by.
718          */
719         if (NONOPTION_P) {
720                 d->custom_optarg = argv[d->custom_optind++];
721                 return 1;
722         }
723         /*
724          * We have found another option-ARGV-element. Skip the initial
725          * punctuation.
726          */
727         d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
728         return 0;
729 }
730
731 /*
732  * Check whether the ARGV-element is a long option.
733  *
734  * If there's a long option "fubar" and the ARGV-element is "-fu", consider
735  * that an abbreviation of the long option, just like "--fu", and not "-f" with
736  * arg "u".
737  *
738  * This distinction seems to be the most useful approach.
739  *
740  */
741 static int check_long_opt(int argc, char *const *argv, const char *optstring,
742                 const struct option *longopts, int *longind,
743                 int print_errors, struct custom_getopt_data *d)
744 {
745         char *nameend;
746         const struct option *p;
747         const struct option *pfound = NULL;
748         int exact = 0;
749         int ambig = 0;
750         int indfound = -1;
751         int option_index;
752
753         for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
754                 /* Do nothing.  */ ;
755
756         /* Test all long options for either exact match or abbreviated matches */
757         for (p = longopts, option_index = 0; p->name; p++, option_index++)
758                 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
759                         if ((unsigned int) (nameend - d->nextchar)
760                                         == (unsigned int) strlen(p->name)) {
761                                 /* Exact match found.  */
762                                 pfound = p;
763                                 indfound = option_index;
764                                 exact = 1;
765                                 break;
766                         } else if (pfound == NULL) {
767                                 /* First nonexact match found.  */
768                                 pfound = p;
769                                 indfound = option_index;
770                         } else if (pfound->has_arg != p->has_arg
771                                         || pfound->flag != p->flag
772                                         || pfound->val != p->val)
773                                 /* Second or later nonexact match found.  */
774                                 ambig = 1;
775                 }
776         if (ambig && !exact) {
777                 if (print_errors) {
778                         fprintf(stderr,
779                                 "%s: option `%s' is ambiguous\n",
780                                 argv[0], argv[d->custom_optind]);
781                 }
782                 d->nextchar += strlen(d->nextchar);
783                 d->custom_optind++;
784                 d->custom_optopt = 0;
785                 return '?';
786         }
787         if (pfound) {
788                 option_index = indfound;
789                 d->custom_optind++;
790                 if (*nameend) {
791                         if (pfound->has_arg != no_argument)
792                                 d->custom_optarg = nameend + 1;
793                         else {
794                                 if (print_errors) {
795                                         if (argv[d->custom_optind - 1][1] == '-') {
796                                                 /* --option */
797                                                 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
798                                                         argv[0], pfound->name);
799                                         } else {
800                                                 /* +option or -option */
801                                                 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
802                                                         argv[0], argv[d->custom_optind - 1][0], pfound->name);
803                                         }
804
805                                 }
806                                 d->nextchar += strlen(d->nextchar);
807                                 d->custom_optopt = pfound->val;
808                                 return '?';
809                         }
810                 } else if (pfound->has_arg == required_argument) {
811                         if (d->custom_optind < argc)
812                                 d->custom_optarg = argv[d->custom_optind++];
813                         else {
814                                 if (print_errors) {
815                                         fprintf(stderr,
816                                                 "%s: option `%s' requires an argument\n",
817                                                 argv[0],
818                                                 argv[d->custom_optind - 1]);
819                                 }
820                                 d->nextchar += strlen(d->nextchar);
821                                 d->custom_optopt = pfound->val;
822                                 return optstring[0] == ':' ? ':' : '?';
823                         }
824                 }
825                 d->nextchar += strlen(d->nextchar);
826                 if (longind != NULL)
827                         *longind = option_index;
828                 if (pfound->flag) {
829                         *(pfound->flag) = pfound->val;
830                         return 0;
831                 }
832                 return pfound->val;
833         }
834         /*
835          * Can't find it as a long option.  If this is not getopt_long_only, or
836          * the option starts with '--' or is not a valid short option, then
837          * it's an error.  Otherwise interpret it as a short option.
838          */
839         if (print_errors) {
840                 if (argv[d->custom_optind][1] == '-') {
841                         /* --option */
842                         fprintf(stderr,
843                                 "%s: unrecognized option `--%s'\n",
844                                 argv[0], d->nextchar);
845                 } else {
846                         /* +option or -option */
847                         fprintf(stderr,
848                                 "%s: unrecognized option `%c%s'\n",
849                                 argv[0], argv[d->custom_optind][0],
850                                 d->nextchar);
851                 }
852         }
853         d->nextchar = (char *) "";
854         d->custom_optind++;
855         d->custom_optopt = 0;
856         return '?';
857 }
858
859 static int check_short_opt(int argc, char *const *argv, const char *optstring,
860                 int print_errors, struct custom_getopt_data *d)
861 {
862         char c = *d->nextchar++;
863         char *temp = strchr(optstring, c);
864
865         /* Increment `custom_optind' when we start to process its last character.  */
866         if (*d->nextchar == '\0')
867                 ++d->custom_optind;
868         if (!temp || c == ':') {
869                 if (print_errors)
870                         fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
871
872                 d->custom_optopt = c;
873                 return '?';
874         }
875         if (temp[1] == ':') {
876                 if (temp[2] == ':') {
877                         /* This is an option that accepts an argument optionally.  */
878                         if (*d->nextchar != '\0') {
879                                 d->custom_optarg = d->nextchar;
880                                 d->custom_optind++;
881                         } else
882                                 d->custom_optarg = NULL;
883                         d->nextchar = NULL;
884                 } else {
885                         /* This is an option that requires an argument.  */
886                         if (*d->nextchar != '\0') {
887                                 d->custom_optarg = d->nextchar;
888                                 /*
889                                  * If we end this ARGV-element by taking the
890                                  * rest as an arg, we must advance to the next
891                                  * element now.
892                                  */
893                                 d->custom_optind++;
894                         } else if (d->custom_optind == argc) {
895                                 if (print_errors) {
896                                         fprintf(stderr,
897                                                 "%s: option requires an argument -- %c\n",
898                                                 argv[0], c);
899                                 }
900                                 d->custom_optopt = c;
901                                 if (optstring[0] == ':')
902                                         c = ':';
903                                 else
904                                         c = '?';
905                         } else
906                                 /*
907                                  * We already incremented `custom_optind' once;
908                                  * increment it again when taking next ARGV-elt
909                                  * as argument.
910                                  */
911                                 d->custom_optarg = argv[d->custom_optind++];
912                         d->nextchar = NULL;
913                 }
914         }
915         return c;
916 }
917
918 /*
919  * Scan elements of ARGV for option characters given in OPTSTRING.
920  *
921  * If an element of ARGV starts with '-', and is not exactly "-" or "--",
922  * then it is an option element.  The characters of this element
923  * (aside from the initial '-') are option characters.  If `getopt'
924  * is called repeatedly, it returns successively each of the option characters
925  * from each of the option elements.
926  *
927  * If `getopt' finds another option character, it returns that character,
928  * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
929  * resume the scan with the following option character or ARGV-element.
930  *
931  * If there are no more option characters, `getopt' returns -1.
932  * Then `custom_optind' is the index in ARGV of the first ARGV-element
933  * that is not an option.  (The ARGV-elements have been permuted
934  * so that those that are not options now come last.)
935  *
936  * OPTSTRING is a string containing the legitimate option characters.
937  * If an option character is seen that is not listed in OPTSTRING,
938  * return '?' after printing an error message.  If you set `custom_opterr' to
939  * zero, the error message is suppressed but we still return '?'.
940  *
941  * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
942  * so the following text in the same ARGV-element, or the text of the following
943  * ARGV-element, is returned in `custom_optarg'.  Two colons mean an option that
944  * wants an optional arg; if there is text in the current ARGV-element,
945  * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
946  *
947  * If OPTSTRING starts with `-' or `+', it requests different methods of
948  * handling the non-option ARGV-elements.
949  * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
950  *
951  * Long-named options begin with `--' instead of `-'.
952  * Their names may be abbreviated as long as the abbreviation is unique
953  * or is an exact match for some defined option.  If they have an
954  * argument, it follows the option name in the same ARGV-element, separated
955  * from the option name by a `=', or else the in next ARGV-element.
956  * When `getopt' finds a long-named option, it returns 0 if that option's
957  * `flag' field is nonzero, the value of the option's `val' field
958  * if the `flag' field is zero.
959  *
960  * The elements of ARGV aren't really const, because we permute them.
961  * But we pretend they're const in the prototype to be compatible
962  * with other systems.
963  *
964  * LONGOPTS is a vector of `struct option' terminated by an
965  * element containing a name which is zero.
966  *
967  * LONGIND returns the index in LONGOPT of the long-named option found.
968  * It is only valid when a long-named option has been found by the most
969  * recent call.
970  *
971  * Return the option character from OPTS just read.  Return -1 when there are
972  * no more options.  For unrecognized options, or options missing arguments,
973  * `custom_optopt' is set to the option letter, and '?' is returned.
974  *
975  * The OPTS string is a list of characters which are recognized option letters,
976  * optionally followed by colons, specifying that that letter takes an
977  * argument, to be placed in `custom_optarg'.
978  *
979  * If a letter in OPTS is followed by two colons, its argument is optional.
980  * This behavior is specific to the GNU `getopt'.
981  *
982  * The argument `--' causes premature termination of argument scanning,
983  * explicitly telling `getopt' that there are no more options.  If OPTS begins
984  * with `--', then non-option arguments are treated as arguments to the option
985  * '\0'.  This behavior is specific to the GNU `getopt'.
986  */
987
988 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
989                 const struct option *longopts, int *longind,
990                 struct custom_getopt_data *d)
991 {
992         int ret, print_errors = d->custom_opterr;
993
994         if (optstring[0] == ':')
995                 print_errors = 0;
996         if (argc < 1)
997                 return -1;
998         d->custom_optarg = NULL;
999
1000         /* 
1001          * This is a big difference with GNU getopt, since optind == 0
1002          * means initialization while here 1 means first call.
1003          */
1004         if (d->custom_optind == 0 || !d->initialized) {
1005                 if (d->custom_optind == 0)
1006                         d->custom_optind = 1;   /* Don't scan ARGV[0], the program name.  */
1007                 custom_getopt_initialize(d);
1008         }
1009         if (d->nextchar == NULL || *d->nextchar == '\0') {
1010                 ret = shuffle_argv(argc, argv, longopts, d);
1011                 if (ret)
1012                         return ret;
1013         }
1014         if (longopts && (argv[d->custom_optind][1] == '-' ))
1015                 return check_long_opt(argc, argv, optstring, longopts,
1016                         longind, print_errors, d);
1017         return check_short_opt(argc, argv, optstring, print_errors, d);
1018 }
1019
1020 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
1021         const struct option *longopts, int *longind)
1022 {
1023         int result;
1024         /* Keep a global copy of all internal members of d */
1025         static struct custom_getopt_data d;
1026
1027         d.custom_optind = custom_optind;
1028         d.custom_opterr = custom_opterr;
1029         result = getopt_internal_r(argc, argv, optstring, longopts,
1030                 longind, &d);
1031         custom_optind = d.custom_optind;
1032         custom_optarg = d.custom_optarg;
1033         custom_optopt = d.custom_optopt;
1034         return result;
1035 }
1036
1037 static int custom_getopt_long (int argc, char *const *argv, const char *options,
1038         const struct option *long_options, int *opt_index)
1039 {
1040         return custom_getopt_internal(argc, argv, options, long_options,
1041                 opt_index);
1042 }
1043
1044
1045 static char *package_name = 0;
1046
1047 /**
1048  * @brief updates an option
1049  * @param field the generic pointer to the field to update
1050  * @param orig_field the pointer to the orig field
1051  * @param field_given the pointer to the number of occurrence of this option
1052  * @param prev_given the pointer to the number of occurrence already seen
1053  * @param value the argument for this option (if null no arg was specified)
1054  * @param possible_values the possible values for this option (if specified)
1055  * @param default_value the default value (in case the option only accepts fixed values)
1056  * @param arg_type the type of this option
1057  * @param check_ambiguity @see cmdline_parser_clitkConnectedComponentLabeling_params.check_ambiguity
1058  * @param override @see cmdline_parser_clitkConnectedComponentLabeling_params.override
1059  * @param no_free whether to free a possible previous value
1060  * @param multiple_option whether this is a multiple option
1061  * @param long_opt the corresponding long option
1062  * @param short_opt the corresponding short option (or '-' if none)
1063  * @param additional_error possible further error specification
1064  */
1065 static
1066 int update_arg(void *field, char **orig_field,
1067                unsigned int *field_given, unsigned int *prev_given, 
1068                char *value, char *possible_values[], const char *default_value,
1069                cmdline_parser_clitkConnectedComponentLabeling_arg_type arg_type,
1070                int check_ambiguity, int override,
1071                int no_free, int multiple_option,
1072                const char *long_opt, char short_opt,
1073                const char *additional_error)
1074 {
1075   char *stop_char = 0;
1076   const char *val = value;
1077   int found;
1078   char **string_field;
1079
1080   stop_char = 0;
1081   found = 0;
1082
1083   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1084     {
1085       if (short_opt != '-')
1086         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
1087                package_name, long_opt, short_opt,
1088                (additional_error ? additional_error : ""));
1089       else
1090         fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
1091                package_name, long_opt,
1092                (additional_error ? additional_error : ""));
1093       return 1; /* failure */
1094     }
1095
1096     
1097   if (field_given && *field_given && ! override)
1098     return 0;
1099   if (prev_given)
1100     (*prev_given)++;
1101   if (field_given)
1102     (*field_given)++;
1103   if (possible_values)
1104     val = possible_values[found];
1105
1106   switch(arg_type) {
1107   case ARG_FLAG:
1108     *((int *)field) = !*((int *)field);
1109     break;
1110   case ARG_INT:
1111     if (val) *((int *)field) = strtol (val, &stop_char, 0);
1112     break;
1113   case ARG_STRING:
1114     if (val) {
1115       string_field = (char **)field;
1116       if (!no_free && *string_field)
1117         free (*string_field); /* free previous string */
1118       *string_field = gengetopt_strdup (val);
1119     }
1120     break;
1121   default:
1122     break;
1123   };
1124
1125   /* check numeric conversion */
1126   switch(arg_type) {
1127   case ARG_INT:
1128     if (val && !(stop_char && *stop_char == '\0')) {
1129       fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1130       return 1; /* failure */
1131     }
1132     break;
1133   default:
1134     ;
1135   };
1136
1137   /* store the original value */
1138   switch(arg_type) {
1139   case ARG_NO:
1140   case ARG_FLAG:
1141     break;
1142   default:
1143     if (value && orig_field) {
1144       if (no_free) {
1145         *orig_field = value;
1146       } else {
1147         if (*orig_field)
1148           free (*orig_field); /* free previous string */
1149         *orig_field = gengetopt_strdup (value);
1150       }
1151     }
1152   };
1153
1154   return 0; /* OK */
1155 }
1156
1157
1158 int
1159 cmdline_parser_clitkConnectedComponentLabeling_internal (int argc, char * const *argv, struct args_info_clitkConnectedComponentLabeling *args_info,
1160                         struct cmdline_parser_clitkConnectedComponentLabeling_params *params, const char *additional_error)
1161 {
1162   int c;        /* Character of the parsed option.  */
1163
1164   int error = 0;
1165   struct args_info_clitkConnectedComponentLabeling local_args_info;
1166   
1167   int override;
1168   int initialize;
1169   int check_required;
1170   int check_ambiguity;
1171   
1172   package_name = argv[0];
1173   
1174   override = params->override;
1175   initialize = params->initialize;
1176   check_required = params->check_required;
1177   check_ambiguity = params->check_ambiguity;
1178
1179   if (initialize)
1180     cmdline_parser_clitkConnectedComponentLabeling_init (args_info);
1181
1182   cmdline_parser_clitkConnectedComponentLabeling_init (&local_args_info);
1183
1184   optarg = 0;
1185   optind = 0;
1186   opterr = params->print_errors;
1187   optopt = '?';
1188
1189   while (1)
1190     {
1191       int option_index = 0;
1192
1193       static struct option long_options[] = {
1194         { "help",       0, NULL, 'h' },
1195         { "version",    0, NULL, 'V' },
1196         { "config",     1, NULL, 0 },
1197         { "verbose",    0, NULL, 'v' },
1198         { "imagetypes", 0, NULL, 0 },
1199         { "verboseOption",      0, NULL, 0 },
1200         { "input",      1, NULL, 'i' },
1201         { "inputBG",    1, NULL, 0 },
1202         { "output",     1, NULL, 'o' },
1203         { "minSize",    1, NULL, 0 },
1204         { "full",       0, NULL, 0 },
1205         { NULL, 0, NULL, 0 }
1206       };
1207
1208       custom_optarg = optarg;
1209       custom_optind = optind;
1210       custom_opterr = opterr;
1211       custom_optopt = optopt;
1212
1213       c = custom_getopt_long (argc, argv, "hVvi:o:", long_options, &option_index);
1214
1215       optarg = custom_optarg;
1216       optind = custom_optind;
1217       opterr = custom_opterr;
1218       optopt = custom_optopt;
1219
1220       if (c == -1) break;       /* Exit from `while (1)' loop.  */
1221
1222       switch (c)
1223         {
1224         case 'h':       /* Print help and exit.  */
1225           cmdline_parser_clitkConnectedComponentLabeling_print_help ();
1226           cmdline_parser_clitkConnectedComponentLabeling_free (&local_args_info);
1227           exit (EXIT_SUCCESS);
1228
1229         case 'V':       /* Print version and exit.  */
1230           cmdline_parser_clitkConnectedComponentLabeling_print_version ();
1231           cmdline_parser_clitkConnectedComponentLabeling_free (&local_args_info);
1232           exit (EXIT_SUCCESS);
1233
1234         case 'v':       /* Verbose.  */
1235         
1236         
1237           if (update_arg((void *)&(args_info->verbose_flag), 0, &(args_info->verbose_given),
1238               &(local_args_info.verbose_given), optarg, 0, 0, ARG_FLAG,
1239               check_ambiguity, override, 1, 0, "verbose", 'v',
1240               additional_error))
1241             goto failure;
1242         
1243           break;
1244         case 'i':       /* Input image filename.  */
1245         
1246         
1247           if (update_arg( (void *)&(args_info->input_arg), 
1248                &(args_info->input_orig), &(args_info->input_given),
1249               &(local_args_info.input_given), optarg, 0, 0, ARG_STRING,
1250               check_ambiguity, override, 0, 0,
1251               "input", 'i',
1252               additional_error))
1253             goto failure;
1254         
1255           break;
1256         case 'o':       /* Output filename.  */
1257         
1258         
1259           if (update_arg( (void *)&(args_info->output_arg), 
1260                &(args_info->output_orig), &(args_info->output_given),
1261               &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1262               check_ambiguity, override, 0, 0,
1263               "output", 'o',
1264               additional_error))
1265             goto failure;
1266         
1267           break;
1268
1269         case 0: /* Long option with no short option */
1270           /* Config file.  */
1271           if (strcmp (long_options[option_index].name, "config") == 0)
1272           {
1273           
1274           
1275             if (update_arg( (void *)&(args_info->config_arg), 
1276                  &(args_info->config_orig), &(args_info->config_given),
1277                 &(local_args_info.config_given), optarg, 0, 0, ARG_STRING,
1278                 check_ambiguity, override, 0, 0,
1279                 "config", '-',
1280                 additional_error))
1281               goto failure;
1282           
1283           }
1284           /* Display allowed image types.  */
1285           else if (strcmp (long_options[option_index].name, "imagetypes") == 0)
1286           {
1287           
1288           
1289             if (update_arg((void *)&(args_info->imagetypes_flag), 0, &(args_info->imagetypes_given),
1290                 &(local_args_info.imagetypes_given), optarg, 0, 0, ARG_FLAG,
1291                 check_ambiguity, override, 1, 0, "imagetypes", '-',
1292                 additional_error))
1293               goto failure;
1294           
1295           }
1296           /* Display options values.  */
1297           else if (strcmp (long_options[option_index].name, "verboseOption") == 0)
1298           {
1299           
1300           
1301             if (update_arg((void *)&(args_info->verboseOption_flag), 0, &(args_info->verboseOption_given),
1302                 &(local_args_info.verboseOption_given), optarg, 0, 0, ARG_FLAG,
1303                 check_ambiguity, override, 1, 0, "verboseOption", '-',
1304                 additional_error))
1305               goto failure;
1306           
1307           }
1308           /* Input Background.  */
1309           else if (strcmp (long_options[option_index].name, "inputBG") == 0)
1310           {
1311           
1312           
1313             if (update_arg( (void *)&(args_info->inputBG_arg), 
1314                  &(args_info->inputBG_orig), &(args_info->inputBG_given),
1315                 &(local_args_info.inputBG_given), optarg, 0, "0", ARG_INT,
1316                 check_ambiguity, override, 0, 0,
1317                 "inputBG", '-',
1318                 additional_error))
1319               goto failure;
1320           
1321           }
1322           /* Minimum component size in voxels.  */
1323           else if (strcmp (long_options[option_index].name, "minSize") == 0)
1324           {
1325           
1326           
1327             if (update_arg( (void *)&(args_info->minSize_arg), 
1328                  &(args_info->minSize_orig), &(args_info->minSize_given),
1329                 &(local_args_info.minSize_given), optarg, 0, "100", ARG_INT,
1330                 check_ambiguity, override, 0, 0,
1331                 "minSize", '-',
1332                 additional_error))
1333               goto failure;
1334           
1335           }
1336           /* Full connecticity.  */
1337           else if (strcmp (long_options[option_index].name, "full") == 0)
1338           {
1339           
1340           
1341             if (update_arg((void *)&(args_info->full_flag), 0, &(args_info->full_given),
1342                 &(local_args_info.full_given), optarg, 0, 0, ARG_FLAG,
1343                 check_ambiguity, override, 1, 0, "full", '-',
1344                 additional_error))
1345               goto failure;
1346           
1347           }
1348           
1349           break;
1350         case '?':       /* Invalid option.  */
1351           /* `getopt_long' already printed an error message.  */
1352           goto failure;
1353
1354         default:        /* bug: option not considered.  */
1355           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE, c, (additional_error ? additional_error : ""));
1356           abort ();
1357         } /* switch */
1358     } /* while */
1359
1360
1361
1362   if (check_required)
1363     {
1364       error += cmdline_parser_clitkConnectedComponentLabeling_required2 (args_info, argv[0], additional_error);
1365     }
1366
1367   cmdline_parser_clitkConnectedComponentLabeling_release (&local_args_info);
1368
1369   if ( error )
1370     return (EXIT_FAILURE);
1371
1372   if (optind < argc)
1373     {
1374       int i = 0 ;
1375       int found_prog_name = 0;
1376       /* whether program name, i.e., argv[0], is in the remaining args
1377          (this may happen with some implementations of getopt,
1378           but surely not with the one included by gengetopt) */
1379
1380
1381       args_info->inputs_num = argc - optind - found_prog_name;
1382       args_info->inputs =
1383         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1384       while (optind < argc)
1385         args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1386     }
1387
1388   return 0;
1389
1390 failure:
1391   
1392   cmdline_parser_clitkConnectedComponentLabeling_release (&local_args_info);
1393   return (EXIT_FAILURE);
1394 }
1395
1396 #ifndef CONFIG_FILE_LINE_SIZE
1397 #define CONFIG_FILE_LINE_SIZE 2048
1398 #endif
1399 #define ADDITIONAL_ERROR " in configuration file "
1400
1401 #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1402 /* 3 is for "--" and "=" */
1403
1404 static int
1405 _cmdline_parser_clitkConnectedComponentLabeling_configfile (char * const filename, int *my_argc)
1406 {
1407   FILE* file;
1408   char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
1409   char linebuf[CONFIG_FILE_LINE_SIZE];
1410   int line_num = 0;
1411   int result = 0, equal;
1412   char *fopt, *farg;
1413   char *str_index;
1414   size_t len, next_token;
1415   char delimiter;
1416
1417   if ((file = fopen(filename, "r")) == NULL)
1418     {
1419       fprintf (stderr, "%s: Error opening configuration file '%s'\n",
1420                CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE, filename);
1421       return EXIT_FAILURE;
1422     }
1423
1424   while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
1425     {
1426       ++line_num;
1427       my_argv[0] = '\0';
1428       len = strlen(linebuf);
1429       if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
1430         {
1431           fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
1432                    CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE, filename, line_num);
1433           result = EXIT_FAILURE;
1434           break;
1435         }
1436
1437       /* find first non-whitespace character in the line */
1438       next_token = strspn (linebuf, " \t\r\n");
1439       str_index  = linebuf + next_token;
1440
1441       if ( str_index[0] == '\0' || str_index[0] == '#')
1442         continue; /* empty line or comment line is skipped */
1443
1444       fopt = str_index;
1445
1446       /* truncate fopt at the end of the first non-valid character */
1447       next_token = strcspn (fopt, " \t\r\n=");
1448
1449       if (fopt[next_token] == '\0') /* the line is over */
1450         {
1451           farg  = NULL;
1452           equal = 0;
1453           goto noarg;
1454         }
1455
1456       /* remember if equal sign is present */
1457       equal = (fopt[next_token] == '=');
1458       fopt[next_token++] = '\0';
1459
1460       /* advance pointers to the next token after the end of fopt */
1461       next_token += strspn (fopt + next_token, " \t\r\n");
1462
1463       /* check for the presence of equal sign, and if so, skip it */
1464       if ( !equal )
1465         if ((equal = (fopt[next_token] == '=')))
1466           {
1467             next_token++;
1468             next_token += strspn (fopt + next_token, " \t\r\n");
1469           }
1470       str_index  += next_token;
1471
1472       /* find argument */
1473       farg = str_index;
1474       if ( farg[0] == '\"' || farg[0] == '\'' )
1475         { /* quoted argument */
1476           str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1477           if (! str_index)
1478             {
1479               fprintf
1480                 (stderr,
1481                  "%s:%s:%d: unterminated string in configuration file\n",
1482                  CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE, filename, line_num);
1483               result = EXIT_FAILURE;
1484               break;
1485             }
1486         }
1487       else
1488         { /* read up the remaining part up to a delimiter */
1489           next_token = strcspn (farg, " \t\r\n#\'\"");
1490           str_index += next_token;
1491         }
1492
1493       /* truncate farg at the delimiter and store it for further check */
1494       delimiter = *str_index, *str_index++ = '\0';
1495
1496       /* everything but comment is illegal at the end of line */
1497       if (delimiter != '\0' && delimiter != '#')
1498         {
1499           str_index += strspn(str_index, " \t\r\n");
1500           if (*str_index != '\0' && *str_index != '#')
1501             {
1502               fprintf
1503                 (stderr,
1504                  "%s:%s:%d: malformed string in configuration file\n",
1505                  CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE, filename, line_num);
1506               result = EXIT_FAILURE;
1507               break;
1508             }
1509         }
1510
1511     noarg:
1512       if (!strcmp(fopt,"include")) {
1513         if (farg && *farg) {
1514           result = _cmdline_parser_clitkConnectedComponentLabeling_configfile(farg, my_argc);
1515         } else {
1516           fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1517                   CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE, filename, line_num);
1518         }
1519         continue;
1520       }
1521       len = strlen(fopt);
1522       strcat (my_argv, len > 1 ? "--" : "-");
1523       strcat (my_argv, fopt);
1524       if (len > 1 && ((farg && *farg) || equal))
1525         strcat (my_argv, "=");
1526       if (farg && *farg)
1527         strcat (my_argv, farg);
1528       ++(*my_argc);
1529
1530       cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1531       cmd_line_list_tmp->next = cmd_line_list;
1532       cmd_line_list = cmd_line_list_tmp;
1533       cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1534     } /* while */
1535
1536   if (file)
1537     fclose(file);
1538   return result;
1539 }
1540
1541 int
1542 cmdline_parser_clitkConnectedComponentLabeling_configfile (char * const filename,
1543                            struct args_info_clitkConnectedComponentLabeling *args_info,
1544                            int override, int initialize, int check_required)
1545 {
1546   struct cmdline_parser_clitkConnectedComponentLabeling_params params;
1547
1548   params.override = override;
1549   params.initialize = initialize;
1550   params.check_required = check_required;
1551   params.check_ambiguity = 0;
1552   params.print_errors = 1;
1553   
1554   return cmdline_parser_clitkConnectedComponentLabeling_config_file (filename, args_info, &params);
1555 }
1556
1557 int
1558 cmdline_parser_clitkConnectedComponentLabeling_config_file (char * const filename,
1559                            struct args_info_clitkConnectedComponentLabeling *args_info,
1560                            struct cmdline_parser_clitkConnectedComponentLabeling_params *params)
1561 {
1562   int i, result;
1563   int my_argc = 1;
1564   char **my_argv_arg;
1565   char *additional_error;
1566
1567   /* store the program name */
1568   cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1569   cmd_line_list_tmp->next = cmd_line_list;
1570   cmd_line_list = cmd_line_list_tmp;
1571   cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_CLITKCONNECTEDCOMPONENTLABELING_PACKAGE);
1572
1573   result = _cmdline_parser_clitkConnectedComponentLabeling_configfile(filename, &my_argc);
1574
1575   if (result != EXIT_FAILURE) {
1576     my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1577     cmd_line_list_tmp = cmd_line_list;
1578
1579     for (i = my_argc - 1; i >= 0; --i) {
1580       my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1581       cmd_line_list_tmp = cmd_line_list_tmp->next;
1582     }
1583
1584     my_argv_arg[my_argc] = 0;
1585
1586     additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1587     strcpy (additional_error, ADDITIONAL_ERROR);
1588     strcat (additional_error, filename);
1589     result =
1590       cmdline_parser_clitkConnectedComponentLabeling_internal (my_argc, my_argv_arg, args_info,
1591                               params,
1592                               additional_error);
1593
1594     free (additional_error);
1595     free (my_argv_arg);
1596   }
1597
1598   free_cmd_list();
1599   if (result == EXIT_FAILURE)
1600     {
1601       cmdline_parser_clitkConnectedComponentLabeling_free (args_info);
1602       exit (EXIT_FAILURE);
1603     }
1604   
1605   return result;
1606 }