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

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sun Mar 30 20:37:10 CEST 2008


Revision: 31334
          http://scummvm.svn.sourceforge.net/scummvm/?rev=31334&view=rev
Author:   fingolfin
Date:     2008-03-30 11:37:09 -0700 (Sun, 30 Mar 2008)

Log Message:
-----------
Removed char &operator [] from class String -- it had the potential to wreak havoc when used on shared strings (thanks to tramboi for pointing this out)

Modified Paths:
--------------
    scummvm/trunk/common/file.cpp
    scummvm/trunk/common/str.cpp
    scummvm/trunk/common/str.h
    scummvm/trunk/engines/agi/predictive.cpp

Modified: scummvm/trunk/common/file.cpp
===================================================================
--- scummvm/trunk/common/file.cpp	2008-03-30 18:32:42 UTC (rev 31333)
+++ scummvm/trunk/common/file.cpp	2008-03-30 18:37:09 UTC (rev 31334)
@@ -146,54 +146,53 @@
 
 static FILE *fopenNoCase(const String &filename, const String &directory, const char *mode) {
 	FILE *file;
-	String buf(directory);
-	uint i;
+	String dirBuf(directory);
+	String fileBuf(filename);
 
 #if !defined(__GP32__) && !defined(PALMOS_MODE)
 	// Add a trailing slash, if necessary.
-	if (!buf.empty()) {
-		const char c = buf.lastChar();
+	if (!dirBuf.empty()) {
+		const char c = dirBuf.lastChar();
 		if (c != ':' && c != '/' && c != '\\')
-			buf += '/';
+			dirBuf += '/';
 	}
 #endif
 
 	// Append the filename to the path string
-	const int offsetToFileName = buf.size();
-	buf += filename;
+	String pathBuf(dirBuf);
+	pathBuf += fileBuf;
 
 	//
 	// Try to open the file normally
 	//
-	file = fopen(buf.c_str(), mode);
+	file = fopen(pathBuf.c_str(), mode);
 
 	//
 	// Try again, with file name converted to upper case
 	//
 	if (!file) {
-		for (i = offsetToFileName; i < buf.size(); ++i) {
-			buf[i] = toupper(buf[i]);
-		}
-		file = fopen(buf.c_str(), mode);
+		fileBuf.toUppercase();
+		pathBuf += fileBuf;
+		file = fopen(pathBuf.c_str(), mode);
 	}
 
 	//
 	// Try again, with file name converted to lower case
 	//
 	if (!file) {
-		for (i = offsetToFileName; i < buf.size(); ++i) {
-			buf[i] = tolower(buf[i]);
-		}
-		file = fopen(buf.c_str(), mode);
+		fileBuf.toLowercase();
+		pathBuf += fileBuf;
+		file = fopen(pathBuf.c_str(), mode);
 	}
 
 	//
 	// Try again, with file name capitalized
 	//
 	if (!file) {
-		i = offsetToFileName;
-		buf[i] = toupper(buf[i]);
-		file = fopen(buf.c_str(), mode);
+		fileBuf.toLowercase();
+		fileBuf.setChar(toupper(fileBuf[0]),0);
+		pathBuf += fileBuf;
+		file = fopen(pathBuf.c_str(), mode);
 	}
 
 #ifdef __amigaos4__

Modified: scummvm/trunk/common/str.cpp
===================================================================
--- scummvm/trunk/common/str.cpp	2008-03-30 18:32:42 UTC (rev 31333)
+++ scummvm/trunk/common/str.cpp	2008-03-30 18:37:09 UTC (rev 31334)
@@ -254,6 +254,13 @@
 	_storage[0] = 0;
 }
 
+void String::setChar(char c, uint32 p) {
+	assert(p <= _len);
+
+	ensureCapacity(_len, true);
+	_str[p] = c;
+}
+
 void String::insertChar(char c, uint32 p) {
 	assert(p <= _len);
 

Modified: scummvm/trunk/common/str.h
===================================================================
--- scummvm/trunk/common/str.h	2008-03-30 18:32:42 UTC (rev 31333)
+++ scummvm/trunk/common/str.h	2008-03-30 18:37:09 UTC (rev 31334)
@@ -146,16 +146,20 @@
 		return _str[idx];
 	}
 
-	char &operator [](int idx) {
-		assert(_str && idx >= 0 && idx < (int)_len);
-		return _str[idx];
-	}
-
+	/** Remove the last character from the string. */
 	void deleteLastChar();
+	
+	/** Remove the character at position p from the string. */
 	void deleteChar(uint32 p);
-	void clear();
+
+	/** Set character c at position p, replacing the previous character there. */
+	void setChar(char c, uint32 p);
+
+	/** Set character c at position p. */
 	void insertChar(char c, uint32 p);
 
+	void clear();
+
 	void toLowercase();
 	void toUppercase();
 

Modified: scummvm/trunk/engines/agi/predictive.cpp
===================================================================
--- scummvm/trunk/engines/agi/predictive.cpp	2008-03-30 18:32:42 UTC (rev 31333)
+++ scummvm/trunk/engines/agi/predictive.cpp	2008-03-30 18:37:09 UTC (rev 31334)
@@ -430,7 +430,7 @@
 							else
 								repeatcount[x - 1] = (repeatcount[x - 1] + 1) % 3;
 							if (_currentCode.lastChar() >= '1')
-								_currentWord[x - 1] = buttons[_currentCode[x - 1] - '1'][3 + repeatcount[x - 1]];
+								_currentWord.setChar(buttons[_currentCode[x - 1] - '1'][3 + repeatcount[x - 1]], x-1);
 						}
 					}
 				} else if (active == 10) { // add


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