[Scummvm-cvs-logs] SF.net SVN: scummvm:[34384] scummvm/trunk
fingolfin at users.sourceforge.net
fingolfin at users.sourceforge.net
Sat Sep 6 18:46:29 CEST 2008
Revision: 34384
http://scummvm.svn.sourceforge.net/scummvm/?rev=34384&view=rev
Author: fingolfin
Date: 2008-09-06 16:46:28 +0000 (Sat, 06 Sep 2008)
Log Message:
-----------
Added some unit tests for Stream::readLine_NEW, and clarified that readLine_NEW is essentially fgets in disguise
Modified Paths:
--------------
scummvm/trunk/common/stream.cpp
scummvm/trunk/common/stream.h
Added Paths:
-----------
scummvm/trunk/test/common/stream.h
Modified: scummvm/trunk/common/stream.cpp
===================================================================
--- scummvm/trunk/common/stream.cpp 2008-09-06 15:17:56 UTC (rev 34383)
+++ scummvm/trunk/common/stream.cpp 2008-09-06 16:46:28 UTC (rev 34384)
@@ -166,10 +166,13 @@
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()))
+ // NULL and the buffer contents remain unchanged.
+ if (len == 0 && eos())
return 0;
+
+ // If an error occurs, return NULL and the buffer contents are indeterminate.
+ if (ioFailed())
+ return 0;
// Check for CR or CR/LF
// * DOS and Windows use CRLF line breaks
Modified: scummvm/trunk/common/stream.h
===================================================================
--- scummvm/trunk/common/stream.h 2008-09-06 15:17:56 UTC (rev 34383)
+++ scummvm/trunk/common/stream.h 2008-09-06 16:46:28 UTC (rev 34384)
@@ -317,20 +317,7 @@
void skip(uint32 offset) { seek(offset, SEEK_CUR); }
/**
- * 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.
- *
- * @deprecated This method has a major flaw: There is no way to detect
- * whether a line exceeeds the length of the buffer, resulting in breakage
- * when overlong lines are encountered.
- * Use readLine_NEW() or readline() 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.
+ * DEPRECATED: Do not use this method! Instead use readLine_NEW() or readline().
*/
virtual char *readLine_OLD(char *buf, size_t bufSize);
@@ -350,6 +337,9 @@
* This method does not distinguish between end-of-file and error;
* callers muse use ioFailed() or eos() to determine which occurred.
*
+ * @note This methods is closely modeled after the standard fgets()
+ * function from stdio.h.
+ *
* @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
@@ -360,7 +350,7 @@
/**
* Reads a full line and returns it as a Common::String. Reading
* stops when the end of a line is reached (CR, CR/LF or LF), and
- * at end-of-file or error.
+ * at end-of-file or error.
*
* Upon successful completion, return a string with the content
* of the line, *without* the end of a line marker. This method
Added: scummvm/trunk/test/common/stream.h
===================================================================
--- scummvm/trunk/test/common/stream.h (rev 0)
+++ scummvm/trunk/test/common/stream.h 2008-09-06 16:46:28 UTC (rev 34384)
@@ -0,0 +1,42 @@
+#include <cxxtest/TestSuite.h>
+
+#include "common/stream.h"
+
+class ReadLineStreamTestSuite : public CxxTest::TestSuite {
+ public:
+ void test_readline(void) {
+ byte contents[] = { 'a', 'b', '\n', '\n', 'c', '\n' };
+ Common::MemoryReadStream ms(contents, sizeof(contents));
+
+ char buffer[100];
+
+ TS_ASSERT(0 != ms.readLine_NEW(buffer, sizeof(buffer)));
+ TS_ASSERT(0 == strcmp(buffer, "ab\n"));
+
+ TS_ASSERT(0 != ms.readLine_NEW(buffer, sizeof(buffer)));
+ TS_ASSERT(0 == strcmp(buffer, "\n"));
+
+ TS_ASSERT(0 != ms.readLine_NEW(buffer, sizeof(buffer)));
+ TS_ASSERT(0 == strcmp(buffer, "c\n"));
+
+ TS_ASSERT(ms.eos());
+ }
+
+ void test_readline2(void) {
+ byte contents[] = { 'a', 'b', '\n', '\n', 'c' };
+ Common::MemoryReadStream ms(contents, sizeof(contents));
+
+ char buffer[100];
+
+ TS_ASSERT(0 != ms.readLine_NEW(buffer, sizeof(buffer)));
+ TS_ASSERT(0 == strcmp(buffer, "ab\n"));
+
+ TS_ASSERT(0 != ms.readLine_NEW(buffer, sizeof(buffer)));
+ TS_ASSERT(0 == strcmp(buffer, "\n"));
+
+ TS_ASSERT(0 != ms.readLine_NEW(buffer, sizeof(buffer)));
+ TS_ASSERT(0 == strcmp(buffer, "c"));
+
+ TS_ASSERT(ms.eos());
+ }
+};
Property changes on: scummvm/trunk/test/common/stream.h
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Date Rev Author URL Id
Added: svn:eol-style
+ native
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