/*
  File autogenerated by gengetopt version 2.18
  generated with the following command:
  gengetopt -i cmdline.ggo -u --conf-parser 

  The developers of gengetopt consider the fixed text that goes in all
  gengetopt output files to be in the public domain:
  we make no copyright claims on it.
*/

/* If we use autoconf.  */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "getopt.h"

#include "cmdline.h"

const char *gengetopt_args_info_purpose = "Detects lanes in street images.";

const char *gengetopt_args_info_usage = "Usage: LinePerceptor [OPTIONS]... [FILES]...";

const char *gengetopt_args_info_help[] = {
  "  -h, --help                   Print help and exit",
  "  -V, --version                Print version and exit",
  "\nBasic options:",
  "      --lanes-conf=STRING      Configuration file for lane detection  \n                                 (default=`Lanes.conf')",
  "      --stoplines-conf=STRING  Configuration file for stopline detection  \n                                 (default=`StopLines.conf')",
  "      --no-stoplines           Don't detect stop lines  (default=on)",
  "      --no-lanes               Don't detect lanes  (default=off)",
  "      --camera-conf=STRING     Configuration file for the camera paramters  \n                                 (default=`CameraInfo.conf')",
  "      --list-file=STRING       Text file containing a list of images one per \n                                 line",
  "      --list-path=STRING       Path where the image files are located, this is \n                                 just appended at the front of each line in \n                                 --list-file  (default=`')",
  "      --image-file=STRING      The path to an image",
  "\nDebugging options:",
  "      --wait=INT               Number of milliseconds to show the detected \n                                 lanes. Put 0 for infinite i.e. waits for \n                                 keypress.  (default=`0')",
  "      --show                   Show the detected lines  (default=off)",
  "      --step                   Step through each image (needs a keypress) or \n                                 fall through (waits for --wait msecs)  \n                                 (default=off)",
  "      --show-lane-numbers      Show the lane numbers on the output image  \n                                 (default=off)",
  "      --output-suffix=STRING   Suffix of images and results  \n                                 (default=`_results')",
  "      --save-images            Export all images with detected lanes to the by \n                                 appending --output-suffix + '.png' to each \n                                 input image  (default=off)",
  "      --save-lanes             Export all detected lanes to a text file by \n                                 appending --output-suffix + '.txt' to \n                                 --list-file  (default=off)",
  "      --debug                  Show debugging information and images  \n                                 (default=off)",
    0
};

static
void clear_given (struct gengetopt_args_info *args_info);
static
void clear_args (struct gengetopt_args_info *args_info);

static int
cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error);

struct line_list
{
  char * string_arg;
  struct line_list * next;
};

static struct line_list *cmd_line_list = 0;
static struct line_list *cmd_line_list_tmp = 0;

static void
free_cmd_list(void)
{
  /* free the list of a previous call */
  if (cmd_line_list)
    {
      while (cmd_line_list) {
        cmd_line_list_tmp = cmd_line_list;
        cmd_line_list = cmd_line_list->next;
        free (cmd_line_list_tmp->string_arg);
        free (cmd_line_list_tmp);
      }
    }
}


static char *
gengetopt_strdup (const char *s);

static
void clear_given (struct gengetopt_args_info *args_info)
{
  args_info->help_given = 0 ;
  args_info->version_given = 0 ;
  args_info->lanes_conf_given = 0 ;
  args_info->stoplines_conf_given = 0 ;
  args_info->no_stoplines_given = 0 ;
  args_info->no_lanes_given = 0 ;
  args_info->camera_conf_given = 0 ;
  args_info->list_file_given = 0 ;
  args_info->list_path_given = 0 ;
  args_info->image_file_given = 0 ;
  args_info->wait_given = 0 ;
  args_info->show_given = 0 ;
  args_info->step_given = 0 ;
  args_info->show_lane_numbers_given = 0 ;
  args_info->output_suffix_given = 0 ;
  args_info->save_images_given = 0 ;
  args_info->save_lanes_given = 0 ;
  args_info->debug_given = 0 ;
}

