diff options
author | David Corrales | 2007-05-03 02:39:33 +0000 |
---|---|---|
committer | David Corrales | 2007-05-03 02:39:33 +0000 |
commit | c459f054b46b8791ce206c2ee13d455a9c10fe4d (patch) | |
tree | b36dc6034bfb9659e57e28de8a509a7b1de4554d /backends/fs/ds | |
parent | 8f5abc1924d5d7bdbc9684b870394f93ad80d2ff (diff) | |
download | scummvm-rg350-c459f054b46b8791ce206c2ee13d455a9c10fe4d.tar.gz scummvm-rg350-c459f054b46b8791ce206c2ee13d455a9c10fe4d.tar.bz2 scummvm-rg350-c459f054b46b8791ce206c2ee13d455a9c10fe4d.zip |
Use abstract factories to initialize FilesystemNode objects.
svn-id: r26739
Diffstat (limited to 'backends/fs/ds')
-rw-r--r-- | backends/fs/ds/DSFilesystemFactory.cpp | 36 | ||||
-rw-r--r-- | backends/fs/ds/DSFilesystemFactory.h | 38 | ||||
-rw-r--r-- | backends/fs/ds/ds-fs.cpp | 229 | ||||
-rw-r--r-- | backends/fs/ds/ds-fs.h | 113 |
4 files changed, 228 insertions, 188 deletions
diff --git a/backends/fs/ds/DSFilesystemFactory.cpp b/backends/fs/ds/DSFilesystemFactory.cpp new file mode 100644 index 0000000000..809dc0e5cf --- /dev/null +++ b/backends/fs/ds/DSFilesystemFactory.cpp @@ -0,0 +1,36 @@ +#include "backends/fs/ds/DSFilesystemFactory.h" +#include "backends/fs/ds/ds-fs.cpp" +#include "dsmain.h" //for the isGBAMPAvailable() function + +DSFilesystemFactory *DSFilesystemFactory::_instance = 0; + +DSFilesystemFactory *DSFilesystemFactory::instance(){ + if(_instance == 0){ + _instance = new DSFilesystemFactory(); + } + return _instance; +} + +AbstractFilesystemNode *DSFilesystemFactory::makeRootFileNode() const { + if (DS::isGBAMPAvailable()) { + return new DS::GBAMPFileSystemNode(); + } else { + return new DS::DSFileSystemNode(); + } +} + +AbstractFilesystemNode *DSFilesystemFactory::makeCurrentDirectoryFileNode() const { + if (DS::isGBAMPAvailable()) { + return new DS::GBAMPFileSystemNode(); + } else { + return new DS::DSFileSystemNode(); + } +} + +AbstractFilesystemNode *DSFilesystemFactory::makeFileNodePath(const String &path) const { + if (DS::isGBAMPAvailable()) { + return new DS::GBAMPFileSystemNode(path); + } else { + return new DS::DSFileSystemNode(path); + } +} diff --git a/backends/fs/ds/DSFilesystemFactory.h b/backends/fs/ds/DSFilesystemFactory.h new file mode 100644 index 0000000000..6eaef1cd1e --- /dev/null +++ b/backends/fs/ds/DSFilesystemFactory.h @@ -0,0 +1,38 @@ +#ifndef DSFILESYSTEMFACTORY_H_ +#define DSFILESYSTEMFACTORY_H_ + +#include "backends/fs/AbstractFilesystemFactory.h" + +/** + * Creates DSFilesystemNode objects. + * + * Parts of this class are documented in the base interface class, AbstractFilesystemFactory. + */ +class DSFilesystemFactory : public AbstractFilesystemFactory { +public: + typedef Common::String String; + + /** + * Creates an instance of DSFilesystemFactory using the Singleton pattern. + * + * @return A unique instance of DSFilesytemFactory. + */ + static DSFilesystemFactory *instance(); + + /** + * Destructor. + */ + virtual ~DSFilesystemFactory() {}; + + virtual AbstractFilesystemNode *makeRootFileNode() const; + virtual AbstractFilesystemNode *makeCurrentDirectoryFileNode() const; + virtual AbstractFilesystemNode *makeFileNodePath(const String &path) const; + +protected: + DSFilesystemFactory() {}; + +private: + static DSFilesystemFactory *_instance; +}; + +#endif /*DSFILESYSTEMFACTORY_H_*/ diff --git a/backends/fs/ds/ds-fs.cpp b/backends/fs/ds/ds-fs.cpp index 40d160bbc8..5243099fbe 100644 --- a/backends/fs/ds/ds-fs.cpp +++ b/backends/fs/ds/ds-fs.cpp @@ -17,7 +17,6 @@ * */ - #include "stdafx.h" #include "str.h" #include "fs.h" @@ -27,14 +26,10 @@ #include "dsmain.h" #include "gba_nds_fat.h" - namespace DS { - - - ////////////////////////////////////////////////////////////// -// DSFileSystemNode - Flash ROM file system using Zip files +// DSFileSystemNode - Flash ROM file system using Zip files // ////////////////////////////////////////////////////////////// ZipFile* DSFileSystemNode::_zipFile = NULL; @@ -63,7 +58,6 @@ DSFileSystemNode::DSFileSystemNode(const String& path) { char disp[128]; char* pathStr = (char *) path.c_str(); - int lastSlash = 3; for (int r = 0; r < (int) strlen(pathStr) - 1; r++) { if (path[r] == '\\') { @@ -78,13 +72,10 @@ DSFileSystemNode::DSFileSystemNode(const String& path) { // _isValid = true; // _isDirectory = false; - - if (!strncmp(pathStr, "ds:/", 4)) { pathStr += 4; } - if (*pathStr == '\0') { _isValid = true; _isDirectory = true; @@ -127,35 +118,10 @@ DSFileSystemNode::DSFileSystemNode(const String& path, bool isDir) { } DSFileSystemNode::DSFileSystemNode(const DSFileSystemNode* node) { - + //TODO: not implemented? } -AbstractFilesystemNode* DSFileSystemNode::parent() const { -// consolePrintf("parent\n"); - DSFileSystemNode *p; - - if (_path != "ds:/") { - char *path = (char *) _path.c_str(); - int lastSlash = 4; - - for (int r = 4; r < (int) strlen((char *) path); r++) { - if (path[r] == '\\') { - lastSlash = r; - } - } - - p = new DSFileSystemNode(String(path, lastSlash)); - ((DSFileSystemNode *) (p))->_isDirectory = true; - } else { - p = new DSFileSystemNode(); - } - - return p; - -} - - -AbstractFilesystemNode *DSFileSystemNode::child(const Common::String& n) const { +AbstractFilesystemNode *DSFileSystemNode::getChild(const Common::String& n) const { if (_path.lastChar() == '\\') { return new DSFileSystemNode(_path + n); } else { @@ -165,14 +131,10 @@ AbstractFilesystemNode *DSFileSystemNode::child(const Common::String& n) const { return NULL; } - -bool DSFileSystemNode::listDir(AbstractFSList &dirList, ListMode mode) const { +bool DSFileSystemNode::getChildren(AbstractFSList &dirList, ListMode mode) const { // consolePrintf("Listdir\n"); - - // consolePrintf("Directory\n"); - char temp[128]; strcpy(temp, _path.c_str()); @@ -187,14 +149,13 @@ bool DSFileSystemNode::listDir(AbstractFSList &dirList, ListMode mode) const { /* // This is the root dir, so add the RAM folder DSFileSystemNode* dsfsn = new DSFileSystemNode("ds:/ram"); dsfsn->_isDirectory = true; - dirList->push_back(wrap(dsfsn));*/ + dirList->push_back(wrap(dsfsn)); +*/ } } else { _zipFile->changeDirectory(temp); } - - if (_zipFile->restartFile()) { do { char n[128]; @@ -215,12 +176,32 @@ bool DSFileSystemNode::listDir(AbstractFSList &dirList, ListMode mode) const { return true; } +AbstractFilesystemNode* DSFileSystemNode::getParent() const { +// consolePrintf("parent\n"); + DSFileSystemNode *p; + if (_path != "ds:/") { + char *path = (char *) _path.c_str(); + int lastSlash = 4; + + for (int r = 4; r < (int) strlen((char *) path); r++) { + if (path[r] == '\\') { + lastSlash = r; + } + } + p = new DSFileSystemNode(String(path, lastSlash)); + ((DSFileSystemNode *) (p))->_isDirectory = true; + } else { + p = new DSFileSystemNode(); + } + + return p; +} -///////////////////////////////////////////////////////////////////////// -// GBAMPFileSystemNode - File system using GBA Movie Player and CF card -///////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// +// GBAMPFileSystemNode - File system using GBA Movie Player and CF card // +////////////////////////////////////////////////////////////////////////// GBAMPFileSystemNode::GBAMPFileSystemNode() { _displayName = "mp:/"; @@ -287,35 +268,10 @@ GBAMPFileSystemNode::GBAMPFileSystemNode(const String& path, bool isDirectory) { GBAMPFileSystemNode::GBAMPFileSystemNode(const GBAMPFileSystemNode* node) { - -} - - -AbstractFilesystemNode* GBAMPFileSystemNode::parent() const { -// consolePrintf("parent\n"); - GBAMPFileSystemNode *p; - - if (_path != "mp:/") { - char *path = (char *) _path.c_str(); - int lastSlash = 4; - - for (int r = 4; r < (int) strlen((char *) path); r++) { - if (path[r] == '/') { - lastSlash = r; - } - } - - p = new GBAMPFileSystemNode(String(path, lastSlash)); - p->_isDirectory = true; - } else { - p = new GBAMPFileSystemNode(); - } - - return p; - + //TODO: not implemented? } -AbstractFilesystemNode *GBAMPFileSystemNode::child(const Common::String& n) const { +AbstractFilesystemNode *GBAMPFileSystemNode::getChild(const Common::String& n) const { if (_path.lastChar() == '\\') { return new DSFileSystemNode(_path + n); } else { @@ -325,7 +281,7 @@ AbstractFilesystemNode *GBAMPFileSystemNode::child(const Common::String& n) cons return NULL; } -bool GBAMPFileSystemNode::listDir(AbstractFSList& dirList, ListMode mode) const { +bool GBAMPFileSystemNode::getChildren(AbstractFSList& dirList, ListMode mode) const { // consolePrintf("Listdir\n"); enum { TYPE_NO_MORE = 0, TYPE_FILE = 1, TYPE_DIR = 2 }; @@ -343,7 +299,6 @@ bool GBAMPFileSystemNode::listDir(AbstractFSList& dirList, ListMode mode) const pathTemp++; } - // consolePrintf("This dir: %s\n", path); FAT_chdir(path); @@ -366,8 +321,6 @@ bool GBAMPFileSystemNode::listDir(AbstractFSList& dirList, ListMode mode) const // dsfsn->_isDirectory = entryType == DIR; dirList.push_back((dsfsn)); } - - } else { // consolePrintf("Skipping %s\n", fname); } @@ -382,6 +335,28 @@ bool GBAMPFileSystemNode::listDir(AbstractFSList& dirList, ListMode mode) const return true; } +AbstractFilesystemNode* GBAMPFileSystemNode::getParent() const { +// consolePrintf("parent\n"); + GBAMPFileSystemNode *p; + + if (_path != "mp:/") { + char *path = (char *) _path.c_str(); + int lastSlash = 4; + + for (int r = 4; r < (int) strlen((char *) path); r++) { + if (path[r] == '/') { + lastSlash = r; + } + } + + p = new GBAMPFileSystemNode(String(path, lastSlash)); + p->_isDirectory = true; + } else { + p = new GBAMPFileSystemNode(); + } + + return p; +} // Stdio replacements #define MAX_FILE_HANDLES 32 @@ -390,9 +365,6 @@ bool inited = false; DS::fileHandle handle[MAX_FILE_HANDLES]; FILE* std_fopen(const char* name, const char* mode) { - - - if (!inited) { for (int r = 0; r < MAX_FILE_HANDLES; r++) { handle[r].used = false; @@ -400,9 +372,6 @@ FILE* std_fopen(const char* name, const char* mode) { inited = true; currentDir[0] = '\0'; } - - - char* realName = (char *) name; @@ -410,7 +379,7 @@ FILE* std_fopen(const char* name, const char* mode) { if ((name[0] == 'd') && (name[1] == 's') && (name[2] == ':') && (name[3] == '/')) { realName += 4; } - + if ((name[0] == 'm') && (name[1] == 'p') && (name[2] == ':') && (name[3] == '/')) { realName += 4; } @@ -418,7 +387,6 @@ FILE* std_fopen(const char* name, const char* mode) { // consolePrintf("Open file:"); // consolePrintf("'%s', [%s]", realName, mode); - if (DS::isGBAMPAvailable()) { FAT_chdir("/"); @@ -440,10 +408,9 @@ FILE* std_fopen(const char* name, const char* mode) { return (FILE *) result; } - // Fail to open file for writing. It's in ROM! - + // Allocate a file handle int r = 0; while (handle[r].used) r++; @@ -456,7 +423,6 @@ FILE* std_fopen(const char* name, const char* mode) { handle[r].sramFile = (DSSaveFile *) DSSaveFileManager::instance()->openSavefile(realName, false); } - if (handle[r].sramFile) { handle[r].used = true; handle[r].pos = 0; @@ -510,6 +476,7 @@ FILE* std_fopen(const char* name, const char* mode) { return NULL; } } + void std_fclose(FILE* handle) { if (DS::isGBAMPAvailable()) { @@ -525,14 +492,9 @@ void std_fclose(FILE* handle) { } size_t std_fread(const void* ptr, size_t size, size_t numItems, FILE* handle) { - // consolePrintf("fread %d,%d %d ", size, numItems, ptr); - - if (DS::isGBAMPAvailable()) { - - int bytes = FAT_fread((void *) ptr, size, numItems, (FAT_FILE *) handle); if (!std_feof(handle)) { return numItems; @@ -557,27 +519,24 @@ size_t std_fread(const void* ptr, size_t size, size_t numItems, FILE* handle) { } - return item;*/ - - + return item; +*/ int items = 0; //for (int r = 0; r < numItems; r++) { if (!std_feof(handle)) { - - - /* for (int t = 0; t < size; t++) { if (feof(handle)) eof = true; *(((char *) (ptr)) + r * size + t) = getc(handle); }*/ int left = size * numItems; int bytesRead = -1; + while ((left > 0) && (!FAT_feof((FAT_FILE *) handle))) { int amount = left > 8192? 8192: left; // do { bytesRead = FAT_fread((void *) ptr, 1, amount, (FAT_FILE *) handle); - /* if (bytesRead == 0) { +/* if (bytesRead == 0) { consolePrintf("Pos:%d items:%d num:%d amount:%d read:%d\n", ftell(handle), items, numItems, amount, bytesRead); left++; @@ -589,27 +548,24 @@ size_t std_fread(const void* ptr, size_t size, size_t numItems, FILE* handle) { fread(ptr, 1024, 1, handle); swiWaitForVBlank(); //while (true); - }*/ - //} while (bytesRead == 0); + } + + } while (bytesRead == 0); +*/ left -= bytesRead; ptr = ((char *) (ptr)) + bytesRead; } items = numItems - (left / size); - - - - + // FAT_fread((void *) ptr, size, 1, ((int) (handle)) - 1); - // ptr = ((char *) (ptr)) + size; - +// ptr = ((char *) (ptr)) + size; } - //} +// } // consolePrintf("...done %d \n", items) return items; - } if (handle->sramFile) { @@ -627,7 +583,6 @@ size_t std_fread(const void* ptr, size_t size, size_t numItems, FILE* handle) { return bytes / size; } - if (handle->pos + size * numItems > handle->size) { numItems = (handle->size - handle->pos) / size; if (numItems < 0) numItems = 0; @@ -636,10 +591,8 @@ size_t std_fread(const void* ptr, size_t size, size_t numItems, FILE* handle) { // consolePrintf("read %d ", size * numItems); memcpy((void *) ptr, handle->data + handle->pos, size * numItems); - handle->pos += size * numItems; - return numItems; } @@ -654,7 +607,6 @@ size_t std_fwrite(const void* ptr, size_t size, size_t numItems, FILE* handle) { //consolePrintf("fwrite size=%d\n", size * numItems); if (DS::isGBAMPAvailable()) { - FAT_fwrite(((char *) (ptr)), size, numItems, (FAT_FILE *) handle); return numItems; @@ -672,7 +624,6 @@ size_t std_fwrite(const void* ptr, size_t size, size_t numItems, FILE* handle) { return numItems; } - if (handle->sramFile) { handle->sramFile->write(ptr, size); return size; @@ -701,6 +652,7 @@ bool std_feof(FILE* handle) { } void std_fflush(FILE* handle) { + //FIXME: not implemented? // consolePrintf("fflush "); } @@ -708,7 +660,6 @@ char* std_fgets(char* str, int size, FILE* file) { // consolePrintf("fgets file=%d ", file); if (DS::isGBAMPAvailable()) { - char* s = str; while ((*s++ = std_getc(file)) >= 32) { // consolePrintf("%d ", *s); @@ -720,7 +671,6 @@ char* std_fgets(char* str, int size, FILE* file) { return str; } - if (file->sramFile) { file->pos--; int p = -1; @@ -740,7 +690,6 @@ char* std_fgets(char* str, int size, FILE* file) { } long int std_ftell(FILE* handle) { - if (DS::isGBAMPAvailable()) { return FAT_ftell((FAT_FILE *) handle); } @@ -755,39 +704,30 @@ int std_fseek(FILE* handle, long int offset, int whence) { return FAT_fseek((FAT_FILE *) handle, offset, whence); } - switch (whence) { - case SEEK_CUR: { + case SEEK_CUR: handle->pos += offset; break; - } - - case SEEK_SET: { + case SEEK_SET: handle->pos = offset; break; - } - - case SEEK_END: { + case SEEK_END: handle->pos = handle->size + offset; break; - } - - default: { + default: handle->pos = offset; break; - } - } return 0; } void std_clearerr(FILE* handle) { + //FIXME: not implemented? // consolePrintf("clearerr "); } int std_getc(FILE* handle) { - if (DS::isGBAMPAvailable()) { char c; FAT_fread(&c, 1, 1, (FAT_FILE *) handle); @@ -849,24 +789,3 @@ int std_ferror(FILE* handle) { } } // namespace DS - -// These functions are added to AbstractFileSystemNode and are therefore outside -// the DS namespace. - -AbstractFilesystemNode *AbstractFilesystemNode::getCurrentDirectory() { -// consolePrintf("New node"); - - if (DS::isGBAMPAvailable()) { - return new DS::GBAMPFileSystemNode(); - } else { - return new DS::DSFileSystemNode(); - } -} - -AbstractFilesystemNode* AbstractFilesystemNode::getNodeForPath(const String& path) { - if (DS::isGBAMPAvailable()) { - return new DS::GBAMPFileSystemNode(path); - } else { - return new DS::DSFileSystemNode(path); - } -} diff --git a/backends/fs/ds/ds-fs.h b/backends/fs/ds/ds-fs.h index 2830ff61ae..8b5cc5bdd0 100644 --- a/backends/fs/ds/ds-fs.h +++ b/backends/fs/ds/ds-fs.h @@ -20,8 +20,6 @@ #ifndef _DS_FS_H #define _DS_FS_H - - //#include <NDS/ARM9/console.h> #include "fs.h" #include "zipreader.h" @@ -29,81 +27,131 @@ #include "scummconsole.h" #include "gba_nds_fat.h" #include "backends/fs/abstract-fs.h" -//#include "backends/fs/fs.h" namespace DS { /** + * Implementation of the ScummVM file system API. * This class is used when a Flash cart is in use. + * + * Parts of this class are documented in the base interface class, AbstractFilesystemNode. */ class DSFileSystemNode : public AbstractFilesystemNode { protected: - static ZipFile* _zipFile; - typedef class Common::String String; + static ZipFile* _zipFile; + String _displayName; + String _path; bool _isDirectory; bool _isValid; - String _path; int _refCountVal; public: + /** + * Creates a DSFilesystemNode with the root node as path. + */ DSFileSystemNode(); + + /** + * Creates a DSFilesystemNode for a given path. + * + * @param path String with the path the new node should point to. + */ DSFileSystemNode(const String &path); - DSFileSystemNode(const DSFileSystemNode *node); + + /** + * Creates a DSFilesystemNode for a given path. + * + * @param path String with the path the new node should point to. + * @param path true if path is a directory, false otherwise. + */ DSFileSystemNode(const String& path, bool isDir); - virtual String displayName() const { return _displayName; } - virtual String name() const { return _displayName; } - virtual bool isValid() const { return _isValid; } + /** + * Copy constructor. + */ + DSFileSystemNode(const DSFileSystemNode *node); + + 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 _isValid; } - virtual bool listDir(AbstractFSList &list, ListMode mode = FilesystemNode::kListDirectoriesOnly) const; - virtual AbstractFilesystemNode *parent() const; + /** + * Returns a copy of this node. + */ virtual AbstractFilesystemNode *clone() const { return new DSFileSystemNode(this); } - virtual AbstractFilesystemNode *child(const Common::String& name) const; + virtual AbstractFilesystemNode *getChild(const Common::String& name) const; + virtual bool getChildren(AbstractFSList &list, ListMode mode = FilesystemNode::kListDirectoriesOnly) const; + virtual AbstractFilesystemNode *getParent() const; + + /** + * Returns the zip file this node points to. + * TODO: check this documentation. + */ static ZipFile* getZip() { return _zipFile; } }; - -/** + /** + * Implementation of the ScummVM file system API. * This class is used when the GBAMP (GBA Movie Player) is used with a CompactFlash card. + * + * Parts of this class are documented in the base interface class, AbstractFilesystemNode. */ class GBAMPFileSystemNode : public AbstractFilesystemNode { protected: typedef class Common::String String; String _displayName; + String _path; bool _isDirectory; bool _isValid; - String _path; - int _refCountVal; public: + /** + * Creates a GBAMPFilesystemNode with the root node as path. + */ GBAMPFileSystemNode(); + + /** + * Creates a GBAMPFilesystemNode for a given path. + * + * @param path String with the path the new node should point to. + */ GBAMPFileSystemNode(const String &path); + + /** + * Creates a DSFilesystemNode for a given path. + * + * @param path String with the path the new node should point to. + * @param path true if path is a directory, false otherwise. + */ GBAMPFileSystemNode(const String &path, bool isDirectory); + + /** + * Copy constructor. + */ GBAMPFileSystemNode(const GBAMPFileSystemNode *node); - virtual String displayName() const { return _displayName; } - virtual String name() const { return _displayName; } - - virtual bool isValid() const { return _isValid; } + 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 listDir(AbstractFSList &list, ListMode mode = FilesystemNode::kListDirectoriesOnly) const; - virtual AbstractFilesystemNode *parent() const; - virtual AbstractFilesystemNode *clone() const { return new GBAMPFileSystemNode(this); } - virtual AbstractFilesystemNode *child(const Common::String& name) const; + virtual bool isValid() const { return _isValid; } + /** + * Returns a copy of this node. + */ + virtual AbstractFilesystemNode *clone() const { return new GBAMPFileSystemNode(this); } + virtual AbstractFilesystemNode *getChild(const Common::String& name) const; + virtual bool getChildren(AbstractFSList &list, ListMode mode = FilesystemNode::kListDirectoriesOnly) const; + virtual AbstractFilesystemNode *getParent() const; }; - - - struct fileHandle { int pos; bool used; @@ -113,7 +161,6 @@ struct fileHandle { DSSaveFile* sramFile; }; - #undef stderr #undef stdout #undef stdin @@ -137,6 +184,6 @@ void std_clearerr(FILE* handle); void std_cwd(char* dir); void std_fflush(FILE* handle); -} +} //namespace DS -#endif +#endif //_DS_FS_H |