-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfigfile.cpp
125 lines (106 loc) · 2.85 KB
/
configfile.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "configfile.h"
#include <QTextStream>
#include <QDir>
#include <iostream>
#include <fstream>
#include <string>
ConfigFile::ConfigFile()
{
}
ConfigFile::ConfigFile(QString fileName)
{
ConfigFileName = fileName;
LoadConfig();
}
void ConfigFile::LoadConfig()
{
if(ConfigFileName.length()==0){return;} // name must be set first
// load file
QFile *file = OpenFile(this->ConfigFileName);
if (!file){
loadSuccess = false;
return;
}
//QByteArray content = file->readAll();
QTextStream stream(file);
QString content;
while(!stream.atEnd())
content += stream.readLine() + '\n';
ConfigContents = content;
file->close();
QString str = stream.readAll();
loadSuccess = true;
}
QFile *ConfigFile::OpenFile(QString filename)
{
QFile *file = new QFile(filename);
if (!file->exists())
return 0;
if (!file->open(QIODevice::ReadOnly|QIODevice::Text))
return 0;
return file;
}
QFile* ConfigFile::OpenFileForWriting(QString filename)
{
QFile *file = new QFile(filename);
if (!file->open(QIODevice::ReadWrite|QIODevice::Text))
return 0;
return file;
/*
if (!file->exists())
return 0;
if (!file->open(QIODevice::WriteOnly|QIODevice::Text))
return 0;
return file;
//*/
}
void ConfigFile::SaveFile(QString path)
{
QString current = QDir::currentPath();
QString destinyFile;
QStringList list = ConfigFileName.split(QDir::separator());
QString fileName = list.at(list.count()-1);
if(path!=""){
//destinyFile = current + QDir::separator() + path + QDir::separator() + fileName;
destinyFile = path + QDir::separator() + fileName;
} else {
destinyFile = current + QDir::separator() + fileName;
}
destinyFile = destinyFile.replace("/", QDir::separator());
destinyFile = destinyFile.replace("\\", QDir::separator());
DeleteOldFileIfExists(destinyFile);
QFile *file = OpenFileForWriting(destinyFile);
if (!file){
saveSuccess = false;
return;
}
std::string str = this->ConfigContents.toStdString();
file->write(str.c_str());
file->flush();
file->close();
saveSuccess=true;
}
void ConfigFile::SaveFileWithAbsolutePath(QString path)
{
path = path.replace("/", QDir::separator());
path = path.replace("\\", QDir::separator());
DeleteOldFileIfExists(path);
QFile *file = OpenFileForWriting(path);
if (!file){
saveSuccess = false;
return;
}
std::string str = this->ConfigContents.toStdString();
file->write(str.c_str());
file->flush();
file->close();
saveSuccess=true;
}
void ConfigFile::DeleteOldFileIfExists(QString filename)
{
QFile *file = new QFile(filename);
if (!file->open(QIODevice::ReadWrite|QIODevice::Text))
return;
file->remove();
file->close();
}