-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio.cpp
84 lines (69 loc) · 1.78 KB
/
fileio.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "fileio.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QFileInfo>
#include <QTextCodec>
FileIo::FileIo(QObject *parent) : QObject(parent)
{
// datapath = getenv("") + "";
//qDebug() << "datapath: " + datapath;
}
QString FileIo::read()
{
if (mSource.isEmpty()){
emit error("source is empty");
return QString();
}
QFile file(mSource);
QFileInfo fileInfo(file.fileName());
QString fileContent;
if ( file.open(QIODevice::ReadOnly) ) {
QString line;
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextStream t( &file );
t.setCodec(codec);
do {
line = t.readLine();
fileContent +=line;
} while (!line.isNull());
file.close();
return fileContent;
} else {
emit error("Unable to open the file");
return QString();
}
}
bool FileIo::write(const QString& data)
{
if (mSource.isEmpty())
return false;
QFile file(mSource);
QFileInfo fileInfo(file.fileName());
if (!file.open(QFile::WriteOnly |QFile::Append))
return false;
QTextStream out(&file);
out <<data<<" ";
file.close();
return true;
}
void FileIo::clear()
{
QFile file(datapath + mSource);
file.resize(0);
file.close();
}
/*QString FileIo::getenv(const QString envVarName) const
{
QByteArray result = qgetenv(envVarName.toStdString().c_str());
QString output = QString::fromLocal8Bit(result);
qDebug() << envVarName << " value is: " << output;
return output;
}*/
/*QString FileIo::getabsoluteroute()
{
QFile file(datapath + mSource);
QFileInfo fileInfo(file.fileName());
// qDebug() << "file path: " << fileInfo.absoluteFilePath();
return fileInfo.absoluteFilePath();
}*/