- 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"); DPrintf (DMSG_ERROR, "No " BOTFILENAME ", so no bots\n");
return false; return false;
} }
try if (!sc.OpenFile(tmp))
{ {
sc.OpenFile(tmp); Printf("Unable to open %s. So no bots\n");
}
catch (CRecoverableError &err)
{
Printf("%s. So no bots\n", err.GetMessage());
return false; return false;
} }

View file

@ -2798,7 +2798,17 @@ void G_DoPlayDemo (void)
{ {
FixPathSeperator (defdemoname); FixPathSeperator (defdemoname);
DefaultExtension (defdemoname, ".lmp"); 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; 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 // PROC M_FindResponseFile

View file

@ -34,8 +34,6 @@ class FIWadManager;
extern FGameConfigFile *GameConfig; extern FGameConfigFile *GameConfig;
bool M_WriteFile (char const *name, void *source, int length); 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); void M_FindResponseFile (void);
// [RH] M_ScreenShot now accepts a filename parameter. // [RH] M_ScreenShot now accepts a filename parameter.

View file

@ -122,27 +122,28 @@ static TArray<FString> ParseSteamRegistry(const char* path)
// Read registry data // Read registry data
FScanner sc; FScanner sc;
sc.OpenFile(path); if (sc.OpenFile(path))
sc.SetCMode(true);
// Find the SteamApps listing
if(PSR_FindAndEnterBlock(sc, "InstallConfigStore"))
{ {
if(PSR_FindAndEnterBlock(sc, "Software")) sc.SetCMode(true);
// Find the SteamApps listing
if (PSR_FindAndEnterBlock(sc, "InstallConfigStore"))
{ {
if(PSR_FindAndEnterBlock(sc, "Valve")) if (PSR_FindAndEnterBlock(sc, "Software"))
{ {
if(PSR_FindAndEnterBlock(sc, "Steam")) if (PSR_FindAndEnterBlock(sc, "Valve"))
{ {
dirs = PSR_ReadBaseInstalls(sc); if (PSR_FindAndEnterBlock(sc, "Steam"))
{
dirs = PSR_ReadBaseInstalls(sc);
}
PSR_FindEndBlock(sc);
} }
PSR_FindEndBlock(sc); PSR_FindEndBlock(sc);
} }
PSR_FindEndBlock(sc); PSR_FindEndBlock(sc);
} }
PSR_FindEndBlock(sc);
} }
return dirs; 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 (); 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); ScriptBuffer = FString((const char *)filebuf, filesize);
delete[] filebuf; delete[] filebuf;
ScriptName = name; // This is used for error messages so the full file name is preferable ScriptName = name; // This is used for error messages so the full file name is preferable
LumpNum = -1; LumpNum = -1;
PrepareScript (); PrepareScript ();
return true;
} }
//========================================================================== //==========================================================================

View file

@ -19,7 +19,7 @@ public:
FScanner &operator=(const FScanner &other); FScanner &operator=(const FScanner &other);
void Open(const char *lumpname); 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 OpenMem(const char *name, const char *buffer, int size);
void OpenString(const char *name, FString buffer); void OpenString(const char *name, FString buffer);
void OpenLumpNum(int lump); void OpenLumpNum(int lump);

View file

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