1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #define FORBIDDEN_SYMBOL_ALLOW_ALL
24
25 #include "dc.h"
26 #include "backends/fs/abstract-fs.h"
27 #include "backends/fs/stdiostream.h"
28
29 #include <ronin/cdfs.h>
30 #include <stdio.h>
31 #define usleep usleep_unistd
32 #include <unistd.h>
33 #undef usleep
34
35 /**
36 * Implementation of the ScummVM file system API based on Ronin.
37 *
38 * Parts of this class are documented in the base interface class, AbstractFSNode.
39 */
40 class RoninCDFileNode : public AbstractFSNode {
41 protected:
42 Common::String _path;
43
44 public:
RoninCDFileNode(const Common::String & path)45 RoninCDFileNode(const Common::String &path) : _path(path) {}
46
exists() const47 virtual bool exists() const override { return true; }
getName() const48 virtual Common::String getName() const override { return lastPathComponent(_path, '/'); }
getDisplayName() const49 virtual Common::U32String getDisplayName() const override { return getName(); }
getPath() const50 virtual Common::String getPath() const override { return _path; }
isDirectory() const51 virtual bool isDirectory() const override { return false; }
isReadable() const52 virtual bool isReadable() const override { return true; }
isWritable() const53 virtual bool isWritable() const override { return false; }
54
getChild(const Common::String & n) const55 virtual AbstractFSNode *getChild(const Common::String &n) const override { return NULL; }
getChildren(AbstractFSList & list,ListMode mode,bool hidden) const56 virtual bool getChildren(AbstractFSList &list, ListMode mode, bool hidden) const override { return false; }
57 virtual AbstractFSNode *getParent() const override;
58
59 virtual Common::SeekableReadStream *createReadStream() override;
createWriteStream()60 virtual Common::SeekableWriteStream *createWriteStream() override { return 0; }
createDirectory()61 virtual bool createDirectory() override { return false; }
62
63 static AbstractFSNode *makeFileNodePath(const Common::String &path);
64 };
65
66 /* A directory */
67 class RoninCDDirectoryNode final : public RoninCDFileNode {
68 public:
RoninCDDirectoryNode(const Common::String & path)69 RoninCDDirectoryNode(const Common::String &path) : RoninCDFileNode(path) {}
70
isDirectory() const71 virtual bool isDirectory() const override { return true; }
72 virtual AbstractFSNode *getChild(const Common::String &n) const override;
73 virtual bool getChildren(AbstractFSList &list, ListMode mode, bool hidden) const override;
createReadStream()74 virtual Common::SeekableReadStream *createReadStream() override { return 0; }
createDirectory()75 virtual bool createDirectory() override { return true; }
76 };
77
78 /* A file/directory which does not exist */
79 class RoninCDNonexistingNode final : public RoninCDFileNode {
80 public:
RoninCDNonexistingNode(const Common::String & path)81 RoninCDNonexistingNode(const Common::String &path) : RoninCDFileNode(path) {}
82
exists() const83 virtual bool exists() const override { return false; }
isReadable() const84 virtual bool isReadable() const override { return false; }
createReadStream()85 virtual Common::SeekableReadStream *createReadStream() override { return 0; }
86 };
87
makeFileNodePath(const Common::String & path)88 AbstractFSNode *RoninCDFileNode::makeFileNodePath(const Common::String &path) {
89 assert(path.size() > 0);
90
91 int fd;
92
93 if ((fd = open(path.c_str(), O_RDONLY)) >= 0) {
94 close(fd);
95 return new RoninCDFileNode(path);
96 } else if ((fd = open(path.c_str(), O_DIR|O_RDONLY)) >= 0) {
97 close(fd);
98 return new RoninCDDirectoryNode(path);
99 } else {
100 return NULL;
101 }
102 }
103
getChild(const Common::String & n) const104 AbstractFSNode *RoninCDDirectoryNode::getChild(const Common::String &n) const {
105 Common::String newPath(_path);
106 if (_path.lastChar() != '/')
107 newPath += '/';
108 newPath += n;
109
110 return makeFileNodePath(newPath);
111 }
112
getChildren(AbstractFSList & myList,ListMode mode,bool hidden) const113 bool RoninCDDirectoryNode::getChildren(AbstractFSList &myList, ListMode mode, bool hidden) const {
114
115 DIR *dirp = opendir(_path.c_str());
116 struct dirent *dp;
117
118 if (dirp == NULL)
119 return false;
120
121 // ... loop over dir entries using readdir
122 while ((dp = readdir(dirp)) != NULL) {
123 Common::String newPath(_path);
124 if (newPath.lastChar() != '/')
125 newPath += '/';
126 newPath += dp->d_name;
127
128 if (dp->d_size < 0) {
129 // Honor the chosen mode
130 if (mode == Common::FSNode::kListFilesOnly)
131 continue;
132
133 myList.push_back(new RoninCDDirectoryNode(newPath));
134 } else {
135 // Honor the chosen mode
136 if (mode == Common::FSNode::kListDirectoriesOnly)
137 continue;
138
139 myList.push_back(new RoninCDFileNode(newPath));
140 }
141 }
142 closedir(dirp);
143
144 return true;
145 }
146
getParent() const147 AbstractFSNode *RoninCDFileNode::getParent() const {
148 if (_path == "/")
149 return 0;
150
151 const char *start = _path.c_str();
152 const char *end = lastPathComponent(_path, '/');
153
154 return new RoninCDDirectoryNode(Common::String(start, end - start));
155 }
156
157
createReadStream()158 Common::SeekableReadStream *RoninCDFileNode::createReadStream() {
159 return StdioStream::makeFromPath(getPath().c_str(), false);
160 }
161
makeRootFileNode() const162 AbstractFSNode *OSystem_Dreamcast::makeRootFileNode() const {
163 return new RoninCDDirectoryNode("/");
164 }
165
makeCurrentDirectoryFileNode() const166 AbstractFSNode *OSystem_Dreamcast::makeCurrentDirectoryFileNode() const {
167 return makeRootFileNode();
168 }
169
makeFileNodePath(const Common::String & path) const170 AbstractFSNode *OSystem_Dreamcast::makeFileNodePath(const Common::String &path) const {
171 AbstractFSNode *node = RoninCDFileNode::makeFileNodePath(path);
172 return (node ? node : new RoninCDNonexistingNode(path));
173 }
174