Atlas-Launcher/source/javascript/module/file_system/object/FileJsObject.hpp

55 lines
1.3 KiB
C++

#pragma once
#include <cstdlib>
#include <QFile>
#include <QJSValue>
#include <QObject>
#include <QTextStream>
#include "javascript/engine/AtlasJsEngine.hpp"
/**
* Represent a JavaScript file.
* This is a wrapper around `QFile`
*/
class FileJsObject : public QObject {
Q_OBJECT
public:
explicit FileJsObject(AtlasJsEngine* engine, const std::filesystem::path& virtualPath);
/**
* Open the file to access its content.
* @param mode the opening mode of the file, using "r", "w", "+", etc...
*/
Q_INVOKABLE void open(const QString& mode = QStringLiteral("r"));
/**
* Read the content of the file.
* @param size the size of the content to read.
* @return the content of the file
*/
Q_INVOKABLE QJSValue read(size_t size = std::numeric_limits<std::size_t>::max()) const;
/**
* Write data into the file
* @param data the data to write into the file
* @return the size of the data written
*/
Q_INVOKABLE std::size_t write(const QJSValue& data) const;
Q_INVOKABLE std::size_t tell() const;
Q_INVOKABLE void seek(std::size_t position) const;
/**
* Close the file.
*/
Q_INVOKABLE void close() const;
private:
AtlasJsEngine* engine;
std::unique_ptr<QFile> _internal;
QIODevice::OpenMode mode;
std::unique_ptr<QTextStream> textStream;
};