forked from royqh1979/RedPanda-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolusagemanager.cpp
121 lines (112 loc) · 3.9 KB
/
symbolusagemanager.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
/*
* Copyright (C) 2020-2022 Roy Qu ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "symbolusagemanager.h"
#include "settings.h"
#include "systemconsts.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
SymbolUsageManager::SymbolUsageManager(QObject *parent) : QObject(parent)
{
}
void SymbolUsageManager::load()
{
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config())
+ DEV_SYMBOLUSAGE_FILE;
if (!fileExists(filename))
return;
QFile file(filename);
if (!file.open(QFile::ReadOnly)) {
QMessageBox::critical(nullptr,
tr("Load symbol usage info failed"),
tr("Can't open symbol usage file '%1' for read.")
.arg(filename));
}
QByteArray contents = file.readAll().trimmed();
if (contents.isEmpty())
return;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(contents,&error);
if (error.error != QJsonParseError::NoError) {
QMessageBox::critical(nullptr,
tr("Load symbol usage info failed"),
tr("Can't parse symbol usage file '%1': %2")
.arg(filename,
error.errorString()));
}
mUsages.clear();
QJsonArray array = doc.array();
for(const QJsonValue& val:array) {
QJsonObject obj = val.toObject();
QString fullname = obj["symbol"].toString();
int count = obj["count"].toInt();
PSymbolUsage usage = std::make_shared<SymbolUsage>();
usage->fullName = fullname;
usage->count = count;
mUsages.insert(fullname,usage);
}
}
void SymbolUsageManager::save()
{
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config())
+ DEV_SYMBOLUSAGE_FILE;
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
QMessageBox::critical(nullptr,
tr("Save symbol usage info failed"),
tr("Can't open symbol usage file '%1' for write.")
.arg(filename));
}
QJsonArray array;
foreach (const PSymbolUsage& usage,mUsages) {
QJsonObject object;
object["symbol"]=usage->fullName;
object["count"]=usage->count;
array.append(object);
}
QJsonDocument doc;
doc.setArray(array);
if (file.write(doc.toJson())<0) {
QMessageBox::critical(nullptr,
tr("Save symbol usage info failed"),
tr("Write to symbol usage file '%1' failed.")
.arg(filename));
}
}
void SymbolUsageManager::reset()
{
mUsages.clear();
save();
}
PSymbolUsage SymbolUsageManager::findUsage(const QString &fullName) const
{
return mUsages.value(fullName,PSymbolUsage());
}
void SymbolUsageManager::updateUsage(const QString &symbol, int count)
{
PSymbolUsage usage = mUsages.value(symbol,PSymbolUsage());
if (usage) {
usage->count = count;
} else {
usage = std::make_shared<SymbolUsage>();
usage->fullName = symbol;
usage->count = count;
mUsages.insert(symbol,usage);
}
}