aboutsummaryrefslogtreecommitdiff
path: root/backends/fs/fs.h
blob: bc72cccc794462c07f41947eef899f112134c54a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002-2004 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 (MacOS 9 doesn't even have the notion of a "current directory").
 * And if we ever want to support devices with no FS in the classical sense (Palm...),
 * we can build upon this.
 */
 
/* 
 * 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.
 */

#include "common/array.h"
#include "common/str.h"

class FilesystemNode;


/**
 * List of multiple file system nodes. E.g. the contents of a given directory.
 */
class FSList : public Common::Array<FilesystemNode> {
};


/**
 * File system node.
 */
class AbstractFilesystemNode {
protected:
	friend class FilesystemNode;
	typedef Common::String String;

	/**
	 * The parent node of this directory.
	 * The parent of the root is the root itself.
	 */
	virtual AbstractFilesystemNode *parent() const = 0;

	/**
	 * This method is a rather ugly hack which is used internally by the 
	 * 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);

public:

	/**
	 * Flag to tell listDir() which kind of files to list.
	 */
	typedef enum {
		kListFilesOnly = 1,
		kListDirectoriesOnly = 2,
		kListAll = 3
	} ListMode;

	virtual ~AbstractFilesystemNode() {}

	/**
	 * Return display name, used by e.g. the GUI to present the file in the file browser.
	 * @return the display name
	 */
	virtual String displayName() const = 0;

	/**
	 * Is this node valid (i.e. referring 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(ListMode mode = kListDirectoriesOnly) const = 0;

	/**
	 * Compare the name of this node to the name of another.
	 */
	virtual bool operator< (const AbstractFilesystemNode& node) const
	{
		return scumm_stricmp(displayName().c_str(), node.displayName().c_str()) < 0;
	}
};

class FilesystemNode : public AbstractFilesystemNode {
	friend class AbstractFilesystemNode;

	typedef Common::String String;
private:
	AbstractFilesystemNode *_realNode;
	int *_refCount;

	/**
	 * 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();

#ifdef MACOSX
	/*
	 * 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 AbstractFilesystemNode *getNodeForPath(const String &path);
#endif


public:
	FilesystemNode();
	FilesystemNode(const FilesystemNode &node);
#ifdef MACOSX
	FilesystemNode(const String &path);
#endif
	~FilesystemNode();

	FilesystemNode &operator  =(const FilesystemNode &node);

	FilesystemNode getParent() const;


	virtual String displayName() const { return _realNode->displayName(); }
	virtual bool isValid() const { return _realNode->isValid(); }
	virtual bool isDirectory() const { return _realNode->isDirectory(); }
	virtual String path() const { return _realNode->path(); }

	virtual FSList listDir(ListMode mode = kListDirectoriesOnly) const { return _realNode->listDir(mode); }

protected:
	void decRefCount();

	virtual AbstractFilesystemNode *parent() const { return 0; }
};


#endif