- got rid of M_ReadFile(Malloc) which werew each used only once in the entire code. These were still using the low level POSIX-style file interface which shouldn't really be used anymore.

- let FScanner::OpenFile return an error instead of throwing an exception. The exception was never used anyway aside from being caught right away to be ignored.
This commit is contained in:
Christoph Oelckers 2017-12-02 13:09:59 +01:00
parent e15b23f132
commit 838e52001c
8 changed files with 39 additions and 81 deletions

View file

@ -532,13 +532,9 @@ bool FCajunMaster::LoadBots ()
DPrintf (DMSG_ERROR, "No " BOTFILENAME ", so no bots\n");
return false;
}
try
if (!sc.OpenFile(tmp))
{
sc.OpenFile(tmp);
}
catch (CRecoverableError &err)
{
Printf("%s. So no bots\n", err.GetMessage());
Printf("Unable to open %s. So no bots\n");
return false;
}

View file

@ -2798,7 +2798,17 @@ void G_DoPlayDemo (void)
{
FixPathSeperator (defdemoname);
DefaultExtension (defdemoname, ".lmp");
M_ReadFileMalloc (defdemoname, &demobuffer);
FileReader fr;
if (!fr.Open(defdemoname))
{
I_Error("Unable to open demo '%s'", defdemoname.GetChars());
}
auto len = fr.GetLength();
demobuffer = (uint8_t*)M_Malloc(len);
if (fr.Read(demobuffer, len) != len)
{
I_Error("Unable to read demo '%s'", defdemoname.GetChars());
}
}
demo_p = demobuffer;

View file

@ -115,60 +115,6 @@ bool M_WriteFile (char const *name, void *source, int length)
}
//
// M_ReadFile
//
int M_ReadFile (char const *name, uint8_t **buffer)
{
int handle, count, length;
struct stat fileinfo;
uint8_t *buf;
handle = open (name, O_RDONLY | O_BINARY, 0666);
if (handle == -1)
I_Error ("Couldn't read file %s", name);
// [BL] Use stat instead of fstat for v140_xp hack
if (stat (name,&fileinfo) == -1)
I_Error ("Couldn't read file %s", name);
length = fileinfo.st_size;
buf = new uint8_t[length];
count = read (handle, buf, length);
close (handle);
if (count < length)
I_Error ("Couldn't read file %s", name);
*buffer = buf;
return length;
}
//
// M_ReadFile (same as above but use malloc instead of new to allocate the buffer.)
//
int M_ReadFileMalloc (char const *name, uint8_t **buffer)
{
int handle, count, length;
struct stat fileinfo;
uint8_t *buf;
handle = open (name, O_RDONLY | O_BINARY, 0666);
if (handle == -1)
I_Error ("Couldn't read file %s", name);
// [BL] Use stat instead of fstat for v140_xp hack
if (stat (name,&fileinfo) == -1)
I_Error ("Couldn't read file %s", name);
length = fileinfo.st_size;
buf = (uint8_t*)M_Malloc(length);
count = read (handle, buf, length);
close (handle);
if (count < length)
I_Error ("Couldn't read file %s", name);
*buffer = buf;
return length;
}
//---------------------------------------------------------------------------
//
// PROC M_FindResponseFile

View file

@ -34,8 +34,6 @@ class FIWadManager;
extern FGameConfigFile *GameConfig;
bool M_WriteFile (char const *name, void *source, int length);
int M_ReadFile (char const *name, uint8_t **buffer);
int M_ReadFileMalloc (char const *name, uint8_t **buffer);
void M_FindResponseFile (void);
// [RH] M_ScreenShot now accepts a filename parameter.

View file

@ -122,7 +122,8 @@ static TArray<FString> ParseSteamRegistry(const char* path)
// Read registry data
FScanner sc;
sc.OpenFile(path);
if (sc.OpenFile(path))
{
sc.SetCMode(true);
// Find the SteamApps listing
@ -142,7 +143,7 @@ static TArray<FString> ParseSteamRegistry(const char* path)
}
PSR_FindEndBlock(sc);
}
}
return dirs;
}

View file

@ -244,18 +244,25 @@ void FScanner::Open (const char *name)
//
//==========================================================================
void FScanner::OpenFile (const char *name)
bool FScanner::OpenFile (const char *name)
{
uint8_t *filebuf;
int filesize;
Close ();
filesize = M_ReadFile (name, &filebuf);
FileReader fr(name);
if (!fr.Open(name)) return false;
auto filesize = fr.GetLength();
auto filebuf = new uint8_t[filesize];
if (fr.Read(filebuf, filesize) != filesize)
{
delete[] filebuf;
return false;
}
ScriptBuffer = FString((const char *)filebuf, filesize);
delete[] filebuf;
ScriptName = name; // This is used for error messages so the full file name is preferable
LumpNum = -1;
PrepareScript ();
return true;
}
//==========================================================================

View file

@ -19,7 +19,7 @@ public:
FScanner &operator=(const FScanner &other);
void Open(const char *lumpname);
void OpenFile(const char *filename);
bool OpenFile(const char *filename);
void OpenMem(const char *name, const char *buffer, int size);
void OpenString(const char *name, FString buffer);
void OpenLumpNum(int lump);

View file

@ -137,7 +137,7 @@ static void ParseStatistics(const char *fn, TArray<FStatistics> &statlist)
try
{
FScanner sc;
sc.OpenFile(fn);
if (!sc.OpenFile(fn)) return;
while (sc.GetString())
{