2020-06-08 16:04:41 -04:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2021-12-26 18:47:58 +01:00
|
|
|
* 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-06-08 16:04:41 -04:00
|
|
|
*
|
|
|
|
* 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
|
2021-12-26 18:47:58 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-06-08 16:04:41 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-04-04 01:14:11 +02:00
|
|
|
/*************************************
|
|
|
|
*
|
|
|
|
* USED IN:
|
|
|
|
* Standard Director Xtra
|
|
|
|
*
|
|
|
|
*************************************/
|
|
|
|
|
2022-11-29 12:08:02 +13:00
|
|
|
/*
|
2023-04-04 01:14:11 +02:00
|
|
|
* --FileIO, Tool, 1.5.0 , 31mar92
|
|
|
|
* --© 1989-1992 MacroMind, Inc.
|
|
|
|
* --by John Thompson and Al McNeil
|
|
|
|
* ---------------------------------------------------------------------
|
|
|
|
* ---------------------------------------------------------------------
|
|
|
|
* --=METHODS=--
|
|
|
|
* --
|
|
|
|
* ISS mNew, mode, fileNameOrType --Creates a new instance of the XObject.
|
|
|
|
* -- Mode can be :
|
|
|
|
* -- "read" - Read "fileName"
|
|
|
|
* -- "?read" - Select and Read "fileType"
|
|
|
|
* -- "write" - Write "fileName"
|
|
|
|
* -- "?write" - Select and Write "fileName"
|
|
|
|
* -- "append" - Append "fileName"
|
|
|
|
* -- "?append" - Select and Append "fileName"
|
|
|
|
* -- FileType for ?read can be :
|
|
|
|
* -- "TEXT" - standard file type
|
|
|
|
* -- "trak" - cd track type
|
|
|
|
* -- etc... - Any four character combination.
|
|
|
|
* --
|
|
|
|
* X mDispose --Disposes of XObject instance.
|
|
|
|
* S mName --Returns the name of the XObject.
|
|
|
|
* /IXX mWrite, countPtr, bufferPtr --Writes out a set of chars. Returns error code.
|
|
|
|
* II mWriteChar, charNum --Writes a single character. Returns error code.
|
|
|
|
* IS mWriteString, string --Writes out a string of chars. Returns error code.
|
|
|
|
* /IXX mRead, countPtr, bufferPtr --Reads into buffer. Returns error code.
|
|
|
|
* I mReadChar --Returns a single character.
|
|
|
|
* S mReadWord --Returns the next word of an input file.
|
|
|
|
* S mReadLine --Returns the next line of an input file.
|
|
|
|
* S mReadFile --Returns the remainder of the file.
|
|
|
|
* --
|
|
|
|
* SSS mReadToken, breakString, skipString
|
|
|
|
* -- --breakstring designates character (or token) that signals to stop reading.
|
|
|
|
* -- --skipstring designates what characters (or tokens) not to read.
|
|
|
|
* I mGetPosition --Returns the file position.
|
|
|
|
* II mSetPosition, newPos --Sets the file position. Returns error code.
|
|
|
|
* I mGetLength --Returns the number of chars in the file.
|
|
|
|
* ISS mSetFinderInfo, typeString, creatorString --Sets the finder info. Returns error code.
|
|
|
|
* S mGetFinderInfo --Gets the finder info.
|
|
|
|
* S mFileName --Returns the name of the file.
|
|
|
|
* S mNativeFileName --Returns the name of the file.
|
|
|
|
* I mDelete --Delete the file and dispose of me.
|
|
|
|
* I mStatus --Returns result code of the last file io activity
|
|
|
|
* --
|
|
|
|
* SI +mError, errorCode --Returns error message string.
|
|
|
|
* -- Possible error codes:
|
|
|
|
* -- -33 :: File directory full
|
|
|
|
* -- -34 :: Volume full
|
|
|
|
* -- -35 :: Volume not found
|
|
|
|
* -- -36 :: I/O Error
|
|
|
|
* -- -37 :: Bad file name
|
|
|
|
* -- -38 :: File not open
|
|
|
|
* -- -42 :: Too many files open
|
|
|
|
* -- -43 :: File not found
|
|
|
|
* -- -56 :: No such drive
|
|
|
|
* -- -65 :: No disk in drive
|
|
|
|
* -- -120 :: Directory not found
|
|
|
|
* V mReadPICT
|
|
|
|
*/
|
2022-11-29 12:08:02 +13:00
|
|
|
|
2020-06-09 00:06:21 -04:00
|
|
|
#include "gui/filebrowser-dialog.h"
|
|
|
|
|
2020-06-12 12:21:31 -04:00
|
|
|
#include "common/memstream.h"
|
|
|
|
#include "common/savefile.h"
|
|
|
|
|
2020-06-08 16:04:41 -04:00
|
|
|
#include "director/director.h"
|
|
|
|
#include "director/lingo/lingo.h"
|
2020-07-06 15:29:36 -04:00
|
|
|
#include "director/lingo/lingo-object.h"
|
2023-04-22 23:33:40 +08:00
|
|
|
#include "director/lingo/lingo-utils.h"
|
2020-06-09 23:05:27 -04:00
|
|
|
#include "director/lingo/xlibs/fileio.h"
|
2020-06-08 16:04:41 -04:00
|
|
|
|
|
|
|
namespace Director {
|
|
|
|
|
2021-08-04 19:24:09 -04:00
|
|
|
const char *FileIO::xlibName = "FileIO";
|
|
|
|
const char *FileIO::fileNames[] = {
|
|
|
|
"FileIO",
|
2023-03-30 01:37:52 +05:30
|
|
|
"shFILEIO", // TD loads this up using openXLib("@:shFILEIO.DLL")
|
2021-11-13 23:40:22 +02:00
|
|
|
nullptr
|
2021-08-04 19:24:09 -04:00
|
|
|
};
|
2020-06-09 23:05:27 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
static MethodProto xlibMethods[] = {
|
2020-08-10 13:32:24 -04:00
|
|
|
{ "delete", FileIO::m_delete, 0, 0, 200 }, // D2
|
2021-08-10 14:11:29 -04:00
|
|
|
{ "error", FileIO::m_error, 1, 1, 200 }, // D2
|
2020-08-10 13:32:24 -04:00
|
|
|
{ "fileName", FileIO::m_fileName, 0, 0, 200 }, // D2
|
2021-08-10 14:11:29 -04:00
|
|
|
{ "getFinderInfo", FileIO::m_getFinderInfo, 0, 0, 200 }, // D2
|
2020-08-10 13:32:24 -04:00
|
|
|
{ "getLength", FileIO::m_getLength, 0, 0, 200 }, // D2
|
|
|
|
{ "getPosition", FileIO::m_getPosition, 0, 0, 200 }, // D2
|
|
|
|
{ "new", FileIO::m_new, 2, 2, 200 }, // D2
|
|
|
|
{ "readChar", FileIO::m_readChar, 0, 0, 200 }, // D2
|
2021-08-10 14:11:29 -04:00
|
|
|
{ "readFile", FileIO::m_readFile, 0, 0, 200 }, // D2
|
2020-08-10 13:32:24 -04:00
|
|
|
{ "readLine", FileIO::m_readLine, 0, 0, 200 }, // D2
|
|
|
|
{ "readToken", FileIO::m_readToken, 2, 2, 200 }, // D2
|
2022-11-29 12:08:48 +13:00
|
|
|
{ "readPict", FileIO::m_readPict, 0, 0, 300 }, // D3
|
2020-08-10 13:32:24 -04:00
|
|
|
{ "readWord", FileIO::m_readWord, 0, 0, 200 }, // D2
|
2021-08-10 14:11:29 -04:00
|
|
|
{ "setFinderInfo", FileIO::m_setFinderInfo, 2, 2, 200 }, // D2
|
2020-08-10 13:32:24 -04:00
|
|
|
{ "setPosition", FileIO::m_setPosition, 1, 1, 200 }, // D2
|
2021-08-10 14:11:29 -04:00
|
|
|
{ "status", FileIO::m_status, 0, 0, 200 }, // D2
|
2020-08-10 13:32:24 -04:00
|
|
|
{ "writeChar", FileIO::m_writeChar, 1, 1, 200 }, // D2
|
|
|
|
{ "writeString", FileIO::m_writeString, 1, 1, 200 }, // D2
|
2021-11-13 23:40:22 +02:00
|
|
|
{ nullptr, nullptr, 0, 0, 0 }
|
2020-06-08 16:04:41 -04:00
|
|
|
};
|
|
|
|
|
2021-07-29 19:12:37 -04:00
|
|
|
void FileIO::open(int type) {
|
|
|
|
if (type == kXObj) {
|
2021-07-29 14:52:45 -04:00
|
|
|
FileObject::initMethods(xlibMethods);
|
|
|
|
FileObject *xobj = new FileObject(kXObj);
|
2022-06-28 00:00:39 -05:00
|
|
|
g_lingo->exposeXObject(xlibName, xobj);
|
2021-07-29 19:12:37 -04:00
|
|
|
} else if (type == kXtraObj) {
|
|
|
|
// TODO - Implement Xtra
|
2020-06-09 23:05:27 -04:00
|
|
|
}
|
2021-07-29 19:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIO::close(int type) {
|
|
|
|
if (type == kXObj) {
|
|
|
|
FileObject::cleanupMethods();
|
|
|
|
g_lingo->_globalvars[xlibName] = Datum();
|
|
|
|
} else if (type == kXtraObj) {
|
2020-06-09 23:05:27 -04:00
|
|
|
// TODO - Implement Xtra
|
2020-06-08 16:04:41 -04:00
|
|
|
}
|
2020-06-09 23:05:27 -04:00
|
|
|
}
|
2020-06-08 16:04:41 -04:00
|
|
|
|
2020-06-09 23:05:27 -04:00
|
|
|
// Initialization/disposal
|
2020-06-08 16:04:41 -04:00
|
|
|
|
2020-07-20 09:48:38 +02:00
|
|
|
FileObject::FileObject(ObjectType objType) : Object<FileObject>("FileIO") {
|
2020-07-07 14:54:44 -04:00
|
|
|
_objType = objType;
|
|
|
|
_filename = nullptr;
|
|
|
|
_inStream = nullptr;
|
|
|
|
_outFile = nullptr;
|
|
|
|
_outStream = nullptr;
|
2020-06-08 16:04:41 -04:00
|
|
|
}
|
|
|
|
|
2020-07-20 09:48:38 +02:00
|
|
|
FileObject::FileObject(const FileObject &obj) : Object<FileObject>(obj) {
|
2020-07-07 14:54:44 -04:00
|
|
|
_filename = nullptr;
|
|
|
|
_inStream = nullptr;
|
|
|
|
_outFile = nullptr;
|
|
|
|
_outStream = nullptr;
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
FileObject::~FileObject() {
|
|
|
|
clear();
|
|
|
|
}
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
void FileObject::clear() {
|
|
|
|
if (_filename) {
|
|
|
|
delete _filename;
|
|
|
|
_filename = nullptr;
|
2020-06-09 12:26:37 -04:00
|
|
|
}
|
2022-03-14 21:00:54 +01:00
|
|
|
if (_inStream) {
|
|
|
|
delete _inStream;
|
2020-07-07 14:54:44 -04:00
|
|
|
_inStream = nullptr;
|
2020-06-09 12:26:37 -04:00
|
|
|
}
|
2020-07-07 14:54:44 -04:00
|
|
|
if (_outFile) {
|
|
|
|
_outFile->write(_outStream->getData(), _outStream->size());
|
|
|
|
_outFile->finalize();
|
|
|
|
delete _outFile;
|
|
|
|
delete _outStream;
|
|
|
|
_outFile = nullptr;
|
|
|
|
_outStream = nullptr;
|
2020-06-09 12:26:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
void FileObject::dispose() {
|
|
|
|
_disposed = true;
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:36:27 -04:00
|
|
|
void FileIO::saveFileError() {
|
|
|
|
Common::SaveFileManager *saves = g_system->getSavefileManager();
|
|
|
|
if (saves->getError().getCode()) {
|
|
|
|
warning("SaveFileManager error %d: %s", saves->getError().getCode(), saves->getErrorDesc().c_str());
|
|
|
|
g_lingo->push(Datum(kErrorIO));
|
|
|
|
} else {
|
|
|
|
g_lingo->push(Datum(kErrorFileNotFound));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:04:41 -04:00
|
|
|
void FileIO::m_new(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 00:06:21 -04:00
|
|
|
|
|
|
|
Datum d2 = g_lingo->pop();
|
|
|
|
Datum d1 = g_lingo->pop();
|
|
|
|
|
2020-06-12 13:36:27 -04:00
|
|
|
Common::SaveFileManager *saves = g_system->getSavefileManager();
|
2020-06-15 09:57:37 -04:00
|
|
|
Common::String option = d1.asString();
|
2022-03-14 21:00:54 +01:00
|
|
|
Common::String path = d2.asString();
|
|
|
|
Common::String origpath = path;
|
2023-03-30 01:36:47 +05:30
|
|
|
char dirSeparator = g_director->_dirSeparator;
|
2020-06-09 00:06:21 -04:00
|
|
|
|
2021-09-01 15:37:37 +02:00
|
|
|
Common::String prefix = g_director->getTargetName() + '-';
|
|
|
|
|
2020-06-09 00:06:21 -04:00
|
|
|
if (option.hasPrefix("?")) {
|
|
|
|
option = option.substr(1);
|
2021-09-01 15:37:37 +02:00
|
|
|
Common::String mask = prefix + "*.txt";
|
2023-03-30 01:36:47 +05:30
|
|
|
dirSeparator = '/';
|
2021-09-01 15:37:37 +02:00
|
|
|
|
2021-11-13 23:40:22 +02:00
|
|
|
GUI::FileBrowserDialog browser(nullptr, "txt", option.equalsIgnoreCase("write") ? GUI::kFBModeSave : GUI::kFBModeLoad, mask.c_str());
|
2020-06-09 00:06:21 -04:00
|
|
|
if (browser.runModal() <= 0) {
|
|
|
|
g_lingo->push(Datum(kErrorFileNotFound));
|
|
|
|
return;
|
|
|
|
}
|
2022-03-14 21:00:54 +01:00
|
|
|
path = browser.getResult();
|
|
|
|
} else if (!path.hasSuffixIgnoreCase(".txt")) {
|
|
|
|
path += ".txt";
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
2021-09-01 15:37:37 +02:00
|
|
|
// Enforce target to the created files so they do not mix up
|
2023-04-19 23:15:04 +08:00
|
|
|
Common::String filenameOrig = lastPathComponent(path, dirSeparator);
|
2022-03-14 21:00:54 +01:00
|
|
|
|
2023-04-19 23:15:04 +08:00
|
|
|
Common::String filename = filenameOrig;
|
2022-03-14 21:00:54 +01:00
|
|
|
if (!filename.hasPrefixIgnoreCase(prefix))
|
2023-04-19 23:15:04 +08:00
|
|
|
filename = prefix + filenameOrig;
|
2021-09-01 15:37:37 +02:00
|
|
|
|
2020-06-09 00:06:21 -04:00
|
|
|
if (option.equalsIgnoreCase("read")) {
|
2022-03-14 21:00:54 +01:00
|
|
|
me->_inStream = saves->openForLoading(filename);
|
|
|
|
if (!me->_inStream) {
|
|
|
|
// Maybe we're trying to read one of the game files
|
|
|
|
Common::File *f = new Common::File;
|
|
|
|
|
2022-03-20 05:11:53 +13:00
|
|
|
if (!f->open(Common::Path(pathMakeRelative(origpath), g_director->_dirSeparator))) {
|
2022-03-14 21:00:54 +01:00
|
|
|
delete f;
|
|
|
|
saveFileError();
|
|
|
|
me->dispose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
me->_inStream = f;
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
} else if (option.equalsIgnoreCase("write")) {
|
2020-06-09 12:26:37 -04:00
|
|
|
// OutSaveFile is not seekable so create a separate seekable stream
|
2020-07-07 14:54:44 -04:00
|
|
|
// which will be written to the _outFile upon disposal
|
|
|
|
me->_outFile = saves->openForSaving(filename, false);
|
|
|
|
me->_outStream = new Common::MemoryWriteStreamDynamic(DisposeAfterUse::YES);
|
|
|
|
if (!me->_outFile) {
|
2020-06-12 13:36:27 -04:00
|
|
|
saveFileError();
|
2020-08-04 12:00:29 -04:00
|
|
|
me->dispose();
|
2020-06-09 00:06:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (option.equalsIgnoreCase("append")) {
|
2022-03-14 21:00:54 +01:00
|
|
|
Common::SeekableReadStream *inFile = saves->openForLoading(filename);
|
|
|
|
if (!inFile) {
|
|
|
|
Common::File *f = new Common::File;
|
|
|
|
|
|
|
|
if (!f->open(origpath)) {
|
|
|
|
delete f;
|
|
|
|
saveFileError();
|
|
|
|
me->dispose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inFile = f;
|
2020-06-09 13:31:01 -04:00
|
|
|
}
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_outStream = new Common::MemoryWriteStreamDynamic(DisposeAfterUse::YES);
|
2022-03-14 21:00:54 +01:00
|
|
|
byte b = inFile->readByte();
|
|
|
|
while (!inFile->eos() && !inFile->err()) {
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_outStream->writeByte(b);
|
2022-03-14 21:00:54 +01:00
|
|
|
b = inFile->readByte();
|
2020-06-11 18:34:33 -04:00
|
|
|
}
|
2022-03-14 21:00:54 +01:00
|
|
|
delete inFile;
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_outFile = saves->openForSaving(filename, false);
|
|
|
|
if (!me->_outFile) {
|
2020-06-12 13:36:27 -04:00
|
|
|
saveFileError();
|
2020-08-04 12:00:29 -04:00
|
|
|
me->dispose();
|
2020-06-09 13:31:01 -04:00
|
|
|
return;
|
|
|
|
}
|
2020-06-09 00:06:21 -04:00
|
|
|
} else {
|
|
|
|
error("Unsupported FileIO option: '%s'", option.c_str());
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_filename = new Common::String(filename);
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2022-11-04 21:19:28 +08:00
|
|
|
g_lingo->push(g_lingo->_state->me);
|
2020-06-08 16:04:41 -04:00
|
|
|
}
|
|
|
|
|
2020-06-09 00:06:21 -04:00
|
|
|
// Read
|
|
|
|
|
|
|
|
void FileIO::m_readChar(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (!me->_inStream || me->_inStream->eos() || me->_inStream->err()) {
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(kErrorEOF));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
int ch = me->_inStream->readByte();
|
|
|
|
if (me->_inStream->eos() || me->_inStream->err()) {
|
2020-06-09 12:26:37 -04:00
|
|
|
ch = kErrorEOF;
|
|
|
|
}
|
|
|
|
g_lingo->push(Datum(ch));
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIO::m_readLine(int nargs) {
|
2020-06-09 12:26:37 -04:00
|
|
|
// file(mReadLine) is equivalent to file(mReadToken, "", RETURN)
|
|
|
|
// See D4 Using Lingo p. 323
|
2020-06-09 00:06:21 -04:00
|
|
|
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(""));
|
2023-03-19 21:26:36 +13:00
|
|
|
g_lingo->push(Datum("\r"));
|
2020-06-09 12:26:37 -04:00
|
|
|
FileIO::m_readToken(2);
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIO::m_readWord(int nargs) {
|
2020-06-09 12:26:37 -04:00
|
|
|
// file(mReadWord) is equivalent to file(mReadToken, " ", " " & RETURN)
|
|
|
|
// See D4 Using Lingo p. 323
|
|
|
|
|
|
|
|
g_lingo->push(Datum(" "));
|
2023-03-19 21:26:36 +13:00
|
|
|
g_lingo->push(Datum(" \r"));
|
2020-06-09 12:26:37 -04:00
|
|
|
FileIO::m_readToken(2);
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
2023-04-22 23:33:40 +08:00
|
|
|
XOBJSTUB(FileIO::m_readPict, "")
|
2022-09-07 12:09:29 +02:00
|
|
|
|
2020-06-12 14:20:19 -04:00
|
|
|
bool FileIO::charInMatchString(char ch, const Common::String &matchString) {
|
|
|
|
return matchString.contains(ch);
|
|
|
|
}
|
|
|
|
|
2020-06-09 00:06:21 -04:00
|
|
|
void FileIO::m_readToken(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 12:26:37 -04:00
|
|
|
|
|
|
|
Datum d2 = g_lingo->pop();
|
|
|
|
Datum d1 = g_lingo->pop();
|
|
|
|
|
2020-06-15 09:57:37 -04:00
|
|
|
Common::String skipString = d1.asString();
|
|
|
|
Common::String breakString = d2.asString();
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (!me->_inStream || me->_inStream->eos() || me->_inStream->err()) {
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(""));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::String tok = "";
|
|
|
|
char ch;
|
|
|
|
do {
|
2020-07-07 14:54:44 -04:00
|
|
|
ch = me->_inStream->readByte();
|
|
|
|
if (me->_inStream->eos() || me->_inStream->err()) {
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(tok));
|
|
|
|
return;
|
|
|
|
}
|
2020-06-12 14:20:19 -04:00
|
|
|
} while (charInMatchString(ch, skipString));
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-06-12 14:20:19 -04:00
|
|
|
while (!charInMatchString(ch, breakString)) {
|
2020-06-09 12:26:37 -04:00
|
|
|
tok += ch;
|
2020-07-07 14:54:44 -04:00
|
|
|
ch = me->_inStream->readByte();
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (me->_inStream->eos() || me->_inStream->err()) {
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(tok));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include the break character when the skipString is empty
|
|
|
|
if (skipString.size() == 0) {
|
|
|
|
tok += ch;
|
|
|
|
} else {
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_inStream->seek(-1, SEEK_CUR);
|
2020-06-09 12:26:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
g_lingo->push(Datum(tok));
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
2021-08-10 14:11:29 -04:00
|
|
|
void FileIO::m_readFile(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2021-08-10 14:21:43 -04:00
|
|
|
|
|
|
|
if (!me->_inStream || me->_inStream->eos() || me->_inStream->err()) {
|
|
|
|
g_lingo->push(Datum(""));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::String res;
|
|
|
|
char ch = me->_inStream->readByte();
|
|
|
|
while (!me->_inStream->eos() && !me->_inStream->err()) {
|
|
|
|
res += ch;
|
|
|
|
ch = me->_inStream->readByte();
|
|
|
|
}
|
|
|
|
|
|
|
|
g_lingo->push(res);
|
2021-08-10 14:11:29 -04:00
|
|
|
}
|
|
|
|
|
2020-06-09 00:06:21 -04:00
|
|
|
// Write
|
|
|
|
|
|
|
|
void FileIO::m_writeChar(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 12:26:37 -04:00
|
|
|
Datum d = g_lingo->pop();
|
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (!me->_outStream) {
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(kErrorReadOnly));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_outStream->writeByte(d.asInt());
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(kErrorNone));
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIO::m_writeString(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 00:06:21 -04:00
|
|
|
Datum d = g_lingo->pop();
|
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (!me->_outStream) {
|
2020-06-09 00:06:21 -04:00
|
|
|
g_lingo->push(Datum(kErrorReadOnly));
|
2020-06-09 12:26:37 -04:00
|
|
|
return;
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_outStream->writeString(d.asString());
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(kErrorNone));
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
2021-08-10 14:11:29 -04:00
|
|
|
// Getters/Setters
|
|
|
|
|
2023-04-22 23:33:40 +08:00
|
|
|
XOBJSTUB(FileIO::m_getFinderInfo, "")
|
|
|
|
XOBJSTUB(FileIO::m_setFinderInfo, 0)
|
2020-06-09 00:06:21 -04:00
|
|
|
|
|
|
|
void FileIO::m_getPosition(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (me->_inStream) {
|
|
|
|
g_lingo->push(Datum((int)me->_inStream->pos()));
|
|
|
|
} else if (me->_outStream) {
|
|
|
|
g_lingo->push(Datum((int)me->_outStream->pos()));
|
2020-06-09 12:26:37 -04:00
|
|
|
} else {
|
|
|
|
warning("FileIO: No file open");
|
|
|
|
g_lingo->push(Datum(kErrorFileNotOpen));
|
|
|
|
}
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIO::m_setPosition(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 12:26:37 -04:00
|
|
|
Datum d = g_lingo->pop();
|
|
|
|
int pos = d.asInt();
|
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (me->_inStream) {
|
|
|
|
if (pos <= me->_inStream->size()) {
|
|
|
|
me->_inStream->seek(pos, SEEK_SET);
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(kErrorNone));
|
|
|
|
} else {
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_inStream->seek(me->_inStream->size(), SEEK_SET);
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(kErrorInvalidPos));
|
|
|
|
}
|
2020-07-07 14:54:44 -04:00
|
|
|
} else if (me->_outStream) {
|
|
|
|
if (pos <= me->_outStream->size()) {
|
|
|
|
me->_outStream->seek(pos, SEEK_SET);
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(kErrorNone));
|
|
|
|
} else {
|
2020-07-07 14:54:44 -04:00
|
|
|
me->_outStream->seek(me->_outStream->size(), SEEK_SET);
|
2020-06-09 12:26:37 -04:00
|
|
|
g_lingo->push(Datum(kErrorInvalidPos));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warning("FileIO: No file open");
|
|
|
|
g_lingo->push(Datum(kErrorFileNotOpen));
|
|
|
|
}
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIO::m_getLength(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (me->_inStream) {
|
|
|
|
g_lingo->push(Datum((int)me->_inStream->size()));
|
|
|
|
} else if (me->_outStream) {
|
|
|
|
g_lingo->push(Datum((int)me->_outStream->size()));
|
2020-06-09 12:26:37 -04:00
|
|
|
} else {
|
|
|
|
warning("FileIO: No file open");
|
|
|
|
g_lingo->push(Datum(kErrorFileNotOpen));
|
|
|
|
}
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIO::m_fileName(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (me->_filename) {
|
2021-09-01 15:37:37 +02:00
|
|
|
Common::String prefix = g_director->getTargetName() + '-';
|
|
|
|
Common::String res = *me->_filename;
|
|
|
|
if (res.hasPrefix(prefix)) {
|
2023-07-05 14:32:21 -07:00
|
|
|
res = Common::String(&me->_filename->c_str()[prefix.size()]);
|
2021-09-01 15:37:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_lingo->push(Datum(res));
|
2020-06-09 12:26:37 -04:00
|
|
|
} else {
|
|
|
|
warning("FileIO: No file open");
|
|
|
|
g_lingo->push(Datum(kErrorFileNotOpen));
|
|
|
|
}
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
2023-04-22 23:33:40 +08:00
|
|
|
XOBJSTUB(FileIO::m_error, "")
|
|
|
|
XOBJSTUB(FileIO::m_status, 0)
|
2021-08-10 14:11:29 -04:00
|
|
|
|
|
|
|
// Other
|
|
|
|
|
2020-06-09 00:06:21 -04:00
|
|
|
void FileIO::m_delete(int nargs) {
|
2022-11-04 21:19:28 +08:00
|
|
|
FileObject *me = static_cast<FileObject *>(g_lingo->_state->me.u.obj);
|
2020-06-09 12:26:37 -04:00
|
|
|
|
2020-07-07 14:54:44 -04:00
|
|
|
if (me->_filename) {
|
|
|
|
Common::String filename = *me->_filename;
|
2020-06-09 12:26:37 -04:00
|
|
|
me->dispose();
|
|
|
|
if (g_system->getSavefileManager()->removeSavefile(filename)) {
|
|
|
|
g_lingo->push(Datum(kErrorNone));
|
|
|
|
} else {
|
|
|
|
g_lingo->push(Datum(kErrorIO));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warning("FileIO: No file open");
|
|
|
|
g_lingo->push(Datum(kErrorFileNotOpen));
|
|
|
|
}
|
2020-06-09 00:06:21 -04:00
|
|
|
}
|
|
|
|
|
2020-06-08 16:04:41 -04:00
|
|
|
} // End of namespace Director
|