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