[Scummvm-cvs-logs] SF.net SVN: scummvm:[34391] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sat Sep 6 22:49:49 CEST 2008


Revision: 34391
          http://scummvm.svn.sourceforge.net/scummvm/?rev=34391&view=rev
Author:   fingolfin
Date:     2008-09-06 20:49:48 +0000 (Sat, 06 Sep 2008)

Log Message:
-----------
Switched class File & DumpFile to use StdioStream internally

Modified Paths:
--------------
    scummvm/trunk/common/file.cpp
    scummvm/trunk/common/file.h
    scummvm/trunk/common/fs.cpp
    scummvm/trunk/common/fs.h
    scummvm/trunk/engines/scumm/file_nes.h

Modified: scummvm/trunk/common/file.cpp
===================================================================
--- scummvm/trunk/common/file.cpp	2008-09-06 20:36:47 UTC (rev 34390)
+++ scummvm/trunk/common/file.cpp	2008-09-06 20:49:48 UTC (rev 34391)
@@ -30,6 +30,8 @@
 #include "common/hash-str.h"
 #include <errno.h>
 
+#include "backends/fs/stdiostream.h"
+
 #if defined(MACOSX) || defined(IPHONE)
 #include "CoreFoundation/CoreFoundation.h"
 #endif
@@ -41,22 +43,6 @@
 	#include "backends/platform/ps2/fileio.h"
 
 	#define fopen(a, b)			ps2_fopen(a, b)
-	#define fclose(a)			ps2_fclose(a)
-	#define fseek(a, b, c)			ps2_fseek(a, b, c)
-	#define ftell(a)			ps2_ftell(a)
-	#define feof(a)				ps2_feof(a)
-	#define fread(a, b, c, d)		ps2_fread(a, b, c, d)
-	#define fwrite(a, b, c, d)		ps2_fwrite(a, b, c, d)
-
-	//#define fprintf				ps2_fprintf	// used in common/util.cpp
-	//#define fflush(a)			ps2_fflush(a)	// used in common/util.cpp
-
-	//#define fgetc(a)			ps2_fgetc(a)	// not used
-	//#define fgets(a, b, c)			ps2_fgets(a, b, c)	// not used
-	//#define fputc(a, b)			ps2_fputc(a, b)	// not used
-	//#define fputs(a, b)			ps2_fputs(a, b)	// not used
-
-	//#define fsize(a)			ps2_fsize(a)	// not used -- and it is not a standard function either
 #endif
 
 #ifdef __DS__
@@ -69,72 +55,20 @@
 
 	// These functions need to be #undef'ed, as their original definition
 	// in devkitarm is done with #includes (ugh!)
-	#undef feof
-	#undef clearerr
-	//#undef getc
-	//#undef ferror
 
 	#include "backends/fs/ds/ds-fs.h"
 
-
-	//void	std_fprintf(FILE* handle, const char* fmt, ...);	// used in common/util.cpp
-	//void	std_fflush(FILE* handle);	// used in common/util.cpp
-
-	//char*	std_fgets(char* str, int size, FILE* file);	// not used
-	//int	std_getc(FILE* handle);	// not used
-	//char*	std_getcwd(char* dir, int dunno);	// not used
-	//void	std_cwd(char* dir);	// not used
-	//int	std_ferror(FILE* handle);	// not used
-
 	// Only functions used in the ScummVM source have been defined here!
 	#define fopen(name, mode)					DS::std_fopen(name, mode)
-	#define fclose(handle)						DS::std_fclose(handle)
-	#define fread(ptr, size, items, file)		DS::std_fread(ptr, size, items, file)
-	#define fwrite(ptr, size, items, file)		DS::std_fwrite(ptr, size, items, file)
-	#define feof(handle)						DS::std_feof(handle)
-	#define ftell(handle)						DS::std_ftell(handle)
-	#define fseek(handle, offset, whence)		DS::std_fseek(handle, offset, whence)
-	#define clearerr(handle)					DS::std_clearerr(handle)
 
-	//#define printf(fmt, ...)					consolePrintf(fmt, ##__VA_ARGS__)
-
-	//#define fprintf(file, fmt, ...)				{ char str[128]; sprintf(str, fmt, ##__VA_ARGS__); DS::std_fwrite(str, strlen(str), 1, file); }
-	//#define fflush(file)						DS::std_fflush(file)	// used in common/util.cpp
-
-	//#define fgets(str, size, file)				DS::std_fgets(str, size, file)	// not used
-	//#define getc(handle)						DS::std_getc(handle)	// not used
-	//#define getcwd(dir, dunno)					DS::std_getcwd(dir, dunno)	// not used
-	#define ferror(handle)						DS::std_ferror(handle)
-
 #endif
 
 #ifdef __SYMBIAN32__
