ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

fs.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/system.h"
00024 #include "common/textconsole.h"
00025 #include "backends/fs/abstract-fs.h"
00026 #include "backends/fs/fs-factory.h"
00027 
00028 namespace Common {
00029 
00030 FSNode::FSNode() {
00031 }
00032 
00033 FSNode::FSNode(AbstractFSNode *realNode)
00034     : _realNode(realNode) {
00035 }
00036 
00037 FSNode::FSNode(const String &p) {
00038     assert(g_system);
00039     FilesystemFactory *factory = g_system->getFilesystemFactory();
00040     AbstractFSNode *tmp = nullptr;
00041 
00042     if (p.empty() || p == ".")
00043         tmp = factory->makeCurrentDirectoryFileNode();
00044     else
00045         tmp = factory->makeFileNodePath(p);
00046     _realNode = SharedPtr<AbstractFSNode>(tmp);
00047 }
00048 
00049 bool FSNode::operator<(const FSNode& node) const {
00050     // Directories come before files, i.e., are "lower".
00051     if (isDirectory() != node.isDirectory())
00052         return isDirectory();
00053 
00054     // If both nodes are of the same type (two files or two dirs),
00055     // then sort by name, ignoring case.
00056     return getDisplayName().compareToIgnoreCase(node.getDisplayName()) < 0;
00057 }
00058 
00059 bool FSNode::exists() const {
00060     return _realNode && _realNode->exists();
00061 }
00062 
00063 FSNode FSNode::getChild(const String &n) const {
00064     // If this node is invalid or not a directory, return an invalid node
00065     if (_realNode == nullptr || !_realNode->isDirectory())
00066         return FSNode();
00067 
00068     AbstractFSNode *node = _realNode->getChild(n);
00069     return FSNode(node);
00070 }
00071 
00072 bool FSNode::getChildren(FSList &fslist, ListMode mode, bool hidden) const {
00073     if (!_realNode || !_realNode->isDirectory())
00074         return false;
00075 
00076     AbstractFSList tmp;
00077 
00078     if (!_realNode->getChildren(tmp, mode, hidden))
00079         return false;
00080 
00081     fslist.clear();
00082     for (AbstractFSList::iterator i = tmp.begin(); i != tmp.end(); ++i) {
00083         fslist.push_back(FSNode(*i));
00084     }
00085 
00086     return true;
00087 }
00088 
00089 String FSNode::getDisplayName() const {
00090     assert(_realNode);
00091     return _realNode->getDisplayName();
00092 }
00093 
00094 String FSNode::getName() const {
00095     assert(_realNode);
00096     return _realNode->getName();
00097 }
00098 
00099 FSNode FSNode::getParent() const {
00100     if (_realNode == nullptr)
00101         return *this;
00102 
00103     AbstractFSNode *node = _realNode->getParent();
00104     if (node == nullptr) {
00105         return *this;
00106     } else {
00107         return FSNode(node);
00108     }
00109 }
00110 
00111 String FSNode::getPath() const {
00112     assert(_realNode);
00113     return _realNode->getPath();
00114 }
00115 
00116 bool FSNode::isDirectory() const {
00117     return _realNode && _realNode->isDirectory();
00118 }
00119 
00120 bool FSNode::isReadable() const {
00121     return _realNode && _realNode->isReadable();
00122 }
00123 
00124 bool FSNode::isWritable() const {
00125     return _realNode && _realNode->isWritable();
00126 }
00127 
00128 SeekableReadStream *FSNode::createReadStream() const {
00129     if (_realNode == nullptr)
00130         return nullptr;
00131 
00132     if (!_realNode->exists()) {
00133         warning("FSNode::createReadStream: '%s' does not exist", getName().c_str());
00134         return nullptr;
00135     } else if (_realNode->isDirectory()) {
00136         warning("FSNode::createReadStream: '%s' is a directory", getName().c_str());
00137         return nullptr;
00138     }
00139 
00140     return _realNode->createReadStream();
00141 }
00142 
00143 WriteStream *FSNode::createWriteStream() const {
00144     if (_realNode == nullptr)
00145         return nullptr;
00146 
00147     if (_realNode->isDirectory()) {
00148         warning("FSNode::createWriteStream: '%s' is a directory", getName().c_str());
00149         return nullptr;
00150     }
00151 
00152     return _realNode->createWriteStream();
00153 }
00154 
00155 FSDirectory::FSDirectory(const FSNode &node, int depth, bool flat)
00156   : _node(node), _cached(false), _depth(depth), _flat(flat) {
00157 }
00158 
00159 FSDirectory::FSDirectory(const String &prefix, const FSNode &node, int depth, bool flat)
00160   : _node(node), _cached(false), _depth(depth), _flat(flat) {
00161 
00162     setPrefix(prefix);
00163 }
00164 
00165 FSDirectory::FSDirectory(const String &name, int depth, bool flat)
00166   : _node(name), _cached(false), _depth(depth), _flat(flat) {
00167 }
00168 
00169 FSDirectory::FSDirectory(const String &prefix, const String &name, int depth, bool flat)
00170   : _node(name), _cached(false), _depth(depth), _flat(flat) {
00171 
00172     setPrefix(prefix);
00173 }
00174 
00175 FSDirectory::~FSDirectory() {
00176 }
00177 
00178 void FSDirectory::setPrefix(const String &prefix) {
00179     _prefix = prefix;
00180 
00181     if (!_prefix.empty() && !_prefix.hasSuffix("/"))
00182         _prefix += "/";
00183 }
00184 
00185 FSNode FSDirectory::getFSNode() const {
00186     return _node;
00187 }
00188 
00189 FSNode *FSDirectory::lookupCache(NodeCache &cache, const String &name) const {
00190     // make caching as lazy as possible
00191     if (!name.empty()) {
00192         ensureCached();
00193 
00194         if (cache.contains(name))
00195             return &cache[name];
00196     }
00197 
00198     return nullptr;
00199 }
00200 
00201 bool FSDirectory::hasFile(const String &name) const {
00202     if (name.empty() || !_node.isDirectory())
00203         return false;
00204 
00205     FSNode *node = lookupCache(_fileCache, name);
00206     return node && node->exists();
00207 }
00208 
00209 const ArchiveMemberPtr FSDirectory::getMember(const String &name) const {
00210     if (name.empty() || !_node.isDirectory())
00211         return ArchiveMemberPtr();
00212 
00213     FSNode *node = lookupCache(_fileCache, name);
00214 
00215     if (!node || !node->exists()) {
00216         warning("FSDirectory::getMember: '%s' does not exist", name.c_str());
00217         return ArchiveMemberPtr();
00218     } else if (node->isDirectory()) {
00219         warning("FSDirectory::getMember: '%s' is a directory", name.c_str());
00220         return ArchiveMemberPtr();
00221     }
00222 
00223     return ArchiveMemberPtr(new FSNode(*node));
00224 }
00225 
00226 SeekableReadStream *FSDirectory::createReadStreamForMember(const String &name) const {
00227     if (name.empty() || !_node.isDirectory())
00228         return nullptr;
00229 
00230     FSNode *node = lookupCache(_fileCache, name);
00231     if (!node)
00232         return nullptr;
00233     SeekableReadStream *stream = node->createReadStream();
00234     if (!stream)
00235         warning("FSDirectory::createReadStreamForMember: Can't create stream for file '%s'", name.c_str());
00236 
00237     return stream;
00238 }
00239 
00240 FSDirectory *FSDirectory::getSubDirectory(const String &name, int depth, bool flat) {
00241     return getSubDirectory(String(), name, depth, flat);
00242 }
00243 
00244 FSDirectory *FSDirectory::getSubDirectory(const String &prefix, const String &name, int depth, bool flat) {
00245     if (name.empty() || !_node.isDirectory())
00246         return nullptr;
00247 
00248     FSNode *node = lookupCache(_subDirCache, name);
00249     if (!node)
00250         return nullptr;
00251 
00252     return new FSDirectory(prefix, *node, depth, flat);
00253 }
00254 
00255 void FSDirectory::cacheDirectoryRecursive(FSNode node, int depth, const String& prefix) const {
00256     if (depth <= 0)
00257         return;
00258 
00259     FSList list;
00260     node.getChildren(list, FSNode::kListAll);
00261 
00262     FSList::iterator it = list.begin();
00263     for ( ; it != list.end(); ++it) {
00264         String name = prefix + it->getName();
00265 
00266         // don't touch name as it might be used for warning messages
00267         String lowercaseName = name;
00268         lowercaseName.toLowercase();
00269 
00270         // since the hashmap is case insensitive, we need to check for clashes when caching
00271         if (it->isDirectory()) {
00272             if (!_flat && _subDirCache.contains(lowercaseName)) {
00273                 warning("FSDirectory::cacheDirectory: name clash when building cache, ignoring sub-directory '%s'", name.c_str());
00274             } else {
00275                 if (_subDirCache.contains(lowercaseName)) {
00276                     warning("FSDirectory::cacheDirectory: name clash when building subDirCache with subdirectory '%s'", name.c_str());
00277                 }
00278                 cacheDirectoryRecursive(*it, depth - 1, _flat ? prefix : lowercaseName + "/");
00279                 _subDirCache[lowercaseName] = *it;
00280             }
00281         } else {
00282             if (_fileCache.contains(lowercaseName)) {
00283                 warning("FSDirectory::cacheDirectory: name clash when building cache, ignoring file '%s'", name.c_str());
00284             } else {
00285                 _fileCache[lowercaseName] = *it;
00286             }
00287         }
00288     }
00289 
00290 }
00291 
00292 void FSDirectory::ensureCached() const  {
00293     if (_cached)
00294         return;
00295     cacheDirectoryRecursive(_node, _depth, _prefix);
00296     _cached = true;
00297 }
00298 
00299 int FSDirectory::listMatchingMembers(ArchiveMemberList &list, const String &pattern) const {
00300     if (!_node.isDirectory())
00301         return 0;
00302 
00303     // Cache dir data
00304     ensureCached();
00305 
00306     // need to match lowercase key, since all entries in our file cache are
00307     // stored as lowercase.
00308     String lowercasePattern(pattern);
00309     lowercasePattern.toLowercase();
00310 
00311     int matches = 0;
00312     NodeCache::const_iterator it = _fileCache.begin();
00313     for ( ; it != _fileCache.end(); ++it) {
00314         if (it->_key.matchString(lowercasePattern, false, true)) {
00315             list.push_back(ArchiveMemberPtr(new FSNode(it->_value)));
00316             matches++;
00317         }
00318     }
00319     return matches;
00320 }
00321 
00322 int FSDirectory::listMembers(ArchiveMemberList &list) const {
00323     if (!_node.isDirectory())
00324         return 0;
00325 
00326     // Cache dir data
00327     ensureCached();
00328 
00329     int files = 0;
00330     for (NodeCache::const_iterator it = _fileCache.begin(); it != _fileCache.end(); ++it) {
00331         list.push_back(ArchiveMemberPtr(new FSNode(it->_value)));
00332         ++files;
00333     }
00334 
00335     return files;
00336 }
00337 
00338 
00339 } // End of namespace Common


Generated on Sat Feb 16 2019 05:00:51 for ResidualVM by doxygen 1.7.1
curved edge   curved edge