diff --git a/common/file.cpp b/common/file.cpp index d943c3af2a5..34e19d8841b 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -26,7 +26,7 @@ Common::StringList File::_defaultDirectories; -FILE *File::fopenNoCase(const char *filename, const char *directory, const char *mode) { +static FILE *fopenNoCase(const char *filename, const char *directory, const char *mode) { FILE *file; char buf[512]; char *ptr; @@ -104,28 +104,27 @@ void File::resetDefaultDirectories() { } File::File() - : _handle(0), _ioFailed(false), _refcount(1), _name(0) { + : _handle(0), _ioFailed(false), _refcount(1) { } //#define DEBUG_FILE_REFCOUNT File::~File() { #ifdef DEBUG_FILE_REFCOUNT - warning("File::~File on file '%s'", _name); + warning("File::~File on file '%s'", _name.c_str()); #endif close(); - delete [] _name; } void File::incRef() { #ifdef DEBUG_FILE_REFCOUNT - warning("File::incRef on file '%s'", _name); + warning("File::incRef on file '%s'", _name.c_str()); #endif _refcount++; } void File::decRef() { #ifdef DEBUG_FILE_REFCOUNT - warning("File::decRef on file '%s'", _name); + warning("File::decRef on file '%s'", _name.c_str()); #endif if (--_refcount == 0) { delete this; @@ -137,7 +136,7 @@ bool File::open(const char *filename, AccessMode mode, const char *directory) { assert(mode == kFileReadMode || mode == kFileWriteMode); if (_handle) { - error("File::open: This file object already is opened (%s), won't open '%s'", _name, filename); + error("File::open: This file object already is opened (%s), won't open '%s'", _name.c_str(), filename); } if (filename == NULL || *filename == 0) { @@ -168,19 +167,22 @@ bool File::open(const char *filename, AccessMode mode, const char *directory) { return false; } - int len = strlen(filename); - if (_name != 0) - delete [] _name; - _name = new char[len+1]; - memcpy(_name, filename, len+1); + + _name = filename; #ifdef DEBUG_FILE_REFCOUNT - warning("File::open on file '%s'", _name); + warning("File::open on file '%s'", _name.c_str()); #endif return true; } +bool File::exists(const char *filename, const char *directory) { + // FIXME: Ugly ugly hack! + File tmp; + return tmp.open(filename, kFileReadMode, directory); +} + void File::close() { if (_handle) fclose(_handle); diff --git a/common/file.h b/common/file.h index 7a1a35bfeae..ceee760afdd 100644 --- a/common/file.h +++ b/common/file.h @@ -39,11 +39,9 @@ protected: int32 _refcount; /** The name of this file, for debugging. */ - char *_name; + Common::String _name; - static FILE *fopenNoCase(const char *filename, const char *directory, const char *mode); - static Common::StringList _defaultDirectories; public: @@ -62,6 +60,8 @@ public: void decRef(); virtual bool open(const char *filename, AccessMode mode = kFileReadMode, const char *directory = NULL); + virtual bool exists(const char *filename, const char *directory = NULL); + virtual void close(); bool isOpen() const; bool ioFailed() const; @@ -69,7 +69,7 @@ public: virtual bool eof(); virtual uint32 pos(); virtual uint32 size(); - const char *name() const { return _name; } + const char *name() const { return _name.c_str(); } virtual void seek(int32 offs, int whence = SEEK_SET); uint32 read(void *ptr, uint32 size); uint32 write(const void *ptr, uint32 size); diff --git a/sky/disk.cpp b/sky/disk.cpp index 976b2ca9d4b..2fd4917048a 100644 --- a/sky/disk.cpp +++ b/sky/disk.cpp @@ -384,14 +384,10 @@ void Disk::dumpFile(uint16 fileNr) { filePtr = loadFile(fileNr); sprintf(buf, "dumps/file-%d.dmp", fileNr); - out.open(buf, File::kFileReadMode, ""); - if (out.isOpen() == false) { - out.open(buf, File::kFileWriteMode, ""); - if (out.isOpen() == false) - return; - out.write(filePtr, _lastLoadedFileSize); + if (!out.exists(buf, "")) { + if (out.open(buf, File::kFileWriteMode, "")) + out.write(filePtr, _lastLoadedFileSize); } - out.close(); free(filePtr); } diff --git a/sword2/resman.cpp b/sword2/resman.cpp index 76f3799de07..16ea8c0d3e5 100644 --- a/sword2/resman.cpp +++ b/sword2/resman.cpp @@ -512,13 +512,10 @@ byte *ResourceManager::openResource(uint32 res, bool dump) { sprintf(buf, "dumps/%s-%d.dmp", tag, res); #endif - if (!out.open(buf, File::kFileReadMode, "")) { + if (!out.exists(buf, "")) { if (out.open(buf, File::kFileWriteMode, "")) out.write(_resList[res].ptr, len); } - - if (out.isOpen()) - out.close(); } // close the cluster