-	#undef feof
-	#undef clearerr
 
 	#define FILE void
 
 	FILE*	symbian_fopen(const char* name, const char* mode);
-	void	symbian_fclose(FILE* handle);
-	size_t	symbian_fread(const void* ptr, size_t size, size_t numItems, FILE* handle);
-	size_t	symbian_fwrite(const void* ptr, size_t size, size_t numItems, FILE* handle);
-	bool	symbian_feof(FILE* handle);
-	long int symbian_ftell(FILE* handle);
-	int		symbian_fseek(FILE* handle, long int offset, int whence);
-	void	symbian_clearerr(FILE* handle);
-	void	symbian_fflush(FILE* handle);
-	int     symbian_ferror(FILE* handle);
-	// Only functions used in the ScummVM source have been defined here!
 	#define fopen(name, mode)					symbian_fopen(name, mode)
-	#define fclose(handle)						symbian_fclose(handle)
-	#define fread(ptr, size, items, file)		symbian_fread(ptr, size, items, file)
-	#define fwrite(ptr, size, items, file)		symbian_fwrite(ptr, size, items, file)
-	#define feof(handle)						symbian_feof(handle)
-	#define ftell(handle)						symbian_ftell(handle)
-	#define fseek(handle, offset, whence)		symbian_fseek(handle, offset, whence)
-	#define clearerr(handle)					symbian_clearerr(handle)
-    	#define fflush(handle)					    symbian_fflush(handle)
-	#define ferror(handle)					    symbian_ferror(handle)
 #endif
 
 namespace Common {
@@ -275,7 +209,7 @@
 }
 
 File::File()
