-
Notifications
You must be signed in to change notification settings - Fork 1
/
systemhelper.cpp
230 lines (194 loc) · 4.74 KB
/
systemhelper.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "var.h"
#include "systemhelper.h"
#include <QMessageBox>
#include <QCoreApplication>
#include <QIcon>
#include <QDebug>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <QSysInfo>
bool SystemHelper::m_eventLoopRunning = false;
SystemHelper::SystemHelper()
{
m_eventLoopRunning = false;
}
QString SystemHelper::OSName()
{
#ifdef Q_OS_WIN
return "Windows";
#elif defined(Q_OS_LINUX)
return "Linux";
#else
return QSysInfo::productType();
#endif
}
QString SystemHelper::OSArch()
{
return QSysInfo::currentCpuArchitecture();
}
QString SystemHelper::ProcArch()
{
return QSysInfo::buildCpuArchitecture();
}
// Linux-Style Hex Dump
void SystemHelper::BinaryDump(const uint8_t buf[], const uint32_t bufsize)
{
uint32_t base = 0;
uint32_t interval = 16;
while (base < bufsize)
{
if (base + 16 < bufsize)
interval = 16;
else
interval = bufsize - base;
printf("0x%04x: ", base);
for (uint32_t i = base; i < base + 16; i++) // i for dump
{
if (i < base + interval)
printf("%02x", buf[i]);
else
{
putchar(' ');
putchar(' ');
}
if ((i+1) % 2 == 0)
putchar(' ');
if ((i+1) % 8 == 0)
putchar(' ');
}
putchar(' ');
putchar(' ');
for (uint32_t i = base; i < base + 16; i++) // i for dump
{
if (i < base + interval)
{
if (0x20 <= buf[i] && buf[i] <= 0x7E)
printf("%c", buf[i]);
else
putchar('.');
}
else
{
putchar(' ');
putchar(' ');
}
if ((i+1) % 8 == 0)
putchar(' ');
}
putchar('\n');
if (base + 16 < bufsize)
base += 16;
else
base = bufsize;
}
return;
}
// Get compiled year from gcc's __DATE__
int SystemHelper::CompileYear()
{
const char macro[16] = __DATE__;
char stmp[8] = {0};
stmp[0] = macro[7];
stmp[1] = macro[8];
stmp[2] = macro[9];
stmp[3] = macro[10];
stmp[4] = '\0';
return atoi(stmp);
}
// Get compiled month from gcc's __DATE__
int SystemHelper::CompileMonth()
{
const char macro[16] = __DATE__;
const char smonth[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int i = 0;
for (i = 0; i < 12; i++)
{
if (strstr(macro, smonth[i]) != nullptr)
return i + 1;
}
// return -1 for error
return -1;
}
// Get compiled day from gcc's __DATE__
int SystemHelper::CompileDay()
{
const char macro[16] = __DATE__;
char stmp[4] = {0};
stmp[0] = macro[4];
stmp[1] = macro[5];
stmp[2] = '\0';
return atoi(stmp);
}
void SystemHelper::SystemWarning(const QString& warnMsg)
{
QMessageBox msgBox;
msgBox.setWindowTitle("BatteryLine Error");
msgBox.setWindowIcon(QIcon(BL_ICON));
msgBox.setText(warnMsg);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
void SystemHelper::SystemError(const QString& errorMsg)
{
QMessageBox msgBox;
msgBox.setWindowTitle("BatteryLine Error");
msgBox.setWindowIcon(QIcon(BL_ICON));
msgBox.setText(errorMsg);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
QtExit(1);
}
void SystemHelper::QtExit(int code)
{
if (SystemHelper::m_eventLoopRunning)
QCoreApplication::exit(code);
else
exit(code);
}
void SystemHelper::eventLoopRunning(bool value)
{
SystemHelper::m_eventLoopRunning = value;
}
bool SystemHelper::setEventLoopRunning()
{
return SystemHelper::m_eventLoopRunning;
}
QString SystemHelper::RGB_QColorToQString(const QColor color)
{
return QString("%1, %2, %3")
.arg(color.red())
.arg(color.green())
.arg(color.blue());
}
QColor SystemHelper::RGB_QStringToQColor(const QString str)
{
QColor color;
QStringList list = str.trimmed().split(",");
if (list.count() != 3)
{
SystemError("[Setting] Invalid color value, must be form of {R, G, B}");
}
for (int i = 0; i < list.count(); i++)
{
int val = list[i].trimmed().toInt();
switch (i)
{
case 0:
color.setRed(val);
break;
case 1:
color.setGreen(val);
break;
case 2:
color.setBlue(val);
break;
}
}
return color;
}