[Scummvm-cvs-logs] CVS: scummvm/common str.cpp,1.5,1.6 str.h,1.3,1.4

Max Horn fingolfin at users.sourceforge.net
Thu Sep 26 04:45:02 CEST 2002


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

Modified Files:
	str.cpp str.h 
Log Message:
added simple message dialog

Index: str.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/str.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- str.cpp	22 Sep 2002 03:53:52 -0000	1.5
+++ str.cpp	26 Sep 2002 11:44:02 -0000	1.6
@@ -21,20 +21,28 @@
 #include "stdafx.h"
 #include "str.h"
 #include "util.h"
-
-#ifdef _MSC_VER
-#	pragma warning( disable : 4068 ) // unknown pragmas
-#endif
+
+
+#ifdef _MSC_VER
+
+#	pragma warning( disable : 4068 ) // unknown pragmas
+
+#endif
+
 
 namespace ScummVM {
 
-String::String(const char *str)
+String::String(const char *str, int len)
 {
 	_refCount = new int(1);
-	if (str) {		
-		_capacity = _len = resStrLen(str);
+	if (str) {
+		if (len)
+			_capacity = _len = len;
+		else
+			_capacity = _len = resStrLen(str);
 		_str = (char *)calloc(1, _capacity+1);
-		memcpy(_str, str, _len+1);
+		memcpy(_str, str, _len);
+		_str[_len] = 0;
 	} else {
 		_capacity = _len = 0;
 		_str = 0;

Index: str.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/str.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- str.h	9 Sep 2002 11:42:24 -0000	1.3
+++ str.h	26 Sep 2002 11:44:02 -0000	1.4
@@ -33,7 +33,7 @@
  Only constructor: ConstString(const char *ptr)
  Then whenever you now use "const String &" in a parameter, use "const ConstString &"
  instead (mayhaps make a typedef even). Thus, parameters are passed w/o 
- causing a free/malloc. Then only for permenant storage, when we assign it to a
+ causing a free/malloc. Then only for permanent storage, when we assign it to a
  real String object, will a malloc be issued (to this end, make String aware of
  ConstString ?!?
 */
@@ -46,7 +46,7 @@
 
 public:
 	ConstString() : _str(0), _len(0) {}
-	ConstString(const char *str) : _str((char*)str) { _len = str ? strlen(str) : 0; }
+	ConstString(const char *str, int len = 0) : _str((char*)str) { _len = str ? (len ? len : strlen(str)) : 0; }
 	virtual ~ConstString() {}
 	
 	bool operator ==(const ConstString& x) const;
@@ -58,6 +58,12 @@
 	bool operator >(const ConstString& x) const;
 	bool operator >=(const ConstString& x) const;
 
+	char operator [](int idx) const
+	{
+		assert(_str && idx >= 0 && idx < _len);
+		return _str[idx];
+	}
+
 	const char *c_str() const		{ return _str; }
 	int size() const				{ return _len; }
 
@@ -71,17 +77,33 @@
 
 public:
 	String() : _capacity(0) { _refCount = new int(1); }
-	String(const char *str);
+	String(const char *str, int len = 0);
 	String(const ConstString &str);
 	String(const String &str);
 	virtual ~String();
 	
 	String& operator  =(const char* str);
+// TODO - We should use RTTI here - that is, not real C++ RTTI but some magic
+// constant in each string object. We want to be able to optimize the case when
+// a real 'String' object is passed to a function as a ConstString obj and then
+// assigned to a 'String' object
 	String& operator  =(const String& str);
 	String& operator +=(const char* str);
 	String& operator +=(const String& str);
 	String& operator +=(char c);
 
+	char operator [](int idx) const
+	{
+		assert(_str && idx >= 0 && idx < _len);
+		return _str[idx];
+	}
+
+	char &operator [](int idx)
+	{
+		assert(_str && idx >= 0 && idx < _len);
+		return _str[idx];
+	}
+
 	void deleteLastChar();
 	void clear();
 
@@ -99,8 +121,19 @@
 	void push_back(const char* str)
 	{
 		ensureCapacity(_size + 1);
-		_data[_size] = str;
-		_size++;
+		_data[_size++] = str;
+	}
+
+	void push_back(const ConstString& str)
+	{
+		ensureCapacity(_size + 1);
+		_data[_size++] = str;
+	}
+	
+	void push_back(const String& str)
+	{
+		ensureCapacity(_size + 1);
+		_data[_size++] = str;
 	}
 };
 





More information about the Scummvm-git-logs mailing list