Skip to content

Commit

Permalink
Refactor|FileSys: Removed the type() method of de::File1
Browse files Browse the repository at this point in the history
Also added a template version of FS1::findAll() which can be used
to filter the found results to include only files which match the
template argument (e.g., to collect a list of all de::Wad files).
  • Loading branch information
danij-deng committed Oct 21, 2012
1 parent fb772c5 commit d5830bb
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 87 deletions.
34 changes: 1 addition & 33 deletions doomsday/engine/portable/include/file.h
Expand Up @@ -26,28 +26,6 @@
#ifndef LIBDENG_FILESYS_FILE_H
#define LIBDENG_FILESYS_FILE_H

#ifdef __cplusplus
extern "C" {
#endif

// File types.
/// @todo Refactor away.
typedef enum {
FT_FILE, ///< Generic file
FT_ZIP,
FT_ZIPFILE,
FT_WAD,
FT_WADFILE,
FT_LUMPFILEADAPTOR,
FILETYPE_COUNT
} filetype_t;

#define VALID_FILETYPE(v) ((v) >= FT_FILE && (v) < FILETYPE_COUNT)

#ifdef __cplusplus
} // extern "C"
#endif

#ifdef __cplusplus

#include <de/Error>
Expand Down Expand Up @@ -91,25 +69,18 @@ class File1

public:
/**
* @param type File type identifier.
* @param path Path to this file in the virtual file system.
* @param hndl Handle to the file. Ownership of the handle is given to this instance.
* @param info Info descriptor for the file. A copy is made.
* @param container Container of this file. Can be @c NULL.
*/
File1(filetype_t _type, char const* _path, FileHandle& hndl, FileInfo const& _info,
File1* container = 0);
File1(char const* _path, FileHandle& hndl, FileInfo const& _info, File1* container = 0);

/**
* Release all memory acquired for objects linked with this resource.
*/
virtual ~File1();

/**
* @return Type of this resource @see filetype_t
*/
filetype_t type() const;

/// @return Absolute (i.e., resolved but possibly virtual/mapped) path to this resource.
ddstring_t const* path() const;

Expand Down Expand Up @@ -321,9 +292,6 @@ class File1
File1* container_;

private:
/// @see filetype_t
filetype_t type_;

/// Categorization flags.
Flags flags;

Expand Down
43 changes: 38 additions & 5 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -48,6 +48,13 @@

namespace de
{
namespace internal {
template <typename Type>
inline bool cannotCastFileTo(File1* file) {
return dynamic_cast<Type*>(file) == NULL;
}
}

/**
* Internally the lump index has two parts: the Primary index (which is populated
* with lumps from loaded data files) and the Auxiliary index (used to temporarily
Expand Down Expand Up @@ -240,7 +247,7 @@ namespace de
*
* @return Number of files found.
*/
int findAll(FileList& found);
int findAll(FileList& found) const;

/**
* Finds all files which meet the supplied @a predicate.
Expand All @@ -254,11 +261,37 @@ namespace de
* @return Number of files found.
*/
int findAll(bool (*predicate)(FileHandle* hndl, void* parameters), void* parameters,
FileList& found);
FileList& found) const;

/**
* Finds all files of a specific type which meet the supplied @a predicate.
* Only files that can be represented as @a Type are included in the results.
*
* @param predicate If not @c NULL, this predicate evaluator callback must
* return @c true for a given file to be included in the
* @a found FileList.
* @param parameters Passed to the predicate evaluator callback.
* @param found Set of files that match the result.
*
* @return Number of files found.
*/
template <typename Type>
int findAll(bool (*predicate)(FileHandle* hndl, void* parameters), void* parameters,
FileList& found) const
{
findAll(predicate, parameters, found);
// Filter out the wrong types.
for(QMutableListIterator<FileHandle*> i(found); i.hasNext(); )
{
if(internal::cannotCastFileTo<Type>(&i.value()->file()))
i.remove();
}
return found.count();
}

