2023-05-19 15:32:52 +02:00
|
|
|
#include "utils.h"
|
2023-08-11 11:03:21 +02:00
|
|
|
#include "message_handler.h"
|
2023-08-22 12:29:52 +02:00
|
|
|
#include "git/git_client.h"
|
2023-05-19 15:32:52 +02:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
2023-08-11 11:03:21 +02:00
|
|
|
#include <QDebug>
|
2023-05-19 15:32:52 +02:00
|
|
|
|
2023-08-02 15:45:20 +02:00
|
|
|
int Utils::read1stLineOfFile(QString fileName) {
|
|
|
|
QFile f(fileName);
|
|
|
|
if (f.exists()) {
|
|
|
|
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
QTextStream in(&f);
|
|
|
|
in.setCodec("UTF-8");
|
|
|
|
while(!in.atEnd()) {
|
|
|
|
return in.readLine().toInt();
|
2023-05-19 15:32:52 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-02 15:45:20 +02:00
|
|
|
}
|
|
|
|
return -1;
|
2023-05-19 15:32:52 +02:00
|
|
|
}
|
|
|
|
|
2023-08-02 15:45:20 +02:00
|
|
|
QString Utils::zoneName(quint8 i) {
|
|
|
|
static constexpr char const *zName[] = {
|
|
|
|
"",
|
|
|
|
"purple",
|
|
|
|
"blue",
|
|
|
|
"yellow",
|
|
|
|
"green",
|
|
|
|
"yellow (mars)",
|
|
|
|
"green (mars)"
|
|
|
|
};
|
|
|
|
if (i < (sizeof(zName)/sizeof(char const *))) {
|
|
|
|
return zName[i];
|
2023-05-19 15:32:52 +02:00
|
|
|
}
|
2023-08-02 15:45:20 +02:00
|
|
|
return "N/A";
|
2023-05-19 15:32:52 +02:00
|
|
|
}
|
2023-08-11 11:03:21 +02:00
|
|
|
|
|
|
|
void Utils::printCriticalErrorMsg(QString const &errorMsg) {
|
|
|
|
qCritical() << QString(80, '!');
|
|
|
|
qCritical() << errorMsg;
|
|
|
|
qCritical() << QString(80, '!');
|
|
|
|
}
|
|
|
|
|
|
|
|
void Utils::printInfoMsg(QString const &infoMsg) {
|
|
|
|
qCritical() << QString(80, '=');
|
|
|
|
qCritical() << infoMsg;
|
|
|
|
qCritical() << QString(80, '=');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Utils::printLineEditInfo(QStringList const &lines) {
|
|
|
|
if (getDebugLevel() == LOG_DEBUG) {
|
|
|
|
for (int i=0; i<lines.size(); ++i) {
|
|
|
|
qInfo() << lines.at(i);
|
|
|
|
} qInfo() << ""; qInfo() << "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Utils::getTariffLoadTime(QString fileName) {
|
|
|
|
QFileInfo fInfo(fileName);
|
|
|
|
if (fInfo.exists()) {
|
|
|
|
QDateTime lastModifiedTime = fInfo.lastModified();
|
|
|
|
if (lastModifiedTime.isValid()) {
|
|
|
|
return lastModifiedTime.toString(Qt::ISODateWithMs);
|
|
|
|
} else {
|
|
|
|
printCriticalErrorMsg(fileName + " HAS INVALID MODIFIED-TIME");
|
|
|
|
QDateTime birthTime = fInfo.birthTime();
|
|
|
|
if (birthTime.isValid()) {
|
|
|
|
return birthTime.toString(Qt::ISODateWithMs);
|
|
|
|
} else {
|
|
|
|
printCriticalErrorMsg(fileName + " HAS INVALID BIRTH-TIME");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printCriticalErrorMsg(fileName + " DOES NOT EXIST");
|
|
|
|
}
|
|
|
|
return "N/A";
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:41:16 +02:00
|
|
|
QString Utils::rstrip(QString const &str) {
|
|
|
|
int n = str.size() - 1;
|
|
|
|
for (; n >= 0; --n) {
|
|
|
|
if (!str.at(n).isSpace()) {
|
|
|
|
return str.left(n + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2023-08-22 12:29:52 +02:00
|
|
|
|
|
|
|
bool Utils::sameFilesInDirs(QDir const &dir1, QDir const &dir2,
|
|
|
|
QStringList const &nameFilters) {
|
|
|
|
if (!dir1.exists()) {
|
|
|
|
printCriticalErrorMsg(dir1.dirName() + " DOES NOT EXIST");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!dir2.exists()) {
|
|
|
|
printCriticalErrorMsg(dir2.dirName() + " DOES NOT EXIST");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (dir1.absolutePath() == dir2.absolutePath()) {
|
|
|
|
printCriticalErrorMsg(dir1.dirName() + " AND "+ dir2.dirName() + " HAVE SAME PATH");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// files, sorted by name
|
|
|
|
QFileInfoList const &lst1 = dir1.entryInfoList(nameFilters, QDir::Files, QDir::Name);
|
|
|
|
QFileInfoList const &lst2 = dir2.entryInfoList(nameFilters, QDir::Files, QDir::Name);
|
|
|
|
|
|
|
|
QStringList fileNameLst1{};
|
|
|
|
QStringList fileNameLst2{};
|
|
|
|
QListIterator<QFileInfo> i1(lst1);
|
|
|
|
while (i1.hasNext()) {
|
|
|
|
fileNameLst1 << i1.next().fileName();
|
|
|
|
}
|
|
|
|
QListIterator<QFileInfo> i2(lst2);
|
|
|
|
while (i2.hasNext()) {
|
|
|
|
fileNameLst2 << i2.next().fileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileNameLst1.isEmpty()) {
|
|
|
|
qCritical() << "DIR1" << dir1.dirName() << " DOES NOT CONTAIN EXPECTED FILES";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fileNameLst2.isEmpty()) {
|
|
|
|
qCritical() << "DIR1" << dir2.dirName() << " DOES NOT CONTAIN EXPECTED FILES";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fileNameLst1 != fileNameLst2) {
|
|
|
|
printCriticalErrorMsg(dir1.dirName() + " AND " + dir2.dirName()
|
|
|
|
+ " DIFFER: [" + fileNameLst1.join(',') + "],["
|
|
|
|
+ fileNameLst2.join(',') + "]");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
printInfoMsg(dir1.dirName() + " AND " + dir2.dirName()
|
|
|
|
+ " ARE EQUAL: [" + fileNameLst1.join(',') + "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList gitBlobLst1{};
|
|
|
|
QStringList gitBlobLst2{};
|
|
|
|
QListIterator<QFileInfo> i3(lst1);
|
|
|
|
while (i3.hasNext()) {
|
|
|
|
gitBlobLst1 << GitClient::gitBlob(i3.next().fileName());
|
|
|
|
}
|
|
|
|
QListIterator<QFileInfo> i4(lst2);
|
|
|
|
while (i4.hasNext()) {
|
|
|
|
gitBlobLst2 << GitClient::gitBlob(i4.next().fileName());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gitBlobLst1.isEmpty()) {
|
|
|
|
qCritical() << "DIR1" << dir1.dirName() << " DOES NOT CONTAIN EXPECTED FILES";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (gitBlobLst2.isEmpty()) {
|
|
|
|
qCritical() << "DIR1" << dir2.dirName() << " DOES NOT CONTAIN EXPECTED FILES";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gitBlobLst1 != gitBlobLst2) {
|
|
|
|
printCriticalErrorMsg(dir1.dirName() + " AND " + dir2.dirName()
|
|
|
|
+ " DIFFER: [" + gitBlobLst1.join(',') + "],["
|
|
|
|
+ gitBlobLst2.join(',') + "]");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
printInfoMsg(dir1.dirName() + " AND " + dir2.dirName()
|
|
|
|
+ " CONTAIN SAME GIT-BLOBS FOR FILES: [" + fileNameLst1.join(',') + "]");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|