diff options
author | Max Horn | 2004-11-27 15:09:53 +0000 |
---|---|---|
committer | Max Horn | 2004-11-27 15:09:53 +0000 |
commit | b78ac6a18b2cb851718ef84b04557f7fba8d399a (patch) | |
tree | 64e637229fef01b43835875a76444eea4ffd1a7f | |
parent | 8d0ab890f8597c90d8f51caf1ed416e1df60848a (diff) | |
download | scummvm-rg350-b78ac6a18b2cb851718ef84b04557f7fba8d399a.tar.gz scummvm-rg350-b78ac6a18b2cb851718ef84b04557f7fba8d399a.tar.bz2 scummvm-rg350-b78ac6a18b2cb851718ef84b04557f7fba8d399a.zip |
Make use of our String class instead of juggling with char pointers; added File::exists method
svn-id: r15913
-rw-r--r-- | common/file.cpp | 28 | ||||
-rw-r--r-- | common/file.h | 8 | ||||
-rw-r--r-- | sky/disk.cpp | 10 | ||||
-rw-r--r-- | sword2/resman.cpp | 5 |
4 files changed, 23 insertions, 28 deletions
diff --git a/common/file.cpp b/common/file.cpp index d943c3af2a..34e19d8841 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 7a1a35bfea..ceee760afd 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 976b2ca9d4..2fd4917048 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 76f3799de0..16ea8c0d3e 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 |