/**
* Finds all paths which match the search criteria. Will search the Zip lump index,
* lump => path mappings and native files in the local file system.
* Finds all paths which match the search criteria. Will search the Zip
* lump index, lump => path mappings and native files in the local system.
*
* @param searchPattern Pattern which defines the scope of the search.
* @param flags @ref searchPathFlags
Expand Down Expand Up @@ -423,7 +456,7 @@ void F_UnlockLump(struct file1_s* file, int lumpIdx);
/**
* Compiles a list of file names, separated by @a delimiter.
*/
void F_ComposeFileList(filetype_t type, boolean markedCustom, char* outBuf, size_t outBufSize, const char* delimiter);
void F_ComposePWADFileList(char* outBuf, size_t outBufSize, const char* delimiter);

uint F_CRCNumber(void);

Expand Down
12 changes: 2 additions & 10 deletions doomsday/engine/portable/src/file.cpp
Expand Up @@ -30,16 +30,13 @@

namespace de {

File1::File1(filetype_t _type, char const* _path, FileHandle& hndl,
FileInfo const& _info, File1* _container)
: handle_(&hndl), info_(_info), container_(_container),
type_(_type), flags(DefaultFlags)
File1::File1(char const* _path, FileHandle& hndl, FileInfo const& _info, File1* _container)
: handle_(&hndl), info_(_info), container_(_container), flags(DefaultFlags)
{
// Used to favor newer files when duplicates are pruned.
/// @todo Does not belong at this level. Load order should be determined
/// at file system level. -ds
static uint fileCounter = 0;
DENG2_ASSERT(VALID_FILETYPE(_type));
order = fileCounter++;

Str_Init(&path_); Str_Set(&path_, _path);
Expand All @@ -52,11 +49,6 @@ File1::~File1()
if(handle_) delete handle_;
}

filetype_t File1::type() const
{
return type_;
}

FileInfo const& File1::info() const
{
return info_;
Expand Down
43 changes: 10 additions & 33 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -920,7 +920,7 @@ uint FS1::loadedFilesCRC()
return iwad->calculateCRC();
}

int FS1::findAll(FS1::FileList& found)
int FS1::findAll(FS1::FileList& found) const
{
int numFound = 0;
DENG2_FOR_EACH(i, d->loadedFiles, FS1::FileList::const_iterator)
Expand All @@ -932,7 +932,7 @@ int FS1::findAll(FS1::FileList& found)
}

int FS1::findAll(bool (*predicate)(de::FileHandle* hndl, void* parameters), void* parameters,
FS1::FileList& found)
FS1::FileList& found) const
{
int numFound = 0;
DENG2_FOR_EACH(i, d->loadedFiles, FS1::FileList::const_iterator)
Expand Down Expand Up @@ -1110,7 +1110,7 @@ de::File1& FS1::interpret(de::FileHandle& hndl, char const* path, FileInfo const
}
else
{
interpretedFile = new File1(FT_FILE, path, hndl, info);
interpretedFile = new File1(path, hndl, info);
}
}

