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 bool FSNode::createDirectory() const {
00156     if (_realNode == nullptr)
00157         return false;
00158 
00159     if (_realNode->exists()) {
00160         if (_realNode->isDirectory()) {
00161             warning("FSNode::createDirectory: '%s' already exists", getName().c_str());
00162         } else {
00163             warning("FSNode::createDirectory: '%s' is a file", getName().c_str());
00164         }
00165         return false;
00166     }
00167 
00168     return _realNode->createDirectory();
00169 }
00170 
00171 FSDirectory::FSDirectory(const FSNode &node, int depth, bool flat, bool ignoreClashes)
00172   : _node(node), _cached(false), _depth(depth), _flat(flat), _ignoreClashes(ignoreClashes) {
00173 }
00174 
00175 FSDirectory::FSDirectory(const String &prefix, const FSNode &node, int depth, bool flat,
00176                          bool ignoreClashes)
00177   : _node(node), _cached(false), _depth(depth), _flat(flat), _ignoreClashes(ignoreClashes) {
00178 
00179     setPrefix(prefix);
00180 }
00181 
00182 FSDirectory::FSDirectory(const String &name, int depth, bool flat, bool ignoreClashes)
00183   : _node(name), _cached(false), _depth(depth), _flat(flat), _ignoreClashes(ignoreClashes) {
00184 }
00185 
00186 FSDirectory::FSDirectory(const String &prefix, const String &name, int depth, bool flat,
00187                          bool ignoreClashes)
00188   : _node(name), _cached(false), _depth(depth), _flat(flat), _ignoreClashes(ignoreClashes) {
00189 
00190     setPrefix(prefix);
00191 }
00192 
00193 FSDirectory::~FSDirectory() {
00194 }
00195 
00196 void FSDirectory::setPrefix(const String &prefix) {
00197     _prefix = prefix;
00198 
00199     if (!_prefix.empty() && !_prefix.hasSuffix("/"))
00200         _prefix += "/";
00201 }
00202 
00203 FSNode FSDirectory::getFSNode() const {
00204     return _node;
00205 }
00206 
00207 FSNode *FSDirectory::lookupCache(NodeCache &cache, const String &name) const {
00208     // make caching as lazy as possible
00209     if (!name.empty()) {
00210         ensureCached();
00211 
00212         if (cache.contains(name))
00213             return &cache[name];
00214     }
00215 
00216     return nullptr;
00217 }
00218 
00219 bool FSDirectory::hasFile(const String &name) const {
00220     if (name.empty() || !_node.isDirectory())
00221         return false;
00222 
00223     FSNode *node = lookupCache(_fileCache, name);
00224     return node && node->exists();
00225 }
00226 
00227 const ArchiveMemberPtr FSDirectory::getMember(const String &name) const {
00228     if (name.empty() || !_node.isDirectory())
00229         return ArchiveMemberPtr();
00230 
00231     FSNode *node = lookupCache(_fileCache, name);
00232 
00233     if (!node || !node->exists()) {
00234         warning("FSDirectory::getMember: '%s' does not exist", name.c_str());
00235         return ArchiveMemberPtr();
00236     } else if (node->isDirectory()) {
00237         warning("FSDirectory::getMember: '%s' is a directory", name.c_str());
00238         return ArchiveMemberPtr();
00239     }
00240 
00241     return ArchiveMemberPtr(new FSNode(*node));
00242 }
00243 
00244 SeekableReadStream *FSDirectory::createReadStreamForMember(const String &name) const {
00245     if (name.empty() || !_node.isDirectory())
00246         return nullptr;
00247 
00248     FSNode *node = lookupCache(_fileCache, name);
00249     if (!node)
00250         return nullptr;
00251     SeekableReadStream *stream = node->createReadStream();
00252     if (!stream)
00253         warning("FSDirectory::createReadStreamForMember: Can't create stream for file '%s'", name.c_str());
00254 
00255     return stream;
00256 }
00257 
00258 FSDirectory *FSDirectory::getSubDirectory(const String &name, int depth, bool flat, bool ignoreClashes) {
00259     return getSubDirectory(String(), name, depth, flat, ignoreClashes);
00260 }
00261 
00262 FSDirectory *FSDirectory::getSubDirectory(const String &prefix, const String &name, int depth,
00263         bool flat, bool ignoreClashes) {
00264     if (name.empty() || !_node.isDirectory())
00265         return nullptr;
00266 
00267     FSNode *node = lookupCache(_subDirCache, name);
00268     if (!node)
00269         return nullptr;
00270 
00271     return new FSDirectory(prefix, *node, depth, flat, ignoreClashes);
00272 }
00273 
00274 void FSDirectory::cacheDirectoryRecursive(FSNode node, int depth, const String& prefix) const {
00275     if (depth <= 0)
00276         return;
00277 
00278     FSList list;
00279     node.getChildren(list, FSNode::kListAll);
00280 
00281     FSList::iterator it = list.begin();
00282     for ( ; it != list.end(); ++it) {
00283         String name = prefix + it->getName();
00284 
00285         // don't touch name as it might be used for warning messages
00286         String lowercaseName = name;
00287         lowercaseName.toLowercase();
00288 
00289         // since the hashmap is case insensitive, we need to check for clashes when caching
00290         if (it->isDirectory()) {
00291             if (!_flat && _subDirCache.contains(lowercaseName)) {
00292                 // Always warn in this case as it's when there are 2 directories at the same place with different case
00293                 // That means a problem in user installation as lookups are always done case insensitive
00294                 warning("FSDirectory::cacheDirectory: name clash when building cache, ignoring sub-directory '%s'",
00295                         name.c_str());
00296             } else {
00297                 if (_subDirCache.contains(lowercaseName)) {
00298                     if (!_ignoreClashes) {
00299                         warning("FSDirectory::cacheDirectory: name clash when building subDirCache with subdirectory '%s'",
00300                                 name.c_str());
00301                     }
00302                 }
00303                 cacheDirectoryRecursive(*it, depth - 1, _flat ? prefix : lowercaseName + "/");
00304                 _subDirCache[lowercaseName] = *it;
00305             }
00306         } else {
00307             if (_fileCache.contains(lowercaseName)) {
00308                 if (!_ignoreClashes) {
00309                     warning("FSDirectory::cacheDirectory: name clash when building cache, ignoring file '%s'",
00310                             name.c_str());
00311                 }
00312             } else {
00313                 _fileCache[lowercaseName] = *it;
00314             }
00315         }
00316     }
00317 
00318 }
00319 
00320 void FSDirectory::ensureCached() const  {
00321     if (_cached)
00322         return;
00323     cacheDirectoryRecursive(_node, _depth, _prefix);
00324     _cached = true;
00325 }
00326 
00327 int FSDirectory::listMatchingMembers(ArchiveMemberList &list, const String &pattern) const {
00328     if (!_node.isDirectory())
00329         return 0;
00330 
00331     // Cache dir data
00332     ensureCached();
00333 
00334     // need to match lowercase key, since all entries in our file cache are
00335     // stored as lowercase.
00336     String lowercasePattern(pattern);
00337     lowercasePattern.toLowercase();
00338 
00339     int matches = 0;
00340     NodeCache::const_iterator it = _fileCache.begin();
00341     for ( ; it != _fileCache.end(); ++it) {
00342         if (it->_key.matchString(lowercasePattern, false, true)) {
00343             list.push_back(ArchiveMemberPtr(new FSNode(it->_value)));
00344             matches++;
00345         }
00346     }
00347     return matches;
00348 }
00349 
00350 int FSDirectory::listMembers(ArchiveMemberList &list) const {
00351     if (!_node.isDirectory())
00352         return 0;
00353 
00354     // Cache dir data
00355     ensureCached();
00356 
00357     int files = 0;
00358     for (NodeCache::const_iterator it = _fileCache.begin(); it != _fileCache.end(); ++it) {
00359         list.push_back(ArchiveMemberPtr(new FSNode(it->_value)));
00360         ++files;
00361     }
00362 
00363     return files;
00364 }
00365 
00366 
00367 } // End of namespace Common


Generated on Sat May 30 2020 05:00:37 for ResidualVM by doxygen 1.7.1
curved edge   curved edge