mirror of
https://github.com/seiichiro0185/sailotp.git
synced 2024-05-19 00:50:53 +00:00
52 lines
841 B
C++
52 lines
841 B
C++
#include "fileio.h"
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
|
|
FileIO::FileIO(QObject *parent) :
|
|
QObject(parent)
|
|
{
|
|
|
|
}
|
|
|
|
QString FileIO::read()
|
|
{
|
|
if (mSource.isEmpty()){
|
|
emit error("source is empty");
|
|
return QString();
|
|
}
|
|
|
|
QFile file(mSource);
|
|
QString fileContent;
|
|
if ( file.open(QIODevice::ReadOnly) ) {
|
|
QString line;
|
|
QTextStream t( &file );
|
|
do {
|
|
line = t.readLine();
|
|
fileContent += line;
|
|
} while (!line.isNull());
|
|
|
|
file.close();
|
|
} else {
|
|
emit error("Unable to open the file");
|
|
return QString();
|
|
}
|
|
return fileContent;
|
|
}
|
|
|
|
bool FileIO::write(const QString& data)
|
|
{
|
|
if (mSource.isEmpty())
|
|
return false;
|
|
|
|
QFile file(mSource);
|
|
if (!file.open(QFile::WriteOnly | QFile::Truncate))
|
|
return false;
|
|
|
|
QTextStream out(&file);
|
|
out << data;
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
}
|