/*
 *$Source: /mit/hst/src/data/RCS/patient.c,v $
 */

#ifndef lint
static char *rcs="$Header: /mit/hst/src/data/RCS/patient.c,v 2.2 90/08/18 18:31:30 tldavis Exp $";
#endif

#include <stdio.h>
#ifdef SVR4
#include <string.h>
#else
#include <strings.h>
#endif
#include <ctype.h>
#include "../sim/CVDefs.h"
#include "Parameter.h"

/*************************************
 *
 *   Public Routines
 *
 * ONLY ONE VARDATA STRUCTURE, RESET BEFORE EACH PATIENT CHANGE
 *************************************/

/*PUBLIC*/
char *LoadPatient(path)
char *path;
{
  FILE *fp;
  static char line[256], *p, history[10000];
  int placeCode = -1, typeCode = -1;
  double value;
  VarData *vdPtr;

  history[0] = '\0';
  if ((fp = fopen(path, "r")) == NULL) {
    fprintf(stderr, "Can't open patient file %s.\n",path);
    exit(1);
  }

  /* discard leading comments*/
  while (fgets(line, sizeof(line), fp) != NULL)
    if (line[0] != '#') break;

  /* read history up until comment */
  do 
    strcat(history, line);
  while (fgets(line, sizeof(line), fp) != NULL && line[0] != '#');

  /* read parameter updates */
  while (fgets(line, sizeof(line), fp) != NULL) {
    if (line[0] == '#') continue; /* discard comments from now on */
    if (line[0] == '\0' || line[1] == '\0') break;
    p = line; while (!isalpha(*p)) p++;

    if (strncmp(p, "l", 1) == 0) placeCode = LV;
    else if (strncmp(p, "r", 1) == 0) placeCode = RV;
    else if (strncmp(p, "a", 1) == 0) placeCode = SA;
    else if (strncmp(p, "v", 1) == 0) placeCode = SV;
    else if (strncmp(p, "pa",2) == 0) placeCode = PA;
    else if (strncmp(p, "pv",2) == 0) placeCode = PV;
    else if (strncmp(p, "micro",1)==0) placeCode = SC;
    else if (strncmp(p, "pmicro",2)==0)placeCode = PC;
    else if (strncmp(p,"sys",3) == 0) placeCode = SYSTEM;
    else {fprintf(stderr,"Bad Place %s in file %s.\n", p, path); 
	  exit(1);}
    while (isalpha(*p)) p++;
    while (isspace(*p)) p++;

    if (strncmp(p, "csys", 4) == 0) typeCode = CAPACITANCE_SYS;
    else if (strncmp(p, "cdias", 4) == 0) typeCode = CAPACITANCE_DIAS;
    else if (strncmp(p, "cap", 3) == 0) typeCode = CAPACITANCE;
    else if (strncmp(p, "zvol",4) == 0) typeCode = ZPVOLUME;
    else if (strncmp(p, "zp", 2) == 0)  typeCode = ZPVOLUME;
    else if (strncmp(p, "res", 3) == 0) typeCode = RESISTANCE;
    else if (strncmp(p, "hr", 2) == 0) typeCode = HEARTRATE;
    else if (strncmp(p, "pres", 4) == 0) typeCode = PRESSURE;
    else if (strncmp(p, "vol",  3) == 0) typeCode = VOLUME;
    else if (strncmp(p, "flow", 4) == 0) typeCode = FLOW;
    else if (strncmp(p, "time", 4) == 0) typeCode = TIME;
    else {fprintf(stderr,"Bad Type %s in file %s.\n", p, path); 
	  exit(1);}
    while (isalpha(*p)) p++;
    while (isspace(*p)) p++;
    
    sscanf(p,"%lf", &value);

    vdPtr = get_vardata(placeCode, typeCode);
#ifdef DEBUG
    printf("Patient: place %d type %d old value %lf new value %lf\n",placeCode,
	   typeCode, vdPtr->normal, value);
#endif
    vdPtr->normal = value;
  }

  return(history);
}