Expand Down Expand Up @@ -1467,26 +1467,12 @@ D_CMD(ListFiles)
DENG2_FOR_EACH(i, foundFiles, FS1::FileList::const_iterator)
{
de::File1& file = (*i)->file();
uint crc;
uint crc = 0;

int fileCount = file.lumpCount();
switch(file.type())
if(de::Wad* wad = dynamic_cast<de::Wad*>(&file))
{
case FT_FILE:
crc = 0;
break;
case FT_ZIP:
crc = 0;
break;
case FT_WAD:
crc = (!file.hasCustom()? reinterpret_cast<Wad&>(file).calculateCRC() : 0);
break;
case FT_LUMPFILEADAPTOR:
crc = 0;
break;
default:
Con_Error("CCmdListLumps: Invalid file type %i.", file.type());
exit(1); // Unreachable.
crc = (!file.hasCustom()? wad->calculateCRC() : 0);
}

Con_Printf("\"%s\" (%i %s%s)", F_PrettyPath(Str_Text(file.path())),
Expand Down Expand Up @@ -1985,18 +1971,10 @@ static ddstring_t* composeFilePathString(FS1::FileList& files, int flags = DEFAU
return str;
}

typedef struct {
filetype_t type; // Only
bool markedCustom;
} findfilespredicate_params_t;

static bool findFilesPredicate(de::FileHandle* hndl, void* parameters)
static bool findCustomFilesPredicate(de::FileHandle* hndl, void* /*parameters*/)
{
DENG_ASSERT(parameters);
findfilespredicate_params_t* p = (findfilespredicate_params_t*)parameters;
de::File1& file = hndl->file();
if((!VALID_FILETYPE(p->type) || p->type == file.type()) &&
((p->markedCustom == file.hasCustom())))
if(file.hasCustom())
{
ddstring_t const* path = file.path();
if(stricmp(Str_Text(path) + Str_Length(path) - 3, "lmp"))
Expand All @@ -2008,14 +1986,13 @@ static bool findFilesPredicate(de::FileHandle* hndl, void* parameters)
/**
* Compiles a list of file names, separated by @a delimiter.
*/
void F_ComposeFileList(filetype_t type, boolean markedCustom, char* outBuf, size_t outBufSize, const char* delimiter)
void F_ComposePWADFileList(char* outBuf, size_t outBufSize, const char* delimiter)
{
if(!outBuf || 0 == outBufSize) return;
memset(outBuf, 0, outBufSize);

findfilespredicate_params_t p = { type, CPP_BOOL(markedCustom) };
FS1::FileList foundFiles;
if(!App_FileSystem()->findAll(findFilesPredicate, (void*)&p, foundFiles)) return;
if(!App_FileSystem()->findAll<de::Wad>(findCustomFilesPredicate, 0/*no params*/, foundFiles)) return;

ddstring_t* str = composeFilePathString(foundFiles, PTSF_TRANSFORM_EXCLUDE_DIR, delimiter);
strncpy(outBuf, Str_Text(str), outBufSize);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/lumpfileadaptor.cpp
Expand Up @@ -27,7 +27,7 @@ namespace de {

LumpFileAdaptor::LumpFileAdaptor(FileHandle& hndl, char const* path,
FileInfo const& info, File1* container)
: File1(FT_LUMPFILEADAPTOR, path, hndl, info, container)
: File1(path, hndl, info, container)
{}

LumpFileAdaptor::~LumpFileAdaptor()
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/sv_main.c
Expand Up @@ -116,7 +116,7 @@ void Sv_GetInfo(serverinfo_t* info)
}

// Some WAD names.
F_ComposeFileList(FT_WAD, true/*only "custom" files*/, info->pwads, sizeof info->pwads, ";");
F_ComposePWADFileList(info->pwads, sizeof(info->pwads), ";");

// This should be a CRC number that describes all the loaded data.
info->wadNumber = F_CRCNumber();
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/wad.cpp
Expand Up @@ -59,7 +59,7 @@ class WadFile : public File1
{
public:
WadFile::WadFile(FileHandle& hndl, char const* path, FileInfo const& info, File1* container)
: File1(FT_WADFILE, path, hndl, info, container), crc_(0)
: File1(path, hndl, info, container), crc_(0)
{}

uint crc() const { return crc_; }
Expand Down Expand Up @@ -289,7 +289,7 @@ struct Wad::Instance
};

Wad::Wad(FileHandle& hndl, char const* path, FileInfo const& info, File1* container)
: File1(FT_WAD, path, hndl, info, container)
: File1(path, hndl, info, container)
{
d = new Instance(this, hndl, path);
}
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/zip.cpp
Expand Up @@ -142,7 +142,7 @@ class ZipFile : public File1
{
public:
ZipFile::ZipFile(FileHandle& hndl, char const* path, FileInfo const& info, File1* container)
: File1(FT_ZIPFILE, path, hndl, info, container)
: File1(path, hndl, info, container)
{}
};

Expand Down Expand Up @@ -458,7 +458,7 @@ struct Zip::Instance
};

Zip::Zip(FileHandle& hndl, char const* path, FileInfo const& info, File1* container)
: File1(FT_ZIP, path, hndl, info, container)
: File1(path, hndl, info, container)
{
d = new Instance(this);
}
Expand Down

0 comments on commit d5830bb

Please sign in to comment.