-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile.cpp
executable file
·56 lines (44 loc) · 1.58 KB
/
file.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
#include "file.h"
#include <QFile>
#include <QStandardPaths>
#include <QDir>
File::File(QObject *parent) : QObject(parent)
{
}
QString File::readFile(QString file, QString homeFolders) const{
// Checking if parameters are null or empty.
if(file.isEmpty() || file.isNull()){
return "ERR: 1st parameter is empty or null." + file;
}
// Creating the file and opening.
QString absolutePath = "";
if (homeFolders == "config") {
absolutePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/";
QDir().mkpath(absolutePath);
}
QFile fi((absolutePath + file));
if(!fi.open(QFile::ReadOnly | QFile::Text))
return "ERR: Can't access the file\n(maybe some program using it):" + file;
// Reading.
QString content = fi.readAll();
fi.close();
return content;
}
QString File::saveFile(QString file, QString data, QString homeFolders) const{
// Checking if parameters are null or empty.
if(file.isEmpty() || file.isNull()){
return "ERR: 1st parameter is empty or null." + file;
}
QString absolutePath = "";
if (homeFolders == "config") {
absolutePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/";
QDir().mkpath(absolutePath);
}
QFile fi((absolutePath + file));
if(!fi.open(QFile::WriteOnly | QFile::Text))
return "ERR: Can't access the file\n(maybe some program using it):" + file;
// Write the file.
fi.write(data.toUtf8());
fi.close();
return "";
}