/* 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"neut_tesr_fscanf_.h"

void
neut_tesr_fscanf (FILE * file, int* bounds, double* scale, struct TESR *pTesr)
{
  char *format = NULL;

  neut_tesr_free (pTesr);

  neut_tesr_fscanf_head (pTesr, bounds, &format, file);
  neut_tesr_fscanf_cell (pTesr, file);
  neut_tesr_fscanf_data (pTesr, bounds, scale, format, file);
  neut_tesr_fscanf_foot (file);

  ut_free_1d_char (format);

  return;
}

void
neut_tesr_name_fscanf (char *name, struct TESR *pTesr)
{
  FILE *file = NULL;
  char** list = NULL;
  int i, qty;
  char** string = ut_alloc_2d_char (6, 1000);
  int* bounds = NULL;
  double *scale = NULL;

  ut_string_separate (name, NEUT_SEP_DEP, &list, &qty);

  if (qty == 2)
  {
    if (!strncmp (list[1], "crop", 4))
    {
      ut_string_fnr (list[1], '|', ' ');
      list[1][strlen (list[1]) - 1] = '\0';

      bounds = ut_alloc_1d_int (6);
      qty = sscanf (list[1], "crop(%s%s%s%s%s%s",
		    string[0], string[1], string[2],
		    string[3], string[4], string[5]);

      for (i = 0; i < qty; i++)
	ut_math_eval_int (string[i], 0, NULL, NULL, &(bounds[i]));
      for (i = qty; i < 6; i++)
	bounds[i] = -1;
    }
    else if (!strncmp (list[1], "scale", 5))
    {
      ut_string_fnr (list[1], '|', ' ');
      list[1][strlen (list[1]) - 1] = '\0';

      scale = ut_alloc_1d (3);
      qty = sscanf (list[1], "scale(%s%s%s",
		    string[0], string[1], string[2]);

      if (qty == 1)
      {
	ut_math_eval (string[0], 0, NULL, NULL, &(scale[0]));
	ut_array_1d_set (scale, 3, scale[0]);
      }
      else
      {
	for (i = 0; i < qty; i++)
	  ut_math_eval (string[i], 0, NULL, NULL, &(scale[i]));
	for (i = qty; i < 3; i++)
	  scale[i] = 1;
      }
    }
    else
      ut_print_message (2, 0, "Failed to process expression `%s'.\n",
	  name);
  }

  file = ut_file_open (list[0], "r");
  neut_tesr_fscanf (file, bounds, scale, pTesr);
  ut_file_close (file, list[0], "r");

  ut_free_1d_int (bounds);
  ut_free_1d (scale);
  ut_free_2d_char (string, 6);

  return;
}
