Skip to content

Commit

Permalink
Refactor: Began work on turning DFile into a C++ class
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Oct 1, 2012
1 parent 5bf7f32 commit fb7256b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 62 deletions.
69 changes: 41 additions & 28 deletions doomsday/engine/portable/include/dfilebuilder.h
Expand Up @@ -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.
Expand Down
36 changes: 16 additions & 20 deletions doomsday/engine/portable/src/dfile.cpp
Expand Up @@ -76,18 +76,18 @@ static void errorIfNotValid(DFile const* file, const char* callerName)
exit(1); // Unreachable.
}

void DFileBuilder_Init(void)
void DFileBuilder::init(void)
{
if(!inited)
{
mutex = Sys_CreateMutex(0);
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)
{
Expand All @@ -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;

Expand All @@ -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;
}

Expand Down
28 changes: 14 additions & 14 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -516,7 +516,7 @@ void F_Shutdown(void)
clearFileIds(); // Should be null-op if bookkeeping is correct.
clearLumpDirectorys();

DFileBuilder_Shutdown();
DFileBuilder::shutdown();

inited = false;
}
Expand Down Expand Up @@ -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))
{
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit fb7256b

Please sign in to comment.