-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.h
More file actions
88 lines (80 loc) · 2.66 KB
/
utils.h
File metadata and controls
88 lines (80 loc) · 2.66 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
#ifndef UTILS_H
#define UTILS_H
#include <type_traits>
#include <QString>
#include <QFile>
#include <QMessageBox>
class DefaultAlertImpl : QMessageBox {
Q_OBJECT
private:
public:
DefaultAlertImpl(QWidget* parent, const QString& message) : QMessageBox(parent) {
setText(message);
}
void operator()() {
exec();
}
};
class Equal{};
class LessThan{};
class MoreThan{};
class LessThanOrEqual{};
class MoreThanOrEqual{};
class Different{};
template <class T = Equal, typename S, typename = std::enable_if_t<std::is_convertible<S, QString>::value>>
bool assert_filesize(S name, qsizetype size) {
QFile file{name};
if (file.open(QIODevice::ReadOnly)) {
bool result;
if constexpr (std::is_same_v<T, Equal>) {
result = file.size() == size;
if (!result) {
DefaultAlertImpl(nullptr, QString::asprintf("This type of file must be exactly %lld bytes in size", size))();
}
}
else if constexpr (std::is_same_v<T, Different>) {
result = file.size() != size;
if (!result) {
DefaultAlertImpl(nullptr, QString::asprintf("This type of file must not be exactly %lld bytes in size", size))();
}
}
else if constexpr (std::is_same_v<T, LessThan>) {
result = file.size() < size;
if (!result) {
DefaultAlertImpl(nullptr, QString::asprintf("This type of file must be less than %lld bytes in size", size))();
}
}
else if constexpr (std::is_same_v<T, MoreThan>) {
result = file.size() > size;
if (!result) {
DefaultAlertImpl(nullptr, QString::asprintf("This type of file must be more than %lld bytes in size", size))();
}
}
else if constexpr (std::is_same_v<T, LessThanOrEqual>) {
result = file.size() <= size;
if (!result) {
DefaultAlertImpl(nullptr, QString::asprintf("This type of file must be less than or equal to %lld bytes in size", size))();
}
}
else if constexpr (std::is_same_v<T, MoreThanOrEqual>) {
result = file.size() == size;
if (!result) {
DefaultAlertImpl(nullptr, QString::asprintf("This type of file must be more than or equal to %lld bytes in size", size))();
}
} else {
Q_ASSERT(false);
}
return result;
}
return false;
}
constexpr qsizetype kb(qsizetype n) {
return n * 1024;
}
constexpr qsizetype mb(qsizetype n) {
return n * 1024 * 1024;
}
constexpr qsizetype gb(qsizetype n) {
return n * 1024 * 1024;
}
#endif // UTILS_H