-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (163 loc) · 6.25 KB
/
main.cpp
File metadata and controls
190 lines (163 loc) · 6.25 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*==============================================================================
** Copyright (C) 2024-2027 WingSummer
**
** This program is free software: you can redistribute it and/or modify it under
** the terms of the GNU Affero General Public License as published by the Free
** Software Foundation, version 3.
**
** 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 Affero General Public License for more
** details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <https://www.gnu.org/licenses/>.
** =============================================================================
*/
#include "class/appmanager.h"
#include "class/languagemanager.h"
#include "class/settingmanager.h"
#include "class/wingmessagebox.h"
#include "define.h"
#include <QDebug>
#include <QProcessEnvironment>
#include <QSettings>
void loadEnvConfig(int argc, char *argv[]) {
QFileInfo info(QString::fromUtf8(argv[0]));
QDir appDir(info.absoluteDir());
if (!appDir.exists(QStringLiteral("config.ini"))) {
return;
}
auto path = appDir.absoluteFilePath(QStringLiteral("config.ini"));
QSettings set(path, QSettings::IniFormat);
// General
for (auto &kv : set.childKeys()) {
qputenv(qPrintable(kv), set.value(kv).toByteArray());
}
auto groups = set.childGroups();
auto evaluate = [](const QProcessEnvironment &env,
const QString &statement) {
// Parse and evaluate statements:
// $NAME -> check existence
// $NAME=VALUE -> ignore-case full match
// $NAME==VALUE -> case-sensitive full match
// $NAME:=VALUE -> ignore-case contains
// $NAME::=VALUE -> case-sensitive contains
// VALUE: unless pure digits, must be enclosed in "" or ''
static const QRegularExpression re(
R"(^\$([A-Za-z_][A-Za-z0-9_]*)(?:\s*(==|::=|:=|=)\s*(\d+|"[^"]*"|'[^']*'))?$)");
auto match = re.match(statement);
if (!match.hasMatch()) {
qWarning("[main::loadEnvConfig] Invalid syntax: %s",
qUtf8Printable(statement));
return false;
}
auto name = match.captured(1);
auto op = match.captured(2);
auto value = match.captured(3);
// Existence check: no operator provided
if (op.isEmpty()) {
return env.contains(name);
}
if (!value.isEmpty() &&
((value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith('\'') && value.endsWith('\'')))) {
value.removeFirst().removeLast();
}
auto var = env.value(name);
// Evaluate based on operator
if (op == QStringLiteral(":=") || op == QStringLiteral("::=")) {
const QStringList items = var.split(QDir::listSeparator());
for (const QString &item : items) {
if (op == QStringLiteral(":=")) {
if (item.contains(value, Qt::CaseInsensitive)) {
return true;
}
} else {
if (item.contains(value, Qt::CaseSensitive)) {
return true;
}
}
}
return false;
}
if (op == "=") {
return QString::compare(var, value, Qt::CaseInsensitive) == 0;
} else if (op == "==") {
return var == value;
} else {
qWarning("[main::loadEnvConfig] Unknown operator: %s",
qUtf8Printable(op));
}
return false;
};
auto env = QProcessEnvironment::systemEnvironment();
constexpr auto syslen = std::char_traits<char>::length(WING_SYSTEM_NAME);
if (syslen) {
set.beginGroup(WING_SYSTEM_NAME);
for (auto &kv : set.childKeys()) {
qputenv(qPrintable(kv), set.value(kv).toByteArray());
}
set.endGroup();
}
for (auto &g : groups) {
if (evaluate(env, g)) {
set.beginGroup(g);
for (auto &kv : set.childKeys()) {
qputenv(qPrintable(kv), set.value(kv).toByteArray());
}
set.endGroup();
}
}
}
int main(int argc, char *argv[]) {
loadEnvConfig(argc, argv);
QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
QApplication::setApplicationName(QStringLiteral(APP_NAME));
QApplication::setOrganizationName(QStringLiteral(APP_ORG));
QApplication::setApplicationVersion(QStringLiteral(WINGHEX_VERSION));
#ifdef QT_DEBUG
QStandardPaths::setTestModeEnabled(true);
#endif
static_assert(CHAR_BIT == 8, APP_NAME " is only supported with 8-bit char");
try {
AppManager a(argc, argv);
auto w = a.mainWindow();
auto &set = SettingManager::instance();
switch (set.defaultWinState()) {
case Qt::WindowNoState:
w->show();
Utilities::moveToCenter(w);
break;
case Qt::WindowMinimized:
w->showMinimized();
break;
case Qt::WindowActive:
case Qt::WindowMaximized:
w->showMaximized();
break;
case Qt::WindowFullScreen:
w->showFullScreen();
break;
}
return a.exec();
} catch (CrashCode errCode) {
return int(errCode);
} catch (const std::bad_alloc &) {
auto &lang = LanguageManager::instance();
auto df = lang.defaultLocale();
// this exception can only occur when your memory are too limit or
// you are writing more than 2GB with QByteArray on 32-bit operating
// system or with Qt5.
if (QLocale::China == df.territory()) {
WingMessageBox::critical(
nullptr, QStringLiteral(APP_NAME),
QStringLiteral("崩溃啦!发生内存溢出异常!"));
} else {
WingMessageBox::critical(
nullptr, QStringLiteral(APP_NAME),
QStringLiteral("WingHexExplorer2 is out of memory. Crashed!"));
}
return int(CrashCode::OutofMemory);
}
}