2002-11-13 17:16:18 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2002-2006 The ScummVM project
|
2002-11-13 17:16:18 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-11-13 17:16:18 +00:00
|
|
|
*
|
2006-02-11 12:47:47 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-11-13 17:16:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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.
|
|
|
|
*
|
2004-12-10 00:11:52 +00:00
|
|
|
* NOTE: Backends still have to provide a way to extract a path from a FSIntern
|
2002-11-13 17:16:18 +00:00
|
|
|
*
|
|
|
|
* 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
|
2004-05-06 09:20:21 +00:00
|
|
|
* paths (MacOS 9 doesn't even have the notion of a "current directory").
|
2002-11-13 17:16:18 +00:00
|
|
|
* And if we ever want to support devices with no FS in the classical sense (Palm...),
|
|
|
|
* we can build upon this.
|
|
|
|
*/
|
2005-07-30 21:11:48 +00:00
|
|
|
|
|
|
|
/*
|
2002-11-14 13:51:59 +00:00
|
|
|
* TODO - Instead of starting with getRoot(), we should rather add a getDefaultDir()
|
|
|
|
* call that on Unix might return the current dir or the users home dir...
|
|
|
|
* i.e. the root dir is usually not the best starting point for browsing.
|
|
|
|
*/
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2004-04-09 15:10:23 +00:00
|
|
|
#include "common/array.h"
|
2002-11-13 17:16:18 +00:00
|
|
|
#include "common/str.h"
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
class FilesystemNode;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of multiple file system nodes. E.g. the contents of a given directory.
|
2006-04-04 23:55:47 +00:00
|
|
|
* This is subclass instead of just a typedef so that we can use forward
|
|
|
|
* declarations of it in other places.
|
2004-11-20 21:35:49 +00:00
|
|
|
*/
|
2006-04-04 23:55:47 +00:00
|
|
|
class FSList : public Common::Array<FilesystemNode> {};
|
2004-11-20 21:35:49 +00:00
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2003-11-06 22:57:33 +00:00
|
|
|
/**
|
|
|
|
* File system node.
|
2002-11-13 17:16:18 +00:00
|
|
|
*/
|
2004-11-20 21:35:49 +00:00
|
|
|
class AbstractFilesystemNode {
|
2002-11-13 17:16:18 +00:00
|
|
|
protected:
|
2004-11-20 21:35:49 +00:00
|
|
|
friend class FilesystemNode;
|
2003-10-02 17:43:02 +00:00
|
|
|
typedef Common::String String;
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
/**
|
|
|
|
* The parent node of this directory.
|
|
|
|
* The parent of the root is the root itself.
|
|
|
|
*/
|
|
|
|
virtual AbstractFilesystemNode *parent() const = 0;
|
|
|
|
|
2006-04-30 22:52:10 +00:00
|
|
|
/**
|
|
|
|
* The child node with the given name. If no child with this name
|
|
|
|
* exists, returns 0. Will never be called on a node which is not
|
|
|
|
* a directory node.
|
|
|
|
*/
|
|
|
|
virtual AbstractFilesystemNode *child(const String &name) const = 0;
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
/**
|
2005-07-30 21:11:48 +00:00
|
|
|
* This method is a rather ugly hack which is used internally by the
|
2004-11-20 21:35:49 +00:00
|
|
|
* actual node implementions to wrap up raw nodes inside FilesystemNode
|
|
|
|
* objects. We probably want to get rid of this eventually and replace it
|
|
|
|
* with a cleaner / more elegant solution, but for now it works.
|
|
|
|
* @note This takes over ownership of node. Do not delete it yourself,
|
|
|
|
* else you'll get ugly crashes. You've been warned!
|
|
|
|
*/
|
|
|
|
static FilesystemNode wrap(AbstractFilesystemNode *node);
|
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
public:
|
2002-11-19 01:36:47 +00:00
|
|
|
|
2003-11-06 22:57:33 +00:00
|
|
|
/**
|
2002-11-19 01:36:47 +00:00
|
|
|
* Flag to tell listDir() which kind of files to list.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
kListFilesOnly = 1,
|
|
|
|
kListDirectoriesOnly = 2,
|
2006-04-27 23:14:54 +00:00
|
|
|
kListAll = 3
|
2002-11-19 01:36:47 +00:00
|
|
|
} ListMode;
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
virtual ~AbstractFilesystemNode() {}
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2003-11-06 22:57:33 +00:00
|
|
|
/**
|
2006-04-30 22:52:10 +00:00
|
|
|
* Return a human readable string for this node, usable for display (e.g.
|
|
|
|
* in the GUI code). Do *not* rely on it being usable for anything else,
|
|
|
|
* like constructing paths!
|
2003-11-06 22:57:33 +00:00
|
|
|
* @return the display name
|
2002-11-13 17:16:18 +00:00
|
|
|
*/
|
|
|
|
virtual String displayName() const = 0;
|
|
|
|
|
2003-11-06 22:57:33 +00:00
|
|
|
/**
|
2006-04-30 22:52:10 +00:00
|
|
|
* Is this node valid? Returns true if the file/directory pointed
|
|
|
|
* to by this node exists, false otherwise.
|
|
|
|
*
|
|
|
|
* @todo Maybe rename this to exists() ? Or maybe even distinguish between
|
|
|
|
* the two? E.g. a path may be non-existant but valid, while another might
|
|
|
|
* be completely invalid). But do we ever need to make that distinction?
|
2002-11-13 17:16:18 +00:00
|
|
|
*/
|
|
|
|
virtual bool isValid() const = 0;
|
|
|
|
|
2003-11-06 22:57:33 +00:00
|
|
|
/**
|
2006-04-30 22:52:10 +00:00
|
|
|
* Is this node pointing to a directory?
|
|
|
|
* @todo Currently we assume that a valid node that is not a directory
|
|
|
|
* automatically is a file (ignoring things like symlinks). That might
|
|
|
|
* actually be OK... but we could still add an isFile method. Or even replace
|
|
|
|
* isValid and isDirectory by a getType() method that can return values like
|
|
|
|
* kDirNodeType, kFileNodeType, kInvalidNodeType.
|
2002-11-13 17:16:18 +00:00
|
|
|
*/
|
|
|
|
virtual bool isDirectory() const = 0;
|
|
|
|
|
2003-11-06 22:57:33 +00:00
|
|
|
/**
|
2006-04-30 22:52:10 +00:00
|
|
|
* Return a string representation of the file which can be passed to fopen(),
|
|
|
|
* and is suitable for archiving (i.e. writing to the config file).
|
|
|
|
* This will usually be a 'path' (hence the name of the method), but can
|
|
|
|
* be anything that fulfilly the above criterions.
|
2002-11-13 17:16:18 +00:00
|
|
|
*/
|
|
|
|
virtual String path() const = 0;
|
|
|
|
|
2003-11-06 22:57:33 +00:00
|
|
|
/**
|
2006-04-30 22:52:10 +00:00
|
|
|
* Return a list of child nodes of this directory node. If called
|
|
|
|
* on a node that does not represent a directory, an error is triggered.
|
|
|
|
* @todo Rename this to listChildren.
|
2002-11-13 17:16:18 +00:00
|
|
|
*/
|
2004-11-20 21:35:49 +00:00
|
|
|
virtual FSList listDir(ListMode mode = kListDirectoriesOnly) const = 0;
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2003-11-06 22:57:33 +00:00
|
|
|
/**
|
2005-04-10 14:33:44 +00:00
|
|
|
* Compare the name of this node to the name of another. Directories
|
|
|
|
* go before normal files.
|
2002-11-21 02:51:50 +00:00
|
|
|
*/
|
2004-11-20 21:35:49 +00:00
|
|
|
virtual bool operator< (const AbstractFilesystemNode& node) const
|
2002-11-21 02:51:50 +00:00
|
|
|
{
|
2005-04-10 14:33:44 +00:00
|
|
|
if (isDirectory() && !node.isDirectory())
|
|
|
|
return true;
|
|
|
|
if (!isDirectory() && node.isDirectory())
|
|
|
|
return false;
|
2004-07-21 14:27:45 +00:00
|
|
|
return scumm_stricmp(displayName().c_str(), node.displayName().c_str()) < 0;
|
2002-11-21 02:51:50 +00:00
|
|
|
}
|
2004-11-21 13:18:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
bool exists();
|
|
|
|
|
|
|
|
bool isDirectory();
|
|
|
|
bool isFile();
|
|
|
|
|
|
|
|
bool isReadable();
|
|
|
|
bool isWriteable();
|
|
|
|
*/
|
2002-11-13 17:16:18 +00:00
|
|
|
};
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
class FilesystemNode : public AbstractFilesystemNode {
|
|
|
|
friend class AbstractFilesystemNode;
|
|
|
|
|
|
|
|
typedef Common::String String;
|
|
|
|
private:
|
|
|
|
AbstractFilesystemNode *_realNode;
|
|
|
|
int *_refCount;
|
|
|
|
|
2006-04-03 21:54:26 +00:00
|
|
|
FilesystemNode(AbstractFilesystemNode *realNode);
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
/**
|
|
|
|
* Returns a special node representing the FS root. The starting point for
|
|
|
|
* any file system browsing.
|
|
|
|
* 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 AbstractFilesystemNode *getRoot();
|
|
|
|
|
2006-05-01 22:04:13 +00:00
|
|
|
/**
|
2004-11-20 21:35:49 +00:00
|
|
|
* 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.
|
2005-02-06 18:37:23 +00:00
|
|
|
*
|
|
|
|
* @TODO: This is of course a place where non-portable code easily will sneak
|
|
|
|
* in, because the format of the path used here is not well-defined.
|
|
|
|
* So we really should reconsider this API and try to come up with
|
|
|
|
* something which is more portable but still flexible enough for our
|
|
|
|
* purposes.
|
2004-11-20 21:35:49 +00:00
|
|
|
*/
|
|
|
|
static AbstractFilesystemNode *getNodeForPath(const String &path);
|
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
|
|
|
|
public:
|
2006-04-30 22:52:10 +00:00
|
|
|
/**
|
|
|
|
* Create a new FilesystemNode refering to the specified path. This is
|
|
|
|
* the counterpart to the path() method.
|
|
|
|
*/
|
|
|
|
FilesystemNode(const String &path);
|
|
|
|
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
FilesystemNode();
|
|
|
|
FilesystemNode(const FilesystemNode &node);
|
|
|
|
~FilesystemNode();
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
FilesystemNode &operator =(const FilesystemNode &node);
|
2004-05-06 10:34:41 +00:00
|
|
|
|
2006-04-30 22:52:10 +00:00
|
|
|
/**
|
|
|
|
* Get the parent node of this node. If this node has no parent node,
|
|
|
|
* then it returns a duplicate of this node.
|
|
|
|
*/
|
2004-11-20 21:35:49 +00:00
|
|
|
FilesystemNode getParent() const;
|
2004-05-06 10:34:41 +00:00
|
|
|
|
2006-04-30 22:52:10 +00:00
|
|
|
/**
|
|
|
|
* Fetch a child node of this node, with the given name. Only valid for
|
|
|
|
* directory nodes (an assertion is triggered otherwise). If no no child
|
|
|
|
* node with the given name exists, an invalid node is returned.
|
|
|
|
*/
|
|
|
|
FilesystemNode getChild(const String &name) const;
|
2004-05-06 10:34:41 +00:00
|
|
|
|
2006-04-30 22:52:10 +00:00
|
|
|
virtual FSList listDir(ListMode mode = kListDirectoriesOnly) const;
|
2006-04-03 21:54:26 +00:00
|
|
|
virtual String displayName() const;
|
|
|
|
virtual bool isValid() const;
|
|
|
|
virtual bool isDirectory() const;
|
|
|
|
virtual String path() const;
|
2003-10-17 12:04:44 +00:00
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
protected:
|
|
|
|
void decRefCount();
|
2003-10-17 12:04:44 +00:00
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
virtual AbstractFilesystemNode *parent() const { return 0; }
|
2006-04-30 22:52:10 +00:00
|
|
|
virtual AbstractFilesystemNode *child(const String &name) const { return 0; }
|
2002-11-13 17:16:18 +00:00
|
|
|
};
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
#endif
|