/*
What's new:
C44 - [Martin Zoubek] first public release
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "FileIO.h"
#include "Memory.h"
#include "Types.h"

//read one char from file ignoring comments
static int FileIO_ReadChar(FILE *f){
  int ch;
  ch=fgetc(f);
  if(ch=='*')  //remove comment line
    while(ch!=10&&ch!=13&&ch!=EOF)
      ch=fgetc(f);
  if(ch==10||ch==13)  //convert all whitespaces to space
    return ' ';
  return ch;
  }

//read one word from file
static BOOL FileIO_ReadWord(FILE *f,char *line){
  uint i=0;
  int ch;
  while((ch=FileIO_ReadChar(f))==' ');  //skip leading spaces
  while(ch!=' '&&ch!=EOF){  //read line
    line[i++]=(char)ch;
    if(i==255)  //max length
      ch=' ';
    else
      ch=FileIO_ReadChar(f);
    }
  line[i]=0;  //terminate string
  return (BOOL)(ch!=EOF);
  }

int FileIO_ReadInteger(FILE *f){
  char number[256];
  if(!f)
    return 0;
  FileIO_ReadWord(f,number);
  return atoi(number);
  }

double FileIO_ReadFloat(FILE *f){
  char number[256];
  if(!f)
    return 0;
  FileIO_ReadWord(f,number);
  return atof(number);
  }

char *FileIO_ReadString(FILE *f){
  char s[256],*s2;
  if(!f)
    return 0;
  FileIO_ReadWord(f,s);
  if(!Memory_GetM(s2,char,strlen(s)+1))
    return 0;
  strcpy(s2,s);
  return s2;
  }

