#include"mbasic.h"

char * InputFilePtr,* InputFileInitPtr;
static int last_char=0;

void io_load_program(char * name)
{
 FILE * f;
 int sz;
 f=fopen(name,"rb");
 if(!f) Error("Unable to open input file");
 fseek(f,0,SEEK_END);
 sz=ftell(f);
 fseek(f,0,SEEK_SET);
 InputFilePtr=InputFileInitPtr=malloc(sz+0x9000);
 memset(InputFilePtr,0,sz+0x9000);
 last_char=sz;
 if(!InputFilePtr) 
 {
  fclose(f);
  Error("Unable to allocate memory for input file");
 }
 fread(InputFilePtr,1,sz,f);
 fclose(f);
}

void io_include(char * name)
{
 FILE * f;
 int sz;
 f=fopen(name,"rb");
 if(!f) Error("Unable to open include");
 fseek(f,0,SEEK_END);
 sz=ftell(f);
 fseek(f,0,SEEK_SET);
 fread(InputFileInitPtr+last_char,1,sz,f);
 fclose(f);
 last_char+=sz;
}

void io_unload_program(void)
{
 free(InputFileInitPtr);
}

extern int CurrentLine;

char GetProgramChar(void)
{
 char p;
 p=*InputFilePtr;
 InputFilePtr++;
 if(p=='\n') CurrentLine++;
 return p;
}

void UngetChar(void)
{
 InputFilePtr--;
}
