Skip to content

Commit

Permalink
SCI: Use the later SCI file functions for the SCI0 ones
Browse files Browse the repository at this point in the history
They are essentially the same (with the exception of the return values),
so unifying them reduces code duplication
  • Loading branch information
bluegr committed Jun 13, 2012
1 parent 944a774 commit 098f162
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 55 deletions.
4 changes: 0 additions & 4 deletions engines/sci/engine/kernel.h
Expand Up @@ -323,10 +323,6 @@ reg_t kTimesCot(EngineState *s, int argc, reg_t *argv);
reg_t kCosDiv(EngineState *s, int argc, reg_t *argv);
reg_t kSinDiv(EngineState *s, int argc, reg_t *argv);
reg_t kValidPath(EngineState *s, int argc, reg_t *argv);
reg_t kFOpen(EngineState *s, int argc, reg_t *argv);
reg_t kFPuts(EngineState *s, int argc, reg_t *argv);
reg_t kFGets(EngineState *s, int argc, reg_t *argv);
reg_t kFClose(EngineState *s, int argc, reg_t *argv);
reg_t kMapKeyToDir(EngineState *s, int argc, reg_t *argv);
reg_t kGlobalToLocal(EngineState *s, int argc, reg_t *argv);
reg_t kLocalToGlobal(EngineState *s, int argc, reg_t *argv);
Expand Down
8 changes: 4 additions & 4 deletions engines/sci/engine/kernel_tables.h
Expand Up @@ -350,10 +350,10 @@ static SciKernelMapEntry s_kernelMap[] = {
{ MAP_CALL(EditControl), SIG_EVERYWHERE, "[o0][o0]", NULL, NULL },
{ MAP_CALL(Empty), SIG_EVERYWHERE, "(.*)", NULL, NULL },
{ MAP_CALL(EmptyList), SIG_EVERYWHERE, "l", NULL, NULL },
{ MAP_CALL(FClose), SIG_EVERYWHERE, "i", NULL, NULL },
{ MAP_CALL(FGets), SIG_EVERYWHERE, "rii", NULL, NULL },
{ MAP_CALL(FOpen), SIG_EVERYWHERE, "ri", NULL, NULL },
{ MAP_CALL(FPuts), SIG_EVERYWHERE, "ir", NULL, NULL },
{ "FClose", kFileIOClose, SIG_EVERYWHERE, "i", NULL, NULL },
{ "FGets", kFileIOReadString, SIG_EVERYWHERE, "rii", NULL, NULL },
{ "FOpen", kFileIOOpen, SIG_EVERYWHERE, "ri", NULL, NULL },
{ "FPuts", kFileIOWriteString, SIG_EVERYWHERE, "ir", NULL, NULL },
{ MAP_CALL(FileIO), SIG_EVERYWHERE, "i(.*)", kFileIO_subops, NULL },
{ MAP_CALL(FindKey), SIG_EVERYWHERE, "l.", NULL, kFindKey_workarounds },
{ MAP_CALL(FirstNode), SIG_EVERYWHERE, "[l0]", NULL, NULL },
Expand Down
65 changes: 18 additions & 47 deletions engines/sci/engine/kfile.cpp
Expand Up @@ -47,47 +47,6 @@ extern int fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle);
extern void listSavegames(Common::Array<SavegameDesc> &saves);
extern int findSavegame(Common::Array<SavegameDesc> &saves, int16 savegameId);

reg_t kFOpen(EngineState *s, int argc, reg_t *argv) {
Common::String name = s->_segMan->getString(argv[0]);
int mode = argv[1].toUint16();

debugC(kDebugLevelFile, "kFOpen(%s,0x%x)", name.c_str(), mode);
return file_open(s, name, mode, true);
}

reg_t kFClose(EngineState *s, int argc, reg_t *argv) {
debugC(kDebugLevelFile, "kFClose(%d)", argv[0].toUint16());
if (argv[0] != SIGNAL_REG) {
FileHandle *f = getFileFromHandle(s, argv[0].toUint16());
if (f)
f->close();
}
return s->r_acc;
}

