aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Horn2008-08-27 20:31:22 +0000
committerMax Horn2008-08-27 20:31:22 +0000
commitcb21c25e417bb546f6678ab9da7440c6e1b4b2fd (patch)
tree0ffb1123245e8ee73d4a4c4db8b166e395bac6d0
parent79fafb7b6ae8b23aaa407b4bca56170d2d46b6ac (diff)
downloadscummvm-rg350-cb21c25e417bb546f6678ab9da7440c6e1b4b2fd.tar.gz
scummvm-rg350-cb21c25e417bb546f6678ab9da7440c6e1b4b2fd.tar.bz2
scummvm-rg350-cb21c25e417bb546f6678ab9da7440c6e1b4b2fd.zip
FSNode code: Merged most versions of lastPathComponent() into one new AbstractFilesystemNode::lastPathComponent() method, with customizable path separator character
svn-id: r34197
-rw-r--r--backends/fs/abstract-fs.cpp39
-rw-r--r--backends/fs/abstract-fs.h15
-rw-r--r--backends/fs/ds/ds-fs.cpp22
-rw-r--r--backends/fs/palmos/palmos-fs.cpp30
-rw-r--r--backends/fs/posix/posix-fs.cpp29
-rw-r--r--backends/fs/ps2/ps2-fs.cpp22
-rw-r--r--backends/fs/psp/psp-fs.cpp28
-rw-r--r--backends/fs/symbian/symbian-fs.cpp30
-rw-r--r--backends/fs/wii/wii-fs.cpp28
-rw-r--r--backends/fs/windows/windows-fs.cpp30
-rw-r--r--backends/module.mk1
-rw-r--r--backends/platform/dc/dc-fs.cpp29
12 files changed, 72 insertions, 231 deletions
diff --git a/backends/fs/abstract-fs.cpp b/backends/fs/abstract-fs.cpp
new file mode 100644
index 0000000000..f6dc8f1891
--- /dev/null
+++ b/backends/fs/abstract-fs.cpp
@@ -0,0 +1,39 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ */
+
+#include "backends/fs/abstract-fs.h"
+
+const char *AbstractFilesystemNode::lastPathComponent(const Common::String &str, const char sep) {
+ if(str.empty())
+ return "";
+
+ const char *start = str.c_str();
+ const char *cur = start + str.size() - 2;
+
+ while (cur >= start && *cur != sep) {
+ --cur;
+ }
+
+ return cur + 1;
+}
diff --git a/backends/fs/abstract-fs.h b/backends/fs/abstract-fs.h
index 97de40a2fc..d81e7f36b0 100644
--- a/backends/fs/abstract-fs.h
+++ b/backends/fs/abstract-fs.h
@@ -72,6 +72,19 @@ protected:
*/
virtual AbstractFilesystemNode *getParent() const = 0;
+ /**
+ * Returns the last component of a given path.
+ *
+ * Examples:
+ * /foo/bar.txt would return /bar.txt
+ * /foo/bar/ would return /bar/
+ *
+ * @param str String containing the path.
+ * @param sep character used to separate path components
+ * @return Pointer to the first char of the last component inside str.
+ */
+ static const char *lastPathComponent(const Common::String &str, const char sep);
+
public:
/**
* Destructor.
@@ -154,4 +167,6 @@ public:
*/
};
+
+
#endif //BACKENDS_ABSTRACT_FS_H
diff --git a/backends/fs/ds/ds-fs.cpp b/backends/fs/ds/ds-fs.cpp
index cffe4c118d..d206941ab9 100644
--- a/backends/fs/ds/ds-fs.cpp
+++ b/backends/fs/ds/ds-fs.cpp
@@ -798,25 +798,3 @@ int std_ferror(FILE* handle) {
}
} // namespace DS
-
-/**
- * Returns the last component of a given path.
- *
- * Examples:
- * /foo/bar.txt would return /bar.txt
- * /foo/bar/ would return /bar/
- *
- * @param str String containing the path.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *lastPathComponent(const Common::String &str) {
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '/' && *cur != '\\') {
- --cur;
- }
-
- return cur + 1;
-}
-
diff --git a/backends/fs/palmos/palmos-fs.cpp b/backends/fs/palmos/palmos-fs.cpp
index 5edb6c2d26..69d9f350f5 100644
--- a/backends/fs/palmos/palmos-fs.cpp
+++ b/backends/fs/palmos/palmos-fs.cpp
@@ -80,30 +80,6 @@ private:
static void addFile(AbstractFSList &list, ListMode mode, const Char *base, FileInfoType* find_data);
};
-/**
- * Returns the last component of a given path.
- *
- * Examples:
- * /foo/bar.txt would return /bar.txt
- * /foo/bar/ would return /bar/
- *
- * @param str String containing the path.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *lastPathComponent(const Common::String &str) {
- if(str.empty())
- return "";
-
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '/') {
- --cur;
- }
-
- return cur + 1;
-}
-
void PalmOSFilesystemNode::addFile(AbstractFSList &list, ListMode mode, const char *base, FileInfoType* find_data) {
PalmOSFilesystemNode entry;
bool isDir;
@@ -138,7 +114,7 @@ PalmOSFilesystemNode::PalmOSFilesystemNode() {
PalmOSFilesystemNode::PalmOSFilesystemNode(const String &p) {
_path = p;
- _displayName = lastPathComponent(_path);
+ _displayName = lastPathComponent(_path, '/');
UInt32 attr;
FileRef handle;
@@ -215,13 +191,13 @@ AbstractFilesystemNode *PalmOSFilesystemNode::getParent() const {
if (!_isPseudoRoot) {
const char *start = _path.c_str();
- const char *end = lastPathComponent(_path);
+ const char *end = lastPathComponent(_path, '/');
p = new PalmOSFilesystemNode();
p->_path = String(start, end - start);
p->_isValid = true;
p->_isDirectory = true;
- p->_displayName = lastPathComponent(p->_path);
+ p->_displayName = lastPathComponent(p->_path, '/');
p->_isPseudoRoot =(p->_path == "/");
}
diff --git a/backends/fs/posix/posix-fs.cpp b/backends/fs/posix/posix-fs.cpp
index efc75bc187..5cd6a909d6 100644
--- a/backends/fs/posix/posix-fs.cpp
+++ b/backends/fs/posix/posix-fs.cpp
@@ -37,31 +37,6 @@
#endif
-
-/**
- * Returns the last component of a given path.
- *
- * Examples:
- * /foo/bar.txt would return /bar.txt
- * /foo/bar/ would return /bar/
- *
- * @param str String containing the path.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *lastPathComponent(const Common::String &str) {
- if(str.empty())
- return "";
-
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '/') {
- --cur;
- }
-
- return cur + 1;
-}
-
void POSIXFilesystemNode::setFlags() {
struct stat st;
@@ -93,7 +68,7 @@ POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p, bool verify) {
_path = p;
}
- _displayName = lastPathComponent(_path);
+ _displayName = lastPathComponent(_path, '/');
if (verify) {
setFlags();
@@ -224,7 +199,7 @@ AbstractFilesystemNode *POSIXFilesystemNode::getParent() const {
return 0;
const char *start = _path.c_str();
- const char *end = lastPathComponent(_path);
+ const char *end = lastPathComponent(_path, '/');
#ifdef __OS2__
if (end == start)
diff --git a/backends/fs/ps2/ps2-fs.cpp b/backends/fs/ps2/ps2-fs.cpp
index 782e97b959..b0f1ddbafb 100644
--- a/backends/fs/ps2/ps2-fs.cpp
+++ b/backends/fs/ps2/ps2-fs.cpp
@@ -100,28 +100,6 @@ public:
virtual AbstractFilesystemNode *getParent() const;
};
-/**
- * Returns the last component of a given path.
- *
- * @param str String containing the path.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *lastPathComponent(const Common::String &str) {
- if (str.empty())
- return "";
-
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '/' && *cur != ':') {
- --cur;
- }
-
- printf("romeo : lastPathComponent = %s\n", cur + 1);
-
- return cur + 1;
-}
-
Ps2FilesystemNode::Ps2FilesystemNode() {
_isDirectory = true;
_isRoot = true;
diff --git a/backends/fs/psp/psp-fs.cpp b/backends/fs/psp/psp-fs.cpp
index 3fe6060928..aa3e253782 100644
--- a/backends/fs/psp/psp-fs.cpp
+++ b/backends/fs/psp/psp-fs.cpp
@@ -71,30 +71,6 @@ public:
virtual AbstractFilesystemNode *getParent() const;
};
-/**
- * Returns the last component of a given path.
- *
- * Examples:
- * /foo/bar.txt would return /bar.txt
- * /foo/bar/ would return /bar/
- *
- * @param str String containing the path.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *lastPathComponent(const Common::String &str) {
- if(str.empty())
- return "";
-
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '/') {
- --cur;
- }
-
- return cur + 1;
-}
-
PSPFilesystemNode::PSPFilesystemNode() {
_isDirectory = true;
_displayName = "Root";
@@ -106,7 +82,7 @@ PSPFilesystemNode::PSPFilesystemNode(const Common::String &p, bool verify) {
assert(p.size() > 0);
_path = p;
- _displayName = lastPathComponent(_path);
+ _displayName = lastPathComponent(_path, '/');
_isValid = true;
_isDirectory = true;
@@ -176,7 +152,7 @@ AbstractFilesystemNode *PSPFilesystemNode::getParent() const {
return 0;
const char *start = _path.c_str();
- const char *end = lastPathComponent(_path);
+ const char *end = lastPathComponent(_path, '/');
return new PSPFilesystemNode(String(start, end - start), false);
}
diff --git a/backends/fs/symbian/symbian-fs.cpp b/backends/fs/symbian/symbian-fs.cpp
index b2a106e0da..af963dda0c 100644
--- a/backends/fs/symbian/symbian-fs.cpp
+++ b/backends/fs/symbian/symbian-fs.cpp
@@ -79,30 +79,6 @@ public:
};
/**
- * Returns the last component of a given path.
- *
- * Examples:
- * c:\foo\bar.txt would return "\bar.txt"
- * c:\foo\bar\ would return "\bar\"
- *
- * @param str Path to obtain the last component from.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *lastPathComponent(const Common::String &str) {
- if(str.empty())
- return "";
-
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '\\') {
- --cur;
- }
-
- return cur + 1;
-}
-
-/**
* Fixes the path by changing all slashes to backslashes.
*
* @param path String with the path to be fixed.
@@ -136,7 +112,7 @@ SymbianFilesystemNode::SymbianFilesystemNode(const String &path) {
fixFilePath(_path);
- _displayName = lastPathComponent(_path);
+ _displayName = lastPathComponent(_path, '\\');
TEntry fileAttribs;
TFileName fname;
@@ -257,12 +233,12 @@ AbstractFilesystemNode *SymbianFilesystemNode::getParent() const {
if (!_isPseudoRoot && _path.size() > 3) {
p = new SymbianFilesystemNode(false);
const char *start = _path.c_str();
- const char *end = lastPathComponent(_path);
+ const char *end = lastPathComponent(_path, '\\');
p->_path = String(start, end - start);
p->_isValid = true;
p->_isDirectory = true;
- p->_displayName = lastPathComponent(p->_path);
+ p->_displayName = lastPathComponent(p->_path, '\\');
}
else
{
diff --git a/backends/fs/wii/wii-fs.cpp b/backends/fs/wii/wii-fs.cpp
index e6d0cf4c7e..4272ffb380 100644
--- a/backends/fs/wii/wii-fs.cpp
+++ b/backends/fs/wii/wii-fs.cpp
@@ -71,30 +71,6 @@ private:
virtual void setFlags();
};
-/**
- * Returns the last component of a given path.
- *
- * Examples:
- * /foo/bar.txt would return /bar.txt
- * /foo/bar/ would return /bar/
- *
- * @param str String containing the path.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *lastPathComponent(const Common::String &str) {
- if(str.empty())
- return "";
-
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '/') {
- --cur;
- }
-
- return cur + 1;
-}
-
void WiiFilesystemNode::setFlags() {
struct stat st;
@@ -123,7 +99,7 @@ WiiFilesystemNode::WiiFilesystemNode(const String &p, bool verify) {
_path = p;
- _displayName = lastPathComponent(_path);
+ _displayName = lastPathComponent(_path, '/');
if (verify)
setFlags();
@@ -187,7 +163,7 @@ AbstractFilesystemNode *WiiFilesystemNode::getParent() const {
return 0;
const char *start = _path.c_str();
- const char *end = lastPathComponent(_path);
+ const char *end = lastPathComponent(_path, '/');
return new WiiFilesystemNode(String(start, end - start), true);
}
diff --git a/backends/fs/windows/windows-fs.cpp b/backends/fs/windows/windows-fs.cpp
index 2105317a96..b06581047c 100644
--- a/backends/fs/windows/windows-fs.cpp
+++ b/backends/fs/windows/windows-fs.cpp
@@ -135,30 +135,6 @@ private:
static const TCHAR* toUnicode(const char *str);
};
-/**
- * Returns the last component of a given path.
- *
- * Examples:
- * c:\foo\bar.txt would return "\bar.txt"
- * c:\foo\bar\ would return "\bar\"
- *
- * @param str Path to obtain the last component from.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *lastPathComponent(const Common::String &str) {
- if(str.empty())
- return "";
-
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '\\') {
- --cur;
- }
-
- return cur + 1;
-}
-
void WindowsFilesystemNode::addFile(AbstractFSList &list, ListMode mode, const char *base, WIN32_FIND_DATA* find_data) {
WindowsFilesystemNode entry;
char *asciiName = toAscii(find_data->cFileName);
@@ -232,7 +208,7 @@ WindowsFilesystemNode::WindowsFilesystemNode(const String &p, const bool current
_path = p;
}
- _displayName = lastPathComponent(_path);
+ _displayName = lastPathComponent(_path, '\\');
// Check whether it is a directory, and whether the file actually exists
DWORD fileAttribs = GetFileAttributes(toUnicode(_path.c_str()));
@@ -322,13 +298,13 @@ AbstractFilesystemNode *WindowsFilesystemNode::getParent() const {
WindowsFilesystemNode *p = new WindowsFilesystemNode();
if (_path.size() > 3) {
const char *start = _path.c_str();
- const char *end = lastPathComponent(_path);
+ const char *end = lastPathComponent(_path, '\\');
p = new WindowsFilesystemNode();
p->_path = String(start, end - start);
p->_isValid = true;
p->_isDirectory = true;
- p->_displayName = lastPathComponent(p->_path);
+ p->_displayName = lastPathComponent(p->_path, '\\');
p->_isPseudoRoot = false;
}
diff --git a/backends/module.mk b/backends/module.mk
index 1a20ffd16c..9e66fba4af 100644
--- a/backends/module.mk
+++ b/backends/module.mk
@@ -1,6 +1,7 @@
MODULE := backends
MODULE_OBJS := \
+ fs/abstract-fs.o \
fs/amigaos4/amigaos4-fs-factory.o \
fs/ds/ds-fs-factory.o \
fs/palmos/palmos-fs-factory.o \
diff --git a/backends/platform/dc/dc-fs.cpp b/backends/platform/dc/dc-fs.cpp
index 596e0d72d5..0da77e317e 100644
--- a/backends/platform/dc/dc-fs.cpp
+++ b/backends/platform/dc/dc-fs.cpp
@@ -37,13 +37,12 @@
class RoninCDFileNode : public AbstractFilesystemNode {
protected:
String _path;
- static const char *lastPathComponent(const Common::String &str);
public:
RoninCDFileNode(const String &path) : _path(path) {};
virtual bool exists() const { return true; }
- virtual String getName() const { return lastPathComponent(_path); }
+ virtual String getName() const { return lastPathComponent(_path, '/'); }
virtual String getPath() const { return _path; }
virtual bool isDirectory() const { return false; }
virtual bool isReadable() const { return true; }
@@ -75,30 +74,6 @@ public:
virtual bool isReadable() const { return false; }
};
-/**
- * Returns the last component of a given path.
- *
- * Examples:
- * /foo/bar.txt would return /bar.txt
- * /foo/bar/ would return /bar/
- *
- * @param str String containing the path.
- * @return Pointer to the first char of the last component inside str.
- */
-const char *RoninCDFileNode::lastPathComponent(const Common::String &str) {
- if(str.empty())
- return "";
-
- const char *start = str.c_str();
- const char *cur = start + str.size() - 2;
-
- while (cur >= start && *cur != '/') {
- --cur;
- }
-
- return cur + 1;
-}
-
AbstractFilesystemNode *RoninCDFileNode::makeFileNodePath(const Common::String &path) {
assert(path.size() > 0);
@@ -163,7 +138,7 @@ AbstractFilesystemNode *RoninCDFileNode::getParent() const {
return 0;
const char *start = _path.c_str();
- const char *end = lastPathComponent(_path);
+ const char *end = lastPathComponent(_path, '/');
return new RoninCDDirectoryNode(String(start, end - start));
}