[Scummvm-cvs-logs] CVS: scummvm/common file.cpp,1.11,1.12 file.h,1.6,1.7

Max Horn fingolfin at users.sourceforge.net
Fri Sep 13 11:03:04 CEST 2002


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

Modified Files:
	file.cpp file.h 
Log Message:
factored out the case-insensitive fopen into its own function - makes it easier to adapt all the code to use it. TODO: improve it to work like in exult, i.e. sometimes other parts of the path have to be changed to upper/lower case (e.g. video vs. VIDEO)

Index: file.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/file.cpp,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- file.cpp	13 Sep 2002 12:16:03 -0000	1.11
+++ file.cpp	13 Sep 2002 18:02:33 -0000	1.12
@@ -22,6 +22,40 @@
 #include "file.h"
 #include "engine.h"	// For debug/warning/error
 
+FILE *fopen_nocase(const char *path, const char *mode)
+{
+	FILE *file;
+
+	file = fopen(path, mode);
+	if (file)
+		return file;
+
+	char buf[256], *ptr;
+	int32 i = 0, pos = 0;
+
+	strcpy(buf, path);
+	while (buf[i] != 0) {
+		if ((buf[i] == '/') || (buf[i] == '\\')) {
+			pos = i + 1;
+		}
+		i++;
+	}
+	
+	ptr = buf + pos;
+	do
+		*ptr++ = toupper(*ptr);
+	while (*ptr);
+	file = fopen(buf, mode);
+	if (file)
+		return file;
+
+	ptr = buf + pos;
+	do
+		*ptr++ = tolower(*ptr);
+	while (*ptr);
+	return fopen(buf, mode);
+}
+
 File::File() {
 	_handle = NULL;
 	_ioFailed = false;
@@ -33,7 +67,7 @@
 }
 
 bool File::open(const char *filename, int mode, byte encbyte) {
-	char buf[256], *ptr;
+
 	if (_handle) {
 		debug(2, "File %s already opened", filename);
 		return false;
@@ -41,53 +75,15 @@
 
 	clearIOFailed();
 
-	int32 i = 0, pos = 0;
-
-	strcpy(buf, filename);
-	while (buf[i] != 0) {
-		if ((buf[i] == '/') || (buf[i] == '\\')) {
-			pos = i + 1;
-		}
-		i++;
-	}
-	
-	if (mode == 1) {
-		_handle = fopen(buf, "rb");
-		if (_handle == NULL) {
-			ptr = buf + pos;
-			do
-				*ptr++ = toupper(*ptr);
-			while (*ptr);
-			_handle = fopen(buf, "rb");
-		}
-		if (_handle == NULL) {
-			ptr = buf + pos;
-			do
-				*ptr++ = tolower(*ptr);
-			while (*ptr);
-			_handle = fopen(buf, "rb");
-		}
+	if (mode == kFileReadMode) {
+		_handle = fopen_nocase(filename, "rb");
 		if (_handle == NULL) {
 			debug(2, "File %s not found", filename);
 			return false;
 		}
 	}
-	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");
-		}
+	else if (mode == kFileWriteMode) {
+		_handle = fopen_nocase(filename, "wb");
 		if (_handle == NULL) {
 			debug(2, "File %s not opened", filename);
 			return false;
@@ -136,6 +132,20 @@
 	}
 
 	return ftell(_handle);
+}
+
+uint32 File::size() {
+	if (_handle == NULL) {
+		error("File is not open!");
+		return 0;
+	}
+
+	uint32 oldPos = ftell(_handle);
+	fseek(_handle, 0, SEEK_END);
+	uint32 length = ftell(_handle);
+	fseek(_handle, oldPos, SEEK_SET);
+	
+	return length;
 }
 
 void File::seek(int32 offs, int whence) {

Index: file.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/file.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- file.h	13 Sep 2002 12:16:03 -0000	1.6
+++ file.h	13 Sep 2002 18:02:34 -0000	1.7
@@ -26,6 +26,10 @@
 #include "stdafx.h"
 #include "scummsys.h"
 
+// fopen_nocase is like fopen only that it will try various variations
+// of the given filename (with different cases) if the initial one isn't found.
+FILE *fopen_nocase(const char *path, const char *mode);
+
 class File {
 private:
 
@@ -34,16 +38,21 @@
 	byte _encbyte;
 
 public:
+	enum {
+		kFileReadMode = 1,
+		kFileWriteMode = 2
+	};
 
 	File();
 	~File();
-	bool open(const char *filename, int mode = 1, byte encbyte = 0);
+	bool open(const char *filename, int mode = kFileReadMode, byte encbyte = 0);
 	void close();
 	bool isOpen();
 	bool ioFailed();
 	void clearIOFailed();
 	bool eof();
 	uint32 pos();
+	uint32 size();
 	void seek(int32 offs, int whence);
 	uint32 read(void *ptr, uint32 size);
 	byte readByte();





More information about the Scummvm-git-logs mailing list