diff --git a/src/b_game.cpp b/src/b_game.cpp index 1cb91ddf49..4300e0ada6 100644 --- a/src/b_game.cpp +++ b/src/b_game.cpp @@ -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; } diff --git a/src/g_game.cpp b/src/g_game.cpp index ec1a7be506..b2e27eed42 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -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; diff --git a/src/m_misc.cpp b/src/m_misc.cpp index bda8dcb806..2b23ef6c2e 100644 --- a/src/m_misc.cpp +++ b/src/m_misc.cpp @@ -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 diff --git a/src/m_misc.h b/src/m_misc.h index 660eb7a587..2eee63b7d3 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -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. diff --git a/src/posix/i_steam.cpp b/src/posix/i_steam.cpp index 9819bc09e3..9542bdd4aa 100644 --- a/src/posix/i_steam.cpp +++ b/src/posix/i_steam.cpp @@ -122,27 +122,28 @@ static TArray ParseSteamRegistry(const char* path) // Read registry data FScanner sc; - sc.OpenFile(path); - sc.SetCMode(true); - - // Find the SteamApps listing - if(PSR_FindAndEnterBlock(sc, "InstallConfigStore")) + if (sc.OpenFile(path)) { - 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); } - return dirs; } diff --git a/src/sc_man.cpp b/src/sc_man.cpp index 2a515d91bd..277604d686 100644 --- a/src/sc_man.cpp +++ b/src/sc_man.cpp @@ -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; } //========================================================================== diff --git a/src/sc_man.h b/src/sc_man.h index 44dd9370a5..9962cbde09 100644 --- a/src/sc_man.h +++ b/src/sc_man.h @@ -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); diff --git a/src/statistics.cpp b/src/statistics.cpp index e70642cf41..865ba04110 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -137,7 +137,7 @@ static void ParseStatistics(const char *fn, TArray &statlist) try { FScanner sc; - sc.OpenFile(fn); + if (!sc.OpenFile(fn)) return; while (sc.GetString()) {