Skip to content

Commit

Permalink
Refactor|FileSys: Use references rather than pointers where appropriate
Browse files Browse the repository at this point in the history
Plus minor cleanup.
  • Loading branch information
danij-deng committed Oct 12, 2012
1 parent b5c84e0 commit 8ade630
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 150 deletions.
4 changes: 2 additions & 2 deletions doomsday/engine/api/dfile.h
Expand Up @@ -72,8 +72,8 @@ class DFile
DFile& setList(struct filelist_s* list);

/// @todo Should not be visible outside the engine.
AbstractFile* file();
AbstractFile* file() const;
AbstractFile& file();
AbstractFile& file() const;

/// @return @c true iff this handle's internal state is valid.
bool isValid() const;
Expand Down
117 changes: 63 additions & 54 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -88,7 +88,10 @@ class FS
*/
void mapPath(char const* source, char const* destination);

/// @note Should be called after WADs have been processed.
/**
* (Re-)Initialize the path => lump mappings.
* @note Should be called after WADs have been processed.
*/
void initLumpPathMap();

/**
Expand All @@ -112,9 +115,6 @@ class FS
*/
bool checkFileId(char const* path);

/// @return Number of files in the currently active primary LumpIndex.
int lumpCount();

/**
* @return @c true if a file exists at @a path which can be opened for reading.
*/
Expand Down Expand Up @@ -152,36 +152,6 @@ class FS

int removeFiles(char const* const* paths, int num, bool permitRequired = false);

/**
* Opens the given file (will be translated) for reading.
*
* @post If @a allowDuplicate = @c false a new file ID for this will have been
* added to the list of known file identifiers if this file hasn't yet been
* opened. It is the responsibility of the caller to release this identifier when done.
*
* @param path Possibly relative or mapped path to the resource being opened.
* @param mode 't' = text mode (with real files, lumps are always binary)
* 'b' = binary
* 'f' = must be a real file in the local file system
* @param baseOffset Offset from the start of the file in bytes to begin.
* @param allowDuplicate @c false = open only if not already opened.
*
* @return Opened file reference/handle else @c NULL.
*/
DFile* openFile(char const* path, char const* mode, size_t baseOffset = 0,
bool allowDuplicate = true);

/**
* Try to locate the specified lump for reading.
*
* @param absoluteLumpNum Logical lumpnum associated to the file being looked up.
*
* @return Handle to the opened file if found.
*/
DFile* openLump(lumpnum_t absoluteLumpNum);

bool isValidLumpNum(lumpnum_t absoluteLumpNum);

/**
* Find a lump in the Zip LumpIndex.
*
Expand All @@ -191,6 +161,11 @@ class FS
*/
AbstractFile* findLumpFile(char const* path, int* lumpIdx = 0);

/// @return Number of lumps in the currently active LumpIndex.
int lumpCount();

bool isValidLumpNum(lumpnum_t absoluteLumpNum);

lumpnum_t lumpNumForName(char const* name, bool silent = true);

char const* lumpName(lumpnum_t absoluteLumpNum);
Expand Down Expand Up @@ -244,14 +219,42 @@ class FS
return 0;
}

/**
* Opens the given file (will be translated) for reading.
*
* @post If @a allowDuplicate = @c false a new file ID for this will have been
* added to the list of known file identifiers if this file hasn't yet been
* opened. It is the responsibility of the caller to release this identifier when done.
*
* @param path Possibly relative or mapped path to the resource being opened.
* @param mode 't' = text mode (with real files, lumps are always binary)
* 'b' = binary
* 'f' = must be a real file in the local file system
* @param baseOffset Offset from the start of the file in bytes to begin.
* @param allowDuplicate @c false = open only if not already opened.
*
* @return Opened file reference/handle else @c NULL.
*/
DFile* openFile(char const* path, char const* mode, size_t baseOffset = 0,
bool allowDuplicate = true);

/**
* Try to locate the specified lump for reading.
*
* @param absoluteLumpNum Logical lumpnum associated to the file being looked up.
*
* @return Handle to the opened file if found.
*/
DFile* openLump(lumpnum_t absoluteLumpNum);

/// Clear all references to this file.
void releaseFile(AbstractFile* file);
void releaseFile(AbstractFile& file);

