From 223f22c7730fe857cf6587e4c1ea6def6e458096 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 26 Jan 2008 19:25:04 +0000 Subject: Reworked FilesystemNode::lookupFile (fixing doxygen comment, making it possible to restrict the search depth, fixed the 'exhaustive' mode and some other tweaks) svn-id: r30644 --- common/fs.cpp | 53 ++++++++++++++++------------------------------------- common/fs.h | 45 ++++++++------------------------------------- 2 files changed, 24 insertions(+), 74 deletions(-) diff --git a/common/fs.cpp b/common/fs.cpp index f357228252..2a8000378a 100644 --- a/common/fs.cpp +++ b/common/fs.cpp @@ -171,61 +171,40 @@ bool FilesystemNode::isWritable() const { return _realNode->isWritable(); } -bool FilesystemNode::lookupFile(FSList &results, FSList &fslist, Common::String &pattern, bool hidden, bool exhaustive) const -{ - int matches = 0; - - for (FSList::iterator entry = fslist.begin(); entry != fslist.end(); ++entry) { - if (entry->isDirectory()) { - matches += lookupFileRec(results, *entry, pattern, hidden, exhaustive); - } - } - - return ((matches > 0) ? true : false); -} - -bool FilesystemNode::lookupFile(FSList &results, Common::String &pattern, bool hidden, bool exhaustive) const -{ - int matches; - +bool FilesystemNode::lookupFile(FSList &results, const Common::String &p, bool hidden, bool exhaustive, int depth) const { if (!isDirectory()) return false; - FilesystemNode dir = *this; - matches = lookupFileRec(results, dir, pattern, hidden, exhaustive); - - return ((matches > 0) ? true : false); -} - -int FilesystemNode::lookupFileRec(FSList &results, FilesystemNode &dir, Common::String &pattern, bool hidden, bool exhaustive) const -{ - FSList entries; FSList children; - int matches = 0; + FSList subdirs; + Common::String pattern = p; + pattern.toUppercase(); - dir.getChildren(entries, FilesystemNode::kListAll, hidden); - //Breadth search (entries in the same level) - for (FSList::iterator entry = entries.begin(); entry != entries.end(); ++entry) { + // First match all files on this level + getChildren(children, FilesystemNode::kListAll, hidden); + for (FSList::iterator entry = children.begin(); entry != children.end(); ++entry) { if (entry->isDirectory()) { - children.push_back(*entry); + if (depth != 0) + subdirs.push_back(*entry); } else { Common::String filename = entry->getName(); filename.toUppercase(); if (Common::matchString(filename.c_str(), pattern.c_str())) { results.push_back(*entry); - matches++; if (!exhaustive) - break; + return true; // Abort on first match if no exhaustive search was requested } } } - //Depth search (entries in lower levels) - for (FSList::iterator child = children.begin(); child != children.end(); ++child) { - matches += lookupFileRec(results, *child, pattern, hidden, exhaustive); + // Now scan all subdirs + for (FSList::iterator child = subdirs.begin(); child != subdirs.end(); ++child) { + child->lookupFile(results, pattern, hidden, exhaustive, depth - 1); + if (!exhaustive && !results.empty()) + return true; // Abort on first match if no exhaustive search was requested } - return matches; + return !results.empty(); } diff --git a/common/fs.h b/common/fs.h index b120a583db..8356f7b441 100644 --- a/common/fs.h +++ b/common/fs.h @@ -216,37 +216,24 @@ public: * @return bool true if the object can be written to, false otherwise. */ virtual bool isWritable() const; - - /** - * Searches recursively for a filename inside the given directories. - * - * For each directory in the directory list a breadth-first search is performed, - * that is, the current directory entries are scanned before going into subdirectories. - * - * @param results List to put the matches in. - * @param fslist List of directories to search within. - * @param pattern Pattern of the files to look for. - * @param hidden Whether to search hidden files or not. - * @param exhaustive Whether to continue searching after one match has been found. - * - * @return true if matches could be found, false otherwise. - */ - virtual bool lookupFile(FSList &results, FSList &fslist, Common::String &pattern, bool hidden, bool exhaustive) const; - + /** - * Searches recursively for a filename inside this directory. + * Searches recursively for files matching the specified pattern inside this directory and + * all its subdirectories. It is safe to call this method for non-directories, in this case + * it will just return false. * - * The search is performed breadth-first, that is, the current directory entries - * are scanned before going into subdirectories. + * The files in each directory are scanned first. Other than that, a depth first search + * is performed. * * @param results List to put the matches in. * @param pattern Pattern of the files to look for. * @param hidden Whether to search hidden files or not. * @param exhaustive Whether to continue searching after one match has been found. + * @param depth How many levels to search through (-1 = search all subdirs, 0 = only the current one) * * @return true if matches could be found, false otherwise. */ - virtual bool lookupFile(FSList &results, Common::String &pattern, bool hidden, bool exhaustive) const; + virtual bool lookupFile(FSList &results, const Common::String &pattern, bool hidden, bool exhaustive, int depth = -1) const; protected: /** @@ -254,22 +241,6 @@ protected: * deletes the corresponding underlying references. */ void decRefCount(); - - /** - * Searches recursively for a filename inside the given directory. - * - * The search is performed breadth-first, that is, the current directory entries - * are scanned before going into subdirectories. - * - * @param results List to put the matches in. - * @param dir Directory to search within. - * @param pattern Pattern of the files to look for. - * @param hidden Whether to search hidden files or not. - * @param exhaustive Whether to continue searching after one match has been found. - * - * @return The number of matches found. - */ - int lookupFileRec(FSList &results, FilesystemNode &dir, Common::String &pattern, bool hidden, bool exhaustive) const; }; //} // End of namespace Common -- cgit v1.2.3