From 13e4fc74e0bcaaa2df75ca60f284a57cbf7ccca8 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 30 Apr 2006 23:08:37 +0000 Subject: Add a File::open variant that takes a FilesystemNode as parameter svn-id: r22251 --- common/file.cpp | 36 ++++++++++++++++++++++++++++++++++++ common/file.h | 3 +++ 2 files changed, 39 insertions(+) diff --git a/common/file.cpp b/common/file.cpp index d3f6c94a81..0fa6993148 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -195,6 +195,7 @@ bool File::open(const String &filename, AccessMode mode) { error("File::open: This file object already is opened (%s), won't open '%s'", _name.c_str(), filename.c_str()); } + _name.clear(); clearIOFailed(); String fname(filename); @@ -261,6 +262,40 @@ bool File::open(const String &filename, AccessMode mode) { return true; } +bool File::open(const FilesystemNode &node, AccessMode mode) { + assert(mode == kFileReadMode || mode == kFileWriteMode); + String filename(node.displayName()); + + if (_handle) { + error("File::open: This file object already is opened (%s), won't open '%s'", _name.c_str(), filename.c_str()); + } + + clearIOFailed(); + _name.clear(); + + const char *modeStr = (mode == kFileReadMode) ? "rb" : "wb"; + + + _handle = fopen(node.path().c_str(), modeStr); + + + if (_handle == NULL) { + if (mode == kFileReadMode) + debug(2, "File %s not found", filename.c_str()); + else + debug(2, "File %s not opened", filename.c_str()); + return false; + } + + _name = filename; + +#ifdef DEBUG_FILE_REFCOUNT + warning("File::open on file '%s'", _name.c_str()); +#endif + + return true; +} + bool File::exists(const String &filename) { // First try to find the file it via a FilesystemNode (in case an absolute // path was passed). But we only use this to filter out directories. @@ -296,6 +331,7 @@ void File::close() { if (_handle) fclose(_handle); _handle = NULL; + _name.clear(); } bool File::isOpen() const { diff --git a/common/file.h b/common/file.h index eafb7adba9..76bee32024 100644 --- a/common/file.h +++ b/common/file.h @@ -28,6 +28,8 @@ #include "common/str.h" #include "common/stream.h" +class FilesystemNode; + namespace Common { class File : public SeekableReadStream, public WriteStream { @@ -61,6 +63,7 @@ public: void decRef(); virtual bool open(const String &filename, AccessMode mode = kFileReadMode); + virtual bool open(const FilesystemNode &node, AccessMode mode = kFileReadMode); static bool exists(const String &filename); virtual void close(); -- cgit v1.2.3