aboutsummaryrefslogtreecommitdiff
path: root/common/stream.cpp
diff options
context:
space:
mode:
authorMax Horn2005-04-22 17:40:09 +0000
committerMax Horn2005-04-22 17:40:09 +0000
commit969ef3dac93a21d981e4cc2a8c81c0f03a834417 (patch)
tree9240363f92815c7e92e53b0b42e8d553d4a478c3 /common/stream.cpp
parentca33ec4563e05b0940e44de78e0f52bbbf6bfe41 (diff)
downloadscummvm-rg350-969ef3dac93a21d981e4cc2a8c81c0f03a834417.tar.gz
scummvm-rg350-969ef3dac93a21d981e4cc2a8c81c0f03a834417.tar.bz2
scummvm-rg350-969ef3dac93a21d981e4cc2a8c81c0f03a834417.zip
* 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 svn-id: r17752
Diffstat (limited to 'common/stream.cpp')
-rw-r--r--common/stream.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/common/stream.cpp b/common/stream.cpp
index 62b045fd0d..a9d5793be3 100644
--- a/common/stream.cpp
+++ b/common/stream.cpp
@@ -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 @@ void MemoryReadStream::seek(int32 offs, int whence) {
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