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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
|
#include "common/utils.h"
#include "common/global.h"
#include "dbobjecttype.h"
#include "rsa/RSA.h"
#include "common/compatibility.h"
#include <QTextCodec>
#include <QString>
#include <QSet>
#include <QVariant>
#include <QDateTime>
#include <QSysInfo>
#include <QDebug>
#include <QRegularExpression>
#include <QDir>
#include <QByteArray>
#include <QBitArray>
#include <QDataStream>
#include <QRandomGenerator>
#ifdef Q_OS_LINUX
#include <sys/utsname.h>
#include <QFileInfo>
#endif
#ifdef Q_OS_WIN
#include <windows.h>
#include <tchar.h>
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
bool is64BitWindows()
{
#if defined(_WIN64)
return true; // 64-bit programs run only on Win64
#elif defined(_WIN32)
// 32-bit programs run on both 32-bit and 64-bit Windows
// so must sniff
BOOL f64 = false;
LPFN_ISWOW64PROCESS fnIsWow64Process;
fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
if (fnIsWow64Process)
{
return fnIsWow64Process(GetCurrentProcess(), &f64) && f64;
}
return false;
#else
return true; // Win64 does not support Win16
#endif
}
#endif
void initUtils()
{
qRegisterMetaType<QList<int>>("QList<int>");
qRegisterMetaType<DbObjectType>("DbObjectType");
qRegisterMetaType<QList<QPair<QString, QString>>>("QList<QPair<QString, QString>>");
}
bool isXDigit(const QChar& c)
{
return c.isDigit() || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
QChar charAt(const QString& str, int pos)
{
if (pos < 0 || pos >= str.size())
return QChar(0);
return str[pos];
}
int rand(int min, int max)
{
return QRandomGenerator::system()->generate() % (max-min) + min;
}
QString randStr(int length, bool numChars, bool whiteSpaces)
{
static const char* alphaNumChars = " abcdefghijklmnopqrstuvwxyz1234567890";
int start = 1;
int range = start + (numChars ? 36 : 26);
if (whiteSpaces)
{
start--;
range++;
}
QString output = "";
for (int i = 0; i < length; i++)
output += alphaNumChars[rand(start, range)];
return output;
}
QString randStr(int length, const QString& charCollection)
{
int range = charCollection.size();
QString output = "";
for (int i = 0; i < length; i++)
output += charCollection[rand(0, range)];
return output;
}
QString randBinStr(int length)
{
char* output = new char[length];
for (int i =0; i < length; i++)
output[i] = rand(0, 256);
return QString::fromLatin1(output, length);
}
QString randStrNotIn(int length, const QSet<QString> set, bool numChars, bool whiteSpaces)
{
if (length == 0)
return "";
QString outStr;
do
{
outStr = randStr(length, numChars, whiteSpaces);
}
while (set.contains(outStr));
return outStr;
}
Range::Range() :
from(0), to(0)
{
}
Range::Range(qint64 from, qint64 to)
:from(from), to(to)
{
fromValid = true;
toValid = true;
}
void Range::setFrom(qint64 from)
{
this->from = from;
fromValid = true;
}
void Range::setTo(qint64 to)
{
this->to = to;
toValid = true;
}
qint64 Range::getFrom() const
{
return from;
}
qint64 Range::getTo() const
{
return to;
}
bool Range::isValid() const
{
return fromValid && toValid && from <= to;
}
bool Range::contains(qint64 position) const
{
return position >= from && position <= to;
}
bool Range::overlaps(const Range& other) const
{
return overlaps(other.from, other.to);
}
bool Range::overlaps(qint64 from, qint64 to) const
{
return (this->from >= from && this->from <= to) || (this->to >= from && this->to <= to);
}
Range Range::common(const Range& other) const
{
return common(other.from, other.to);
}
Range Range::common(qint64 from, qint64 to) const
{
if (!isValid() || from > to)
return Range();
if (this->from >= from)
{
if (this->from > to)
return Range();
if (this->to < to)
return Range(this->from, this->to);
else
return Range(this->from, to);
}
else
{
if (from > this->to)
return Range();
if (to < this->to)
return Range(from, to);
else
return Range(from, this->to);
}
}
qint64 Range::length() const
{
return to - from + 1;
}
QString generateUniqueName(const QString& baseName, const QStringList& existingNames, Qt::CaseSensitivity cs)
{
QString name = baseName;
int i = 0;
while (existingNames.contains(name, cs))
name = baseName + QString::number(i++);
return name;
}
bool isNumeric(const QVariant& value)
{
bool ok;
value.toLongLong(&ok);
if (ok)
return true;
value.toDouble(&ok);
return ok;
}
QString rStrip(const QString& str)
{
if (str.isNull())
return str;
for (int n = str.size() - 1; n >= 0; n--)
{
if (!str.at(n).isSpace())
return str.left(n + 1);
}
return "";
}
QStringList tokenizeArgs(const QString& str)
{
QStringList results;
QString token;
bool quote = false;
bool escape = false;
QChar c;
for (int i = 0; i < str.length(); i++)
{
c = str[i];
if (escape)
{
token += c;
}
else if (c == '\\')
{
escape = true;
}
else if (quote)
{
if (c == '"')
{
results << token;
token.clear();
quote = false;
}
else
{
token += c;
}
}
else if (c == '"')
{
if (token.length() > 0)
token += c;
else
quote = true;
}
else if (c.isSpace())
{
if (token.length() > 0)
{
results << token;
token.clear();
}
}
else
{
token += c;
}
}
if (token.length() > 0)
results << token;
return results;
}
QStringList prefixEach(const QString& prefix, const QStringList& list)
{
QStringList result;
for (const QString& item : list)
result << (prefix + item);
return result;
}
QByteArray hashToBytes(const QHash<QString,QVariant>& hash)
{
QByteArray bytes;
QDataStream stream(&bytes, QIODevice::WriteOnly);
stream << QVariant(hash);
return bytes;
}
QHash<QString,QVariant> bytesToHash(const QByteArray& bytes)
{
if (bytes.isNull())
return QHash<QString,QVariant>();
QVariant deserializedValue;
QDataStream stream(bytes);
stream >> deserializedValue;
return deserializedValue.toHash();
}
int indexOf(const QStringList& list, const QString& value, Qt::CaseSensitivity cs)
{
return indexOf(list, value, 0, cs);
}
int indexOf(const QStringList& list, const QString& value, int from, Qt::CaseSensitivity cs)
{
if (cs == Qt::CaseSensitive)
return list.indexOf(value, from);
int cnt = list.size();
for (int i = from; i < cnt; i++)
if (list[i].compare(value, cs) == 0)
return i;
return -1;
}
QString pad(const QString& str, int length, const QChar& fillChar)
{
if (str.length() >= abs(length))
return str;
QString result = str;
QString fill = QString(fillChar).repeated(abs(length) - str.length());
if (length >= 0)
return result.append(fill);
else
return result.prepend(fill);
}
QString center(const QString& str, int length, const QChar& fillChar)
{
if (str.length() >= length)
return str;
QString result = str;
QString fillLeft = QString(fillChar).repeated((length - str.length()) / 2);
QString fillRight = fillLeft;
if ((fillLeft.length() + fillRight.length() + str.length()) < length)
fillLeft += fillChar;
return result.prepend(fillLeft).append(fillRight);
}
QString longest(const QStringList& strList)
{
int max = 0;
QString result;
for (const QString& str : strList)
{
if (str.size() > max)
{
result = str;
max = str.size();
}
}
return result;
}
QString shortest(const QStringList& strList)
{
int max = INT_MAX;
QString result;
for (const QString& str : strList)
{
if (str.size() < max)
{
result = str;
max = str.size();
}
}
return result;
}
QString longestCommonPart(const QStringList& strList)
{
if (strList.size() == 0)
return QString();
QString common;
QString first = strList.first();
for (int i = 0; i < first.length(); i++)
{
common += first[i];
for (const QString& str : strList)
{
if (!str.startsWith(common))
return common.left(i);
}
}
return common;
}
QStringList applyMargin(const QString& str, int margin)
{
QStringList lines;
QString line;
for (QString word : str.split(" "))
{
if (((line + word).length() + 1) > margin)
{
if (!line.isEmpty())
{
lines << line;
line.clear();
}
while ((line + word).length() > margin)
{
line += word.leftRef(margin);
lines << line;
word = word.mid(margin);
}
}
if (!line.isEmpty())
line += " ";
line += word;
if (line.endsWith("\n"))
{
lines << line.trimmed();
line.clear();
}
}
if (!line.isEmpty())
lines << line;
if (lines.size() == 0)
lines << QString();
return lines;
}
QDateTime toGregorian(double julianDateTime)
{
int Z = (int)julianDateTime;
double F = julianDateTime - Z;
int A;
if (Z < 2299161)
{
A = Z;
}
else
{
int alpha = (int)((Z - 1867216.25)/36524.25);
A = Z + 1 + alpha - (int)(alpha / 4);
}
int B = A + 1524;
int C = (int)((B - 122.1) / 365.25);
int D = (int)(365.25 * C);
int E = (int)((B-D) / 30.6001);
int DD = B - D - (int)(30.6001 * E) + F;
int MM = (E <= 13) ? E - 1 : E - 13;
int YYYY = (MM <= 2) ? C - 4715 : C - 4716;
int mmmBase = qRound(F * 86400000.0);
int mmm = mmmBase % 1000;
int ssBase = mmmBase / 1000;
int ss = ssBase % 60;
int mmBase = ssBase / 60;
int mm = mmBase % 60;
int hh = (mmBase / 60) + 12;
if (hh >= 24)
{
hh -= 24;
DD++;
}
QDateTime dateTime;
dateTime.setDate(QDate(YYYY, MM, DD));
dateTime.setTime(QTime(hh, mm, ss, mmm));
return dateTime;
}
double toJulian(const QDateTime& gregDateTime)
{
QDate date = gregDateTime.date();
QTime time = gregDateTime.time();
return toJulian(date.year(), date.month(), date.day(), time.hour(), time.minute(), time.second(), time.msec());
}
double toJulian(int year, int month, int day, int hour, int minute, int second, int msec)
{
int a = (14 - month) / 12;
int y = year + 4800 + a;
int m = month + 12 * a - 3;
// Julian Day
int jnd = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
// Julian Day + Julian Time
double jndt = jnd + (hour - 12.0) / 24.0 + minute / 1440.0 + second / 86400.0 + msec / 86400000.0;
return jndt;
}
QString formatFileSize(quint64 size)
{
quint64 bytes = size;
quint64 kb = 0;
quint64 mb = 0;
quint64 gb = 0;
QStringList words;
if (bytes > (1024*1024*1024))
{
gb = bytes / (1024*1024*1024);
bytes %= (1024*1024*1024);
words << QString("%1GB").arg(gb);
}
if (bytes > (1024*1024))
{
mb = bytes / (1024*1024);
bytes %= (1024*1024);
words << QString("%1MB").arg(mb);
}
if (bytes > 1024)
{
kb = bytes / 1024;
bytes %= 1024;
words << QString("%1KB").arg(kb);
}
if (bytes > 0)
words << QString("%1B").arg(bytes);
return words.join(" ");
}
QString formatTimePeriod(int msecs)
{
int hours = 0;
int minutes = 0;
int seconds = 0;
QStringList words;
if (msecs > (1000*60*60))
{
hours = msecs / (1000*60*60);
msecs %= (1000*60*60);
words << QString("%1h").arg(hours);
}
if (msecs > (1000*60))
{
minutes = msecs / (1000*60);
msecs %= (1000*60);
words << QString("%1m").arg(minutes);
}
if (msecs > (1000))
{
seconds = msecs / 1000;
msecs %= 1000;
words << QString("%1s").arg(seconds);
}
if (msecs > 0)
words << QString("%1ms").arg(msecs);
return words.join(" ");
}
QStringList common(const QStringList& list1, const QStringList& list2, Qt::CaseSensitivity cs)
{
QStringList newList;
for (const QString& str : list1)
{
if (list2.contains(str, cs))
newList << str;
}
return newList;
}
QStringList textCodecNames()
{
QList<QByteArray> codecs = QTextCodec::availableCodecs();
QStringList names;
QSet<QString> nameSet;
for (QByteArray& codec : codecs)
nameSet << QString::fromLatin1(codec.constData());
names = nameSet.values();
sSort(names);
return names;
}
QTextCodec* codecForName(const QString& name)
{
return QTextCodec::codecForName(name.toLatin1());
}
QTextCodec* defaultCodec()
{
return QTextCodec::codecForLocale();
}
QString defaultCodecName()
{
return QString::fromLatin1(QTextCodec::codecForLocale()->name());
}
QStringList splitByLines(const QString& str)
{
return str.split(QRegExp("\r?\n"));
}
QString joinLines(const QStringList& lines)
{
#ifdef Q_OS_WIN
static_char* newLine = "\r\n";
#else
static_char* newLine = "\n";
#endif
return lines.join(newLine);
}
int sum(const QList<int>& integers)
{
int res = 0;
for (int i : integers)
res += i;
return res;
}
QString getOsString()
{
#if defined(Q_OS_WIN)
QString os = "Windows";
switch (QSysInfo::WindowsVersion)
{
case QSysInfo::WV_XP:
os += " XP";
break;
case QSysInfo::WV_2003:
os += " 2003";
break;
case QSysInfo::WV_VISTA:
os += " Vista";
break;
case QSysInfo::WV_WINDOWS7:
os += " 7";
break;
case QSysInfo::WV_WINDOWS8:
os += " 8";
break;
case QSysInfo::WV_WINDOWS8_1:
os += " 8.1";
break;
#if QT_VERSION >= 0x050500
case QSysInfo::WV_WINDOWS10:
os += " 10";
break;
#endif
case QSysInfo::WV_32s:
case QSysInfo::WV_95:
case QSysInfo::WV_98:
case QSysInfo::WV_Me:
case QSysInfo::WV_DOS_based:
case QSysInfo::WV_NT:
case QSysInfo::WV_2000:
case QSysInfo::WV_NT_based:
case QSysInfo::WV_CE:
case QSysInfo::WV_CENET:
case QSysInfo::WV_CE_5:
case QSysInfo::WV_CE_6:
case QSysInfo::WV_CE_based:
#if QT_VERSION >= 0x050500
case QSysInfo::WV_None:
#endif
break;
}
#elif defined(Q_OS_LINUX)
QString os = "Linux";
utsname uts;
if (uname(&uts) != 0)
{
qWarning() << "Error while calling uname() for OS version. Error code: " << errno;
}
else
{
os += " " + QString::fromLatin1(uts.release);
}
#elif defined(Q_OS_OSX)
QString os = "MacOS X";
switch (QSysInfo::MacintoshVersion)
{
case QSysInfo::MV_10_4:
os += " 10.4 Tiger";
break;
case QSysInfo::MV_10_5:
os += " 10.5 Leopard";
break;
case QSysInfo::MV_10_6:
os += " 10.6 Snow Leopard";
break;
case QSysInfo::MV_10_7:
os += " 10.7 Lion";
break;
case QSysInfo::MV_10_8:
os += " 10.8 Mountain Lion";
break;
case QSysInfo::MV_10_9:
os += " 10.9 Mavericks";
break;
case QSysInfo::MV_10_10:
os += " 10.10 Yosemite";
break;
case QSysInfo::MV_10_11:
os += " 10.11 El Capitan";
break;
case QSysInfo::MV_10_12:
os += " 10.12 Sierra";
break;
case QSysInfo::MV_9:
case QSysInfo::MV_10_0:
case QSysInfo::MV_10_1:
case QSysInfo::MV_10_2:
case QSysInfo::MV_10_3:
case QSysInfo::MV_IOS:
case QSysInfo::MV_IOS_4_3:
case QSysInfo::MV_IOS_5_0:
case QSysInfo::MV_IOS_5_1:
case QSysInfo::MV_IOS_6_0:
case QSysInfo::MV_IOS_6_1:
case QSysInfo::MV_IOS_7_0:
case QSysInfo::MV_IOS_7_1:
case QSysInfo::MV_IOS_8_0:
case QSysInfo::MV_IOS_8_1:
case QSysInfo::MV_IOS_8_2:
case QSysInfo::MV_IOS_8_3:
case QSysInfo::MV_IOS_8_4:
case QSysInfo::MV_IOS_9_0:
case QSysInfo::MV_None:
case QSysInfo::MV_Unknown:
break;
}
#elif defined(Q_OS_UNIX)
QString os = "Unix";
#else
QString os = "Unknown";
#endif
os += ", ";
#ifdef Q_OS_WIN
os += (is64BitWindows() ? "64" : "32");
#else
os += QString::number(QSysInfo::WordSize);
#endif
os += "bit";
return os;
}
DistributionType getDistributionType()
{
#if defined(Q_OS_OSX)
return DistributionType::OSX_BUNDLE;
#elif defined(PORTABLE_CONFIG)
return DistributionType::PORTABLE;
#else
return DistributionType::OS_MANAGED;
#endif
}
bool validateEmail(const QString& email)
{
static const QRegularExpression re("^[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-\\.]+$");
return re.match(email).hasMatch();
}
bool isHex(const QString& str)
{
bool ok;
str.toLongLong(&ok, 16);
return ok;
}
bool isHex(const QChar& c)
{
return isHex(c.toLatin1());
}
bool isHex(const char c)
{
switch (c)
{
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
case 'd':
case 'D':
case 'e':
case 'E':
case 'f':
case 'F':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return true;
}
return false;
}
QString formatVersion(int version)
{
int majorVer = version / 10000;
int minorVer = version % 10000 / 100;
int patchVer = version % 100;
return QString::number(majorVer) + "." + QString::number(minorVer) + "." + QString::number(patchVer);
}
bool copyRecursively(const QString& src, const QString& dst)
{
// Code taken from QtCreator:
// https://qt.gitorious.org/qt-creator/qt-creator/source/1a37da73abb60ad06b7e33983ca51b266be5910e:src/app/main.cpp#L13-189
QFileInfo srcFileInfo(src);
if (srcFileInfo.isDir())
{
QDir targetDir(dst);
targetDir.cdUp();
if (!targetDir.mkdir(QFileInfo(dst).fileName()))
return false;
QDir sourceDir(src);
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
for (QString &fileName : fileNames)
{
const QString newSrcFilePath = src + QLatin1Char('/') + fileName;
const QString newTgtFilePath = dst + QLatin1Char('/') + fileName;
if (!copyRecursively(newSrcFilePath, newTgtFilePath))
return false;
}
}
else if (srcFileInfo.isSymLink())
{
QString trg = QFile(src).symLinkTarget();
QFile::link(trg, dst);
}
else
{
if (!QFile::copy(src, dst))
return false;
}
return true;
}
bool renameBetweenPartitions(const QString& src, const QString& dst)
{
if (QDir(dst).exists())
return false;
int res = copyRecursively(src, dst);
if (res)
QDir(src).removeRecursively();
else
QDir(dst).removeRecursively();
return res;
}
bool isWritableRecursively(const QString& dir)
{
QFileInfo fi(dir);
if (!fi.isWritable())
return false;
if (fi.isDir())
{
QStringList fileNames = QDir(dir).entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
for (QString &fileName : fileNames)
{
if (!isWritableRecursively(dir + QLatin1Char('/') + fileName))
return false;
}
}
return true;
}
QString encryptRsa(const QString& input, const QString& modulus, const QString& exponent)
{
std::string inputStdStr = input.toStdString();
Key key = Key(BigInt(modulus.toStdString()), BigInt(exponent.toStdString()));
std::string result = RSA::Encrypt(inputStdStr, key);
return QString::fromStdString(result);
}
QString decryptRsa(const QString& input, const QString& modulus, const QString& exponent)
{
std::string inputStdStr = input.toStdString();
Key key = Key(BigInt(modulus.toStdString()), BigInt(exponent.toStdString()));
std::string result = RSA::Decrypt(inputStdStr, key);
return QString::fromStdString(result);
}
QStringList concat(const QList<QStringList>& list)
{
QStringList result;
for (const QStringList& item : list)
result.append(item);
return result;
}
QString doubleToString(const QVariant& val, bool enforceDecimal)
{
double d = val.toDouble();
if (std::isnan(d) || std::isinf(d))
return QString();
double absd = std::fabs(d);
if (absd == 0.0)
return "0.0";
int k = static_cast<int>(std::floor(std::log10(absd)));
QString result;
if (k < -16 && !enforceDecimal)
{
// Scientific notation
result = QString::number(d, 'g', 15);
}
else
{
// Decimal notation
int decimals = std::max(0, 15 - (k + 1));
result = QString::number(d, 'f', decimals);
while (result.contains('.') && result.endsWith('0'))
result.chop(1);
if (result.endsWith('.'))
result.chop(1);
}
if (!result.contains('.') && !result.contains('e'))
result += ".0";
return result;
}
void sortWithReferenceList(QList<QString>& listToSort, const QList<QString>& referenceList, Qt::CaseSensitivity cs)
{
sSort(listToSort, [referenceList, cs](const QString& s1, const QString& s2) -> bool
{
int idx1 = indexOf(referenceList, s1, cs);
int idx2 = indexOf(referenceList, s2, cs);
if (idx1 == -1 || idx2 == -1)
{
if (idx1 == -1 && idx2 == -1)
return false;
return (idx1 == -1);
}
if (idx1 == idx2)
return false;
return (idx1 > idx2);
});
}
QByteArray serializeToBytes(const QVariant& value)
{
QByteArray bytes;
QDataStream stream(&bytes, QIODevice::WriteOnly);
stream << value;
return bytes;
}
QVariant deserializeFromBytes(const QByteArray& bytes)
{
if (bytes.isNull())
return QVariant();
QVariant deserializedValue;
QDataStream stream(bytes);
stream >> deserializedValue;
return deserializedValue;
}
QString readFileContents(const QString& path, QString* err)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
if (err)
*err = QObject::tr("Could not open file '%1' for reading: %2").arg(path, file.errorString());
return QString();
}
QTextStream stream(&file);
stream.setCodec("UTF-8");
QString contents = stream.readAll();
file.close();
return contents;
}
uint qHash(const QVariant& var)
{
if (!var.isValid() || var.isNull())
return -1;
switch (var.type())
{
case QVariant::Int:
return qHash(var.toInt());
case QVariant::UInt:
return qHash(var.toUInt());
case QVariant::Bool:
return qHash(var.toUInt());
case QVariant::Double:
return qHash(var.toUInt());
case QVariant::LongLong:
return qHash(var.toLongLong());
case QVariant::ULongLong:
return qHash(var.toULongLong());
case QVariant::String:
return qHash(var.toString());
case QVariant::Char:
return qHash(var.toChar());
case QVariant::StringList:
return qHash(var.toString());
case QVariant::ByteArray:
return qHash(var.toByteArray());
case QVariant::Date:
case QVariant::Time:
case QVariant::DateTime:
case QVariant::Url:
case QVariant::Locale:
case QVariant::RegExp:
return qHash(var.toString());
case QVariant::Hash:
return qHash(var.toHash());
case QVariant::Map:
return qHash(var.toMap());
case QVariant::List:
return qHash(var.toList());
case QVariant::BitArray:
return qHash(var.toBitArray());
case QVariant::Size:
case QVariant::SizeF:
case QVariant::Rect:
case QVariant::LineF:
case QVariant::Line:
case QVariant::RectF:
case QVariant::Point:
case QVariant::PointF:
// not supported yet
break;
case QVariant::UserType:
case QVariant::Invalid:
default:
return -3;
}
// could not generate a hash for the given variant
return -2;
}
QString indentMultiline(const QString& str)
{
QStringList lines = str.split("\n");
for (QString& line : lines)
line = line.prepend(" ");
return lines.join("\n");
}
QString toNativePath(const QString& path)
{
return QDir::toNativeSeparators(path);
}
QStringList sharedLibFileFilters()
{
static QStringList filters({
#ifdef Q_OS_WIN
"*.dll"
#elif defined Q_OS_MACOS
"*.dylib"
#elif defined Q_OS_LINUX || Q_OS_BSD
"*.so"
#endif
});
return filters;
}
|