You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.7 KiB
73 lines
1.7 KiB
13 years ago
|
/*! @file
|
||
|
|
||
|
@id $Id$
|
||
|
*/
|
||
|
// 1 2 3 4 5 6 7 8
|
||
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||
|
|
||
|
#include <QtCore/QFile>
|
||
|
#include <QtCore/QFileInfo>
|
||
|
#include <QtCore/QFileSystemWatcher>
|
||
|
#include <QtCore/QString>
|
||
|
#include <QtCore/QStringList>
|
||
|
|
||
|
#include <cassert>
|
||
|
|
||
|
class Storage: public QObject {
|
||
|
Q_OBJECT;
|
||
|
Q_SIGNALS:
|
||
|
void changed();
|
||
|
public:
|
||
|
Storage() {}
|
||
|
bool valid() {
|
||
|
return readable() || writeable();
|
||
|
}
|
||
|
operator bool() {
|
||
|
return valid();
|
||
|
}
|
||
|
virtual bool readable() = 0;
|
||
|
virtual bool writeable() = 0;
|
||
|
virtual QStringList read() = 0;
|
||
|
virtual bool write(const QStringList& out) = 0;
|
||
|
};
|
||
|
|
||
|
class FileStorage: public Storage {
|
||
|
Q_OBJECT;
|
||
|
public:
|
||
|
FileStorage(QString file): _file(file) {
|
||
|
if (valid()) {
|
||
|
assert(connect(new QFileSystemWatcher(QStringList()<<_file.fileName(),
|
||
|
this),
|
||
|
SIGNAL(fileChanged(const QString&)),
|
||
|
SLOT(changed())));
|
||
|
}
|
||
|
}
|
||
|
bool readable() {
|
||
|
return QFileInfo(_file).exists();
|
||
|
}
|
||
|
bool writeable() {
|
||
|
return readable() || !_file.fileName().isEmpty();
|
||
|
}
|
||
|
QStringList read() {
|
||
|
QStringList res;
|
||
|
if (readable()) {
|
||
|
if (_file.open(QIODevice::ReadOnly))
|
||
|
res=QString::fromUtf8(_file.readAll()).split("\n");
|
||
|
_file.close();
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
bool write(const QStringList& out) {
|
||
|
bool res(false);
|
||
|
if (writeable()) {
|
||
|
if (_file.open(QIODevice::WriteOnly))
|
||
|
if (_file.write(out.join("\n").toUtf8())>=0) res=true;
|
||
|
_file.close();
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
private:
|
||
|
QFile _file;
|
||
|
};
|
||
|
|