diff options
| author | Timotej Lazar <timotej.lazar@araneo.si> | 2021-06-14 19:09:53 +0200 |
|---|---|---|
| committer | Timotej Lazar <timotej.lazar@araneo.si> | 2021-06-14 19:13:14 +0200 |
| commit | 8d57bfb1aef3b71557bc408154ee028751fd688e (patch) | |
| tree | 22ee6eaa22a3cfc7dfd4f938be16a6e28d7eaf1f /io.h | |
First commit
There was history before but now there is no more.
Diffstat (limited to 'io.h')
| -rw-r--r-- | io.h | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -0,0 +1,37 @@ +// SPDX-License-Identifier: Unlicense + +#ifndef IO_H +#define IO_H + +#include <QFile> +#include <QObject> +#include <QString> +#include <QUrl> +#include <QtDebug> + +class IO : public QObject { + Q_OBJECT +public slots: + void write(const QUrl &url, const QString &data) { + QFile file{urlToPath(url)}; + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) + file.write(data.toUtf8()); + else + qWarning() << "error opening file" << url; + } + + QString read(const QUrl &url) { + QFile file{urlToPath(url)}; + if (file.open(QIODevice::ReadOnly)) + return file.readAll(); + qWarning() << "error opening file" << url; + return {}; + } + +private: + static const QString urlToPath(const QUrl &path) { + return path.scheme() == "qrc" ? (":" + path.path()) : path.toLocalFile(); + } +}; + +#endif |