-	: _handle(0), _ioFailed(false) {
+	: _handle(0) {
 }
 
 File::~File() {
@@ -284,6 +218,7 @@
 
 
 bool File::open(const String &filename) {
+	FILE *file = 0;
 	assert(!filename.empty());
 	assert(!_handle);
 
@@ -296,36 +231,36 @@
 	if (_filesMap && _filesMap->contains(fname)) {
 		fname = (*_filesMap)[fname];
 		debug(3, "Opening hashed: %s", fname.c_str());
-		_handle = fopen(fname.c_str(), "rb");
+		file = fopen(fname.c_str(), "rb");
 	} else if (_filesMap && _filesMap->contains(fname + ".")) {
 		// WORKAROUND: Bug #1458388: "SIMON1: Game Detection fails"
 		// sometimes instead of "GAMEPC" we get "GAMEPC." (note trailing dot)
 		fname = (*_filesMap)[fname + "."];
 		debug(3, "Opening hashed: %s", fname.c_str());
-		_handle = fopen(fname.c_str(), "rb");
+		file = fopen(fname.c_str(), "rb");
 	} else {
 
 		if (_defaultDirectories) {
 			// Try all default directories
 			StringIntMap::const_iterator x(_defaultDirectories->begin());
-			for (; _handle == NULL && x != _defaultDirectories->end(); ++x) {
-				_handle = fopenNoCase(filename, x->_key, "rb");
+			for (; file == NULL && x != _defaultDirectories->end(); ++x) {
+				file = fopenNoCase(filename, x->_key, "rb");
 			}
 		}
 
 		// Last resort: try the current directory
-		if (_handle == NULL)
-			_handle = fopenNoCase(filename, "", "rb");
+		if (file == NULL)
+			file = fopenNoCase(filename, "", "rb");
 
 		// Last last (really) resort: try looking inside the application bundle on Mac OS X for the lowercase file.
 #if defined(MACOSX) || defined(IPHONE)
-		if (!_handle) {
+		if (!file) {
 			CFStringRef cfFileName = CFStringCreateWithBytes(NULL, (const UInt8 *)filename.c_str(), filename.size(), kCFStringEncodingASCII, false);
 			CFURLRef fileUrl = CFBundleCopyResourceURL(CFBundleGetMainBundle(), cfFileName, NULL, NULL);
 			if (fileUrl) {
 				UInt8 buf[256];
 				if (CFURLGetFileSystemRepresentation(fileUrl, false, (UInt8 *)buf, 256)) {
-					_handle = fopen((char *)buf, "rb");
+					file = fopen((char *)buf, "rb");
 				}
 				CFRelease(fileUrl);
 			}
@@ -334,6 +269,9 @@
 #endif
 
 	}
+	
+	if (file)
+		_handle = new StdioStream(file);
 
 	if (_handle == NULL)
 		debug(2, "File %s not opened", filename.c_str());
@@ -368,7 +306,7 @@
 	clearIOFailed();
 	_name.clear();
 
-	_handle = fopen(node.getPath().c_str(), "rb");
+	_handle = node.openForReading();
 
 	if (_handle == NULL)
 		debug(2, "File %s not found", filename.c_str());
@@ -410,8 +348,7 @@
 }
 
 void File::close() {
-	if (_handle)
-		fclose((FILE *)_handle);
+	delete _handle;
 	_handle = NULL;
 }
 
@@ -421,59 +358,37 @@
 
 bool File::ioFailed() const {
 	// TODO/FIXME: Just use ferror() here?
-	return _ioFailed != 0;
+	return !_handle || _handle->ioFailed();
 }
 
 void File::clearIOFailed() {
-	// TODO/FIXME: Just use clearerr() here?
-	_ioFailed = false;
+	if (_handle)
+		_handle->clearIOFailed();
 }
 
-bool File::eof() const {
+bool File::eos() const {
 	assert(_handle);
-
-	return feof((FILE *)_handle) != 0;
+	return _handle->eos();
 }
 
 uint32 File::pos() const {
 	assert(_handle);
-
-	return ftell((FILE *)_handle);
+	return _handle->pos();
 }
 
 uint32 File::size() const {
 	assert(_handle);
-
-	uint32 oldPos = ftell((FILE *)_handle);
-	fseek((FILE *)_handle, 0, SEEK_END);
-	uint32 length = ftell((FILE *)_handle);
-	fseek((FILE *)_handle, oldPos, SEEK_SET);
-
-	return length;
+	return _handle->size();
 }
 
 void File::seek(int32 offs, int whence) {
 	assert(_handle);
-
-	if (fseek((FILE *)_handle, offs, whence) != 0)
-		clearerr((FILE *)_handle);
+	_handle->seek(offs, whence);
 }
 
 uint32 File::read(void *ptr, uint32 len) {
-	byte *ptr2 = (byte *)ptr;
-	uint32 real_len;
-
 	assert(_handle);
-
-	if (len == 0)
-		return 0;
-
-	real_len = fread(ptr2, 1, len, (FILE *)_handle);
-	if (real_len < len) {
-		_ioFailed = true;
-	}
-
-	return real_len;
+	return _handle->read(ptr, len);
 }
 
 
@@ -491,7 +406,7 @@
 	String fname(filename);
 	fname.toLowercase();
 	
-	_handle = fopenNoCase(filename, "", "wb");
+	_handle = StdioStream::makeFromPath(filename, true);
 
 	if (_handle == NULL)
 		debug(2, "Failed to open '%s' for writing", filename.c_str());
@@ -513,7 +428,7 @@
 		return false;
 	}*/
 
-	_handle = fopen(node.getPath().c_str(), "wb");
+	_handle = node.openForWriting();
 
 	if (_handle == NULL)
 		debug(2, "File %s not found", node.getName().c_str());
@@ -522,8 +437,7 @@
 }
 
 void DumpFile::close() {
-	if (_handle)
-		fclose((FILE *)_handle);
+	delete _handle;
 	_handle = NULL;
 }
 
@@ -533,33 +447,22 @@
 
 bool DumpFile::ioFailed() const {
 	assert(_handle);
-	return ferror((FILE *)_handle) != 0;
+	return _handle->ioFailed();
 }
 
 void DumpFile::clearIOFailed() {
 	assert(_handle);
-	clearerr((FILE *)_handle);
+	_handle->clearIOFailed();
 }
 
-bool DumpFile::eof() const {
-	assert(_handle);
-	return feof((FILE *)_handle) != 0;
-}
-
 uint32 DumpFile::write(const void *ptr, uint32 len) {
 	assert(_handle);
-
-	if (len == 0)
-		return 0;
-
-	return (uint32)fwrite(ptr, 1, len, (FILE *)_handle);
+	return _handle->write(ptr, len);
 }
 
 void DumpFile::flush() {
 	assert(_handle);
-	// TODO: Should check the return value of fflush, and if it is non-zero,
-	// check errno and set an error flag.
-	fflush((FILE *)_handle);
+	_handle->flush();
 }
 
 }	// End of namespace Common

Modified: scummvm/trunk/common/file.h
===================================================================
--- scummvm/trunk/common/file.h	2008-09-06 20:36:47 UTC (rev 34390)
+++ scummvm/trunk/common/file.h	2008-09-06 20:49:48 UTC (rev 34391)
@@ -35,18 +35,14 @@
 
 class FilesystemNode;
 
-
 /**
  * TODO: vital to document this core class properly!!! For both users and implementors
  */
 class File : public SeekableReadStream, public NonCopyable {
 protected:
 	/** File handle to the actual file; 0 if no file is open. */
-	void *_handle;
+	SeekableReadStream *_handle;
 
-	/** Status flag which tells about recent I/O failures. */
-	bool _ioFailed;
-
 	/** The name of this file, for debugging. */
 	String _name;
 
@@ -94,14 +90,16 @@
 
 	bool ioFailed() const;
 	void clearIOFailed();
-	bool eos() const { return eof(); }
+	bool eos() const;
 
 	/**
 	 * Checks for end of file.
 	 *
+	 * @deprecated: use eos() instead
+	 *
 	 * @return: true if the end of file is reached, false otherwise.
 	 */
-	virtual bool eof() const;
+	virtual bool eof() const { return eos(); }
 
 	virtual uint32 pos() const;
 	virtual uint32 size() const;
@@ -119,7 +117,7 @@
 class DumpFile : public WriteStream, public NonCopyable {
 protected:
 	/** File handle to the actual file; 0 if no file is open. */
-	void *_handle;
+	WriteStream *_handle;
 
 public:
 	DumpFile();
@@ -140,15 +138,7 @@
 
 	bool ioFailed() const;
 	void clearIOFailed();
-	bool eos() const { return eof(); }
 
-	/**
-	 * Checks for end of file.
-	 *
-	 * @return: true if the end of file is reached, false otherwise.
-	 */
-	virtual bool eof() const;
-
 	virtual uint32 write(const void *dataPtr, uint32 dataSize);
 
 	virtual void flush();

Modified: scummvm/trunk/common/fs.cpp
===================================================================
--- scummvm/trunk/common/fs.cpp	2008-09-06 20:36:47 UTC (rev 34390)
+++ scummvm/trunk/common/fs.cpp	2008-09-06 20:49:48 UTC (rev 34391)
@@ -173,7 +173,7 @@
 	return !results.empty();
 }
 
-Common::SeekableReadStream *FilesystemNode::openForReading() {
+Common::SeekableReadStream *FilesystemNode::openForReading() const {
 	if (_realNode == 0)
 		return 0;
 
@@ -188,7 +188,7 @@
 	return _realNode->openForReading();
 }
 
-Common::WriteStream *FilesystemNode::openForWriting() {
+Common::WriteStream *FilesystemNode::openForWriting() const {
 	if (_realNode == 0)
 		return 0;
 

Modified: scummvm/trunk/common/fs.h
===================================================================
--- scummvm/trunk/common/fs.h	2008-09-06 20:36:47 UTC (rev 34390)
+++ scummvm/trunk/common/fs.h	2008-09-06 20:49:48 UTC (rev 34391)
@@ -229,7 +229,7 @@
 	 *
 	 * @return pointer to the stream object, 0 in case of a failure
 	 */
-	virtual Common::SeekableReadStream *openForReading();
+	virtual Common::SeekableReadStream *openForReading() const;
 
 	/**
 	 * Creates a WriteStream instance corresponding to the file
@@ -238,7 +238,7 @@
 	 *
 	 * @return pointer to the stream object, 0 in case of a failure
 	 */
-	virtual Common::WriteStream *openForWriting();
+	virtual Common::WriteStream *openForWriting() const;
 };
 
 } // End of namespace Common

Modified: scummvm/trunk/engines/scumm/file_nes.h
===================================================================
--- scummvm/trunk/engines/scumm/file_nes.h	2008-09-06 20:36:47 UTC (rev 34390)
+++ scummvm/trunk/engines/scumm/file_nes.h	2008-09-06 20:49:48 UTC (rev 34391)
@@ -68,7 +68,7 @@
 	bool openSubFile(const Common::String &filename);
 
 	void close();
-	bool eof() { return _stream->eos(); }
+	bool eos() { return _stream->eos(); }
 	uint32 pos() { return _stream->pos(); }
 	uint32 size() { return _stream->size(); }
 	void seek(int32 offs, int whence = SEEK_SET) { _stream->seek(offs, whence); }


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list