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

void
neut_tesr_fprintf (FILE * file, char *format, struct TESR Tesr)
{
  char *format2 = ut_alloc_1d_char (100);

  if (format == NULL || strlen (format) == 0 || !strcmp (format, "default"))
  {
    if (Tesr.CellQty < 256)
      strcpy (format2, "binary8");
    else if (Tesr.CellQty < 65536)
      strcpy (format2, "binary16");
    else
      strcpy (format2, "binary32");
  }
  else
    strcpy (format2, format);

  neut_tesr_fprintf_head (file, format2, Tesr);
  neut_tesr_fprintf_cell (file, Tesr);
  neut_tesr_fprintf_data (file, format2, Tesr);
  neut_tesr_fprintf_foot (file);

  ut_free_1d_char (format2);

  return;
}

void
neut_tesr_name_fprintf_raw (char *tesr, char *raw, char *format, struct TESR Tesr)
{
  char *format2 = ut_alloc_1d_char (100);
  FILE *fp1 = NULL;

  fp1 = ut_file_open (tesr, "w");

  if (format == NULL || strlen (format) == 0 || !strcmp (format, "default"))
  {
    if (Tesr.CellQty < 256)
      strcpy (format2, "binary8");
    else if (Tesr.CellQty < 65536)
      strcpy (format2, "binary16");
    else
      strcpy (format2, "binary32");
  }
  else
    strcpy (format2, format);

  neut_tesr_fprintf_head (fp1, format2, Tesr);
  neut_tesr_fprintf_cell (fp1, Tesr);
  neut_tesr_fprintf_data_raw (fp1, raw, format2, Tesr);
  neut_tesr_fprintf_foot (fp1);

  ut_file_close (fp1, tesr, "w");

  ut_free_1d_char (format2);

  return;
}
