diff options
author | David Corrales | 2007-10-16 20:03:23 +0000 |
---|---|---|
committer | David Corrales | 2007-10-16 20:03:23 +0000 |
commit | 6bf925cb6ec7a1cec547cb0eaf4facd52c4a5b0f (patch) | |
tree | a2368801a5f8a0cf59eddef678a6ef0748919ad3 /common | |
parent | 485b70b2b004d2683e31920b8f33053b46fb811d (diff) | |
download | scummvm-rg350-6bf925cb6ec7a1cec547cb0eaf4facd52c4a5b0f.tar.gz scummvm-rg350-6bf925cb6ec7a1cec547cb0eaf4facd52c4a5b0f.tar.bz2 scummvm-rg350-6bf925cb6ec7a1cec547cb0eaf4facd52c4a5b0f.zip |
Make the exists() method take into account default directories. This makes the method more robust, since previously it checked absolute paths only.
svn-id: r29227
Diffstat (limited to 'common')
-rw-r--r-- | common/file.cpp | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/common/file.cpp b/common/file.cpp index eeecb2f7d2..d20b90d571 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -447,43 +447,37 @@ bool File::removeFile(const FilesystemNode &node){ } bool File::exists(const String &filename) { + FilesystemNode* file; + String fname = filename; + // First try to find the file via a FilesystemNode (in case an absolute - // path was passed). But we only use this to filter out directories. - FilesystemNode file(filename); + // path was passed). This is only used to filter out directories. + file = new FilesystemNode(fname); + if (file->exists()) + return !file->isDirectory(); - // FIXME: Since (as stated in the comment above) the FilesystemNode - // creation just works for absolute paths and we use this to tell if - // a file exists in any of the setup paths, we cannot use - // return (!file.isDirectory() && file.exists()); - // - // I.e.: - // FilesystemNode("foofile"); would fail for most (even all?) - // implementations where the file 'foofile' does not exist in the CWD, - // so we can not rely on FilesystemNode::exists, which would return false. - if (file.exists()) - return !file.isDirectory(); + // See if the file is already mapped + if (_filesMap && _filesMap->contains(fname)) { + fname = (*_filesMap)[fname]; + file = new FilesystemNode(fname); + + if (file->exists()) + return !file->isDirectory(); + } - //***DEPRECATED COMMENTS BELOW, LEFT FOR DISCUSSION*** - // Next, try to locate the file by *opening* it in read mode. This has - // multiple effects: - // 1) It takes _filesMap and _defaultDirectories into consideration -> good - // 2) It returns true if and only if File::open is possible on the file -> good - // 3) If this method is misused, it could lead to an fopen call on a directory - // -> bad! - // 4) It also checks whether we can read the file. This is not 100% - // desirable; after all, even when we can't read it, the file is present. - // Since this method is often used to check whether a file should be - // re-created, that's not nice. - // - // TODO/FIXME: We should clarify the semantics of this method, and then - // maybe should introduce several new methods: - // fileExistsAndReadable - // fileExists - // fileExistsAtPath - // dirExists - // dirExistsAtPath - // or maybe only 1-2 methods which take some params :-). + // Try all default directories + if (_defaultDirectories) { + StringIntMap::const_iterator i(_defaultDirectories->begin()); + for (; i != _defaultDirectories->end(); ++i) { + fname = i->_key+fname; + file = new FilesystemNode(fname); + + if(file->exists()) + return !file->isDirectory(); + } + } + //Try opening the file inside the local directory as a last resort File tmp; return tmp.open(filename, kFileReadMode); } |