FSNode code: Merged most versions of lastPathComponent() into one new AbstractFilesystemNode::lastPathComponent() method, with customizable path separator character

svn-id: r34197
This commit is contained in:
Max Horn 2008-08-27 20:31:22 +00:00
parent 79fafb7b6a
commit cb21c25e41
12 changed files with 72 additions and 231 deletions

View file

@ -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;
}

View file

@ -72,6 +72,19 @@ protected:
*/ */
virtual AbstractFilesystemNode *getParent() const = 0; 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: public:
/** /**
* Destructor. * Destructor.
@ -154,4 +167,6 @@ public:
*/ */
}; };
#endif //BACKENDS_ABSTRACT_FS_H #endif //BACKENDS_ABSTRACT_FS_H

View file

@ -798,25 +798,3 @@ int std_ferror(FILE* handle) {
} }
} // namespace DS } // 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;
}

View file

@ -80,30 +80,6 @@ private:
static void addFile(AbstractFSList &list, ListMode mode, const Char *base, FileInfoType* find_data); 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) { void PalmOSFilesystemNode::addFile(AbstractFSList &list, ListMode mode, const char *base, FileInfoType* find_data) {
PalmOSFilesystemNode entry; PalmOSFilesystemNode entry;
bool isDir; bool isDir;
@ -138,7 +114,7 @@ PalmOSFilesystemNode::PalmOSFilesystemNode() {
PalmOSFilesystemNode::PalmOSFilesystemNode(const String &p) { PalmOSFilesystemNode::PalmOSFilesystemNode(const String &p) {
_path = p; _path = p;
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path, '/');
UInt32 attr; UInt32 attr;
FileRef handle; FileRef handle;
@ -215,13 +191,13 @@ AbstractFilesystemNode *PalmOSFilesystemNode::getParent() const {
if (!_isPseudoRoot) { if (!_isPseudoRoot) {
const char *start = _path.c_str(); const char *start = _path.c_str();
const char *end = lastPathComponent(_path); const char *end = lastPathComponent(_path, '/');
p = new PalmOSFilesystemNode(); p = new PalmOSFilesystemNode();
p->_path = String(start, end - start); p->_path = String(start, end - start);
p->_isValid = true; p->_isValid = true;
p->_isDirectory = true; p->_isDirectory = true;
p->_displayName = lastPathComponent(p->_path); p->_displayName = lastPathComponent(p->_path, '/');
p->_isPseudoRoot =(p->_path == "/"); p->_isPseudoRoot =(p->_path == "/");
} }

View file

@ -37,31 +37,6 @@
#endif #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() { void POSIXFilesystemNode::setFlags() {
struct stat st; struct stat st;
@ -93,7 +68,7 @@ POSIXFilesystemNode::POSIXFilesystemNode(const Common::String &p, bool verify) {
_path = p; _path = p;
} }
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path, '/');
if (verify) { if (verify) {
setFlags(); setFlags();
@ -224,7 +199,7 @@ AbstractFilesystemNode *POSIXFilesystemNode::getParent() const {
return 0; return 0;
const char *start = _path.c_str(); const char *start = _path.c_str();
const char *end = lastPathComponent(_path); const char *end = lastPathComponent(_path, '/');
#ifdef __OS2__ #ifdef __OS2__
if (end == start) if (end == start)

View file

@ -100,28 +100,6 @@ public:
virtual AbstractFilesystemNode *getParent() const; 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() { Ps2FilesystemNode::Ps2FilesystemNode() {
_isDirectory = true; _isDirectory = true;
_isRoot = true; _isRoot = true;

View file

@ -71,30 +71,6 @@ public:
virtual AbstractFilesystemNode *getParent() const; 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() { PSPFilesystemNode::PSPFilesystemNode() {
_isDirectory = true; _isDirectory = true;
_displayName = "Root"; _displayName = "Root";
@ -106,7 +82,7 @@ PSPFilesystemNode::PSPFilesystemNode(const Common::String &p, bool verify) {
assert(p.size() > 0); assert(p.size() > 0);
_path = p; _path = p;
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path, '/');
_isValid = true; _isValid = true;
_isDirectory = true; _isDirectory = true;
@ -176,7 +152,7 @@ AbstractFilesystemNode *PSPFilesystemNode::getParent() const {
return 0; return 0;
const char *start = _path.c_str(); const char *start = _path.c_str();
const char *end = lastPathComponent(_path); const char *end = lastPathComponent(_path, '/');
return new PSPFilesystemNode(String(start, end - start), false); return new PSPFilesystemNode(String(start, end - start), false);
} }

View file

@ -78,30 +78,6 @@ public:
virtual AbstractFilesystemNode *getParent() const; virtual AbstractFilesystemNode *getParent() const;
}; };
/**
* 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. * Fixes the path by changing all slashes to backslashes.
* *
@ -136,7 +112,7 @@ SymbianFilesystemNode::SymbianFilesystemNode(const String &path) {
fixFilePath(_path); fixFilePath(_path);
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path, '\\');
TEntry fileAttribs; TEntry fileAttribs;
TFileName fname; TFileName fname;
@ -257,12 +233,12 @@ AbstractFilesystemNode *SymbianFilesystemNode::getParent() const {
if (!_isPseudoRoot && _path.size() > 3) { if (!_isPseudoRoot && _path.size() > 3) {
p = new SymbianFilesystemNode(false); p = new SymbianFilesystemNode(false);
const char *start = _path.c_str(); const char *start = _path.c_str();
const char *end = lastPathComponent(_path); const char *end = lastPathComponent(_path, '\\');
p->_path = String(start, end - start); p->_path = String(start, end - start);
p->_isValid = true; p->_isValid = true;
p->_isDirectory = true; p->_isDirectory = true;
p->_displayName = lastPathComponent(p->_path); p->_displayName = lastPathComponent(p->_path, '\\');
} }
else else
{ {

View file

@ -71,30 +71,6 @@ private:
virtual void setFlags(); 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() { void WiiFilesystemNode::setFlags() {
struct stat st; struct stat st;
@ -123,7 +99,7 @@ WiiFilesystemNode::WiiFilesystemNode(const String &p, bool verify) {
_path = p; _path = p;
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path, '/');
if (verify) if (verify)
setFlags(); setFlags();
@ -187,7 +163,7 @@ AbstractFilesystemNode *WiiFilesystemNode::getParent() const {
return 0; return 0;
const char *start = _path.c_str(); const char *start = _path.c_str();
const char *end = lastPathComponent(_path); const char *end = lastPathComponent(_path, '/');
return new WiiFilesystemNode(String(start, end - start), true); return new WiiFilesystemNode(String(start, end - start), true);
} }

View file

@ -135,30 +135,6 @@ private:
static const TCHAR* toUnicode(const char *str); 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) { void WindowsFilesystemNode::addFile(AbstractFSList &list, ListMode mode, const char *base, WIN32_FIND_DATA* find_data) {
WindowsFilesystemNode entry; WindowsFilesystemNode entry;
char *asciiName = toAscii(find_data->cFileName); char *asciiName = toAscii(find_data->cFileName);
@ -232,7 +208,7 @@ WindowsFilesystemNode::WindowsFilesystemNode(const String &p, const bool current
_path = p; _path = p;
} }
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path, '\\');
// Check whether it is a directory, and whether the file actually exists // Check whether it is a directory, and whether the file actually exists
DWORD fileAttribs = GetFileAttributes(toUnicode(_path.c_str())); DWORD fileAttribs = GetFileAttributes(toUnicode(_path.c_str()));
@ -322,13 +298,13 @@ AbstractFilesystemNode *WindowsFilesystemNode::getParent() const {
WindowsFilesystemNode *p = new WindowsFilesystemNode(); WindowsFilesystemNode *p = new WindowsFilesystemNode();
if (_path.size() > 3) { if (_path.size() > 3) {
const char *start = _path.c_str(); const char *start = _path.c_str();
const char *end = lastPathComponent(_path); const char *end = lastPathComponent(_path, '\\');
p = new WindowsFilesystemNode(); p = new WindowsFilesystemNode();
p->_path = String(start, end - start); p->_path = String(start, end - start);
p->_isValid = true; p->_isValid = true;
p->_isDirectory = true; p->_isDirectory = true;
p->_displayName = lastPathComponent(p->_path); p->_displayName = lastPathComponent(p->_path, '\\');
p->_isPseudoRoot = false; p->_isPseudoRoot = false;
} }

View file

@ -1,6 +1,7 @@
MODULE := backends MODULE := backends
MODULE_OBJS := \ MODULE_OBJS := \
fs/abstract-fs.o \
fs/amigaos4/amigaos4-fs-factory.o \ fs/amigaos4/amigaos4-fs-factory.o \
fs/ds/ds-fs-factory.o \ fs/ds/ds-fs-factory.o \
fs/palmos/palmos-fs-factory.o \ fs/palmos/palmos-fs-factory.o \

View file

@ -37,13 +37,12 @@
class RoninCDFileNode : public AbstractFilesystemNode { class RoninCDFileNode : public AbstractFilesystemNode {
protected: protected:
String _path; String _path;
static const char *lastPathComponent(const Common::String &str);
public: public:
RoninCDFileNode(const String &path) : _path(path) {}; RoninCDFileNode(const String &path) : _path(path) {};
virtual bool exists() const { return true; } 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 String getPath() const { return _path; }
virtual bool isDirectory() const { return false; } virtual bool isDirectory() const { return false; }
virtual bool isReadable() const { return true; } virtual bool isReadable() const { return true; }
@ -75,30 +74,6 @@ public:
virtual bool isReadable() const { return false; } 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) { AbstractFilesystemNode *RoninCDFileNode::makeFileNodePath(const Common::String &path) {
assert(path.size() > 0); assert(path.size() > 0);
@ -163,7 +138,7 @@ AbstractFilesystemNode *RoninCDFileNode::getParent() const {
return 0; return 0;
const char *start = _path.c_str(); const char *start = _path.c_str();
const char *end = lastPathComponent(_path); const char *end = lastPathComponent(_path, '/');
return new RoninCDDirectoryNode(String(start, end - start)); return new RoninCDDirectoryNode(String(start, end - start));
} }