[Scummvm-cvs-logs] CVS: scummvm/common file.cpp,1.60,1.61 file.h,1.24,1.25

Max Horn fingolfin at users.sourceforge.net
Sun Jun 27 17:08:03 CEST 2004


Update of /cvsroot/scummvm/scummvm/common
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6146/common

Modified Files:
	file.cpp file.h 
Log Message:
Enhanced default directory support in the File class; now one can specify arbitrary many default search directories

Index: file.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/file.cpp,v
retrieving revision 1.60
retrieving revision 1.61
diff -u -d -r1.60 -r1.61
--- file.cpp	27 Jun 2004 22:14:32 -0000	1.60
+++ file.cpp	28 Jun 2004 00:06:15 -0000	1.61
@@ -23,7 +23,7 @@
 #include "common/util.h"
 
 
-Common::String File::_defaultDirectory;
+Common::StringList File::_defaultDirectories;
 
 
 FILE *File::fopenNoCase(const char *filename, const char *directory, const char *mode) {
@@ -41,17 +41,16 @@
 	}
 #endif
 
-	// Record the length of the dir name (so we can cut of anything trailing it
-	// later, when we try with different file names).
+	// Determine the length of the dir name.
 	const int dirLen = strlen(buf);
 
 	if (dirLen > 0) {
 #ifdef __MORPHOS__
-		if (buf[dirLen-1] != ':' && buf[dirLen-1] != '/')
+		if (buf[dirLen-1] != ':' && buf[dirLen-1] != '/') // prevent double /
 #endif
 
 #if !defined(__GP32__) && !defined(__PALM_OS__)
-		strcat(buf, "/");	// prevent double /
+		strcat(buf, "/");
 #endif
 	}
 	strcat(buf, filename);
@@ -60,73 +59,49 @@
 	if (file)
 		return file;
 
-	// FIXME this should probably be engine specific...
-	const char *dirs[] = {
-		"",
-		"rooms/",
-		"ROOMS/",
-		"Rooms 1/",
-		"Rooms 2/",
-		"Rooms 3/",
-		"video/",
-		"VIDEO/",
-		"data/",
-		"DATA/",
-		"resource/",
-		"RESOURCE/",
-		// Simon the Sorcerer 1 Acorn
-		"execute/",
-		"EXECUTE/",
-		// Simon the Sorcerer 2 Amiga/Mac
-		"../",
-		"voices/",
-		"VOICES/",
-		// sword1/2 stuff if user just copied files without putting
-		// them all into the same dir like original installer did
-		"CLUSTERS/",
-		"clusters/",
-		"SPEECH/",
-		"speech/",
-		"SWORD2/",
-		"sword2/"
-	};
-
-	for (int dirIdx = 0; dirIdx < ARRAYSIZE(dirs); dirIdx++) {
-		buf[dirLen] = 0;
-		if (buf[0] != 0) {
+	buf[dirLen] = 0;
+	if (buf[0] != 0) {
 #ifdef __MORPHOS__
-			if (buf[strlen(buf) - 1] != ':' && buf[strlen(buf) - 1] != '/')
+		if (buf[strlen(buf) - 1] != ':' && buf[strlen(buf) - 1] != '/')
 #endif
 #ifndef __PALM_OS__
-			strcat(buf, "/");	// PALMOS
+		strcat(buf, "/");	// PALMOS
 #endif
-		}
-		strcat(buf, dirs[dirIdx]);
-		int8 len = strlen(buf);
-		strcat(buf, filename);
+	}
+	const int8 len = strlen(buf);
+	strcat(buf, filename);
 
-		ptr = buf + len;
-		do
-			*ptr = toupper(*ptr);
-		while (*ptr++);
-		file = fopen(buf, mode);
-		if (file)
-			return file;
+	//
+	// Try again, with file name converted to upper case
+	//
+	ptr = buf + len;
+	while (*ptr) {
+		*ptr = toupper(*ptr);
+		ptr++;
+	}
+	file = fopen(buf, mode);
+	if (file)
+		return file;
 
-		ptr = buf + len;
-		do
-			*ptr = tolower(*ptr);
-		while (*ptr++);
-		file = fopen(buf, mode);
-		if (file)
-			return file;
+	//
+	// Try again, with file name converted to lower case
+	//
+	ptr = buf + len;
+	while (*ptr) {
+		*ptr = tolower(*ptr);
+		ptr++;
 	}
+	file = fopen(buf, mode);
 
 	return NULL;
 }
 
-void File::setDefaultDirectory(const Common::String &directory) {
-	_defaultDirectory = directory;
+void File::addDefaultDirectory(const Common::String &directory) {
+	_defaultDirectories.push_back(directory);
+}
+
+void File::resetDefaultDirectories() {
+	_defaultDirectories.clear();
 }
 
 File::File() {
@@ -146,30 +121,32 @@
 		return false;
 	}
 
-	if (filename == NULL || *filename == 0)
+	if (filename == NULL || *filename == 0) {
 		return false;
-	
-	// If no directory was specified, use the default directory (if any).
-	if (directory == NULL)
-		directory = _defaultDirectory.isEmpty() ? "" : _defaultDirectory.c_str();
+	}
+
+	if (mode != kFileReadMode && mode != kFileWriteMode) {
+		warning("Only read/write mode supported!");
+		return false;
+	}
 
 	clearIOFailed();
 
-	if (mode == kFileReadMode) {
-		_handle = fopenNoCase(filename, directory, "rb");
-		if (_handle == NULL) {
-			debug(2, "File %s not found", filename);
-			return false;
+	const char *modeStr = (mode == kFileReadMode) ? "rb" : "wb";
+	if (directory) {
+		_handle = fopenNoCase(filename, directory, modeStr);
+	} else {
+		Common::StringList::const_iterator x;
+		for (x = _defaultDirectories.begin(); _handle == NULL && x != _defaultDirectories.end(); ++x) {
+			_handle = fopenNoCase(filename, x->c_str(), modeStr);
 		}
 	}
-	else if (mode == kFileWriteMode) {
-		_handle = fopenNoCase(filename, directory, "wb");
-		if (_handle == NULL) {
+
+	if (_handle == NULL) {
+		if (mode == kFileReadMode)
+			debug(2, "File %s not found", filename);
+		else
 			debug(2, "File %s not opened", filename);
-			return false;
-		}
-	}	else {
-		warning("Only read/write mode supported!");
 		return false;
 	}
 

Index: file.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/file.h,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- file.h	27 Jun 2004 22:14:32 -0000	1.24
+++ file.h	28 Jun 2004 00:06:15 -0000	1.25
@@ -36,7 +36,7 @@
 
 	static FILE *fopenNoCase(const char *filename, const char *directory, const char *mode);
 	
-	static Common::String _defaultDirectory;
+	static Common::StringList _defaultDirectories;
 
 public:
 	enum AccessMode {
@@ -44,7 +44,8 @@
 		kFileWriteMode = 2
 	};
 	
-	static void setDefaultDirectory(const Common::String &directory);
+	static void addDefaultDirectory(const Common::String &directory);
+	static void resetDefaultDirectories();
 	
 	File();
 	virtual ~File();





More information about the Scummvm-git-logs mailing list