static
void clear_args (struct gengetopt_args_info *args_info)
{
  args_info->lanes_conf_arg = gengetopt_strdup ("Lanes.conf");
  args_info->lanes_conf_orig = NULL;
  args_info->stoplines_conf_arg = gengetopt_strdup ("StopLines.conf");
  args_info->stoplines_conf_orig = NULL;
  args_info->no_stoplines_flag = 1;
  args_info->no_lanes_flag = 0;
  args_info->camera_conf_arg = gengetopt_strdup ("CameraInfo.conf");
  args_info->camera_conf_orig = NULL;
  args_info->list_file_arg = NULL;
  args_info->list_file_orig = NULL;
  args_info->list_path_arg = gengetopt_strdup ("");
  args_info->list_path_orig = NULL;
  args_info->image_file_arg = NULL;
  args_info->image_file_orig = NULL;
  args_info->wait_arg = 0;
  args_info->wait_orig = NULL;
  args_info->show_flag = 0;
  args_info->step_flag = 0;
  args_info->show_lane_numbers_flag = 0;
  args_info->output_suffix_arg = gengetopt_strdup ("_results");
  args_info->output_suffix_orig = NULL;
  args_info->save_images_flag = 0;
  args_info->save_lanes_flag = 0;
  args_info->debug_flag = 0;
  
}

static
void init_args_info(struct gengetopt_args_info *args_info)
{
  args_info->help_help = gengetopt_args_info_help[0] ;
  args_info->version_help = gengetopt_args_info_help[1] ;
  args_info->lanes_conf_help = gengetopt_args_info_help[3] ;
  args_info->stoplines_conf_help = gengetopt_args_info_help[4] ;
  args_info->no_stoplines_help = gengetopt_args_info_help[5] ;
  args_info->no_lanes_help = gengetopt_args_info_help[6] ;
  args_info->camera_conf_help = gengetopt_args_info_help[7] ;
  args_info->list_file_help = gengetopt_args_info_help[8] ;
  args_info->list_path_help = gengetopt_args_info_help[9] ;
  args_info->image_file_help = gengetopt_args_info_help[10] ;
  args_info->wait_help = gengetopt_args_info_help[12] ;
  args_info->show_help = gengetopt_args_info_help[13] ;
  args_info->step_help = gengetopt_args_info_help[14] ;
  args_info->show_lane_numbers_help = gengetopt_args_info_help[15] ;
  args_info->output_suffix_help = gengetopt_args_info_help[16] ;
  args_info->save_images_help = gengetopt_args_info_help[17] ;
  args_info->save_lanes_help = gengetopt_args_info_help[18] ;
  args_info->debug_help = gengetopt_args_info_help[19] ;
  
}

void
cmdline_parser_print_version (void)
{
  printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
}

void
cmdline_parser_print_help (void)
{
  int i = 0;
  cmdline_parser_print_version ();

  if (strlen(gengetopt_args_info_purpose) > 0)
    printf("\n%s\n", gengetopt_args_info_purpose);

  printf("\n%s\n\n", gengetopt_args_info_usage);
  while (gengetopt_args_info_help[i])
    printf("%s\n", gengetopt_args_info_help[i++]);
}

void
cmdline_parser_init (struct gengetopt_args_info *args_info)
{
  clear_given (args_info);
  clear_args (args_info);
  init_args_info (args_info);

  args_info->inputs = NULL;
  args_info->inputs_num = 0;
}

