From fb7256bd01a9ad3539ec8cd054e4cc7d5f267884 Mon Sep 17 00:00:00 2001 From: danij Date: Mon, 1 Oct 2012 01:01:38 +0100 Subject: [PATCH] Refactor: Began work on turning DFile into a C++ class --- .../engine/portable/include/dfilebuilder.h | 69 +++++++++++-------- doomsday/engine/portable/src/dfile.cpp | 36 +++++----- doomsday/engine/portable/src/fs_main.cpp | 28 ++++---- 3 files changed, 71 insertions(+), 62 deletions(-) diff --git a/doomsday/engine/portable/include/dfilebuilder.h b/doomsday/engine/portable/include/dfilebuilder.h index e081f060a6..9c36d96f67 100644 --- a/doomsday/engine/portable/include/dfilebuilder.h +++ b/doomsday/engine/portable/include/dfilebuilder.h @@ -26,36 +26,49 @@ #include "fs_main.h" #ifdef __cplusplus -extern "C" { -#endif -void DFileBuilder_Init(void); -void DFileBuilder_Shutdown(void); - -/** - * Open a new read-only stream on the specified file. - * - * @param af File system object representing the file being opened. - */ -DFile* DFileBuilder_NewFromAbstractFile(AbstractFile* af); +class DFileBuilder +{ +public: + static void init(); + static void shutdown(); + + /** + * Create a new handle on the AbstractFile @a af. + * + * @param af VFS object representing the file being opened. + */ + static DFile* fromAbstractFile(AbstractFile& af); + + /** + * Create a new handle on a lump of AbstractFile @a af. + * + * @param af VFS object representing the container of the lump to be opened. + * @param lumpIdx Logical index of the lump within @a af to be opened. + * @param dontBuffer @c true= do not buffer a copy of the lump. + */ + static DFile* fromAbstractFileLump(AbstractFile& af, int lumpIdx, bool dontBuffer); + + /** + * Open a new handle on the specified system file. + * + * @param file File system handle to the file being opened. + * @param baseOffset Offset from the start of the file in bytes to begin. + */ + static DFile* fromFile(FILE& file, size_t baseOffset); + + /** + * Create a duplicate of handle @a hndl. Note that the duplicate is in + * fact a "reference" to the original, so all changes to the file which they + * represent are implicitly shared. + * + * @param hndl DFile handle to be duplicated. + */ + static DFile* dup(DFile const& hndl); +}; -/** - * Open a new read-only stream on the specified lump-file. - * - * @param af File system object representing the container of the lump to be opened. - * @param lumpIdx Logical index of the lump within @a container to be opened. - * @param dontBuffer @c true= do not buffer a copy of the lump. - */ -DFile* DFileBuilder_NewFromAbstractFileLump(AbstractFile* af, int lumpIdx, boolean dontBuffer); - -/** - * Open a new read-only stream on the specified system file. - * @param file File system handle to the file being opened. - * @param baseOffset Offset from the start of the file in bytes to begin. - */ -DFile* DFileBuilder_NewFromFile(FILE* hndl, size_t baseOffset); - -DFile* DFileBuilder_NewCopy(DFile const* file); +extern "C" { +#endif /** * Non-public methods of DFile. Placed here temporarily. diff --git a/doomsday/engine/portable/src/dfile.cpp b/doomsday/engine/portable/src/dfile.cpp index 3959963f54..6704d8bb3a 100644 --- a/doomsday/engine/portable/src/dfile.cpp +++ b/doomsday/engine/portable/src/dfile.cpp @@ -76,7 +76,7 @@ static void errorIfNotValid(DFile const* file, const char* callerName) exit(1); // Unreachable. } -void DFileBuilder_Init(void) +void DFileBuilder::init(void) { if(!inited) { @@ -84,10 +84,10 @@ void DFileBuilder_Init(void) inited = true; return; } - Con_Error("DFileBuilder_Init: Already initialized."); + Con_Error("DFileBuilder::init: Already initialized."); } -void DFileBuilder_Shutdown(void) +void DFileBuilder::shutdown(void) { if(inited) { @@ -100,13 +100,13 @@ void DFileBuilder_Shutdown(void) return; } #if _DEBUG - Con_Error("DFileBuilder_Shutdown: Not presently initialized."); + Con_Error("DFileBuilder::shutdown: Not presently initialized."); #endif } -DFile* DFileBuilder_NewFromAbstractFileLump(AbstractFile* container, int lumpIdx, boolean dontBuffer) +DFile* DFileBuilder::fromAbstractFileLump(AbstractFile& container, int lumpIdx, bool dontBuffer) { - LumpInfo const* info = F_LumpInfo(container, lumpIdx); + LumpInfo const* info = F_LumpInfo(&container, lumpIdx); DFile* file; if(!info) return NULL; @@ -118,49 +118,45 @@ DFile* DFileBuilder_NewFromAbstractFileLump(AbstractFile* container, int lumpIdx file->size = info->size; file->pos = file->data = (uint8_t*) M_Malloc(file->size); if(!file->data) - Con_Error("DFileBuilder_NewFromAbstractFileLump: Failed on allocation of %lu bytes for data buffer.", + Con_Error("DFileBuilder::newFromAbstractFileLump: Failed on allocation of %lu bytes for data buffer.", (unsigned long) file->size); #if _DEBUG VERBOSE2( - AutoStr* path = F_ComposeLumpPath(container, lumpIdx); + AutoStr* path = F_ComposeLumpPath(&container, lumpIdx); Con_Printf("DFile [%p] buffering \"%s:%s\"...\n", (void*)file, - F_PrettyPath(Str_Text(AbstractFile_Path(container))), + F_PrettyPath(Str_Text(AbstractFile_Path(&container))), F_PrettyPath(Str_Text(path))); ) #endif - F_ReadLumpSection(container, lumpIdx, (uint8_t*)file->data, 0, info->size); + F_ReadLumpSection(&container, lumpIdx, (uint8_t*)file->data, 0, info->size); } return file; } -DFile* DFileBuilder_NewFromAbstractFile(AbstractFile* af) +DFile* DFileBuilder::fromAbstractFile(AbstractFile& af) { DFile* file = DFile_New(); - DENG_ASSERT(af); - file->file = af; + file->file = ⁡ file->flags.open = true; file->flags.reference = true; return file; } -DFile* DFileBuilder_NewFromFile(FILE* hndl, size_t baseOffset) +DFile* DFileBuilder::fromFile(FILE& hndl, size_t baseOffset) { DFile* file = DFile_New(); - DENG_ASSERT(hndl); file->flags.open = true; - file->hndl = hndl; + file->hndl = &hndl; file->baseOffset = baseOffset; return file; } -DFile* DFileBuilder_NewCopy(const DFile* file) +DFile* DFileBuilder::dup(DFile const& file) { - DENG_ASSERT(inited && file); - DFile* clone = DFile_New(); clone->flags.open = true; clone->flags.reference = true; - clone->file = DFile_File_const(file); + clone->file = DFile_File_const(&file); return clone; } diff --git a/doomsday/engine/portable/src/fs_main.cpp b/doomsday/engine/portable/src/fs_main.cpp index 314969f267..3d8a552834 100644 --- a/doomsday/engine/portable/src/fs_main.cpp +++ b/doomsday/engine/portable/src/fs_main.cpp @@ -484,7 +484,7 @@ void F_Init(void) // This'll force the loader NOT to flag new files as "runtime". loadingForStartup = true; - DFileBuilder_Init(); + DFileBuilder::init(); openFiles = new FileList(); loadedFiles = new FileList(); @@ -516,7 +516,7 @@ void F_Shutdown(void) clearFileIds(); // Should be null-op if bookkeeping is correct. clearLumpDirectorys(); - DFileBuilder_Shutdown(); + DFileBuilder::shutdown(); inited = false; } @@ -889,7 +889,7 @@ lumpnum_t F_OpenAuxiliary3(const char* path, size_t baseOffset, boolean silent) return -1; } - dfile = DFileBuilder_NewFromFile(file, baseOffset); + dfile = DFileBuilder::fromFile(*file, baseOffset); if(WadFile_Recognise(dfile)) { @@ -906,10 +906,10 @@ lumpnum_t F_OpenAuxiliary3(const char* path, size_t baseOffset, boolean silent) F_InitLumpInfo(&info); info.lastModified = F_LastModified(Str_Text(foundPath)); - dfile = DFileBuilder_NewFromAbstractFile(newWadFile(dfile, Str_Text(foundPath), &info)); + dfile = DFileBuilder::fromAbstractFile(*newWadFile(dfile, Str_Text(foundPath), &info)); openFiles->push_back(dfile); DFile_SetList(dfile, openFiles); - DFile* loadedFilesHndl = DFileBuilder_NewCopy(dfile); + DFile* loadedFilesHndl = DFileBuilder::dup(*dfile); loadedFiles->push_back(loadedFilesHndl); DFile_SetList(loadedFilesHndl, loadedFiles); WadFile_PublishLumpsToDirectory((WadFile*)DFile_File(dfile), ActiveWadLumpDirectory); @@ -1781,7 +1781,7 @@ static DFile* openAsLumpFile(AbstractFile* container, int lumpIdx, // Get a handle to the lump we intend to open. /// @todo The way this buffering works is nonsensical it should not be done here /// but should instead be deferred until the content of the lump is read. - hndl = DFileBuilder_NewFromAbstractFileLump(container, lumpIdx, false/*dontBuffer*/); + hndl = DFileBuilder::fromAbstractFileLump(*container, lumpIdx, false/*dontBuffer*/); // Prepare the temporary info descriptor. F_InitLumpInfo(&info); @@ -1793,7 +1793,7 @@ static DFile* openAsLumpFile(AbstractFile* container, int lumpIdx, // If not opened; assume its a generic LumpFile. if(!file) { - file = DFileBuilder_NewFromAbstractFile(newLumpFile(hndl, Str_Text(&absPath), &info)); + file = DFileBuilder::fromAbstractFile(*newLumpFile(hndl, Str_Text(&absPath), &info)); } assert(file); @@ -1809,7 +1809,7 @@ static DFile* tryOpenAsZipFile(DFile* file, const char* path, const LumpInfo* in { if(inited && ZipFile_Recognise(file)) { - return DFileBuilder_NewFromAbstractFile(newZipFile(file, path, info)); + return DFileBuilder::fromAbstractFile(*newZipFile(file, path, info)); } return NULL; } @@ -1818,7 +1818,7 @@ static DFile* tryOpenAsWadFile(DFile* file, const char* path, const LumpInfo* in { if(inited && WadFile_Recognise(file)) { - return DFileBuilder_NewFromAbstractFile(newWadFile(file, path, info)); + return DFileBuilder::fromAbstractFile(*newWadFile(file, path, info)); } return NULL; } @@ -1935,7 +1935,7 @@ static DFile* tryOpenFile2(const char* path, const char* mode, size_t baseOffset } // Acquire a handle on the file we intend to open. - hndl = DFileBuilder_NewFromFile(file, baseOffset); + hndl = DFileBuilder::fromFile(*file, baseOffset); // Prepare the temporary info descriptor. F_InitLumpInfo(&info); @@ -1949,7 +1949,7 @@ static DFile* tryOpenFile2(const char* path, const char* mode, size_t baseOffset // If still not loaded; this an unknown format. if(!dfile) { - dfile = DFileBuilder_NewFromAbstractFile(newUnknownFile(hndl, Str_Text(&searchPath), &info)); + dfile = DFileBuilder::fromAbstractFile(*newUnknownFile(hndl, Str_Text(&searchPath), &info)); } assert(dfile); @@ -2045,7 +2045,7 @@ DFile* F_AddFile(const char* path, size_t baseOffset, boolean allowDuplicate) if(loadingForStartup) AbstractFile_SetStartup(fsObject, true); - DFile* loadedFilesHndl = DFileBuilder_NewCopy(file); + DFile* loadedFilesHndl = DFileBuilder::dup(*file); loadedFiles->push_back(loadedFilesHndl); DFile_SetList(loadedFilesHndl, loadedFiles); // Publish lumps to one or more LumpDirectorys. @@ -2136,12 +2136,12 @@ DFile* F_OpenLump(lumpnum_t absoluteLumpNum) { AutoStr* path = F_ComposeLumpPath(container, lumpIdx); AbstractFile* fsObject = newLumpFile( - DFileBuilder_NewFromAbstractFileLump(container, lumpIdx, false), + DFileBuilder::fromAbstractFileLump(*container, lumpIdx, false), Str_Text(path), F_LumpInfo(container, lumpIdx)); if(fsObject) { - DFile* openFileHndl = DFileBuilder_NewFromAbstractFile(fsObject); + DFile* openFileHndl = DFileBuilder::fromAbstractFile(*fsObject); openFiles->push_back(openFileHndl); DFile_SetList(openFileHndl, openFiles); return openFileHndl; }