aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engines/glk/streams.cpp147
-rw-r--r--engines/glk/streams.h61
2 files changed, 127 insertions, 81 deletions
diff --git a/engines/glk/streams.cpp b/engines/glk/streams.cpp
index 4d517a2b89..a2d5ca2aff 100644
--- a/engines/glk/streams.cpp
+++ b/engines/glk/streams.cpp
@@ -775,60 +775,28 @@ uint MemoryStream::getLineUni(uint32 *ubuf, uint len) {
/*--------------------------------------------------------------------------*/
-FileStream::FileStream(Streams *streams, frefid_t fref, uint fmode, uint rock, bool unicode) :
- Stream(streams, fmode == filemode_Read, fmode != filemode_Read, rock, unicode), _lastOp(0),
- _textFile(fref->_textMode), _inFile(nullptr), _outFile(nullptr), _inStream(nullptr) {
- Common::String fname = fref->_slotNumber == -1 ? fref->_filename : fref->getSaveName();
-
- if (fmode == filemode_Write || fmode == filemode_ReadWrite || fmode == filemode_WriteAppend) {
- _outFile = g_system->getSavefileManager()->openForSaving(fname, false);
- if (!_outFile)
- error("Could open file for writing - %s", fname.c_str());
-
- } else if (fmode == filemode_Read) {
- if (_file.open(fname)) {
- _inStream = &_file;
- } else {
- _inFile = g_system->getSavefileManager()->openForLoading(fname);
- _inStream = _inFile;
- }
-
- if (!_inStream)
- error("Could not open for reading - %s", fname.c_str());
- }
-}
-
-FileStream::~FileStream() {
- _file.close();
- delete _inFile;
- if (_outFile) {
- _outFile->finalize();
- delete _outFile;
- }
-}
-
-void FileStream::ensureOp(FileMode mode) {
+void IOStream::ensureOp(FileMode mode) {
// No implementation
}
-void FileStream::putChar(unsigned char ch) {
+void IOStream::putChar(unsigned char ch) {
if (!_writable)
return;
++_writeCount;
ensureOp(filemode_Write);
if (!_unicode) {
- _outFile->writeByte(ch);
+ _outStream->writeByte(ch);
} else if (_textFile) {
putCharUtf8((uint)ch);
} else {
- _outFile->writeUint32BE(ch);
+ _outStream->writeUint32BE(ch);
}
- _outFile->flush();
+ _outStream->flush();
}
-void FileStream::putCharUni(uint32 ch) {
+void IOStream::putCharUni(uint32 ch) {
if (!_writable)
return;
++_writeCount;
@@ -837,17 +805,17 @@ void FileStream::putCharUni(uint32 ch) {
if (!_unicode) {
if (ch >= 0x100)
ch = '?';
- _outFile->writeByte(ch);
+ _outStream->writeByte(ch);
} else if (_textFile) {
putCharUtf8(ch);
} else {
- _outFile->writeUint32BE(ch);
+ _outStream->writeUint32BE(ch);
}
- _outFile->flush();
+ _outStream->flush();
}
-void FileStream::putBuffer(const char *buf, size_t len) {
+void IOStream::putBuffer(const char *buf, size_t len) {
if (!_writable)
return;
_writeCount += len;
@@ -856,18 +824,18 @@ void FileStream::putBuffer(const char *buf, size_t len) {
for (size_t lx = 0; lx < len; lx++) {
unsigned char ch = ((const unsigned char *)buf)[lx];
if (!_unicode) {
- _outFile->writeByte(ch);
+ _outStream->writeByte(ch);
} else if (_textFile) {
putCharUtf8((uint)ch);
} else {
- _outFile->writeUint32BE(ch);
+ _outStream->writeUint32BE(ch);
}
}
- _outFile->flush();
+ _outStream->flush();
}
-void FileStream::putBufferUni(const uint32 *buf, size_t len) {
+void IOStream::putBufferUni(const uint32 *buf, size_t len) {
if (!_writable)
return;
_writeCount += len;
@@ -879,38 +847,38 @@ void FileStream::putBufferUni(const uint32 *buf, size_t len) {
if (!_unicode) {
if (ch >= 0x100)
ch = '?';
- _outFile->writeByte(ch);
+ _outStream->writeByte(ch);
} else if (_textFile) {
putCharUtf8(ch);
} else {
- _outFile->writeUint32BE(ch);
+ _outStream->writeUint32BE(ch);
}
}
- _outFile->flush();
+ _outStream->flush();
}
-void FileStream::putCharUtf8(uint val) {
+void IOStream::putCharUtf8(uint val) {
if (val < 0x80) {
- _outFile->writeByte(val);
+ _outStream->writeByte(val);
} else if (val < 0x800) {
- _outFile->writeByte((0xC0 | ((val & 0x7C0) >> 6)));
- _outFile->writeByte((0x80 | (val & 0x03F)));
+ _outStream->writeByte((0xC0 | ((val & 0x7C0) >> 6)));
+ _outStream->writeByte((0x80 | (val & 0x03F)));
} else if (val < 0x10000) {
- _outFile->writeByte((0xE0 | ((val & 0xF000) >> 12)));
- _outFile->writeByte((0x80 | ((val & 0x0FC0) >> 6)));
- _outFile->writeByte((0x80 | (val & 0x003F)));
+ _outStream->writeByte((0xE0 | ((val & 0xF000) >> 12)));
+ _outStream->writeByte((0x80 | ((val & 0x0FC0) >> 6)));
+ _outStream->writeByte((0x80 | (val & 0x003F)));
} else if (val < 0x200000) {
- _outFile->writeByte((0xF0 | ((val & 0x1C0000) >> 18)));
- _outFile->writeByte((0x80 | ((val & 0x03F000) >> 12)));
- _outFile->writeByte((0x80 | ((val & 0x000FC0) >> 6)));
- _outFile->writeByte((0x80 | (val & 0x00003F)));
+ _outStream->writeByte((0xF0 | ((val & 0x1C0000) >> 18)));
+ _outStream->writeByte((0x80 | ((val & 0x03F000) >> 12)));
+ _outStream->writeByte((0x80 | ((val & 0x000FC0) >> 6)));
+ _outStream->writeByte((0x80 | (val & 0x00003F)));
} else {
- _outFile->writeByte('?');
+ _outStream->writeByte('?');
}
}
-int FileStream::getCharUtf8() {
+int IOStream::getCharUtf8() {
uint res;
uint val0, val1, val2, val3;
@@ -998,11 +966,11 @@ int FileStream::getCharUtf8() {
return '?';
}
-uint FileStream::getPosition() const {
- return _outFile ? _outFile->pos() : _inStream->pos();
+uint IOStream::getPosition() const {
+ return _outStream ? _outStream->pos() : _inStream->pos();
}
-void FileStream::setPosition(int pos, uint seekMode) {
+void IOStream::setPosition(int pos, uint seekMode) {
_lastOp = 0;
if (_unicode)
pos *= 4;
@@ -1014,7 +982,7 @@ void FileStream::setPosition(int pos, uint seekMode) {
}
}
-int FileStream::getChar() {
+int IOStream::getChar() {
if (!_readable)
return -1;
@@ -1054,7 +1022,7 @@ int FileStream::getChar() {
}
}
-int FileStream::getCharUni() {
+int IOStream::getCharUni() {
if (!_readable)
return -1;
@@ -1092,7 +1060,7 @@ int FileStream::getCharUni() {
}
}
-uint FileStream::getBuffer(char *buf, uint len) {
+uint IOStream::getBuffer(char *buf, uint len) {
ensureOp(filemode_Read);
if (!_unicode) {
uint res;
@@ -1142,7 +1110,7 @@ uint FileStream::getBuffer(char *buf, uint len) {
}
}
-uint FileStream::getBufferUni(uint32 *buf, uint len) {
+uint IOStream::getBufferUni(uint32 *buf, uint len) {
if (!_readable)
return 0;
@@ -1199,7 +1167,7 @@ uint FileStream::getBufferUni(uint32 *buf, uint len) {
}
}
-uint FileStream::getLine(char *buf, uint len) {
+uint IOStream::getLine(char *buf, uint len) {
uint lx;
bool gotNewline;
@@ -1269,7 +1237,7 @@ uint FileStream::getLine(char *buf, uint len) {
}
}
-uint FileStream::getLineUni(uint32 *ubuf, uint len) {
+uint IOStream::getLineUni(uint32 *ubuf, uint len) {
bool gotNewline;
int lx;
@@ -1340,6 +1308,43 @@ uint FileStream::getLineUni(uint32 *ubuf, uint len) {
/*--------------------------------------------------------------------------*/
+FileStream::FileStream(Streams *streams, frefid_t fref, uint fmode, uint rock, bool unicode) :
+ IOStream(streams, fmode == filemode_Read, fmode != filemode_Read, rock, unicode),
+ _inSave(nullptr), _outSave(nullptr) {
+
+ _textFile = fref->_textMode;
+ Common::String fname = fref->_slotNumber == -1 ? fref->_filename : fref->getSaveName();
+
+ if (fmode == filemode_Write || fmode == filemode_ReadWrite || fmode == filemode_WriteAppend) {
+ _outSave = g_system->getSavefileManager()->openForSaving(fname, false);
+ if (!_outSave)
+ error("Could open file for writing - %s", fname.c_str());
+ setStream(_outSave);
+
+ } else if (fmode == filemode_Read) {
+ if (_file.open(fname)) {
+ setStream(&_file);
+ } else {
+ _inSave = g_system->getSavefileManager()->openForLoading(fname);
+ setStream(_inSave);
+
+ if (!_inSave)
+ error("Could not open for reading - %s", fname.c_str());
+ }
+ }
+}
+
+FileStream::~FileStream() {
+ _file.close();
+ delete _inSave;
+ if (_outSave) {
+ _outSave->finalize();
+ delete _outSave;
+ }
+}
+
+/*--------------------------------------------------------------------------*/
+
Streams::Streams() : _streamList(nullptr), _currentStream(nullptr) {
}
diff --git a/engines/glk/streams.h b/engines/glk/streams.h
index 2d6d48f1e1..df0d5df738 100644
--- a/engines/glk/streams.h
+++ b/engines/glk/streams.h
@@ -432,16 +432,13 @@ public:
};
/**
- * Implements a file stream
+ * Base class for I/O streams
*/
-class FileStream : public Stream {
+class IOStream : public Stream {
private:
- Common::File _file;
- Common::OutSaveFile *_outFile;
- Common::InSaveFile *_inFile;
Common::SeekableReadStream *_inStream;
+ Common::WriteStream *_outStream;
uint _lastOp; ///< 0, filemode_Write, or filemode_Read
- bool _textFile;
private:
/**
* Ensure the stream is ready for the given operation
@@ -457,16 +454,40 @@ private:
* Get a UTF8 character
*/
int getCharUtf8();
+protected:
+ bool _textFile;
public:
/**
* Constructor
*/
- FileStream(Streams *streams, frefid_t fref, uint fmode, uint rock, bool unicode);
+ IOStream(Streams *streams, bool readable, bool writable, uint rock, bool unicode) :
+ Stream(streams, readable, writable, rock, unicode) {}
+ IOStream(Streams *streams, uint rock = 0) : Stream(streams, false, false, rock, false),
+ _inStream(nullptr), _outStream(nullptr), _lastOp(0), _textFile(false) {}
+ IOStream(Streams *streams, Common::SeekableReadStream *inStream, uint rock = 0) :
+ Stream(streams, true, false, rock, false), _inStream(inStream), _outStream(nullptr), _lastOp(0), _textFile(false) {}
+ IOStream(Streams *streams, Common::WriteStream *outStream, uint rock = 0) :
+ Stream(streams, false, true, rock, false), _inStream(nullptr), _outStream(outStream), _lastOp(0), _textFile(false) {}
+
+ /**
+ * Sets the stream to use
+ */
+ void setStream(Common::SeekableReadStream *rs) {
+ _inStream = rs;
+ _outStream = nullptr;
+ _readable = true;
+ _writable = false;
+ }
/**
- * Destructor
+ * Sets the stream to use
*/
- virtual ~FileStream();
+ void setStream(Common::WriteStream *ws) {
+ _inStream = nullptr;
+ _outStream = ws;
+ _readable = false;
+ _writable = true;
+ }
/**
* Write a character
@@ -525,7 +546,7 @@ public:
/**
* Cast a stream to a ScummVM write stream
*/
- virtual operator Common::WriteStream *() const override { return _outFile; }
+ virtual operator Common::WriteStream *() const override { return _outStream; }
/**
* Cast a stream to a ScummVM read stream
@@ -534,6 +555,26 @@ public:
};
/**
+ * Implements a file stream
+ */
+class FileStream : public IOStream {
+private:
+ Common::File _file;
+ Common::InSaveFile *_inSave;
+ Common::OutSaveFile *_outSave;
+public:
+ /**
+ * Constructor
+ */
+ FileStream(Streams *streams, frefid_t fref, uint fmode, uint rock, bool unicode);
+
+ /**
+ * Destructor
+ */
+ virtual ~FileStream();
+};
+
+/**
* Streams manager
*/
class Streams {