From c459f054b46b8791ce206c2ee13d455a9c10fe4d Mon Sep 17 00:00:00 2001 From: David Corrales Date: Thu, 3 May 2007 02:39:33 +0000 Subject: Use abstract factories to initialize FilesystemNode objects. svn-id: r26739 --- backends/fs/ps2/Ps2FilesystemFactory.cpp | 23 ++++++ backends/fs/ps2/Ps2FilesystemFactory.h | 38 ++++++++++ backends/fs/ps2/ps2-fs.cpp | 125 ++++++++++++++++--------------- 3 files changed, 127 insertions(+), 59 deletions(-) create mode 100644 backends/fs/ps2/Ps2FilesystemFactory.cpp create mode 100644 backends/fs/ps2/Ps2FilesystemFactory.h (limited to 'backends/fs/ps2') diff --git a/backends/fs/ps2/Ps2FilesystemFactory.cpp b/backends/fs/ps2/Ps2FilesystemFactory.cpp new file mode 100644 index 0000000000..c07387eae1 --- /dev/null +++ b/backends/fs/ps2/Ps2FilesystemFactory.cpp @@ -0,0 +1,23 @@ +#include "backends/fs/ps2/Ps2FilesystemFactory.h" +#include "backends/fs/ps2/ps2-fs.cpp" + +Ps2FilesystemFactory *Ps2FilesystemFactory::_instance = 0; + +Ps2FilesystemFactory *Ps2FilesystemFactory::instance(){ + if(_instance == 0){ + _instance = new Ps2FilesystemFactory(); + } + return _instance; +} + +AbstractFilesystemNode *Ps2FilesystemFactory::makeRootFileNode() const { + return new Ps2FilesystemNode(); +} + +AbstractFilesystemNode *Ps2FilesystemFactory::makeCurrentDirectoryFileNode() const { + return new Ps2FilesystemNode(); +} + +AbstractFilesystemNode *Ps2FilesystemFactory::makeFileNodePath(const String &path) const { + return new Ps2FilesystemNode(path); +} diff --git a/backends/fs/ps2/Ps2FilesystemFactory.h b/backends/fs/ps2/Ps2FilesystemFactory.h new file mode 100644 index 0000000000..e394865e80 --- /dev/null +++ b/backends/fs/ps2/Ps2FilesystemFactory.h @@ -0,0 +1,38 @@ +#ifndef PS2FILESYSTEMFACTORY_H_ +#define PS2FILESYSTEMFACTORY_H_ + +#include "backends/fs/AbstractFilesystemFactory.h" + +/** + * Creates PS2FilesystemNode objects. + * + * Parts of this class are documented in the base interface class, AbstractFilesystemFactory. + */ +class Ps2FilesystemFactory : public AbstractFilesystemFactory { +public: + typedef Common::String String; + + /** + * Creates an instance of Ps2FilesystemFactory using the Singleton pattern. + * + * @return A unique instance of Ps2FilesytemFactory. + */ + static Ps2FilesystemFactory *instance(); + + /** + * Destructor. + */ + virtual ~Ps2FilesystemFactory() {}; + + virtual AbstractFilesystemNode *makeRootFileNode() const; + virtual AbstractFilesystemNode *makeCurrentDirectoryFileNode() const; + virtual AbstractFilesystemNode *makeFileNodePath(const String &path) const; + +protected: + Ps2FilesystemFactory() {}; + +private: + static Ps2FilesystemFactory *_instance; +}; + +#endif /*PS2FILESYSTEMFACTORY_H_*/ diff --git a/backends/fs/ps2/ps2-fs.cpp b/backends/fs/ps2/ps2-fs.cpp index 18d6df5bb3..0661175c57 100644 --- a/backends/fs/ps2/ps2-fs.cpp +++ b/backends/fs/ps2/ps2-fs.cpp @@ -29,44 +29,49 @@ extern AsyncFio fio; extern OSystem_PS2 *g_systemPs2; +/** + * Implementation of the ScummVM file system API based on the Ps2SDK. + * + * Parts of this class are documented in the base interface class, AbstractFilesystemNode. + */ class Ps2FilesystemNode : public AbstractFilesystemNode { protected: String _displayName; + String _path; bool _isDirectory; bool _isRoot; - String _path; public: - Ps2FilesystemNode(void); - Ps2FilesystemNode(const Ps2FilesystemNode *node); + /** + * Creates a PS2FilesystemNode with the root node as path. + */ + Ps2FilesystemNode(); + + /** + * Creates a PS2FilesystemNode for a given path. + * + * @param path String with the path the new node should point to. + */ Ps2FilesystemNode(const String &path); + + /** + * Copy constructor. + */ + Ps2FilesystemNode(const Ps2FilesystemNode *node); - virtual String displayName() const { return _displayName; } - virtual String name() const { return _displayName; } - virtual bool isValid() const { return !_isRoot; } + virtual String getDisplayName() const { return _displayName; } + virtual String getName() const { return _displayName; } + virtual String getPath() const { return _path; } virtual bool isDirectory() const { return _isDirectory; } - virtual String path() const { return _path; } + virtual bool isValid() const { return !_isRoot; } - //virtual FSList listDir(ListMode) const; - virtual bool listDir(AbstractFSList &list, ListMode mode) const; - virtual AbstractFilesystemNode *parent() const; virtual AbstractFilesystemNode *clone() const { return new Ps2FilesystemNode(this); } - virtual AbstractFilesystemNode *child(const String &n) const; + virtual AbstractFilesystemNode *getChild(const String &n) const; + virtual bool getChildren(AbstractFSList &list, ListMode mode) const; + virtual AbstractFilesystemNode *getParent() const; }; -AbstractFilesystemNode *AbstractFilesystemNode::getCurrentDirectory() { - return AbstractFilesystemNode::getRoot(); -} - -AbstractFilesystemNode *AbstractFilesystemNode::getRoot(void) { - return new Ps2FilesystemNode(); -} - -AbstractFilesystemNode *AbstractFilesystemNode::getNodeForPath(const String &path) { - return new Ps2FilesystemNode(path); -} - -Ps2FilesystemNode::Ps2FilesystemNode(void) { +Ps2FilesystemNode::Ps2FilesystemNode() { _isDirectory = true; _isRoot = true; _displayName = "PlayStation 2"; @@ -105,7 +110,41 @@ Ps2FilesystemNode::Ps2FilesystemNode(const Ps2FilesystemNode *node) { _isRoot = node->_isRoot; } -bool Ps2FilesystemNode::listDir(AbstractFSList &list, ListMode mode) const { +AbstractFilesystemNode *Ps2FilesystemNode::getChild(const String &n) const { + if (!_isDirectory) + return NULL; + + char listDir[256]; + sprintf(listDir, "%s/", _path.c_str()); + int fd = fio.dopen(listDir); + + if (fd >= 0) { + iox_dirent_t dirent; + + while (fio.dread(fd, &dirent) > 0) { + if (strcmp(n.c_str(), dirent.name) == 0) { + Ps2FilesystemNode *dirEntry = new Ps2FilesystemNode(); + + dirEntry->_isDirectory = (bool)(dirent.stat.mode & FIO_S_IFDIR); + dirEntry->_isRoot = false; + + dirEntry->_path = _path; + dirEntry->_path += "/"; + dirEntry->_path += dirent.name; + + dirEntry->_displayName = dirent.name; + + fio.dclose(fd); + return dirEntry; + } + } + fio.dclose(fd); + } + + return NULL; +} + +bool Ps2FilesystemNode::getChildren(AbstractFSList &list, ListMode mode) const { if (!_isDirectory) return false; @@ -132,6 +171,7 @@ bool Ps2FilesystemNode::listDir(AbstractFSList &list, ListMode mode) const { } else { char listDir[256]; int fd; + if (_path.lastChar() == '/') fd = fio.dopen(_path.c_str()); else { @@ -170,7 +210,7 @@ bool Ps2FilesystemNode::listDir(AbstractFSList &list, ListMode mode) const { } } -AbstractFilesystemNode *Ps2FilesystemNode::parent() const { +AbstractFilesystemNode *Ps2FilesystemNode::getParent() const { if (_isRoot) return new Ps2FilesystemNode(this); @@ -188,36 +228,3 @@ AbstractFilesystemNode *Ps2FilesystemNode::parent() const { else return new Ps2FilesystemNode(); } - -AbstractFilesystemNode *Ps2FilesystemNode::child(const String &n) const { - if (!_isDirectory) - return NULL; - - char listDir[256]; - sprintf(listDir, "%s/", _path.c_str()); - int fd = fio.dopen(listDir); - - if (fd >= 0) { - iox_dirent_t dirent; - - while (fio.dread(fd, &dirent) > 0) { - if (strcmp(n.c_str(), dirent.name) == 0) { - Ps2FilesystemNode *dirEntry = new Ps2FilesystemNode(); - - dirEntry->_isDirectory = (bool)(dirent.stat.mode & FIO_S_IFDIR); - dirEntry->_isRoot = false; - - dirEntry->_path = _path; - dirEntry->_path += "/"; - dirEntry->_path += dirent.name; - - dirEntry->_displayName = dirent.name; - - fio.dclose(fd); - return dirEntry; - } - } - fio.dclose(fd); - } - return NULL; -} -- cgit v1.2.3