reg_t kFPuts(EngineState *s, int argc, reg_t *argv) {
int handle = argv[0].toUint16();
Common::String data = s->_segMan->getString(argv[1]);

FileHandle *f = getFileFromHandle(s, handle);
if (f)
f->_out->write(data.c_str(), data.size());

return s->r_acc;
}

reg_t kFGets(EngineState *s, int argc, reg_t *argv) {
int maxsize = argv[1].toUint16();
char *buf = new char[maxsize];
int handle = argv[2].toUint16();

debugC(kDebugLevelFile, "kFGets(%d, %d)", handle, maxsize);
int readBytes = fgets_wrapper(s, buf, maxsize, handle);
s->_segMan->memcpy(argv[0], (const byte*)buf, maxsize);
delete[] buf;
return readBytes ? argv[0] : NULL_REG;
}

/**
* Writes the cwd to the supplied address and returns the address in acc.
*/
Expand Down Expand Up @@ -559,6 +518,9 @@ reg_t kFileIOOpen(EngineState *s, int argc, reg_t *argv) {
reg_t kFileIOClose(EngineState *s, int argc, reg_t *argv) {
debugC(kDebugLevelFile, "kFileIO(close): %d", argv[0].toUint16());

if (argv[0] == SIGNAL_REG)
return s->r_acc;

uint16 handle = argv[0].toUint16();

#ifdef ENABLE_SCI32
Expand All @@ -571,8 +533,13 @@ reg_t kFileIOClose(EngineState *s, int argc, reg_t *argv) {
FileHandle *f = getFileFromHandle(s, handle);
if (f) {
f->close();
if (getSciVersion() <= SCI_VERSION_0_LATE)
return s->r_acc; // SCI0 semantics: no value returned
return SIGNAL_REG;
}

if (getSciVersion() <= SCI_VERSION_0_LATE)
return s->r_acc; // SCI0 semantics: no value returned
return NULL_REG;
}

Expand Down Expand Up @@ -635,20 +602,20 @@ reg_t kFileIOWriteRaw(EngineState *s, int argc, reg_t *argv) {
}

reg_t kFileIOReadString(EngineState *s, int argc, reg_t *argv) {
uint16 size = argv[1].toUint16();
char *buf = new char[size];
uint16 maxsize = argv[1].toUint16();
char *buf = new char[maxsize];
uint16 handle = argv[2].toUint16();
debugC(kDebugLevelFile, "kFileIO(readString): %d, %d", handle, size);
debugC(kDebugLevelFile, "kFileIO(readString): %d, %d", handle, maxsize);
uint32 bytesRead;

#ifdef ENABLE_SCI32
if (handle == VIRTUALFILE_HANDLE)
bytesRead = s->_virtualIndexFile->readLine(buf, size);
bytesRead = s->_virtualIndexFile->readLine(buf, maxsize);
else
#endif
bytesRead = fgets_wrapper(s, buf, size, handle);
bytesRead = fgets_wrapper(s, buf, maxsize, handle);

s->_segMan->memcpy(argv[0], (const byte*)buf, size);
s->_segMan->memcpy(argv[0], (const byte*)buf, maxsize);
delete[] buf;
return bytesRead ? argv[0] : NULL_REG;
}
Expand All @@ -669,9 +636,13 @@ reg_t kFileIOWriteString(EngineState *s, int argc, reg_t *argv) {

if (f) {
f->_out->write(str.c_str(), str.size());
if (getSciVersion() <= SCI_VERSION_0_LATE)
return s->r_acc; // SCI0 semantics: no value returned
return NULL_REG;
}

if (getSciVersion() <= SCI_VERSION_0_LATE)
return s->r_acc; // SCI0 semantics: no value returned
return make_reg(0, 6); // DOS - invalid handle
}

Expand Down

0 comments on commit 098f162

Please sign in to comment.