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