-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
81 lines (74 loc) · 2 KB
/
config.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
/*
* @Author: Tfly6 [email protected]
* @Date: 2023-07-22 21:57:16
* @LastEditors: Tfly6 [email protected]
* @LastEditTime: 2023-07-23 21:06:51
* @Description: 解析配置文件
*/
#include "config.h"
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QJsonObject> // { }
//#include <QJsonArray> // [ ]
#include <QJsonDocument> // 解析Json
#include <QJsonValue> // int float double bool null { } [ ]
#include <QJsonParseError>
Config::Config(QObject *parent) : QObject(parent)
{
init();
}
void Config::init()
{
QFile file("config.json");
if(!file.exists()){
file.open(QIODevice::WriteOnly);
file.close();
return;
}
if (!file.open(QFile::ReadOnly | QFile::Text)) {
qDebug() << "can't open error!";
return;
}
qDebug()<<"ok";
QTextStream stream(&file);
//stream.setCodec("UTF-8"); // 设置读取编码是UTF8
QString str = stream.readAll();
con = str;
file.close();
}
QString Config::readConfig()
{
QString ret;
if(con.isEmpty())
{
qDebug()<<"文件内容为空";
return ret;
}
// QJsonParseError类用于在JSON解析期间报告错误。
QJsonParseError jsonError;
// 将json解析为UTF-8编码的json文档,并从中创建一个QJsonDocument。
// 如果解析成功,返回QJsonDocument对象,否则返回null
QJsonDocument doc = QJsonDocument::fromJson(con.toUtf8(), &jsonError);
// 判断是否解析失败
if (jsonError.error != QJsonParseError::NoError && !doc.isNull()) {
qDebug() << "Json格式错误!" << jsonError.error;
return ret;
}
QJsonObject rootObj = doc.object();
QJsonValue nameValue = rootObj.value("path");
bool ok = fileExists(nameValue.toString());
if(!ok)
{
qDebug()<<"环境错误";
return ret;
}
ret = nameValue.toString();
//qDebug() << "path = " << ret;
return ret;
}
bool Config::fileExists(QString fileName)
{
QFileInfo fi(fileName);
return fi.isFile();
}