Make use of our String class instead of juggling with char pointers; added File::exists method

svn-id: r15913
This commit is contained in:
Max Horn 2004-11-27 15:09:53 +00:00
parent 8d0ab890f8
commit b78ac6a18b
4 changed files with 23 additions and 28 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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