/*
 * SUMMARY:      Files.c - File functions
 * USAGE:        Part of DHSVM
 *
 * AUTHOR:       Bart Nijssen
 * ORG:          University of Washington, Department of Civil Engineering
 * E-MAIL:       nijssen@u.washington.edu
 * ORIG-DATE:    Apr-96
 * Last Change: Tue Apr  3 08:45:18 2001 by Scott Waichler <waichler@tuff.pnl.gov>
 * DESCRIPTION:  File and I/O functions
 * DESCRIP-END.
 * FUNCTIONS:    OpenFile() 
 *               ScanInts() 
 *               ScanFloats() 
 *               SkipLines()             
 *               SkipHeader() 
 *               ScanDoubles() 
 *               ScanUChars() 
 * COMMENTS:     
 */

#ifndef lint
static char vcid[] = "$Id: Files.c,v 1.4 2001/04/04 16:43:47 waichler Exp $";
#endif /* lint */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "settings.h"
#include "data.h"
#include "DHSVMerror.h"
#include "functions.h"
#include "constants.h"
#include "init.h"
#include "fileio.h"

/*****************************************************************************
  OpenFile()
*****************************************************************************/
void OpenFile(FILE **FilePtr, char *FileName, char *Mode, 
	      uchar OverWrite)
{
  struct stat FileInfo;

  /* if OverWrite is FALSE and the Mode is not read, check whether the file 
     already exists */ 

  if (!OverWrite && strstr(Mode, "w")) {
    if (!(stat(FileName, &FileInfo))) 
      ReportError(FileName, 4);
  }
  if (!(*FilePtr = fopen(FileName, Mode)))
    ReportError(FileName, 3);


}

/*****************************************************************************
  ScanUChars()
*****************************************************************************/
uchar ScanUChars(FILE *FilePtr, uchar *X, int N)
{
  int Ctr;
  char Str[2];

  for (Ctr = 0; Ctr < N; Ctr++) {
    if (fscanf(FilePtr, "%1s", Str) != 1)
      break;
    X[Ctr] = Str[0];
  }

  return Ctr;
}

/*****************************************************************************
  ScanInts()
*****************************************************************************/
int ScanInts(FILE *FilePtr, int *X, int N)
{
  int Ctr;

  for (Ctr = 0; Ctr < N; Ctr++) {
    if (fscanf(FilePtr, "%d", &X[Ctr]) != 1)
      break;
  }

  return Ctr;
}

/*****************************************************************************
  ScanFloats()
*****************************************************************************/
int ScanFloats(FILE *FilePtr, float *X, int N)
{
  int Ctr;

  for (Ctr = 0; Ctr < N; Ctr++) {
    if (fscanf(FilePtr, "%f", &X[Ctr]) != 1)
      break;
  }

  return Ctr;
}

/*****************************************************************************
  ScanDoubles()
*****************************************************************************/
int ScanDoubles(FILE *FilePtr, double *X, int N)
{
  int Ctr;

  for (Ctr = 0; Ctr < N; Ctr++) {
    if (fscanf(FilePtr, "%lf", &X[Ctr]) != 1)
      break;
  }

  return Ctr;
}

/*****************************************************************************
  SkipLines()
*****************************************************************************/
void SkipLines(FILES *InFile, int NLines)
{
  int Ctr;
  char str[BUFSIZE+1];

  for (Ctr = 0; Ctr < NLines; Ctr++) {
    if(!fgets(str, BUFSIZE, InFile->FilePtr)) 
      ReportError(InFile->FileName, 5);
  } 
}
 
/*****************************************************************************
  SkipHeader()
*****************************************************************************/
void SkipHeader(FILES *InFile, int NLines)
{
  SkipLines(InFile, NLines);
}


/*  SRW 12-15-00 adds this function below from another version of Files.c
	in order for MakeModelStateBin.c to compile.  */

/*****************************************************************************
  Function name: MakeFileNameGen()

  Purpose      : Generic file name generator, called by format specific file 
                 name generating functions

  Required     :
    Path     - directory where file will be created
    Str1     - first part of file name
    Str2     - second part of file name
    Str3     - third part of file name
    FileName - new file name

  Returns      : void

  Modifies     : FileName

  Comments     : Str3 is intended to be used as an extension indicating the 
                 file format , e.g. ".bin" or ".hdf"
*****************************************************************************/
void MakeFileNameGen(char *Path, char *Str1, char *Str2, char *Str3,
                     char *FileName)
{
  char TempStr[MAXSTRING+1];
  const char *Routine = "MakeFileNameGen";

  sprintf(TempStr, "%s%s%s%s", Path, Str1, Str2, Str3);

  if (strlen(TempStr) > NAMESIZE) 
    TempStr[NAMESIZE] = '\0';

  InitCharArray(FileName, NAMESIZE+1);
  strcpy(FileName, TempStr);
}