static void
cmdline_parser_release (struct gengetopt_args_info *args_info)
{
  
  unsigned int i;
  if (args_info->lanes_conf_arg)
    {
      free (args_info->lanes_conf_arg); /* free previous argument */
      args_info->lanes_conf_arg = 0;
    }
  if (args_info->lanes_conf_orig)
    {
      free (args_info->lanes_conf_orig); /* free previous argument */
      args_info->lanes_conf_orig = 0;
    }
  if (args_info->stoplines_conf_arg)
    {
      free (args_info->stoplines_conf_arg); /* free previous argument */
      args_info->stoplines_conf_arg = 0;
    }
  if (args_info->stoplines_conf_orig)
    {
      free (args_info->stoplines_conf_orig); /* free previous argument */
      args_info->stoplines_conf_orig = 0;
    }
  if (args_info->camera_conf_arg)
    {
      free (args_info->camera_conf_arg); /* free previous argument */
      args_info->camera_conf_arg = 0;
    }
  if (args_info->camera_conf_orig)
    {
      free (args_info->camera_conf_orig); /* free previous argument */
      args_info->camera_conf_orig = 0;
    }
  if (args_info->list_file_arg)
    {
      free (args_info->list_file_arg); /* free previous argument */
      args_info->list_file_arg = 0;
    }
  if (args_info->list_file_orig)
    {
      free (args_info->list_file_orig); /* free previous argument */
      args_info->list_file_orig = 0;
    }
  if (args_info->list_path_arg)
    {
      free (args_info->list_path_arg); /* free previous argument */
      args_info->list_path_arg = 0;
    }
  if (args_info->list_path_orig)
    {
      free (args_info->list_path_orig); /* free previous argument */
      args_info->list_path_orig = 0;
    }
  if (args_info->image_file_arg)
    {
      free (args_info->image_file_arg); /* free previous argument */
      args_info->image_file_arg = 0;
    }
  if (args_info->image_file_orig)
    {
      free (args_info->image_file_orig); /* free previous argument */
      args_info->image_file_orig = 0;
    }
  if (args_info->wait_orig)
    {
      free (args_info->wait_orig); /* free previous argument */
      args_info->wait_orig = 0;
    }
  if (args_info->output_suffix_arg)
    {
      free (args_info->output_suffix_arg); /* free previous argument */
      args_info->output_suffix_arg = 0;
    }
  if (args_info->output_suffix_orig)
    {
      free (args_info->output_suffix_orig); /* free previous argument */
      args_info->output_suffix_orig = 0;
    }
  
  for (i = 0; i < args_info->inputs_num; ++i)
    free (args_info->inputs [i]);
  
  if (args_info->inputs_num)
    free (args_info->inputs);
  
  clear_given (args_info);
}

int
cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
{
  FILE *outfile;
  int i = 0;

  outfile = fopen(filename, "w");

  if (!outfile)
    {
      fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
      return EXIT_FAILURE;
    }

  if (args_info->help_given) {
    fprintf(outfile, "%s\n", "help");
  }
  if (args_info->version_given) {
    fprintf(outfile, "%s\n", "version");
  }
  if (args_info->lanes_conf_given) {
    if (args_info->lanes_conf_orig) {
      fprintf(outfile, "%s=\"%s\"\n", "lanes-conf", args_info->lanes_conf_orig);
    } else {
      fprintf(outfile, "%s\n", "lanes-conf");
    }
  }
  if (args_info->stoplines_conf_given) {
    if (args_info->stoplines_conf_orig) {
      fprintf(outfile, "%s=\"%s\"\n", "stoplines-conf", args_info->stoplines_conf_orig);
    } else {
      fprintf(outfile, "%s\n", "stoplines-conf");
    }
  }
  if (args_info->no_stoplines_given) {
    fprintf(outfile, "%s\n", "no-stoplines");
  }
  if (args_info->no_lanes_given) {
    fprintf(outfile, "%s\n", "no-lanes");
  }
  if (args_info->camera_conf_given) {
    if (args_info->camera_conf_orig) {
      fprintf(outfile, "%s=\"%s\"\n", "camera-conf", args_info->camera_conf_orig);
    } else {
      fprintf(outfile, "%s\n", "camera-conf");
    }
  }
  if (args_info->list_file_given) {
    if (args_info->list_file_orig) {
      fprintf(outfile, "%s=\"%s\"\n", "list-file", args_info->list_file_orig);
    } else {
      fprintf(outfile, "%s\n", "list-file");
    }
  }
  if (args_info->list_path_given) {
    if (args_info->list_path_orig) {
      fprintf(outfile, "%s=\"%s\"\n", "list-path", args_info->list_path_orig);
    } else {
      fprintf(outfile, "%s\n", "list-path");
    }
  }
  if (args_info->image_file_given) {
    if (args_info->image_file_orig) {
      fprintf(outfile, "%s=\"%s\"\n", "image-file", args_info->image_file_orig);
    } else {
      fprintf(outfile, "%s\n", "image-file");
    }
  }
  if (args_info->wait_given) {
    if (args_info->wait_orig) {
      fprintf(outfile, "%s=\"%s\"\n", "wait", args_info->wait_orig);
    } else {
      fprintf(outfile, "%s\n", "wait");
    }
  }
  if (args_info->show_given) {
    fprintf(outfile, "%s\n", "show");
  }
  if (args_info->step_given) {
    fprintf(outfile, "%s\n", "step");
  }
  if (args_info->show_lane_numbers_given) {
    fprintf(outfile, "%s\n", "show-lane-numbers");
  }
  if (args_info->output_suffix_given) {
    if (args_info->output_suffix_orig) {
      fprintf(outfile, "%s=\"%s\"\n", "output-suffix", args_info->output_suffix_orig);
    } else {
      fprintf(outfile, "%s\n", "output-suffix");
    }
  }
  if (args_info->save_images_given) {
    fprintf(outfile, "%s\n", "save-images");
  }
  if (args_info->save_lanes_given) {
    fprintf(outfile, "%s\n", "save-lanes");
  }
  if (args_info->debug_given) {
    fprintf(outfile, "%s\n", "debug");
  }
  
  fclose (outfile);

  i = EXIT_SUCCESS;
  return i;
}

