From a8d43b64901c96adb3fb67c50fc0e58cb3f71eba Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 13 Nov 2002 17:16:18 +0000 Subject: experimental FS browsing code. Right now only for POSIX systems, some things not yet implemented, and probably full of bugs svn-id: r5540 --- backends/fs/fs.h | 166 +++++++++++++++++++++++++++++++++++++++++ backends/fs/posix/posix-fs.cpp | 115 ++++++++++++++++++++++++++++ 2 files changed, 281 insertions(+) create mode 100644 backends/fs/fs.h create mode 100644 backends/fs/posix/posix-fs.cpp (limited to 'backends') diff --git a/backends/fs/fs.h b/backends/fs/fs.h new file mode 100644 index 0000000000..1dc23abbfd --- /dev/null +++ b/backends/fs/fs.h @@ -0,0 +1,166 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2002 The ScummVM project + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Header$ + */ + +#ifndef FS_H +#define FS_H + +/* + * The API described in this header is meant to allow for file system browsing in a + * portable fashions. To this ends, multiple or single roots have to be supported + * (compare Unix with a single root, Windows with multiple roots C:, D:, ...). + * + * To this end, we abstract away from paths; implementations can be based on + * paths (and it's left to them whether / or \ or : is the path separator :-); + * but it is also possible to use inodes or vrefs (MacOS 9) or anything else. + * + * NOTE 1: currently ONLY the directory listing mode is used; that is, we only are + * interested in listing directories. Thus, for now implementation only have to + * list directories. + * + * NOTE 2: Backends still have to provide a way to extract a path from a FSIntern + * + * You may ask now: "isn't this cheating? Why do we go through all this when we use + * a path in the end anyway?!?". + * Well, for once as long as we don't provide our own file open/read/write API, we + * still have to use fopen(). Since all our targets already support fopen(), it should + * be possible to get a fopen() compatible string for any file system node. + * + * Secondly, with this abstraction layer, we still avoid a lot of complications based on + * differences in FS roots, different path separators, or even systems with no real + * paths (Mac OS 9, which doesn't even have the notion of a "current director"). + * And if we ever want to support devices with no FS in the classical sense (Palm...), + * we can build upon this. + */ + + +#include "common/list.h" +#include "common/str.h" + +class FSList; + +/* + * A single file system node. + */ +class FilesystemNode { +protected: + typedef ScummVM::String String; + +public: + /* + * The starting point for any file system browsing. Returns a special node + * representing the FS root. + * On Unix, this will be simply the node for / (the root directory). + * On Windows, it will be a special node which "contains" all drives (C:, D:, E:). + */ + static FilesystemNode *getRoot(); + + /* + * Construct a node based on a path; the path is in the same format as it + * would be for calls to fopen(). + * + * I.e. getNodeForPath(oldNode.path()) should create a new node identical to oldNode. + */ +// static FilesystemNode *getNodeForPath(const String &path); + + virtual ~FilesystemNode() {} + + /* + * The display name, used by e.g. the GUI to present the file in the file browser. + */ + virtual String displayName() const = 0; + + /* + * Is this node valid (i.e. refering to an actual FS object)? + */ + virtual bool isValid() const = 0; + + /* + * Is this node a directory or not? + */ + virtual bool isDirectory() const = 0; + + /* + * A path representation suitable for use with fopen() + */ + virtual String path() const = 0; + + /* + * List the content of this directory node. + * If this node is not a directory, throw an exception or call error(). + */ + virtual FSList *listDir() const = 0; + + /* + * The parent node of this directory. + * The parent of the root is the root itself + */ + virtual FilesystemNode *parent() const = 0; + + /* + * Return a clone of this node allocated with new(). + */ + virtual FilesystemNode *clone() const = 0; +}; + + +/* + * A list of multiple file system nodes. E.g. the contents of a given directory. + */ +class FSList : ScummVM::List { +public: + ~FSList() + { + for (int i = 0; i < _size; i++) + delete _data[i]; + } + + void push_back(const FilesystemNode& element) + { + ensureCapacity(_size + 1); + _data[_size++] = element.clone(); + } + + const FilesystemNode& operator [](int idx) const + { + assert(idx >= 0 && idx < _size); + return *_data[idx]; + } + + int size() const { return _size; } +}; + + +/* + * Finally, a piece of sample code that does print out the content of your root directory: + +void test_FS_stuff() { + FilesystemNode *root = FilesystemNode::getRoot(); + FSList *filesInRoot = root->listDir(); + + printf("Content of your root directory:\n"); + for (int i = 0; i < filesInRoot->size(); i++) { + const FilesystemNode &node = (*filesInRoot)[i]; + printf("File: '%s', path: '%s'\n", node.displayName().c_str(), node.path().c_str()); + } +} + + * + */ +#endif diff --git a/backends/fs/posix/posix-fs.cpp b/backends/fs/posix/posix-fs.cpp new file mode 100644 index 0000000000..a9c70673bd --- /dev/null +++ b/backends/fs/posix/posix-fs.cpp @@ -0,0 +1,115 @@ +/* ScummVM - Scumm Interpreter + * Copyright (C) 2002 The ScummVM project + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Header$ + */ + +#if defined(UNIX) + +#include "../fs.h" + +#ifdef MACOSX +#include +#endif +#include + +/* + * Implementation of the ScummVM file system API based on POSIX. + */ + +class POSIXFilesystemNode : public FilesystemNode { +protected: + String _displayName; + bool _isDirectory; + bool _isValid; + String _path; + +public: + POSIXFilesystemNode(); + POSIXFilesystemNode(const String &path); + POSIXFilesystemNode(const POSIXFilesystemNode *node); + + virtual String displayName() const { return _displayName; } + virtual bool isValid() const { return _isValid; } + virtual bool isDirectory() const { return _isDirectory; } + virtual String path() const { return _path; } + + virtual FSList *listDir() const; + virtual FilesystemNode *parent() const; + virtual FilesystemNode *clone() const { return new POSIXFilesystemNode(this); } +}; + + +FilesystemNode *FilesystemNode::getRoot() { + return new POSIXFilesystemNode(); +} + +POSIXFilesystemNode::POSIXFilesystemNode() { + _displayName = "/"; + _isValid = true; + _isDirectory = true; + _path = "/"; +} + +/* +POSIXFilesystemNode::POSIXFilesystemNode(const String &p) { + // TODO - extract last component from path + _displayName = p; + // TODO - check whether it is a directory, and whether the file actually exists + _isValid = true; + _isDirectory = true; + _path = p; +} +*/ + +POSIXFilesystemNode::POSIXFilesystemNode(const POSIXFilesystemNode *node) { + _displayName = node->_displayName; + _isValid = node->_isValid; + _isDirectory = node->_isDirectory; + _path = node->_path; +} + +FSList *POSIXFilesystemNode::listDir() const { + assert(_isDirectory); + DIR *dirp = opendir(_path.c_str()); + assert(dirp != 0); + + struct dirent *dp; + FSList *myList = new FSList(); + + // ... loop over dir entries using readdir + while ((dp = readdir(dirp)) != NULL) { + POSIXFilesystemNode entry; + entry._displayName = dp->d_name; + entry._isDirectory = (dp->d_type == DT_DIR); // TODO - add support for symlinks to dirs? + + entry._path = _path; + entry._path += dp->d_name; + if (entry._isDirectory) + entry._path += "/"; + myList->push_back(entry); + } + closedir(dirp); + return myList; +} + +FilesystemNode *POSIXFilesystemNode::parent() const { + // TODO !!! + return 0; +} + +#endif // defined(UNIX) -- cgit v1.2.3