[Scummvm-cvs-logs] SF.net SVN: scummvm:[34307] scummvm/trunk/backends/fs/posix

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Wed Sep 3 16:55:25 CEST 2008


Revision: 34307
          http://scummvm.svn.sourceforge.net/scummvm/?rev=34307&view=rev
Author:   fingolfin
Date:     2008-09-03 14:55:19 +0000 (Wed, 03 Sep 2008)

Log Message:
-----------
POSIX FSNode: got rid of Double-slashes in paths for childs of the root; simplified code

Modified Paths:
--------------
    scummvm/trunk/backends/fs/posix/posix-fs-factory.cpp
    scummvm/trunk/backends/fs/posix/posix-fs.cpp
    scummvm/trunk/backends/fs/posix/posix-fs.h

Modified: scummvm/trunk/backends/fs/posix/posix-fs-factory.cpp
===================================================================
--- scummvm/trunk/backends/fs/posix/posix-fs-factory.cpp	2008-09-03 14:06:54 UTC (rev 34306)
+++ scummvm/trunk/backends/fs/posix/posix-fs-factory.cpp	2008-09-03 14:55:19 UTC (rev 34307)
@@ -27,17 +27,17 @@
 #include "backends/fs/posix/posix-fs.cpp"
 
 AbstractFilesystemNode *POSIXFilesystemFactory::makeRootFileNode() const {
-	return new POSIXFilesystemNode();
+	return new POSIXFilesystemNode("/");
 }
 
 AbstractFilesystemNode *POSIXFilesystemFactory::makeCurrentDirectoryFileNode() const {
 	char buf[MAXPATHLEN];
 	getcwd(buf, MAXPATHLEN);
-	return new POSIXFilesystemNode(buf, true);
+	return new POSIXFilesystemNode(buf);
 }
 
 AbstractFilesystemNode *POSIXFilesystemFactory::makeFileNodePath(const Common::String &path) const {
 	assert(!path.empty());
-	return new POSIXFilesystemNode(path, true);
+	return new POSIXFilesystemNode(path);
 }
 #endif

Modified: scummvm/trunk/backends/fs/posix/posix-fs.cpp
===================================================================
--- scummvm/trunk/backends/fs/posix/posix-fs.cpp	2008-09-03 14:06:54 UTC (rev 34306)
+++ scummvm/trunk/backends/fs/posix/posix-fs.cpp	2008-09-03 14:55:19 UTC (rev 34307)
@@ -46,14 +46,7 @@
 	_isDirectory = _isValid ? S_ISDIR(st.st_mode) : false;
 }
 
-POSIXFilesystemNode::POSIXFilesystemNode() {
-	// The root dir.
-	_displayName = _path = "/";
-	_isValid = true;
-	_isDirectory = true;
-}
-
-POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p, bool verify) {
+POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p) {
 	assert(p.size() > 0);
 
 	// Expand "~/" to the value of the HOME env variable
@@ -73,7 +66,7 @@
 	_path = Common::normalizePath(_path, '/');
 	_displayName = Common::lastPathComponent(_path, '/');
 
-	// TODO: should we turn relative paths into absolut ones?
+	// TODO: should we turn relative paths into absolute ones?
 	// Pro: Ensures the "getParent" works correctly even for relative dirs.
 	// Contra: The user may wish to use (and keep!) relative paths in his
 	//   config file, and converting relative to absolute paths may hurt him...
@@ -91,24 +84,24 @@
 	// TODO: Should we enforce that the path is absolute at this point?
 	//assert(_path.hasPrefix("/"));
 
-	if (verify) {
-		setFlags();
-	}
+	setFlags();
 }
 
 AbstractFilesystemNode *POSIXFilesystemNode::getChild(const Common::String &n) const {
+	assert(!_path.empty());
 	assert(_isDirectory);
 	
 	// Make sure the string contains no slashes
 	assert(Common::find(n.begin(), n.end(), '/') == n.end());
 
 	// We assume here that _path is already normalized (hence don't bother to call
-	//  Common::normalizePath on the final path
+	//  Common::normalizePath on the final path).
 	Common::String newPath(_path);
-	newPath += '/';
+	if (_path != "/")
+		newPath += '/';
 	newPath += n;
 
-	return new POSIXFilesystemNode(newPath, true);
+	return makeNode(newPath);
 }
 
 bool POSIXFilesystemNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const {
@@ -164,12 +157,13 @@
 			continue;
 		}
 
-		Common::String newPath(_path);
-		newPath += '/';
-		newPath += dp->d_name;
+		// Start with a clone of this node, with the correct path set
+		POSIXFilesystemNode entry(*this);
+		entry._displayName = dp->d_name;
+		if (_path != "/")
+			entry._path += '/';
+		entry._path += entry._displayName;
 
-		POSIXFilesystemNode entry(newPath, false);
-
 #if defined(SYSTEM_NOT_SUPPORTING_D_TYPE)
 		/* TODO: d_type is not part of POSIX, so it might not be supported
 		 * on some of our targets. For those systems where it isn't supported,
@@ -223,13 +217,18 @@
 	
 	// Strip of the last component. We make use of the fact that at this
 	// point, _path is guaranteed to be normalized
-	while (end > start && *end != '/')
+	while (end > start && *(end-1) != '/')
 		end--;
 
-	if (end == start)
-		return new POSIXFilesystemNode();
-	else
-		return new POSIXFilesystemNode(Common::String(start, end), true);
+	if (end == start) {
+		// This only happens if we were called with a relative path, for which
+		// there simply is no parent.
+		// TODO: We could also resolve this by assuming that the parent is the
+		//       current working directory, and returning a node referring to that.
+		return 0;
+	}
+
+	return makeNode(Common::String(start, end));
 }
 
 Common::SeekableReadStream *POSIXFilesystemNode::openForReading() {

Modified: scummvm/trunk/backends/fs/posix/posix-fs.h
===================================================================
--- scummvm/trunk/backends/fs/posix/posix-fs.h	2008-09-03 14:06:54 UTC (rev 34306)
+++ scummvm/trunk/backends/fs/posix/posix-fs.h	2008-09-03 14:55:19 UTC (rev 34307)
@@ -43,20 +43,18 @@
 	Common::String _path;
 	bool _isDirectory;
 	bool _isValid;
+	
+	virtual AbstractFilesystemNode *makeNode(const Common::String &path) const {
+		return new POSIXFilesystemNode(path);
+	}
 
 public:
 	/**
-	 * Creates a POSIXFilesystemNode with the root node as path.
-	 */
-	POSIXFilesystemNode();
-
-	/**
 	 * Creates a POSIXFilesystemNode for a given path.
 	 *
-	 * @param path String with the path the new node should point to.
-	 * @param verify true if the isValid and isDirectory flags should be verified during the construction.
+	 * @param path the path the new node should point to.
 	 */
-	POSIXFilesystemNode(const Common::String &path, bool verify);
+	POSIXFilesystemNode(const Common::String &path);
 
 	virtual bool exists() const { return access(_path.c_str(), F_OK) == 0; }
 	virtual Common::String getDisplayName() const { return _displayName; }


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