[Scummvm-cvs-logs] CVS: scummvm/common file.cpp,1.7,1.8 file.h,1.3,1.4

Pawe? Ko?odziejski aquadran at users.sourceforge.net
Mon Sep 2 15:09:14 CEST 2002


Update of /cvsroot/scummvm/scummvm/common
In directory usw-pr-cvs1:/tmp/cvs-serv26861

Modified Files:
	file.cpp file.h 
Log Message:
added write support to File

Index: file.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/file.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- file.cpp	2 Sep 2002 21:12:57 -0000	1.7
+++ file.cpp	2 Sep 2002 22:06:26 -0000	1.8
@@ -23,7 +23,7 @@
 
 File::File() {
 	_handle = NULL;
-	_readFailed = false;
+	_ioFailed = false;
 	_encbyte = 0;
 }
 
@@ -38,7 +38,7 @@
 		return false;
 	}
 
-	clearReadFailed();
+	clearIOFailed();
 
 	int32 i = 0, pos = 0;
 
@@ -70,8 +70,29 @@
 			debug(2, "File %s not found", filename);
 			return false;
 		}
-	} else {
-		warning("Only read mode supported!");
+	}
+	else if (mode == 2) {
+		_handle = fopen(buf, "wb");
+		if (_handle == NULL) {
+			ptr = buf + pos;
+			do
+				*ptr++ = toupper(*ptr);
+			while (*ptr);
+			_handle = fopen(buf, "wb");
+		}
+		if (_handle == NULL) {
+			ptr = buf + pos;
+			do
+				*ptr++ = tolower(*ptr);
+			while (*ptr);
+			_handle = fopen(buf, "wb");
+		}
+		if (_handle == NULL) {
+			debug(2, "File %s not opened", filename);
+			return false;
+		}
+	}	else {
+		warning("Only read/write mode supported!");
 		return false;
 	}
 
@@ -88,12 +109,12 @@
 	return _handle != NULL;
 }
 
-bool File::readFailed() {
-	return _readFailed != 0;
+bool File::ioFailed() {
+	return _ioFailed != 0;
 }
 
-void File::clearReadFailed() {
-	_readFailed = false;
+void File::clearIOFailed() {
+	_ioFailed = false;
 }
 
 bool File::eof() {
@@ -137,7 +158,7 @@
 
 	if ((uint32)fread(ptr2, 1, size, _handle) != size) {
 		clearerr(_handle);
-		_readFailed = true;
+		_ioFailed = true;
 	}
 
 	if (_encbyte != 0) {
@@ -155,7 +176,7 @@
 
 	if (fread(&b, 1, 1, _handle) != 1) {
 		clearerr(_handle);
-		_readFailed = true;
+		_ioFailed = true;
 	}
 	return b ^ _encbyte;
 }
@@ -182,4 +203,59 @@
 	uint32 b = readWordBE();
 	uint32 a = readWordBE();
 	return (b << 16) | a;
+}
+
+uint32 File::write(void *ptr, uint32 size) {
+	byte *ptr2 = (byte *)ptr;
+
+	if (_handle == NULL) {
+		error("File is not open!");
+		return 0;
+	}
+
+	if (size == 0)
+		return 0;
+
+	if (_encbyte != 0) {
+		uint32 t_size = size;
+		do {
+			*ptr2++ ^= _encbyte;
+		} while (--t_size);
+	}
+
+	if ((uint32)fwrite(ptr2, 1, size, _handle) != size) {
+		clearerr(_handle);
+		_ioFailed = true;
+	}
+
+	return size;
+}
+
+void File::writeByte(byte value) {
+	value ^= _encbyte;
+
+	if (fwrite(&value, 1, 1, _handle) != 1) {
+		clearerr(_handle);
+		_ioFailed = true;
+	}
+}
+
+void File::writeWordLE(uint16 value) {
+	writeByte((byte)(value & 0xff));
+	writeByte((byte)(value >> 8));
+}
+
+void File::writeDwordLE(uint32 value) {
+	writeWordLE((uint16)(value & 0xffff));
+	writeWordLE((uint16)(value >> 16));
+}
+
+void File::writeWordBE(uint16 value) {
+	writeByte((byte)(value >> 8));
+	writeByte((byte)(value & 0xff));
+}
+
+void File::writeDwordBE(uint32 value) {
+	writeWordBE((uint16)(value >> 16));
+	writeWordBE((uint16)(value & 0xffff));
 }

Index: file.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/file.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- file.h	2 Sep 2002 07:53:43 -0000	1.3
+++ file.h	2 Sep 2002 22:06:26 -0000	1.4
@@ -31,7 +31,7 @@
 private:
 
 	FILE * _handle;
-	bool _readFailed;
+	bool _ioFailed;
 	byte _encbyte;
 
 public:
@@ -41,8 +41,8 @@
 	bool open(const char *filename, int mode = 1, byte encbyte = 0);
 	void close();
 	bool isOpen();
-	bool readFailed();
-	void clearReadFailed();
+	bool ioFailed();
+	void clearIOFailed();
 	bool eof();
 	uint32 pos();
 	void seek(uint32 offs, int whence);
@@ -52,7 +52,12 @@
 	uint32 readDwordLE();
 	uint16 readWordBE();
 	uint32 readDwordBE();
-
+	uint32 write(void *ptr, uint32 size);
+	void writeByte(byte value);
+	void writeWordLE(uint16 value);
+	void writeDwordLE(uint32 value);
+	void writeWordBE(uint16 value);
+	void writeDwordBE(uint32 value);
 };
 
 #endif





More information about the Scummvm-git-logs mailing list