/* This file is part of the Neper software package. */
/* Copyright (C) 2003-2014, Romain Quey. */
/* See the COPYING file in the top-level directory. */

#include"ned_input_.h"

/* SetDefaultOptions set the options to their default values */
void
ned_input_options_default (struct IN *pIn)
{
  (*pIn).bin = NULL;

  (*pIn).runmode = ut_alloc_1d_char (8);
  strcpy ((*pIn).runmode, "normal");

  (*pIn).modules = ut_alloc_1d_char (100);
  (*pIn).tests = ut_alloc_1d_char (4);
  strcpy ((*pIn).tests, "all");

  return;
}

void
ned_input_options_set (struct IN *pIn, int argc, char **argv)
{
  int i, ArgQty, Res;
  char **ArgList = ut_alloc_2d_char (101, 101);
  char *Arg = NULL;

  // Argument list =====================================================

  // Input data --------------------------------------------------------
  ArgQty = 0;
  strcpy (ArgList[++ArgQty], "-binary");
  strcpy (ArgList[++ArgQty], "-runmode");
  strcpy (ArgList[++ArgQty], "-test");

  // Treating arguments ================================================

  for (i = 1; i < argc; i++)
  {
    if (argv[i][0] != '-'
     || (strlen (argv[i]) >= 2 && (argv[i][1] == 'T'
	                        || argv[i][1] == 'M' || argv[i][1] == 'V')))
    {
      if (strlen ((*pIn).modules) == 0)
	strcpy ((*pIn).modules, argv[i]);
      else
	ut_print_message (2, 0, "Input data already loaded\n");
    }

    else
    {
      /* Searching option name (string completion stuff) */
      Res = ut_string_comp (argv[i], ArgList, ArgQty, &Arg);
      if (Res == 1)
      {
	ut_print_lineheader (2);
	printf ("Several possibilities for option `%s'.\n", argv[i]);
	ut_arg_badarg ();
      }
      else if (Res == -1)
      {
	ut_print_lineheader (2);
	printf ("Unknown option `%s'.\n", argv[i]);
	ut_arg_badarg ();
      }

      // Reading arguments 

      if (!strcmp (Arg, "-binary"))
	ut_arg_nextasstring (argv, &i, Arg, &((*pIn).bin));
      else if (!strcmp (Arg, "-runmode"))
	ut_arg_nextasstring (argv, &i, Arg, &((*pIn).runmode));
      else if (!strcmp (Arg, "-test"))
	ut_arg_nextasstring (argv, &i, Arg, &((*pIn).tests));
      else
	ut_arg_badarg ();
    }
  }

  ut_free_1d_char (Arg);
  ut_free_2d_char (ArgList, 101);

  return;
}
