[Scummvm-cvs-logs] SF.net SVN: scummvm: [30644] scummvm/trunk/common

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sat Jan 26 20:25:05 CET 2008


Revision: 30644
          http://scummvm.svn.sourceforge.net/scummvm/?rev=30644&view=rev
Author:   fingolfin
Date:     2008-01-26 11:25:04 -0800 (Sat, 26 Jan 2008)

Log Message:
-----------
Reworked FilesystemNode::lookupFile (fixing doxygen comment, making it possible to restrict the search depth, fixed the 'exhaustive' mode and some other tweaks)

Modified Paths:
--------------
    scummvm/trunk/common/fs.cpp
    scummvm/trunk/common/fs.h

Modified: scummvm/trunk/common/fs.cpp
===================================================================
--- scummvm/trunk/common/fs.cpp	2008-01-26 12:51:41 UTC (rev 30643)
+++ scummvm/trunk/common/fs.cpp	2008-01-26 19:25:04 UTC (rev 30644)
@@ -171,61 +171,40 @@
 	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();
 }

Modified: scummvm/trunk/common/fs.h
===================================================================
--- scummvm/trunk/common/fs.h	2008-01-26 12:51:41 UTC (rev 30643)
+++ scummvm/trunk/common/fs.h	2008-01-26 19:25:04 UTC (rev 30644)
@@ -216,37 +216,24 @@
 	 * @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.
+	 * 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.
 	 * 
-	 * 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.
+	 * 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 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.
+	 * @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, FSList &fslist, Common::String &pattern, bool hidden, bool exhaustive) const;
-	
-	/**
-	 * Searches recursively for a filename inside this 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 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, 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 @@
 	 * 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


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list