[Scummvm-cvs-logs] CVS: scummvm/common file.cpp,1.77,1.78 file.h,1.32,1.33 savefile.cpp,1.18,1.19 savefile.h,1.16,1.17 stream.cpp,1.7,1.8 stream.h,1.15,1.16

Max Horn fingolfin at users.sourceforge.net
Fri Apr 22 10:40:44 CEST 2005


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

Modified Files:
	file.cpp file.h savefile.cpp savefile.h stream.cpp stream.h 
Log Message:
* Added new virtual base class 'Stream', ReadStream and
  WriteStream are now subclasses of it.
* Added new methods eos(), ioFailed(), clearIOFailed() to
  all streams. This allows better error checking.
* SaveFile classes take advantage of these new standard
  stream APIS
* Removed File::gets()
* Added SeekableReadStream::readLine() (replaces File::gets)
* Added WriteStream::writeString, for convenience


Index: file.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/file.cpp,v
retrieving revision 1.77
retrieving revision 1.78
diff -u -d -r1.77 -r1.78
--- file.cpp	9 Jan 2005 01:41:42 -0000	1.77
+++ file.cpp	22 Apr 2005 17:40:07 -0000	1.78
@@ -278,68 +278,3 @@
 
 	return len;
 }
-
-#define LF 0x0A
-#define CR 0x0D
-
-char *File::gets(void *ptr, uint32 len) {
-	char *ptr2 = (char *)ptr;
-	char *res = ptr2;
-	uint32 read_chars = 1;
-
-	if (_handle == NULL) {
-		error("File::gets: File is not open!");
-		return 0;
-	}
-
-	if (len == 0 || !ptr)
-		return NULL;
-
-	// We don't include the newline character(s) in the buffer, and we
-	// always terminate it - we never read more than len-1 characters.
-
-	// EOF is treated as a line break, unless it was the first character
-	// that was read.
-
-	// 0 is treated as a line break, even though it should never occur in
-	// a text file.
-
-	// DOS and Windows use CRLF line breaks
-	// Unix and OS X use LF line breaks
-	// Macintosh before OS X uses CR line breaks
-
-	bool first = true;
-
-	while (read_chars < len) {
-		int c = getc(_handle);
-
-		if (c == EOF) {
-			if (first)
-				return NULL;
-			break;
-		}
-
-		first = false;
-
-		if (c == 0)
-			break;
-
-		if (c == LF)
-			break;
-
-		if (c == CR) {
-			c = getc(_handle);
-			// Don't use ungetc() here. It might be slightly more
-			// elegant, but PalmOS doesn't have it.
-			if (c != LF && c != EOF)
-				fseek(_handle, -1, SEEK_CUR);
-			break;
-		}
-
-		*ptr2++ = (char) c;
-		read_chars++;
-	}
-
-	*ptr2 = 0;
-	return res;
-}

Index: file.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/file.h,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -d -r1.32 -r1.33
--- file.h	9 Jan 2005 01:41:43 -0000	1.32
+++ file.h	22 Apr 2005 17:40:07 -0000	1.33
@@ -66,6 +66,7 @@
 	bool isOpen() const;
 	bool ioFailed() const;
 	void clearIOFailed();
+	bool eos() const { return eof(); }
 	bool eof() const;
 	uint32 pos() const;
 	uint32 size() const;
@@ -73,7 +74,6 @@
 	void seek(int32 offs, int whence = SEEK_SET);
 	uint32 read(void *ptr, uint32 size);
 	uint32 write(const void *ptr, uint32 size);
-	char *gets(void *ptr, uint32 size);
 };
 
 #endif

Index: savefile.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/savefile.cpp,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- savefile.cpp	16 Apr 2005 17:04:03 -0000	1.18
+++ savefile.cpp	22 Apr 2005 17:40:07 -0000	1.19
@@ -73,8 +73,10 @@
 			::fclose(fh);
 	}
 
-	bool readingFailed() const { return ferror(fh) != 0; }
-	bool writingFailed() const { return ferror(fh) != 0; }
+	bool eos() const { return feof(fh) != 0; }
+	bool ioFailed() const { return ferror(fh) != 0; }
+	void clearIOFailed() { clearerr(fh); }
+
 	bool isOpen() const { return fh != 0; }
 
 	uint32 read(void *buf, uint32 cnt) {
@@ -101,8 +103,10 @@
 			::gzclose(fh);
 	}
 
