diff options
-rw-r--r-- | engines/sci/engine/kfile.cpp | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/engines/sci/engine/kfile.cpp b/engines/sci/engine/kfile.cpp index 5a38e16dff..76ac6254ac 100644 --- a/engines/sci/engine/kfile.cpp +++ b/engines/sci/engine/kfile.cpp @@ -218,18 +218,22 @@ reg_t kFPuts(EngineState *s, int argc, reg_t *argv) { return s->r_acc; } -static void fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle) { +static bool fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle) { FileHandle *f = getFileFromHandle(s, handle); if (!f) - return; + return false; if (!f->_in) { error("fgets_wrapper: Trying to read from file '%s' opened for writing", f->_name.c_str()); - return; + return false; } if (maxsize > 1) { memset(dest, 0, maxsize); f->_in->readLine(dest, maxsize); + + if (f->_in->eos() || f->_in->err()) + return false; + // The returned string must not have an ending LF int strSize = strlen(dest); if (strSize > 0) { @@ -238,9 +242,14 @@ static void fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle) { } } else { *dest = f->_in->readByte(); + + if (f->_in->eos() || f->_in->err()) + return false; } debugC(2, kDebugLevelFile, " -> FGets'ed \"%s\"", dest); + + return true; } reg_t kFGets(EngineState *s, int argc, reg_t *argv) { @@ -249,7 +258,9 @@ reg_t kFGets(EngineState *s, int argc, reg_t *argv) { int handle = argv[2].toUint16(); debugC(2, kDebugLevelFile, "kFGets(%d, %d)", handle, maxsize); - fgets_wrapper(s, buf, maxsize, handle); + if (!fgets_wrapper(s, buf, maxsize, handle)) + *buf = 0; + s->_segMan->memcpy(argv[0], (const byte*)buf, maxsize); return argv[0]; } @@ -773,6 +784,12 @@ reg_t kFileIOReadRaw(EngineState *s, int argc, reg_t *argv) { FileHandle *f = getFileFromHandle(s, handle); if (f) { bytesRead = f->_in->read(buf, size); + + if (f->_in->eos() || f->_in->err()) { + *buf = 0; + bytesRead = 0; + } + s->_segMan->memcpy(argv[1], (const byte*)buf, size); } @@ -841,7 +858,9 @@ reg_t kFileIOReadString(EngineState *s, int argc, reg_t *argv) { int handle = argv[2].toUint16(); debugC(2, kDebugLevelFile, "kFileIO(readString): %d, %d", handle, size); - fgets_wrapper(s, buf, size, handle); + if (!fgets_wrapper(s, buf, size, handle)) + *buf = 0; + s->_segMan->memcpy(argv[0], (const byte*)buf, size); delete[] buf; return argv[0]; |