[Scummvm-cvs-logs] SF.net SVN: scummvm: [27551] scummvm/branches/gsoc2007-fsnode

david_corrales at users.sourceforge.net david_corrales at users.sourceforge.net
Wed Jun 20 02:28:04 CEST 2007


Revision: 27551
          http://scummvm.svn.sourceforge.net/scummvm/?rev=27551&view=rev
Author:   david_corrales
Date:     2007-06-19 17:28:04 -0700 (Tue, 19 Jun 2007)

Log Message:
-----------
Initial implementation of the lookupFile() function. It's meant to search recursively for given
filename within a set of directories.

Modified Paths:
--------------
    scummvm/branches/gsoc2007-fsnode/backends/fs/posix/posix-fs.cpp
    scummvm/branches/gsoc2007-fsnode/common/fs.cpp
    scummvm/branches/gsoc2007-fsnode/common/fs.h

Modified: scummvm/branches/gsoc2007-fsnode/backends/fs/posix/posix-fs.cpp
===================================================================
--- scummvm/branches/gsoc2007-fsnode/backends/fs/posix/posix-fs.cpp	2007-06-20 00:02:25 UTC (rev 27550)
+++ scummvm/branches/gsoc2007-fsnode/backends/fs/posix/posix-fs.cpp	2007-06-20 00:28:04 UTC (rev 27551)
@@ -174,8 +174,13 @@
 	// loop over dir entries using readdir
 	while ((dp = readdir(dirp)) != NULL) {
 		// Skip 'invisible' files if necessary
-		if (dp->d_name[0] == '.' && !hidden)
+		if (dp->d_name[0] == '.' && !hidden) {
 			continue;
+		}
+		// Skip '.' and '..' to avoid cycles
+		if((dp->d_name[0] == '.' && dp->d_name[1] == 0) || (dp->d_name[0] == '.' && dp->d_name[1] == '.')) {
+			continue;
+		}
 
 		String newPath(_path);
 		if (newPath.lastChar() != '/')

Modified: scummvm/branches/gsoc2007-fsnode/common/fs.cpp
===================================================================
--- scummvm/branches/gsoc2007-fsnode/common/fs.cpp	2007-06-20 00:02:25 UTC (rev 27550)
+++ scummvm/branches/gsoc2007-fsnode/common/fs.cpp	2007-06-20 00:28:04 UTC (rev 27551)
@@ -27,6 +27,43 @@
 #include "backends/fs/abstract-fs.h"
 #include "backends/fs/fs-factory-maker.cpp"
 
+/*
+ * Simple DOS-style pattern matching function (understands * and ? like used in DOS).
+ * Taken from exult/files/listfiles.cc
+ */
+static bool matchString(const char *str, const char *pat) {
+	const char *p = 0;
+	const char *q = 0;
+	
+	for (;;) {
+		switch (*pat) {
+		case '*':
+			p = ++pat;
+			q = str;
+			break;
+
+		default:
+			if (*pat != *str) {
+				if (p) {
+					pat = p;
+					str = ++q;
+					if(!*str)
+						return !*pat;
+					break;
+				}
+				else
+					return false;
+			}
+			// fallthrough
+		case '?':
+			if(!*str)
+				return !*pat;
+			pat++;
+			str++;
+		}
+	}
+}
+
 FilesystemNode::FilesystemNode() {
 	_realNode = 0;
 	_refCount = 0;
@@ -166,3 +203,48 @@
 		return false;
 	return _realNode->isWritable();
 }
+
+bool FilesystemNode::lookupFile(FSList &results, FSList &fslist, Common::String &filename, bool hidden, bool exhaustive) const
+{
+	for(FSList::iterator entry = fslist.begin(); entry != fslist.end(); ++entry)
+	{
+		if(entry->isDirectory()) {
+			lookupFileRec(results, *entry, filename, hidden, exhaustive);
+		}
+	}
+	
+	//TODO: we would return true even if no matches were found, if the initial results list isn't empty
+	return ((results.size() > 0) ? true : false);
+}
+
+bool FilesystemNode::lookupFile(FSList &results, FilesystemNode &dir, Common::String &filename, bool hidden, bool exhaustive) const
+{
+	lookupFileRec(results, dir, filename, hidden, exhaustive);
+	
+	//TODO: we would return true even if no matches were found, if the initial results list isn't empty
+	return ((results.size() > 0) ? true : false);
+}
+
+void FilesystemNode::lookupFileRec(FSList &results, FilesystemNode &dir, Common::String &filename, bool hidden, bool exhaustive) const
+{
+	FSList entries;
+	dir.getChildren(entries, FilesystemNode::kListAll, hidden);
+	
+	for(FSList::iterator entry = entries.begin(); entry != entries.end(); ++entry)
+	{
+		if(entry->isDirectory()) {
+			lookupFileRec(results, *entry, filename, hidden, exhaustive);
+		} else {
+			//TODO: here we assume all backends implement the lastPathComponent method. It is currently static,
+			//		so it might be a good idea to include it inside the backend class. This would enforce its
+			//		implementation by all ports.
+			if(matchString(lastPathComponent(entry->getPath()), filename.c_str())) {
+				results.push_back(*entry);
+				
+				if(!exhaustive) {
+					break;
+				}
+			}
+		}
+	}
+}

Modified: scummvm/branches/gsoc2007-fsnode/common/fs.h
===================================================================
--- scummvm/branches/gsoc2007-fsnode/common/fs.h	2007-06-20 00:02:25 UTC (rev 27550)
+++ scummvm/branches/gsoc2007-fsnode/common/fs.h	2007-06-20 00:28:04 UTC (rev 27551)
@@ -196,6 +196,32 @@
 	 * Indicates whether this path can be written to or not.
 	 */
 	virtual bool isWritable() const;
+	
+	/**
+	 * Searches recursively for a filename inside the given directories.
+	 * 
+	 * @param results List to put the matches in.
+	 * @param fslist List of directories to search within.
+	 * @param filename Name of the file to look for.
+	 * @param hidden Whether to search hidden files or not. Default: false
+	 * @param exhaustive Whether to continue searching after one match has been found. Default: false
+	 * 
+	 * @return true if matches could be found, false otherwise.
+	 */
+	virtual bool lookupFile(FSList &results, FSList &fslist, Common::String &filename, bool hidden, bool exhaustive) const;
+	
+	/**
+	 * Searches recursively for a filename inside the given directory.
+	 * 
+	 * @param results List to put the matches in.
+	 * @param FilesystemNode Directory to search within.
+	 * @param filename Name of the file to look for.
+	 * @param hidden Whether to search hidden files or not. Default: false
+	 * @param exhaustive Whether to continue searching after one match has been found. Default: false
+	 * 
+	 * @return true if matches could be found, false otherwise.
+	 */
+	virtual bool lookupFile(FSList &results, FilesystemNode &dir, Common::String &filename, bool hidden, bool exhaustive) const;
 
 protected:
 	/**
@@ -203,6 +229,17 @@
 	 * deletes the corresponding underlying references.
 	 */
 	void decRefCount();
+	
+	/**
+	 * Searches recursively for a filename inside the given directory.
+	 * 
+	 * @param results List to put the matches in.
+	 * @param FilesystemNode Directory to search within.
+	 * @param filename Name of the file to look for.
+	 * @param hidden Whether to search hidden files or not.
+	 * @param exhaustive Whether to continue searching after one match has been found.
+	 */
+	void lookupFileRec(FSList &results, FilesystemNode &dir, Common::String &filename, 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