summaryrefslogtreecommitdiff
path: root/io.h
blob: 0d9b2383c97677bcbe9bf64d4775a5c1d278e69a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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