/*  FtablesDHSVM.cpp:  Interface for application of ftable4.cpp in DHSVM */

/* #pragma warning( disable : 4244 ) */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

extern "C" {
#include "settings.h"
#include "data.h"
#include "DHSVMerror.h"
#include "functions.h"
#include "fileio.h"
#include "getinit.h"
}
#include "ftablesdhsvm.h"
#include "ftable.h"

//static Ftable *ftab;
static int num_ftab;

// for use in channel.c:  channel_route_segment() -------------------
extern "C" { float route_segment( unsigned short int seg_id, float ivol, 
                                 float dT, float *out_1 ); 
}

/*****************************************************************************
InitFtables() called in MainDHSVM.c
*****************************************************************************/
void
InitFtables(LISTPTR Input )
{
  enum KEYS {mult_file, table_file};
  
  int i;
  STRINIENTRY StrEnv[] = {
    {"FTABLE", "FTABLE MULTIPLIER FILE"    , ""  , NULL},
    {"FTABLE", "FTABLE DATA FILE", ""  , NULL},
    {NULL     , NULL                  , ""  , NULL}
  };
  
  printf("Initializing Ftables\n");
  
  /* Read the key-entry pairs from the [FTABLE] section of the input file */
  for (i = 0; StrEnv[i].SectionName; i++) {
    GetInitString(StrEnv[i].SectionName, StrEnv[i].KeyName, StrEnv[i].Default,
      StrEnv[i].VarStr, (unsigned long) BUFSIZE, Input);
    if (!StrEnv[i].VarStr)
      ReportError(StrEnv[i].KeyName, 51);
  }
  
  ftab = buildFtables( StrEnv[mult_file].VarStr, StrEnv[table_file].VarStr, &num_ftab );
  
}

/*****************************************************************************
route_segment()
*****************************************************************************/
float route_segment( unsigned short int seg_id, float ivol, float dT, float *out_1 ){
  RESULTS r;
  
  // need to check and make sure we don't try to access nonexistent table
  ftab[seg_id-1].adjustTable();

  r = ftab[seg_id-1].route( ivol, dT );

  *out_1 = r.outVol[1];

// printf("RSeg=%d, RI=%.4f \n", seg_id, r.outVol[1]);
  
  return r.outVol[0];
}
