diff --git a/scriptengine.cpp b/scriptengine.cpp index b9f0141..c15a5c6 100644 --- a/scriptengine.cpp +++ b/scriptengine.cpp @@ -26,7 +26,9 @@ ScriptEngine::ScriptEngine(Database *database, BatchProcessing *parent) QJSValue core = _jsEngine->newQObject(this); _jsEngine->globalObject().setProperty("core", core); QJSValue fitsRecordObject = _jsEngine->newQMetaObject(&FITSRecordModify::staticMetaObject); + QJSValue textFile = _jsEngine->newQMetaObject(&TextFile::staticMetaObject); _jsEngine->globalObject().setProperty("FITSRecordModify", fitsRecordObject); + _jsEngine->globalObject().setProperty("TextFile", textFile); _semaphore.release(_pool->maxThreadCount()); _pool->setThreadPriority(QThread::LowPriority); @@ -149,6 +151,25 @@ void ScriptEngine::plot(const QJSValue &pointsArray) } } +QJSValue ScriptEngine::openFile(const QString &fileName, const QString &mode) +{ + QFileInfo info(fileName); + if(!info.isAbsolute()) + info = QFileInfo(outputDir() + fileName); + + TextFile *textFile = new TextFile; + if(textFile->open(info.absoluteFilePath(), mode)) + { + return _jsEngine->newQObject(textFile); + } + else + { + logError("Failed to open file " + fileName); + delete textFile; + return false; + } +} + bool ScriptEngine::convert(File *file, QString &outpath, const QString &format, const QVariantMap ¶ms, bool async) { QString path; @@ -835,4 +856,60 @@ void FITSRecordModify::addKeyword(const QString &key, const QVariant &value, con _update.append({key.toLatin1(), value, comment.toLatin1()}); } +bool TextFile::open(const QString &path, const QString &mode) +{ + _fr.setFileName(path); + QIODevice::OpenMode openMode; + if(mode == "r") + openMode = QIODevice::ReadOnly; + else if(mode == "w") + openMode = QIODevice::WriteOnly; + else if(mode == "a") + openMode = QIODevice::WriteOnly | QIODevice::Append; + else if(mode == "r+") + openMode = QIODevice::ReadWrite | QIODevice::ExistingOnly; + else if(mode == "w+") + openMode = QIODevice::ReadWrite; + else if(mode == "a+") + openMode = QIODevice::ReadWrite | QIODevice::Append; + else + return false; + + openMode |= QIODevice::Text;//always open as text + return _fr.open(openMode); +} + +void TextFile::write(const QString &data) +{ + _fr.write(data.toUtf8()); +} + +QString TextFile::read(int maxlen) +{ + QByteArray data = _fr.read(maxlen); + return data; +} + +QString TextFile::readLine() +{ + QByteArray data = _fr.readLine(); + return QString::fromUtf8(data); +} + +QString TextFile::readAll() +{ + QByteArray data = _fr.readAll(); + return QString::fromUtf8(data); +} + +bool TextFile::seek(qint64 offset) +{ + return _fr.seek(offset); +} + +qint64 TextFile::pos() +{ + return _fr.pos(); +} + } diff --git a/scriptengine.h b/scriptengine.h index 77542c0..add3199 100644 --- a/scriptengine.h +++ b/scriptengine.h @@ -8,7 +8,7 @@ #include #include #include "database.h" -#include "imageinfo.h" +#include "imageinfodata.h" class BatchProcessing; class Solver; @@ -48,6 +48,7 @@ public: Q_INVOKABLE QJSValue getFloat(const QString &label = QString(), double value = 0, int decimals = 3) const; Q_INVOKABLE QJSValue getItem(const QStringList &items, const QString &label = "", int current = 0) const; Q_INVOKABLE void plot(const QJSValue &pointsArray); + Q_INVOKABLE QJSValue openFile(const QString &fileName, const QString &mode = "r"); bool convert(File *file, QString &outpath, const QString &format, const QVariantMap ¶ms, bool async); #ifdef PLATESOLVER Q_INVOKABLE void setSolverProfile(int index); @@ -143,6 +144,20 @@ public: Q_INVOKABLE void addKeyword(const QString &key, const QVariant &value, const QString &comment = QString()); }; +class TextFile : public QObject +{ + Q_OBJECT + QFile _fr; +public: + bool open(const QString &path, const QString &mode); + Q_INVOKABLE void write(const QString &data); + Q_INVOKABLE QString read(int maxlen); + Q_INVOKABLE QString readLine(); + Q_INVOKABLE QString readAll(); + Q_INVOKABLE bool seek(qint64 offset); + Q_INVOKABLE qint64 pos(); +}; + } #endif // SCRIPTENGINE_H