mirror of
https://github.com/libretro/scummvm.git
synced 2024-12-15 14:18:37 +00:00
969ef3dac9
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
114 lines
2.4 KiB
C++
114 lines
2.4 KiB
C++
/* ScummVM - Scumm Interpreter
|
|
* Copyright (C) 2002-2005 The ScummVM project
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
*
|
|
* $Header$
|
|
*
|
|
*/
|
|
|
|
#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
|
|
assert(_pos <= _bufSize);
|
|
switch (whence) {
|
|
case SEEK_END:
|
|
// SEEK_END works just like SEEK_SET, only 'reversed',
|
|
// i.e. from the end.
|
|
offs = _bufSize - offs;
|
|
// Fall through
|
|
case SEEK_SET:
|
|
_ptr = _ptrOrig + offs;
|
|
_pos = offs;
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
_ptr += offs;
|
|
_pos += offs;
|
|
break;
|
|
}
|
|
// Post-Condition
|
|
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
|