void
cmdline_parser_free (struct gengetopt_args_info *args_info)
{
  cmdline_parser_release (args_info);
}


/* gengetopt_strdup() */
/* strdup.c replacement of strdup, which is not standard */
char *
gengetopt_strdup (const char *s)
{
  char *result = NULL;
  if (!s)
    return result;

  result = (char*)malloc(strlen(s) + 1);
  if (result == (char*)0)
    return (char*)0;
  strcpy(result, s);
  return result;
}

int
cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
{
  return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
}

int
cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
{
  int result;

  result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);

  if (result == EXIT_FAILURE)
    {
      cmdline_parser_free (args_info);
      exit (EXIT_FAILURE);
    }
  
  return result;
}

int
cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
{
  return EXIT_SUCCESS;
}

int
cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error)
{
  int c;	/* Character of the parsed option.  */

  int error = 0;
  struct gengetopt_args_info local_args_info;

  if (initialize)
    cmdline_parser_init (args_info);

  cmdline_parser_init (&local_args_info);

  optarg = 0;
  optind = 0;
  opterr = 1;
  optopt = '?';

  while (1)
    {
      int option_index = 0;
      char *stop_char;

      static struct option long_options[] = {
        { "help",	0, NULL, 'h' },
        { "version",	0, NULL, 'V' },
        { "lanes-conf",	1, NULL, 0 },
        { "stoplines-conf",	1, NULL, 0 },
        { "no-stoplines",	0, NULL, 0 },
        { "no-lanes",	0, NULL, 0 },
        { "camera-conf",	1, NULL, 0 },
        { "list-file",	1, NULL, 0 },
        { "list-path",	1, NULL, 0 },
        { "image-file",	1, NULL, 0 },
        { "wait",	1, NULL, 0 },
        { "show",	0, NULL, 0 },
        { "step",	0, NULL, 0 },
        { "show-lane-numbers",	0, NULL, 0 },
        { "output-suffix",	1, NULL, 0 },
        { "save-images",	0, NULL, 0 },
        { "save-lanes",	0, NULL, 0 },
        { "debug",	0, NULL, 0 },
        { NULL,	0, NULL, 0 }
      };

      stop_char = 0;
      c = getopt_long (argc, argv, "hV", long_options, &option_index);

      if (c == -1) break;	/* Exit from `while (1)' loop.  */

      switch (c)
        {
        case 'h':	/* Print help and exit.  */
          cmdline_parser_print_help ();
          cmdline_parser_free (&local_args_info);
          exit (EXIT_SUCCESS);

        case 'V':	/* Print version and exit.  */
          cmdline_parser_print_version ();
          cmdline_parser_free (&local_args_info);
          exit (EXIT_SUCCESS);


        case 0:	/* Long option with no short option */
          /* Configuration file for lane detection.  */
          if (strcmp (long_options[option_index].name, "lanes-conf") == 0)
          {
            if (local_args_info.lanes_conf_given)
              {
                fprintf (stderr, "%s: `--lanes-conf' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->lanes_conf_given && ! override)
              continue;
            local_args_info.lanes_conf_given = 1;
            args_info->lanes_conf_given = 1;
            if (args_info->lanes_conf_arg)
              free (args_info->lanes_conf_arg); /* free previous string */
            args_info->lanes_conf_arg = gengetopt_strdup (optarg);
            if (args_info->lanes_conf_orig)
              free (args_info->lanes_conf_orig); /* free previous string */
            args_info->lanes_conf_orig = gengetopt_strdup (optarg);
          }
          /* Configuration file for stopline detection.  */
          else if (strcmp (long_options[option_index].name, "stoplines-conf") == 0)
          {
            if (local_args_info.stoplines_conf_given)
              {
                fprintf (stderr, "%s: `--stoplines-conf' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->stoplines_conf_given && ! override)
              continue;
            local_args_info.stoplines_conf_given = 1;
            args_info->stoplines_conf_given = 1;
            if (args_info->stoplines_conf_arg)
              free (args_info->stoplines_conf_arg); /* free previous string */
            args_info->stoplines_conf_arg = gengetopt_strdup (optarg);
            if (args_info->stoplines_conf_orig)
              free (args_info->stoplines_conf_orig); /* free previous string */
            args_info->stoplines_conf_orig = gengetopt_strdup (optarg);
          }
          /* Don't detect stop lines.  */
          else if (strcmp (long_options[option_index].name, "no-stoplines") == 0)
          {
            if (local_args_info.no_stoplines_given)
              {
                fprintf (stderr, "%s: `--no-stoplines' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->no_stoplines_given && ! override)
              continue;
            local_args_info.no_stoplines_given = 1;
            args_info->no_stoplines_given = 1;
            args_info->no_stoplines_flag = !(args_info->no_stoplines_flag);
          }
          /* Don't detect lanes.  */
          else if (strcmp (long_options[option_index].name, "no-lanes") == 0)
          {
            if (local_args_info.no_lanes_given)
              {
                fprintf (stderr, "%s: `--no-lanes' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->no_lanes_given && ! override)
              continue;
            local_args_info.no_lanes_given = 1;
            args_info->no_lanes_given = 1;
            args_info->no_lanes_flag = !(args_info->no_lanes_flag);
          }
          /* Configuration file for the camera paramters.  */
          else if (strcmp (long_options[option_index].name, "camera-conf") == 0)
          {
            if (local_args_info.camera_conf_given)
              {
                fprintf (stderr, "%s: `--camera-conf' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->camera_conf_given && ! override)
              continue;
            local_args_info.camera_conf_given = 1;
            args_info->camera_conf_given = 1;
            if (args_info->camera_conf_arg)
              free (args_info->camera_conf_arg); /* free previous string */
            args_info->camera_conf_arg = gengetopt_strdup (optarg);
            if (args_info->camera_conf_orig)
              free (args_info->camera_conf_orig); /* free previous string */
            args_info->camera_conf_orig = gengetopt_strdup (optarg);
          }
          /* Text file containing a list of images one per line.  */
          else if (strcmp (long_options[option_index].name, "list-file") == 0)
          {
            if (local_args_info.list_file_given)
              {
                fprintf (stderr, "%s: `--list-file' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->list_file_given && ! override)
              continue;
            local_args_info.list_file_given = 1;
            args_info->list_file_given = 1;
            if (args_info->list_file_arg)
              free (args_info->list_file_arg); /* free previous string */
            args_info->list_file_arg = gengetopt_strdup (optarg);
            if (args_info->list_file_orig)
              free (args_info->list_file_orig); /* free previous string */
            args_info->list_file_orig = gengetopt_strdup (optarg);
          }
          /* Path where the image files are located, this is just appended at the front of each line in --list-file.  */
          else if (strcmp (long_options[option_index].name, "list-path") == 0)
          {
            if (local_args_info.list_path_given)
              {
                fprintf (stderr, "%s: `--list-path' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->list_path_given && ! override)
              continue;
            local_args_info.list_path_given = 1;
            args_info->list_path_given = 1;
            if (args_info->list_path_arg)
              free (args_info->list_path_arg); /* free previous string */
            args_info->list_path_arg = gengetopt_strdup (optarg);
            if (args_info->list_path_orig)
              free (args_info->list_path_orig); /* free previous string */
            args_info->list_path_orig = gengetopt_strdup (optarg);
          }
          /* The path to an image.  */
          else if (strcmp (long_options[option_index].name, "image-file") == 0)
          {
            if (local_args_info.image_file_given)
              {
                fprintf (stderr, "%s: `--image-file' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->image_file_given && ! override)
              continue;
            local_args_info.image_file_given = 1;
            args_info->image_file_given = 1;
            if (args_info->image_file_arg)
              free (args_info->image_file_arg); /* free previous string */
            args_info->image_file_arg = gengetopt_strdup (optarg);
            if (args_info->image_file_orig)
              free (args_info->image_file_orig); /* free previous string */
            args_info->image_file_orig = gengetopt_strdup (optarg);
          }
          /* Number of milliseconds to show the detected lanes. Put 0 for infinite i.e. waits for keypress..  */
          else if (strcmp (long_options[option_index].name, "wait") == 0)
          {
            if (local_args_info.wait_given)
              {
                fprintf (stderr, "%s: `--wait' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->wait_given && ! override)
              continue;
            local_args_info.wait_given = 1;
            args_info->wait_given = 1;
            args_info->wait_arg = strtol (optarg, &stop_char, 0);
            if (!(stop_char && *stop_char == '\0')) {
              fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
              goto failure;
            }
            if (args_info->wait_orig)
              free (args_info->wait_orig); /* free previous string */
            args_info->wait_orig = gengetopt_strdup (optarg);
          }
          /* Show the detected lines.  */
          else if (strcmp (long_options[option_index].name, "show") == 0)
          {
            if (local_args_info.show_given)
              {
                fprintf (stderr, "%s: `--show' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->show_given && ! override)
              continue;
            local_args_info.show_given = 1;
            args_info->show_given = 1;
            args_info->show_flag = !(args_info->show_flag);
          }
          /* Step through each image (needs a keypress) or fall through (waits for --wait msecs).  */
          else if (strcmp (long_options[option_index].name, "step") == 0)
          {
            if (local_args_info.step_given)
              {
                fprintf (stderr, "%s: `--step' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->step_given && ! override)
              continue;
            local_args_info.step_given = 1;
            args_info->step_given = 1;
            args_info->step_flag = !(args_info->step_flag);
          }
          /* Show the lane numbers on the output image.  */
          else if (strcmp (long_options[option_index].name, "show-lane-numbers") == 0)
          {
            if (local_args_info.show_lane_numbers_given)
              {
                fprintf (stderr, "%s: `--show-lane-numbers' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->show_lane_numbers_given && ! override)
              continue;
            local_args_info.show_lane_numbers_given = 1;
            args_info->show_lane_numbers_given = 1;
            args_info->show_lane_numbers_flag = !(args_info->show_lane_numbers_flag);
          }
          /* Suffix of images and results.  */
          else if (strcmp (long_options[option_index].name, "output-suffix") == 0)
          {
            if (local_args_info.output_suffix_given)
              {
                fprintf (stderr, "%s: `--output-suffix' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->output_suffix_given && ! override)
              continue;
            local_args_info.output_suffix_given = 1;
            args_info->output_suffix_given = 1;
            if (args_info->output_suffix_arg)
              free (args_info->output_suffix_arg); /* free previous string */
            args_info->output_suffix_arg = gengetopt_strdup (optarg);
            if (args_info->output_suffix_orig)
              free (args_info->output_suffix_orig); /* free previous string */
            args_info->output_suffix_orig = gengetopt_strdup (optarg);
          }
          /* Export all images with detected lanes to the by appending --output-suffix + '.png' to each input image.  */
          else if (strcmp (long_options[option_index].name, "save-images") == 0)
          {
            if (local_args_info.save_images_given)
              {
                fprintf (stderr, "%s: `--save-images' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->save_images_given && ! override)
              continue;
            local_args_info.save_images_given = 1;
            args_info->save_images_given = 1;
            args_info->save_images_flag = !(args_info->save_images_flag);
          }
          /* Export all detected lanes to a text file by appending --output-suffix + '.txt' to --list-file.  */
          else if (strcmp (long_options[option_index].name, "save-lanes") == 0)
          {
            if (local_args_info.save_lanes_given)
              {
                fprintf (stderr, "%s: `--save-lanes' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->save_lanes_given && ! override)
              continue;
            local_args_info.save_lanes_given = 1;
            args_info->save_lanes_given = 1;
            args_info->save_lanes_flag = !(args_info->save_lanes_flag);
          }
          /* Show debugging information and images.  */
          else if (strcmp (long_options[option_index].name, "debug") == 0)
          {
            if (local_args_info.debug_given)
              {
                fprintf (stderr, "%s: `--debug' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
                goto failure;
              }
            if (args_info->debug_given && ! override)
              continue;
            local_args_info.debug_given = 1;
            args_info->debug_given = 1;
            args_info->debug_flag = !(args_info->debug_flag);
          }
          
          break;
        case '?':	/* Invalid option.  */
          /* `getopt_long' already printed an error message.  */
          goto failure;

        default:	/* bug: option not considered.  */
          fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
          abort ();
        } /* switch */
    } /* while */




  cmdline_parser_release (&local_args_info);

  if ( error )
    return (EXIT_FAILURE);

  if (optind < argc)
    {
      int i = 0 ;
      int found_prog_name = 0;
      /* whether program name, i.e., argv[0], is in the remaining args
         (this may happen with some implementations of getopt,
          but surely not with the one included by gengetopt) */

      i = optind;
      while (i < argc)
        if (argv[i++] == argv[0]) {
          found_prog_name = 1;
          break;
        }
      i = 0;

      args_info->inputs_num = argc - optind - found_prog_name;
      args_info->inputs =
        (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
      while (optind < argc)
        if (argv[optind++] != argv[0])
          args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
    }

  return 0;

failure:
  
  cmdline_parser_release (&local_args_info);
  return (EXIT_FAILURE);
}

#ifndef CONFIG_FILE_LINE_SIZE
#define CONFIG_FILE_LINE_SIZE 2048
#endif
#define ADDITIONAL_ERROR " in configuration file "

#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
/* 3 is for "--" and "=" */

char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];

int
cmdline_parser_configfile (char * const filename, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
{
  FILE* file;
  char linebuf[CONFIG_FILE_LINE_SIZE];
  int line_num = 0;
  int i, result, equal;
  char *fopt, *farg;
  char *str_index;
  size_t len, next_token;
  char delimiter;
  int my_argc = 0;
  char **my_argv_arg;
  char *additional_error;

  /* store the program name */
  cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
  cmd_line_list_tmp->next = cmd_line_list;
  cmd_line_list = cmd_line_list_tmp;
  cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);

  if ((file = fopen(filename, "r")) == NULL)
    {
      fprintf (stderr, "%s: Error opening configuration file '%s'\n",
               CMDLINE_PARSER_PACKAGE, filename);
      result = EXIT_FAILURE;
      goto conf_failure;
    }

  while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL)
    {
      ++line_num;
      my_argv[0] = '\0';
      len = strlen(linebuf);
      if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
        {
          fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
                   CMDLINE_PARSER_PACKAGE, filename, line_num);
          result = EXIT_FAILURE;
          goto conf_failure;
        }

      /* find first non-whitespace character in the line */
      next_token = strspn ( linebuf, " \t\r\n");
      str_index  = linebuf + next_token;

      if ( str_index[0] == '\0' || str_index[0] == '#')
        continue; /* empty line or comment line is skipped */

      fopt = str_index;

      /* truncate fopt at the end of the first non-valid character */
      next_token = strcspn (fopt, " \t\r\n=");

      if (fopt[next_token] == '\0') /* the line is over */
        {
          farg  = NULL;
          equal = 0;
          goto noarg;
        }

      /* remember if equal sign is present */
      equal = (fopt[next_token] == '=');
      fopt[next_token++] = '\0';

      /* advance pointers to the next token after the end of fopt */
      next_token += strspn (fopt + next_token, " \t\r\n");
      /* check for the presence of equal sign, and if so, skip it */
      if ( !equal )
        if ((equal = (fopt[next_token] == '=')))
          {
            next_token++;
            next_token += strspn (fopt + next_token, " \t\r\n");
          }
      str_index  += next_token;

      /* find argument */
      farg = str_index;
      if ( farg[0] == '\"' || farg[0] == '\'' )
        { /* quoted argument */
          str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
          if (! str_index)
            {
              fprintf
                (stderr,
                 "%s:%s:%d: unterminated string in configuration file\n",
                 CMDLINE_PARSER_PACKAGE, filename, line_num);
              result = EXIT_FAILURE;
              goto conf_failure;
            }
        }
      else
        { /* read up the remaining part up to a delimiter */
          next_token = strcspn (farg, " \t\r\n#\'\"");
          str_index += next_token;
        }

      /* truncate farg at the delimiter and store it for further check */
      delimiter = *str_index, *str_index++ = '\0';

      /* everything but comment is illegal at the end of line */
      if (delimiter != '\0' && delimiter != '#')
        {
          str_index += strspn(str_index, " \t\r\n");
          if (*str_index != '\0' && *str_index != '#')
            {
              fprintf
                (stderr,
                 "%s:%s:%d: malformed string in configuration file\n",
                 CMDLINE_PARSER_PACKAGE, filename, line_num);
              result = EXIT_FAILURE;
              goto conf_failure;
            }
        }

    noarg:
      ++my_argc;
      len = strlen(fopt);

      strcat (my_argv, len > 1 ? "--" : "-");
      strcat (my_argv, fopt);
      if (len > 1 && ((farg &&*farg) || equal))
          strcat (my_argv, "=");
      if (farg && *farg)
          strcat (my_argv, farg);

      cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
      cmd_line_list_tmp->next = cmd_line_list;
      cmd_line_list = cmd_line_list_tmp;
      cmd_line_list->string_arg = gengetopt_strdup(my_argv);
    } /* while */

  ++my_argc; /* for program name */
  my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
  cmd_line_list_tmp = cmd_line_list;
  for (i = my_argc - 1; i >= 0; --i) {
    my_argv_arg[i] = cmd_line_list_tmp->string_arg;
    cmd_line_list_tmp = cmd_line_list_tmp->next;
  }
  my_argv_arg[my_argc] = 0;

  additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
  strcpy (additional_error, ADDITIONAL_ERROR);
  strcat (additional_error, filename);
  result =
    cmdline_parser_internal (my_argc, my_argv_arg, args_info, override, initialize, check_required, additional_error);

  free (additional_error);
  free (my_argv_arg);

conf_failure:
  if (file)
    fclose(file);

  free_cmd_list();
  if (result == EXIT_FAILURE)
    {
      cmdline_parser_free (args_info);
      exit (EXIT_FAILURE);
    }
  
  return result;
}
