-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.cpp
131 lines (113 loc) · 4.63 KB
/
dir.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
125
126
127
128
129
130
131
#include "dir.h"
bool loadxml()
{
QFile file("615A.xml");
bool ok;
uint dec;
if(file.open(QIODevice::ReadOnly)){
QXmlStreamReader xmlReader(&file);
while(!xmlReader.atEnd() && !xmlReader.hasError()){
xmlReader.readNext();
if(xmlReader.isStartElement()){
if(xmlReader.name() == "path"){
QString path = xmlReader.readElementText();
if(QFile::exists(path)) memcpy(dir, path.toUtf8().data(), strlen(path.toUtf8().data()) + 1);
else strcpy(dir, QProcessEnvironment::systemEnvironment().value("USERPROFILE").toUtf8().data());
//qDebug() << "path" << path;
}
else if(xmlReader.name() == "max-retrans-times"){
dec = xmlReader.readElementText().toULongLong(&ok, 10);
if(ok){
max_retrans_times = dec;
}
//qDebug() << "max-retrans-times" << max_retrans_times;
}
else if(xmlReader.name() == "wait-time-ms"){
dec = xmlReader.readElementText().toULongLong(&ok, 10);
if(ok){
wait_time_ms = dec;
}
}
else if(xmlReader.name() == "max-find-response-time-ms"){
dec = xmlReader.readElementText().toULongLong(&ok, 10);
if(ok){
max_find_response_time_ms = dec;
}
}
else if(xmlReader.name() == "state-file-send-interval"){
dec = xmlReader.readElementText().toULongLong(&ok, 10);
if(ok){
state_file_send_interval = dec;
}
}
}
else{
continue;
}
}
file.close();
}
else{
strcpy(dir, QProcessEnvironment::systemEnvironment().value("USERPROFILE").toUtf8().data());
}
return true;
}
bool savexml()
{
char buffer[20];
QFile file("615A.xml");
if(file.open(QIODevice::ReadWrite)){
QDomDocument doc;
// 创建XML处理类,通常用于处理第一行描述信息
QDomProcessingInstruction instruction;
// 创建XML头部格式
instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
// 添加到XML文件中
doc.appendChild(instruction);
// 创建根节点
QDomElement root = doc.createElement("paralist");
// 添加子节点path
QDomElement path = doc.createElement("path");
QDomText path_text = doc.createTextNode(dir);
path.appendChild(path_text);
root.appendChild(path);
//添加子节点max_retrans_times
QDomElement max_retrans_times_element = doc.createElement("max-retrans-times");
QDomText max_retrans_times_text = doc.createTextNode(itoa(max_retrans_times, buffer, 10));
max_retrans_times_element.appendChild(max_retrans_times_text);
root.appendChild(max_retrans_times_element);
//添加子节点wait_time_ms
QDomElement wait_time_ms_element = doc.createElement("wait-time-ms");
QDomText wait_time_ms_text = doc.createTextNode(itoa(wait_time_ms, buffer, 10));
wait_time_ms_element.appendChild(wait_time_ms_text);
root.appendChild(wait_time_ms_element);
//添加子节点max-find-response-time-ms
QDomElement max_find_response_time_ms_element = doc.createElement("max-find-response-time-ms");
QDomText max_find_response_time_ms_text = doc.createTextNode(itoa(max_find_response_time_ms, buffer, 10));
max_find_response_time_ms_element.appendChild(max_find_response_time_ms_text);
root.appendChild(max_find_response_time_ms_element);
doc.appendChild(root);
//添加子节点state-file-send-interval
newElement(&doc, &root, "state-file-send-interval" , ulonglongType, &state_file_send_interval);
QTextStream stream(&file);
doc.save(stream, 4); // 缩进四格
file.close();
}
else{
return false;
}
return true;
}
bool newElement(QDomDocument *doc, QDomElement* root, QString name, Type type, void* data){
char buffer[20];
QDomElement element = doc->createElement(name);
QDomText text;
if(type == ulonglongType){
text = doc->createTextNode(itoa(*(size_t*)data, buffer, 10));
}
else if(type == stringType){
text = doc->createTextNode((char*)data);
}
element.appendChild(text);
root->appendChild(element);
}