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

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sun Jul 20 18:47:52 CEST 2008


Revision: 33139
          http://scummvm.svn.sourceforge.net/scummvm/?rev=33139&view=rev
Author:   fingolfin
Date:     2008-07-20 16:47:52 +0000 (Sun, 20 Jul 2008)

Log Message:
-----------
New SeekableReadStream::readLine_NEW() method, closely modelled after fgets, w/o the line length limitations of the old eekableReadStream::readLine() (which it will replace, after the feature freeze has been lifted)

Modified Paths:
--------------
    scummvm/trunk/common/stream.cpp
    scummvm/trunk/common/stream.h

Modified: scummvm/trunk/common/stream.cpp
===================================================================
--- scummvm/trunk/common/stream.cpp	2008-07-20 16:47:34 UTC (rev 33138)
+++ scummvm/trunk/common/stream.cpp	2008-07-20 16:47:52 UTC (rev 33139)
@@ -148,6 +148,61 @@
 	return buf;
 }
 
+char *SeekableReadStream::readLine_NEW(char *buf, size_t bufSize) {
+	assert(buf != 0 && bufSize > 1);
+	char *p = buf;
+	size_t len = 0;
+	char c;
+
+	// If end-of-file occurs before any characters are read, return NULL
+	// and the buffer contents remain unchanged. 
+	if (eos() || ioFailed()) {
+		return 0;
+	}
+
+	// Loop as long as the stream has not ended, there is still free
+	// space in the buffer, and the line has not ended
+	while (!eos() && len + 1 < bufSize && c != LF) {
+		c = readByte();
+		
+		// If end-of-file occurs before any characters are read, return
+		// NULL and the buffer contents remain unchanged. If an error
+		/// occurs, return NULL and the buffer contents are indeterminate.
+		if (ioFailed() || (len == 0 && eos()))
+			return 0;
+
+		// Check for CR or CR/LF
+		// * DOS and Windows use CRLF line breaks
+		// * Unix and OS X use LF line breaks
+		// * Macintosh before OS X used CR line breaks
+		if (c == CR) {
+			// Look at the next char -- is it LF? If not, seek back
+			c = readByte();
+			if (c != LF && !eos())
+				seek(-1, SEEK_CUR);
+			// Treat CR & CR/LF as plain LF
+			c = LF;
+		}
+		
+		*p++ = c;
+		len++;
+	}
+
+	// FIXME:
+	// This should fix a bug while using readLine with Common::File
+	// it seems that it sets the eos flag after an invalid read
+	// and at the same time the ioFailed flag
+	// the config file parser fails out of that reason for the new themes
+	if (eos()) {
+		clearIOFailed();
+	}
+
+	// We always terminate the buffer if no error occured
+	*p = 0;
+	return buf;
+}
+
+
 uint32 SubReadStream::read(void *dataPtr, uint32 dataSize) {
 	dataSize = MIN(dataSize, _end - _pos);
 

Modified: scummvm/trunk/common/stream.h
===================================================================
--- scummvm/trunk/common/stream.h	2008-07-20 16:47:34 UTC (rev 33138)
+++ scummvm/trunk/common/stream.h	2008-07-20 16:47:52 UTC (rev 33139)
@@ -304,13 +304,40 @@
 	 * 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.
 	 *
+	 * @bug A main difference (and flaw) in this function is that there is no
+	 * way to detect that a line exceeeds the length of the buffer.
+	 * Code which needs this should use the new readLine_NEW() method instead.
+	 *
 	 * @param buf	the buffer to store into
 	 * @param bufSize	the size of the buffer
 	 * @return a pointer to the read string, or NULL if an error occurred
+	 *
 	 * @note The line terminator (CR or CR/LF) is stripped and not inserted
 	 *       into the buffer.
 	 */
 	virtual char *readLine(char *buf, size_t bufSize);
+
+	/**
+	 * Reads at most one less than the number of characters specified
+	 * by bufSize from the and stores them in the string buf. Reading
+	 * stops when the end of a line is reached (CR, CR/LF or LF), at
+	 * end-of-file or error.  The newline, if any, is retained (CR and
+	 * CR/LF are translated to LF = 0xA = '\n').  If any characters are
+	 * read and there is no error, a `\0' character is appended to end
+	 * the string.
+	 *
+	 * Upon successful completion, return a pointer to the string. If
+	 * end-of-file occurs before any characters are read, returns NULL
+	 * and the buffer contents remain unchanged.  If an error occurs,
+	 * returns NULL and the buffer contents are indeterminate.
+	 * This method does not distinguish between end-of-file and error;
+	 * callers muse use ioFailed() or eos() to determine which occurred.
+	 *
+	 * @param buf	the buffer to store into
+	 * @param bufSize	the size of the buffer
+	 * @return a pointer to the read string, or NULL if an error occurred
+	 */
+	virtual char *readLine_NEW(char *s, size_t bufSize);
 };
 
 /**


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