From d5830bbbc24b9308e0cbd0c9c3207c6b62846c8f Mon Sep 17 00:00:00 2001 From: danij Date: Sun, 21 Oct 2012 13:54:34 +0100 Subject: [PATCH] Refactor|FileSys: Removed the type() method of de::File1 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). --- doomsday/engine/portable/include/file.h | 34 +-------------- doomsday/engine/portable/include/fs_main.h | 43 ++++++++++++++++--- doomsday/engine/portable/src/file.cpp | 12 +----- doomsday/engine/portable/src/fs_main.cpp | 43 +++++-------------- .../engine/portable/src/lumpfileadaptor.cpp | 2 +- doomsday/engine/portable/src/sv_main.c | 2 +- doomsday/engine/portable/src/wad.cpp | 4 +- doomsday/engine/portable/src/zip.cpp | 4 +- 8 files changed, 57 insertions(+), 87 deletions(-) diff --git a/doomsday/engine/portable/include/file.h b/doomsday/engine/portable/include/file.h index 6f8d6fec98..b67a5d382c 100644 --- a/doomsday/engine/portable/include/file.h +++ b/doomsday/engine/portable/include/file.h @@ -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 @@ -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; @@ -321,9 +292,6 @@ class File1 File1* container_; private: - /// @see filetype_t - filetype_t type_; - /// Categorization flags. Flags flags; diff --git a/doomsday/engine/portable/include/fs_main.h b/doomsday/engine/portable/include/fs_main.h index 0c2a803076..6f52fb40c2 100644 --- a/doomsday/engine/portable/include/fs_main.h +++ b/doomsday/engine/portable/include/fs_main.h @@ -48,6 +48,13 @@ namespace de { + namespace internal { + template + inline bool cannotCastFileTo(File1* file) { + return dynamic_cast(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 @@ -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. @@ -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 + int findAll(bool (*predicate)(FileHandle* hndl, void* parameters), void* parameters, + FileList& found) const + { + findAll(predicate, parameters, found); + // Filter out the wrong types. + for(QMutableListIterator i(found); i.hasNext(); ) + { + if(internal::cannotCastFileTo(&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 @@ -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); diff --git a/doomsday/engine/portable/src/file.cpp b/doomsday/engine/portable/src/file.cpp index 468370d03b..64c3e672c3 100644 --- a/doomsday/engine/portable/src/file.cpp +++ b/doomsday/engine/portable/src/file.cpp @@ -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); @@ -52,11 +49,6 @@ File1::~File1() if(handle_) delete handle_; } -filetype_t File1::type() const -{ - return type_; -} - FileInfo const& File1::info() const { return info_; diff --git a/doomsday/engine/portable/src/fs_main.cpp b/doomsday/engine/portable/src/fs_main.cpp index f4ddf29c07..7486b44224 100644 --- a/doomsday/engine/portable/src/fs_main.cpp +++ b/doomsday/engine/portable/src/fs_main.cpp @@ -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) @@ -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) @@ -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); } } @@ -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(&file)) { - case FT_FILE: - crc = 0; - break; - case FT_ZIP: - crc = 0; - break; - case FT_WAD: - crc = (!file.hasCustom()? reinterpret_cast(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())), @@ -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")) @@ -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(findCustomFilesPredicate, 0/*no params*/, foundFiles)) return; ddstring_t* str = composeFilePathString(foundFiles, PTSF_TRANSFORM_EXCLUDE_DIR, delimiter); strncpy(outBuf, Str_Text(str), outBufSize); diff --git a/doomsday/engine/portable/src/lumpfileadaptor.cpp b/doomsday/engine/portable/src/lumpfileadaptor.cpp index 42fc3cadcc..78cf3e0026 100644 --- a/doomsday/engine/portable/src/lumpfileadaptor.cpp +++ b/doomsday/engine/portable/src/lumpfileadaptor.cpp @@ -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() diff --git a/doomsday/engine/portable/src/sv_main.c b/doomsday/engine/portable/src/sv_main.c index b20a146627..f6a2e5717c 100644 --- a/doomsday/engine/portable/src/sv_main.c +++ b/doomsday/engine/portable/src/sv_main.c @@ -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(); diff --git a/doomsday/engine/portable/src/wad.cpp b/doomsday/engine/portable/src/wad.cpp index d1a7f82632..c31ae9ae7b 100644 --- a/doomsday/engine/portable/src/wad.cpp +++ b/doomsday/engine/portable/src/wad.cpp @@ -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_; } @@ -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); } diff --git a/doomsday/engine/portable/src/zip.cpp b/doomsday/engine/portable/src/zip.cpp index b021159438..8e3fd1cfaf 100644 --- a/doomsday/engine/portable/src/zip.cpp +++ b/doomsday/engine/portable/src/zip.cpp @@ -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) {} }; @@ -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); }