/// Close this file handle.
void closeFile(DFile* hndl);
void closeFile(DFile& hndl);

/// Completely destroy this file; close if open, clear references and any acquired identifiers.
void deleteFile(DFile* hndl);
void deleteFile(DFile& hndl);

/**
* Parm is passed on to the callback, which is called for each file
Expand All @@ -262,20 +265,6 @@ class FS
*/
int allResourcePaths(char const* searchPath, int flags, int (*callback) (char const* path, PathDirectoryNodeType type, void* parameters), void* parameters = 0);

/**
* Calculate a CRC for the loaded file list.
*/
uint loadedFilesCRC();

/**
* Try to open the specified WAD archive into the auxiliary lump cache.
*
* @return Base index for lumps in this archive.
*/
lumpnum_t openAuxiliary(char const* fileName, size_t baseOffset = 0);

void closeAuxiliary();

/**
* Finds all files.
*
Expand Down Expand Up @@ -309,16 +298,36 @@ class FS
*/
void printIndex();

/**
* Calculate a CRC for the loaded file list.
*/
uint loadedFilesCRC();

/**
* Try to open the specified WAD archive into the auxiliary lump index.
*
* @return Base index for lumps in this archive.
*/
lumpnum_t openAuxiliary(char const* fileName, size_t baseOffset = 0);

/**
* Close the auxiliary lump index if open.
*/
void closeAuxiliary();

private:
struct Instance;
Instance* d;

void unlinkFile(AbstractFile* file);
/**
* Removes a file from any lump indexes.
*
* @param file File to remove from the index.
*/
void deindex(AbstractFile& file);

bool unloadFile(char const* path, bool permitRequired = false, bool quiet = false);

int unloadListFiles(FileList& list, bool nonStartup);

FILE* findRealFile(char const* path, char const* mymode, ddstring_t** foundPath);

/**
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/abstractfile.cpp
Expand Up @@ -48,7 +48,7 @@ AbstractFile::AbstractFile(filetype_t _type, char const* _path, DFile& file, Lum

AbstractFile::~AbstractFile()
{
App_FileSystem()->releaseFile(this);
App_FileSystem()->releaseFile(*this);
Str_Free(&path_);
if(file) delete file;
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/dd_games.cpp
Expand Up @@ -200,7 +200,7 @@ static bool recognizeZIP(char const* filePath, void* parameters)
{
result = de::ZipFile::recognise(*dfile);
/// @todo Check files. We should implement an auxiliary zip lump index...
App_FileSystem()->closeFile(dfile);
App_FileSystem()->closeFile(*dfile);
}
return result;
}
Expand Down
14 changes: 7 additions & 7 deletions doomsday/engine/portable/src/dfile.cpp
Expand Up @@ -172,7 +172,7 @@ DFile* DFileBuilder::dup(de::DFile const& hndl)
de::DFile* clone = new de::DFile();
clone->d->flags.open = true;
clone->d->flags.reference = true;
clone->d->file = hndl.file();
clone->d->file = &hndl.file();
return clone;
}

Expand Down Expand Up @@ -254,16 +254,16 @@ DFile& DFile::setList(struct filelist_s* list)
return *this;
}

AbstractFile* DFile::file()
AbstractFile& DFile::file()
{
errorIfNotValid(*this, "DFile::file");
return d->file;
return *d->file;
}

AbstractFile* DFile::file() const
AbstractFile& DFile::file() const
{
errorIfNotValid(*this, "DFile::file const");
return d->file;
return *d->file;
}

size_t DFile::baseOffset() const
Expand Down Expand Up @@ -480,13 +480,13 @@ void DFile_Rewind(struct dfile_s* hndl)
struct abstractfile_s* DFile_File(struct dfile_s* hndl)
{
SELF(hndl);
return reinterpret_cast<struct abstractfile_s*>(self->file());
return reinterpret_cast<struct abstractfile_s*>(&self->file());
}

struct abstractfile_s* DFile_File_const(struct dfile_s const* hndl)
{
SELF_CONST(hndl);
return reinterpret_cast<struct abstractfile_s*>(self->file());
return reinterpret_cast<struct abstractfile_s*>(&self->file());
}

struct filelist_s* DFile_List(struct dfile_s* hndl)
Expand Down

0 comments on commit 8ade630

Please sign in to comment.