Added more functions to JS File object.
This commit is contained in:
parent
72b297980b
commit
2f2279f5ff
|
@ -126,6 +126,11 @@ FileScriptObject::~FileScriptObject()
|
|||
//printf("FileScriptObject %p Destructor: %i\n", this, numInstances);
|
||||
}
|
||||
//----------------------------------------------------
|
||||
void FileScriptObject::setTemporary(bool value)
|
||||
{
|
||||
tmp = value;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
void FileScriptObject::setFilePath(const QString& path)
|
||||
{
|
||||
if (isOpen())
|
||||
|
@ -161,7 +166,14 @@ bool FileScriptObject::open(int mode)
|
|||
return false;
|
||||
}
|
||||
|
||||
file = new QFile(filepath);
|
||||
if (tmp)
|
||||
{
|
||||
file = new QTemporaryFile();
|
||||
}
|
||||
else
|
||||
{
|
||||
file = new QFile(filepath);
|
||||
}
|
||||
|
||||
if (file == nullptr)
|
||||
{
|
||||
|
@ -184,6 +196,10 @@ bool FileScriptObject::open(int mode)
|
|||
{
|
||||
deviceMode |= QIODevice::Append;
|
||||
}
|
||||
if (mode & Truncate)
|
||||
{
|
||||
deviceMode |= QIODevice::Truncate;
|
||||
}
|
||||
|
||||
bool success = file->open(deviceMode);
|
||||
|
||||
|
@ -203,6 +219,116 @@ bool FileScriptObject::isOpen()
|
|||
return flag;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
bool FileScriptObject::isReadable()
|
||||
{
|
||||
bool flag = false;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
flag = file->isReadable();
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
bool FileScriptObject::isWritable()
|
||||
{
|
||||
bool flag = false;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
flag = file->isWritable();
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
bool FileScriptObject::atEnd()
|
||||
{
|
||||
bool retval = false;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
retval = file->atEnd();
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
bool FileScriptObject::truncate()
|
||||
{
|
||||
bool retval = false;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
retval = file->resize(0);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
bool FileScriptObject::resize(int64_t size)
|
||||
{
|
||||
bool retval = false;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
retval = file->resize(size);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
int64_t FileScriptObject::skip(int64_t size)
|
||||
{
|
||||
int64_t retval = 0;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
retval = file->skip(size);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
bool FileScriptObject::seek(int64_t pos)
|
||||
{
|
||||
bool retval = false;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
retval = file->seek(pos);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
int64_t FileScriptObject::pos()
|
||||
{
|
||||
int64_t retval = 0;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
retval = file->pos();
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
int64_t FileScriptObject::bytesAvailable()
|
||||
{
|
||||
int64_t retval = 0;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
retval = file->bytesAvailable();
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
bool FileScriptObject::flush()
|
||||
{
|
||||
bool retval = false;
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
retval = file->flush();
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
void FileScriptObject::close()
|
||||
{
|
||||
if (file != nullptr)
|
||||
|
@ -211,6 +337,10 @@ void FileScriptObject::close()
|
|||
{
|
||||
file->close();
|
||||
}
|
||||
if (tmp)
|
||||
{
|
||||
file->remove();
|
||||
}
|
||||
delete file;
|
||||
file = nullptr;
|
||||
}
|
||||
|
@ -229,30 +359,112 @@ int FileScriptObject::writeString(const QString& s)
|
|||
return bytesWritten;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
QJSValue FileScriptObject::readLine()
|
||||
int FileScriptObject::writeData(const QByteArray& s)
|
||||
{
|
||||
QJSValue obj;
|
||||
if ( (file == nullptr) || !file->isOpen())
|
||||
{
|
||||
auto* engine = FCEU::JSEngine::getCurrent();
|
||||
engine->throwError(QJSValue::GenericError, "file is not open ");
|
||||
return -1;
|
||||
}
|
||||
int bytesWritten = file->write( s );
|
||||
|
||||
return bytesWritten;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
QString FileScriptObject::readLine()
|
||||
{
|
||||
QString line;
|
||||
|
||||
if ( (file == nullptr) || !file->isOpen())
|
||||
{
|
||||
auto* engine = FCEU::JSEngine::getCurrent();
|
||||
engine->throwError(QJSValue::GenericError, "file is not open ");
|
||||
return obj;
|
||||
return line;
|
||||
}
|
||||
auto* engine = FCEU::JSEngine::getCurrent();
|
||||
|
||||
QByteArray byteArray = file->readLine();
|
||||
|
||||
QString line = QString::fromLocal8Bit(byteArray);
|
||||
line = QString::fromLocal8Bit(byteArray);
|
||||
|
||||
//printf("ReadLine: %s\n", line.toLocal8Bit().constData());
|
||||
|
||||
obj = engine->newObject();
|
||||
return line;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
QByteArray FileScriptObject::readData(unsigned int maxBytes)
|
||||
{
|
||||
QJSValue arrayBuffer;
|
||||
QByteArray byteArray;
|
||||
|
||||
obj.setProperty("text", line);
|
||||
obj.setProperty("size", static_cast<int>(line.size()));
|
||||
auto* engine = FCEU::JSEngine::getCurrent();
|
||||
|
||||
return obj;
|
||||
if ( (file == nullptr) || !file->isOpen())
|
||||
{
|
||||
engine->throwError(QJSValue::GenericError, "file is not open ");
|
||||
return byteArray;
|
||||
}
|
||||
if (maxBytes > 0)
|
||||
{
|
||||
byteArray = file->read(maxBytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
byteArray = file->readAll();
|
||||
}
|
||||
|
||||
//arrayBuffer = engine->toScriptValue(byteArray);
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
QByteArray FileScriptObject::peekData(unsigned int maxBytes)
|
||||
{
|
||||
QJSValue arrayBuffer;
|
||||
QByteArray byteArray;
|
||||
|
||||
auto* engine = FCEU::JSEngine::getCurrent();
|
||||
|
||||
if ( (file == nullptr) || !file->isOpen())
|
||||
{
|
||||
engine->throwError(QJSValue::GenericError, "file is not open ");
|
||||
return byteArray;
|
||||
}
|
||||
byteArray = file->peek(maxBytes);
|
||||
|
||||
//arrayBuffer = engine->toScriptValue(byteArray);
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
bool FileScriptObject::putChar(char c)
|
||||
{
|
||||
if ( (file == nullptr) || !file->isOpen())
|
||||
{
|
||||
auto* engine = FCEU::JSEngine::getCurrent();
|
||||
engine->throwError(QJSValue::GenericError, "file is not open ");
|
||||
return -1;
|
||||
}
|
||||
bool success = file->putChar(c);
|
||||
|
||||
return success;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
char FileScriptObject::getChar()
|
||||
{
|
||||
if ( (file == nullptr) || !file->isOpen())
|
||||
{
|
||||
auto* engine = FCEU::JSEngine::getCurrent();
|
||||
engine->throwError(QJSValue::GenericError, "file is not open ");
|
||||
return -1;
|
||||
}
|
||||
char c = -1;
|
||||
bool success = file->getChar(&c);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
c = -1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
//----------------------------------------------------
|
||||
//---- Joypad Object
|
||||
|
|
|
@ -124,13 +124,15 @@ public:
|
|||
ReadOnly = 0x01,
|
||||
WriteOnly = 0x02,
|
||||
ReadWrite = 0x03,
|
||||
Append = 0x04
|
||||
Append = 0x04,
|
||||
Truncate = 0x08
|
||||
};
|
||||
Q_ENUM(Mode);
|
||||
|
||||
private:
|
||||
static int numInstances;
|
||||
QString filepath;
|
||||
bool tmp = false;
|
||||
|
||||
QFile *file = nullptr;
|
||||
|
||||
|
@ -138,12 +140,29 @@ public slots:
|
|||
Q_INVOKABLE bool open(int mode = ReadOnly);
|
||||
Q_INVOKABLE void close();
|
||||
Q_INVOKABLE bool isOpen();
|
||||
Q_INVOKABLE bool isReadable();
|
||||
Q_INVOKABLE bool isWritable();
|
||||
Q_INVOKABLE bool flush();
|
||||
Q_INVOKABLE bool atEnd();
|
||||
Q_INVOKABLE bool truncate();
|
||||
Q_INVOKABLE bool resize(int64_t size);
|
||||
Q_INVOKABLE bool seek(int64_t pos);
|
||||
Q_INVOKABLE int64_t pos();
|
||||
Q_INVOKABLE int64_t bytesAvailable();
|
||||
Q_INVOKABLE bool isTemporary(){ return tmp; }
|
||||
Q_INVOKABLE void setTemporary(bool value);
|
||||
Q_INVOKABLE void setFilePath(const QString& path);
|
||||
Q_INVOKABLE QString fileName();
|
||||
Q_INVOKABLE QString fileSuffix();
|
||||
Q_INVOKABLE QString filePath(){ return filepath; }
|
||||
Q_INVOKABLE QJSValue readLine();
|
||||
Q_INVOKABLE QString readLine();
|
||||
Q_INVOKABLE int64_t skip(int64_t size);
|
||||
Q_INVOKABLE QByteArray readData(unsigned int maxBytes = 0);
|
||||
Q_INVOKABLE QByteArray peekData(unsigned int maxSize);
|
||||
Q_INVOKABLE int writeString(const QString& s);
|
||||
Q_INVOKABLE int writeData(const QByteArray& data);
|
||||
Q_INVOKABLE bool putChar(char c);
|
||||
Q_INVOKABLE char getChar();
|
||||
};
|
||||
|
||||
class JoypadScriptObject: public QObject
|
||||
|
|
|
@ -1298,7 +1298,7 @@ int KillJoysticks(void)
|
|||
{
|
||||
jsDev[n].close();
|
||||
}
|
||||
FCEU_printf("Shutting down SDL joystick/game constroller subsystem\n");
|
||||
FCEU_printf("Shutting down SDL joystick/gamepad subsystem\n");
|
||||
SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
|
||||
|
||||
s_jinited = 0;
|
||||
|
@ -1421,7 +1421,7 @@ int InitJoysticks(void)
|
|||
{
|
||||
return 1;
|
||||
}
|
||||
FCEU_printf("Initializing SDL joystick/game constroller subsystem\n");
|
||||
FCEU_printf("Initializing SDL joystick/gamepad subsystem\n");
|
||||
SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
|
||||
|
||||
total = SDL_NumJoysticks();
|
||||
|
|
Loading…
Reference in New Issue