SBSPSS/Utils/Scripter/pfile.cpp

213 lines
3.9 KiB
C++
Raw Normal View History

2000-12-11 21:54:12 +01:00
/*=========================================================================
pfile.cpp
Author: PKG
Created:
Project: Spongebob
Purpose:
Copyright (c) 2000 Climax Development Ltd
===========================================================================*/
/*----------------------------------------------------------------------
Includes
-------- */
#include "pfile.h"
/* Std Lib
------- */
2000-12-11 23:18:54 +01:00
#include <stdlib.h>
#include <string.h>
2000-12-11 21:54:12 +01:00
/* Data
---- */
/*----------------------------------------------------------------------
Tyepdefs && Defines
------------------- */
/*----------------------------------------------------------------------
Structure defintions
-------------------- */
/*----------------------------------------------------------------------
Function Prototypes
------------------- */
/*----------------------------------------------------------------------
Vars
---- */
2000-12-11 23:18:54 +01:00
CPFile *CPFile::s_stack=NULL;
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
extern int openPFile(char *_filename)
{
CPFile *pf;
pf=new CPFile;
return pf->open(_filename);
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
extern int closePFile()
{
CPFile *pf;
int ret;
pf=CPFile::getCurrentFile();
ret=pf->close();
delete pf;
return ret;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
CPFile::CPFile()
{
m_fh=NULL;
m_filename=NULL;
m_charCount=0;
m_lineCount=0;
m_currentCharOnLine=0;
m_errorCount=0;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
int CPFile::open(char *_filename)
{
2000-12-12 17:29:42 +01:00
int ret;
2000-12-11 23:18:54 +01:00
if((m_fh=fopen(_filename,"rt"))==NULL)
{
2000-12-12 21:51:59 +01:00
printf("FATAL: Couldn't open %sfor reading\n",_filename);
2000-12-12 17:29:42 +01:00
ret=false;
2000-12-11 23:18:54 +01:00
}
else
{
m_filename=(char*)malloc(strlen(_filename)+1);
strcpy(m_filename,_filename);
m_next=s_stack;
s_stack=this;
2000-12-12 17:29:42 +01:00
ret=true;
2000-12-11 23:18:54 +01:00
}
2000-12-12 17:29:42 +01:00
2000-12-11 23:18:54 +01:00
return ret;
}
2000-12-11 21:54:12 +01:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-12-11 23:18:54 +01:00
int CPFile::close()
{
2000-12-18 17:58:33 +01:00
printf(" Processed %d char(s) on %d line(s) in file %s\n",m_charCount,m_lineCount,m_filename);
2000-12-11 23:18:54 +01:00
if(m_errorCount)
{
2000-12-18 17:58:33 +01:00
printf(" Found %d error(s) :(\n",m_errorCount);
2000-12-11 23:18:54 +01:00
}
fclose(m_fh);
if(m_next)
{
s_stack=m_next;
}
else
{
s_stack=NULL;
}
2000-12-12 17:29:42 +01:00
return true;
2000-12-11 23:18:54 +01:00
}
2000-12-11 21:54:12 +01:00
2000-12-12 21:51:59 +01:00
/*----------------------------------------------------------------------
Function:
Purpose:
Params: Returns ASCII code, or -1 for eof, as per yygetchar()
Returns:
---------------------------------------------------------------------- */
int CPFile::readChar()
{
int ret;
char c;
if(fread(&c,sizeof(c),1,m_fh)==1)
{
m_charCount++;
m_currentCharOnLine++;
if(c=='\n')
{
m_lineCount++;
m_currentCharOnLine=0;
}
ret=c;
}
else
{
ret=-1;
if(ferror(m_fh))
{
printf("FATAL: Read error!\n");
}
else
{
closePFile();
if(getCurrentFile())
{
return getCurrentFile()->readChar();
}
}
}
return ret;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params: Returns ASCII code, or -1 for eof, as per yygetchar()
Returns:
---------------------------------------------------------------------- */
void CPFile::error(FILE *_fh)
{
fprintf(_fh,"ERROR IN %s LINE %d, COLUMN %d\n",m_filename,m_lineCount+1,m_currentCharOnLine);
m_errorCount++;
}
2000-12-11 21:54:12 +01:00
/*===========================================================================
end */