From a5df4fba77214a93442a0a9e9607cd0a523f24a8 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 28 Jun 2004 22:34:22 +0000 Subject: Added simple ref-counting to the File class svn-id: r14106 --- common/file.cpp | 31 +++++++++++++++++++++++++++---- common/file.h | 18 +++++++++++++++--- scumm/smush/chunk.cpp | 32 ++++---------------------------- scumm/smush/chunk.h | 6 +++--- 4 files changed, 49 insertions(+), 38 deletions(-) diff --git a/common/file.cpp b/common/file.cpp index 4043143fd4..411e1d64a8 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -104,16 +104,35 @@ void File::resetDefaultDirectories() { _defaultDirectories.clear(); } -File::File() { - _handle = NULL; - _ioFailed = false; - _name = 0; +File::File() + : _handle(0), _ioFailed(false), _refcount(1), _name(0) { } +//#define DEBUG_FILE_REFCOUNT + File::~File() { +#ifdef DEBUG_FILE_REFCOUNT + warning("File::~File on file '%s'", _name); +#endif close(); delete [] _name; } +void File::incRef() { +#ifdef DEBUG_FILE_REFCOUNT + warning("File::incRef on file '%s'", _name); +#endif + _refcount++; +} + +void File::decRef() { +#ifdef DEBUG_FILE_REFCOUNT + warning("File::decRef on file '%s'", _name); +#endif + if (--_refcount == 0) { + delete this; + } +} + bool File::open(const char *filename, AccessMode mode, const char *directory) { if (_handle) { @@ -156,6 +175,10 @@ bool File::open(const char *filename, AccessMode mode, const char *directory) { _name = new char[len+1]; memcpy(_name, filename, len+1); +#ifdef DEBUG_FILE_REFCOUNT + warning("File::open on file '%s'", _name); +#endif + return true; } diff --git a/common/file.h b/common/file.h index d2bc217dbb..ad2c5d5980 100644 --- a/common/file.h +++ b/common/file.h @@ -29,10 +29,18 @@ class File : public Common::ReadStream, public Common::WriteStream { private: - - FILE * _handle; + /** POSIX file handle to the actual file; 0 if no file is open. */ + FILE *_handle; + + /** Status flag which tells about recent I/O failures. */ bool _ioFailed; - char *_name; // For debugging + + /** Simple ref-counter for File objects. */ + int32 _refcount; + + /** The name of this file, for debugging. */ + char *_name; + static FILE *fopenNoCase(const char *filename, const char *directory, const char *mode); @@ -49,6 +57,10 @@ public: File(); virtual ~File(); + + void incRef(); + void decRef(); + bool open(const char *filename, AccessMode mode = kFileReadMode, const char *directory = NULL); void close(); bool isOpen() const; diff --git a/scumm/smush/chunk.cpp b/scumm/smush/chunk.cpp index 04a57557f2..8df68c1b15 100644 --- a/scumm/smush/chunk.cpp +++ b/scumm/smush/chunk.cpp @@ -28,33 +28,6 @@ namespace Scumm { -class FilePtr : public File { - Common::String _filename; - int32 _refcount; -public: - FilePtr(const char *fname) : - _filename(fname), - _refcount(1) { - debug(9, "FilePtr created for %s", fname); - open(fname); - if (isOpen() == false) - error("FilePtr unable to read file %s", fname); - } - - ~FilePtr() { - debug(9, "FilePtr destroyed for %s", _filename.c_str()); - } - - void incRef() { - _refcount++; - } - - void decRef() { - if (--_refcount == 0) - delete this; - } -}; - const char *Chunk::ChunkString(Chunk::type t) { static char data[5]; data[0] = (char)((t >> 24) & 0xFF); @@ -116,7 +89,10 @@ FileChunk::FileChunk() : } FileChunk::FileChunk(const char *fname) { - _data = new FilePtr(fname); + _data = new File(); + if (!_data->open(fname)) + error("FileChunk: Unable to open file %s", fname); + _type = _data->readUint32BE(); _size = _data->readUint32BE(); _offset = sizeof(Chunk::type) + sizeof(uint32); diff --git a/scumm/smush/chunk.h b/scumm/smush/chunk.h index b37eb806c2..3419d536a3 100644 --- a/scumm/smush/chunk.h +++ b/scumm/smush/chunk.h @@ -24,6 +24,8 @@ #include "common/scummsys.h" +class File; + namespace Scumm { class Chunk { @@ -46,8 +48,6 @@ public: virtual uint32 getDword()= 0; }; -class FilePtr; - // Common functionality for concrete chunks (FileChunk, MemoryChunk) class BaseChunk : public Chunk { protected: @@ -67,7 +67,7 @@ public: class FileChunk : public BaseChunk { private: - FilePtr *_data; + File *_data; uint32 _offset; protected: -- cgit v1.2.3