-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.cpp
408 lines (346 loc) · 10.2 KB
/
util.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Copyright (c) 2012-2024 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include <QtGlobal>
#include <QCoreApplication>
#include <QDir>
#include <QDesktopServices>
#include <QProcessEnvironment>
#include <limits.h>
#include <stdlib.h>
#include <memory>
#include "deconz/dbg_trace.h"
#include "deconz/util.h"
#ifdef PL_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <limits.h>
#include <intrin.h>
typedef unsigned __int32 uint32_t;
#else
#include <stdint.h>
#endif
/*! \ingroup utils
@{
*/
static QString resolvePath(const QString &origPath)
{
#ifdef PL_UNIX
QString path = origPath;
if (!path.isEmpty())
{
std::unique_ptr<char[]> realp(new char[PATH_MAX]);
if (path.startsWith('~'))
{
path.replace('~', QDir::homePath());
}
const char *p = realpath(qPrintable(path), realp.get());
if (p && strlen(p) > 0)
{
path = p;
}
}
return path;
#else
return origPath;
#endif
}
class CPUID {
uint32_t regs[4] = {0};
public:
explicit CPUID(unsigned i) {
#ifdef PL_WINDOWS
__cpuid((int *)regs, (int)i);
#elif defined(Q_PROCESSOR_X86)
asm volatile
("cpuid" : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
: "a" (i), "c" (0));
// ECX is set to zero for CPUID function 4
#else
(void)i;
#endif
}
const uint32_t &EAX() const {return regs[0];}
const uint32_t &EBX() const {return regs[1];}
const uint32_t &ECX() const {return regs[2];}
const uint32_t &EDX() const {return regs[3];}
};
namespace deCONZ {
/*! @addtogroup utils
@{
*/
/*! Get the value of a commandline argument
\param arg the commandline argument
\param defaultValue
\return number found or default value
*/
int appArgumentNumeric(const QString &arg, int defaultValue)
{
QStringList args = QCoreApplication::arguments();
QStringList::iterator i = args.begin();
QStringList::iterator end = args.end();
for (int n = 0; i != end; ++i, n++)
{
if (i->startsWith(arg))
{
QStringList ls = i->split('=');
if (!ls.isEmpty() && (ls[0] != arg))
{
continue;
}
if (ls.size() == 2 && !ls[1].isEmpty())
{
bool ok;
int num = ls[1].toInt(&ok);
if (ok)
{
return num;
}
else
{
DBG_Printf(DBG_INFO, "Invalid numeric app argument %s\n", qPrintable(ls[1]));
}
}
else
{
DBG_Printf(DBG_INFO, "Invalid app argument %s\n", qPrintable(*i));
}
break;
}
}
return defaultValue;
}
/*! Get the value of a commandline argument
\param arg the commandline argument
\param defaultValue
\return number found or default value
*/
QString DECONZ_DLLSPEC appArgumentString(const QString &arg, const QString &defaultValue)
{
QStringList args = QCoreApplication::arguments();
QStringList::iterator i = args.begin();
QStringList::iterator end = args.end();
for (int n = 0; i != end; ++i, n++)
{
if (i->startsWith(arg))
{
QStringList ls = i->split('=');
if (!ls.isEmpty() && (ls[0] != arg))
{
continue;
}
if (ls.size() == 2 && !ls[1].isEmpty())
{
return ls[1];
}
else
{
DBG_Printf(DBG_INFO, "Invalid app argument %s\n", qPrintable(*i));
}
break;
}
}
return defaultValue;
}
QString getStorageLocation(StorageLocation location)
{
QString path;
switch (location)
{
case HomeLocation:
{
path = QDir::homePath();
}
break;
case ApplicationsLocation:
{
QDir dir(QCoreApplication::applicationDirPath()); // leave /bin
dir.cdUp();
path = dir.absolutePath();
//path = QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation);
path.replace("//", "/");
}
break;
case ApplicationsDataLocation:
{
path = deCONZ::appArgumentString(QLatin1String("--appdata"), QLatin1String(""));
if (!path.isEmpty())
{
return resolvePath(path);
}
bool found = false;
static const char *paths[] = {
#ifdef PL_LINUX
"/.local/share/data/dresden-elektronik/deCONZ",
"/.local/share/dresden-elektronik/deCONZ",
"/.local/share/deCONZ",
#endif
#ifdef PL_WINDOWS
"/AppData/Local/dresden-elektronik/deCONZ",
#endif
0
};
auto loc = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
if (!loc.isEmpty())
{
path = loc.first();
}
for (int i = 0; paths[i]; i++)
{
if (QFile::exists(path + QLatin1String(paths[i])))
{
path += QLatin1String(paths[i]);
found = true;
break;
}
}
if (!found)
{
loc = QStandardPaths::standardLocations(QStandardPaths::DataLocation);
if (!loc.isEmpty())
{
path = loc.first();
}
}
path.replace("//", "/");
}
break;
case DdfLocation:
{
#ifdef PL_LINUX
path = deCONZ::appArgumentString(QLatin1String("--ddf-root"),
QLatin1String("/usr/share/deCONZ/devices"));
#elif defined (PL_WINDOWS)
path = deCONZ::appArgumentString(QLatin1String("--ddf-root"),
deCONZ::getStorageLocation(deCONZ::ApplicationsLocation) + QLatin1String("/devices"));
#elif defined (PL_MACOS)
path = deCONZ::appArgumentString(QLatin1String("--ddf-root"),
deCONZ::getStorageLocation(deCONZ::ApplicationsLocation) + QLatin1String("/Resources/devices"));
#else
path = deCONZ::appArgumentString(QLatin1String("--ddf-root"),
deCONZ::getStorageLocation(deCONZ::ApplicationsDataLocation) + QLatin1String("/devices"));
#endif
}
break;
case DdfBundleLocation:
{
#ifdef PL_LINUX
path = QLatin1String("/usr/share/deCONZ/bundles");
#elif defined (PL_WINDOWS)
path = deCONZ::getStorageLocation(deCONZ::ApplicationsLocation) + QLatin1String("/bundles");
#elif defined (PL_MACOS)
path = deCONZ::getStorageLocation(deCONZ::ApplicationsLocation) + QLatin1String("/Resources/bundles");
#else
path = deCONZ::getStorageLocation(deCONZ::ApplicationsDataLocation) + QLatin1String("/bundles");
#endif
}
break;
case DdfUserLocation:
{
path = deCONZ::getStorageLocation(ApplicationsDataLocation) + QLatin1String("/devices");
}
break;
case DdfBundleUserLocation:
{
path = deCONZ::getStorageLocation(ApplicationsDataLocation) + QLatin1String("/bundles");
}
break;
case DocumentsLocation:
{
auto loc = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
if (!loc.isEmpty())
{
path = loc.first();
}
}
break;
case ZcldbLocation:
path = deCONZ::getStorageLocation(ApplicationsDataLocation) + "/zcldb.txt";
break;
case ConfigLocation:
path = deCONZ::getStorageLocation(ApplicationsDataLocation) + "/config.ini";
break;
case NodeCacheLocation:
path = deCONZ::getStorageLocation(ApplicationsDataLocation) + "/session.default";
break;
case RuntimeLocation:
{
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
if (env.contains(QLatin1String("XDG_RUNTIME_DIR")))
{
path = env.value("XDG_RUNTIME_DIR") + QLatin1String("/deconz");
}
}
break;
default:
break;
}
return resolvePath(path);
}
bool isVirtualMachine()
{
CPUID cpuid(1);
// https://kb.vmware.com/s/article/1009458
return cpuid.ECX() & (1 << 31); // hypervisor bit set
}
//! @} end of group utils
} // namespace deCONZ
/*! \brief Convert utf-8 to unicode code point.
Returns pointer to remainder of text. 'codepoint' is a valid codepoint
or set to U_INVALID_UNICODE_CODEPOINT for invalid utf8.
*/
const char * U_utf8_codepoint(const char *text, unsigned *codepoint)
{
unsigned cp;
cp = (unsigned)*text & 0xFF;
text++;
if ((cp & 0x80) == 0)
{
// 1-byte ASCII
}
else if ((cp & 0xE0) == 0xC0 && text[0] != 0) /* 110 prefix 2-byte char */
{
/* 110x xxxx 10xx xxxx */
cp &= 0x1F;
cp <<= 6;
cp |= (unsigned)*text & 0x3F;
text++;
}
else if ((cp & 0xF0) == 0xE0 && text[0] != 0 && text[1] != 0) /* 1110 prefix 3-byte char */
{
/* 1110xxxx 10xxxxxx 10xxxxxx */
cp &= 0x0F;
cp <<= 6;
cp |= (unsigned)*text & 0x3F;
text++;
cp <<= 6;
cp |= (unsigned)*text & 0x3F;
text++;
}
else if ((cp & 0xF8) == 0xF0 && text[0] != 0 && text[1] != 0 && text[2] != 0) /* 11110 prefix 4-byte char */
{
/* 1110xxxx 10xxxxxx 10xxxxxx */
cp &= 0x07;
cp <<= 6;
cp |= (unsigned)*text & 0x3F;
text++;
cp <<= 6;
cp |= (unsigned)*text & 0x3F;
text++;
cp <<= 6;
cp |= (unsigned)*text & 0x3F;
text++;
}
else
{
cp = U_INVALID_UNICODE_CODEPOINT;
}
*codepoint = cp;
return text;
}