-	bool readingFailed() const { return _ioError; }
-	bool writingFailed() const { return _ioError; }
+	bool eos() const { return gzeof(fh) != 0; }
+	bool ioFailed() const { return _ioError; }
+	void clearIOFailed() { _ioError = false; }
+
 	bool isOpen() const { return fh != 0; }
 
 	uint32 read(void *buf, uint32 cnt) {

Index: savefile.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/savefile.h,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- savefile.h	13 Apr 2005 18:36:54 -0000	1.16
+++ savefile.h	22 Apr 2005 17:40:07 -0000	1.17
@@ -40,9 +40,6 @@
 class InSaveFile : public Common::ReadStream {
 public:
 	virtual ~InSaveFile() {}
-
-	virtual bool readingFailed() const { return false; }
-	//bool eof() const;
 };
 
 /**
@@ -55,8 +52,6 @@
 class OutSaveFile : public Common::WriteStream {
 public:
 	virtual ~OutSaveFile() {}
-
-	virtual bool writingFailed() const { return false; }
 };
 
 /**

Index: stream.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/stream.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- stream.cpp	9 Jan 2005 14:57:41 -0000	1.7
+++ stream.cpp	22 Apr 2005 17:40:07 -0000	1.8
@@ -21,9 +21,13 @@
 
 #include "stdafx.h"
 #include "common/stream.h"
+#include "common/str.h"
 
 namespace Common {
 
+void WriteStream::writeString(const String &str) {
+	write(str.c_str(), str.size());
+}
 
 void MemoryReadStream::seek(int32 offs, int whence) {
 	// Pre-Condition
@@ -48,5 +52,62 @@
 	assert(_pos <= _bufSize);
 }
 
+#define LF 0x0A
+#define CR 0x0D
+
+char *SeekableReadStream::readLine(char *buf, size_t bufSize) {
+	assert(buf && bufSize > 0);
+	char *p = buf;
+	size_t len = 0;
+	char c;
+
+	if (buf == 0 || bufSize == 0 || eos()) {
+		return 0;
+	}
+
+	// We don't include the newline character(s) in the buffer, and we
+	// always terminate it - we never read more than len-1 characters.
+
+	// EOF is treated as a line break, unless it was the first character
+	// that was read.
+
+	// 0 is treated as a line break, even though it should never occur in
+	// a text file.
+
+	// DOS and Windows use CRLF line breaks
+	// Unix and OS X use LF line breaks
+	// Macintosh before OS X uses CR line breaks
+
+	
+	c = readByte();
+	if (eos() || ioFailed()) {
+		return 0;
+	}
+
+	while (!eos() && len + 1 < bufSize) {
+		
+		if (ioFailed())
+			return 0;
+
+		if (c == 0 || c == LF)
+			break;
+
+		if (c == CR) {
+			c = readByte();
+			if (c != LF && !eos())
+				seek(-1, SEEK_CUR);
+			break;
+		}
+
+		*p++ = c;
+		len++;
+		
+		c = readByte();
+	}
+
+	*p = 0;
+	return buf;
+}
+
 
 }	// End of namespace Common

Index: stream.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/common/stream.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- stream.h	30 Mar 2005 22:01:17 -0000	1.15
+++ stream.h	22 Apr 2005 17:40:07 -0000	1.16
@@ -28,10 +28,38 @@
 
 namespace Common {
 
+class String;
+
+/**
+ * Virtual base class for both ReadStream and WriteStream.
+ */
+class Stream {
+public:
+	/**
+	 * Returns true if the end of the stream has been reached.
+	 */
+	virtual bool eos() const = 0;
+
+	/**
+	 * Returns true if any I/O failure occured.
+	 * This flag is never cleared automatically. In order to clear it,
+	 * client code has to call clearIOFailed() explicitly.
+	 * 
+	 * @todo Instead of returning a plain bool, maybe we should define
+	 *       a list of error codes which can be returned here.
+	 */
+	virtual bool ioFailed() const { return false; }
+	
+	/**
+	 * Reset the I/O error status.
+	 */
+	virtual void clearIOFailed() {}
+};
+
 /**
  * Generic interface for a writable data stream.
  */
-class WriteStream {
+class WriteStream : virtual public Stream {
 public:
 	/**
 	 * Write data into the stream. Subclasses must implement this
@@ -90,13 +118,15 @@
 	void writeSint32BE(int32 value) {
 		writeUint32BE((uint32)value);
 	}
+	
+	void writeString(const String &str);
 };
 
 
 /**
  * Generic interface for a readable data stream.
  */
-class ReadStream {
+class ReadStream : virtual public Stream {
 public:
 	/**
 	 * Read data from the stream. Subclasses must implement this
@@ -175,11 +205,22 @@
 class SeekableReadStream : public ReadStream {
 public:
 	
-	virtual bool eof() const = 0;
 	virtual uint32 pos() const = 0;
 	virtual uint32 size() const = 0;
 
 	virtual void seek(int32 offs, int whence = SEEK_SET) = 0;
+	
+	/**
+	 * Read one line of text from a CR or CR/LF terminated plain text file.
+	 * This method is a rough analog of the (f)gets function. 
+	 * 
+	 * @param buf	the buffer to store into
+	 * @param size	the size of the buffer
+	 * @return a pointer to the read string, or NULL if an error occured
+	 * @note The line terminator (CR or CR/LF) is stripped and not inserted
+	 *       into the buffer.
+	 */
+	virtual char *readLine(char *buf, size_t bufSize);
 };
 
 
@@ -200,6 +241,10 @@
 	void setStream(ReadStream *in) { _realStream = in; }
 	void setEnc(byte value) { _encbyte = value; }
 
+	virtual bool eos() const { return _realStream->eos(); }
+	virtual bool ioFailed() const { return _realStream->ioFailed(); }
+	virtual void clearIOFailed() { _realStream->clearIOFailed(); }
+
 	uint32 read(void *ptr, uint32 size) {
 		assert(_realStream);
 		uint32 len = _realStream->read(ptr, size);
@@ -249,7 +294,7 @@
 		return len;
 	}
 
-	bool eof() const { return _pos == _bufSize; }
+	bool eos() const { return _pos == _bufSize; }
 	uint32 pos() const { return _pos; }
 	uint32 size() const { return _bufSize; }
 
@@ -279,7 +324,7 @@
 		return len;
 	}
 
-	bool eof() const { return _pos == _bufSize; }
+	bool eos() const { return _pos == _bufSize; }
 	uint32 pos() const { return _pos; }
 	uint32 size() const { return _bufSize; }
 };





More information about the Scummvm-git-logs mailing list