-
Notifications
You must be signed in to change notification settings - Fork 15
/
lqtutils_data.h
253 lines (224 loc) · 5.88 KB
/
lqtutils_data.h
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
/**
* MIT License
*
* Copyright (c) 2022 Luca Carlon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
**/
#include <QByteArray>
#include <QString>
#include <QCryptographicHash>
#include <QFile>
#include <QRandomGenerator>
#include <QSet>
#include <QDebug>
#include <QBuffer>
#include "lqtutils_string.h"
namespace lqt {
/**
* Computes the hash of a file.
*
* @brief lqt_hash
* @param fileName
* @param algo
* @return
*/
QByteArray hash(const QString &fileName,
QCryptographicHash::Algorithm algo = QCryptographicHash::Md5)
{
QFile f(fileName);
if (!f.open(QFile::ReadOnly))
return QByteArray();
QCryptographicHash hash(algo);
if (hash.addData(&f))
return hash.result();
return QByteArray();
}
/**
* Writes random data to a device.
*
* @brief lqt_random_file
* @param fileName
* @param size
* @return
*/
bool random_data(QIODevice* device, qint64 size)
{
const int chunks = size/1024;
for (int i = 0; i < chunks; i++) {
QVector<quint32> list;
list.resize(1024/4);
QRandomGenerator::global()->fillRange(list.data(), list.size());
const int size = list.size()*sizeof(quint32);
if (device->write(reinterpret_cast<const char*>(list.data()), size) != size)
return false;
}
const int tailSize = size - chunks*1024;
for (int i = 0; i < tailSize; i++) {
const quint32 v = QRandomGenerator::global()->generate();
device->write(reinterpret_cast<const char*>(&v), 1);
}
return true;
}
/**
* Creates a random file of a specified size.
*
* @brief lqt_random_file
* @param fileName
* @param size
* @return
*/
bool random_file(const QString& fileName, qint64 size)
{
QFile f(fileName);
if (!f.open(QIODevice::WriteOnly))
return false;
return random_data(&f, size);
}
/**
* Returns random data in a buffer.
*
* @brief random_data
* @param size
* @return
*/
QByteArray random_data(qint64 size)
{
QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
return random_data(&buffer, size) ? buffer.buffer() : QByteArray();
}
template<typename T>
inline const T* get_any(const QSet<T>& set)
{
if (set.isEmpty())
return nullptr;
typename QSet<T>::const_iterator it = set.constBegin();
return &(*it);
}
/**
* Reads an entire file into memory.
*
* @brief readAll
* @param io
* @return
*/
inline QByteArray read_all(QIODevice* io)
{
if (!io)
return QByteArray();
if (!io->open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open I/O device";
return QByteArray();
}
return io->readAll();
}
/**
* Reads all data in a file.
*
* @brief read_all
* @param filePath
* @return
*/
inline QByteArray read_all(const QString& filePath)
{
QFile f(filePath);
return read_all(&f);
}
/**
* Writes data to the device.
*
* @brief write_all
* @param io
* @param data
* @return
*/
inline bool write_all(QIODevice* io, const QByteArray& data)
{
if (!io)
return false;
if (!io->open(QIODevice::WriteOnly)) {
qWarning() << "Failed to open I/O device";
return false;
}
return io->write(data) == data.size();
}
/**
* Writes data to the file.
*
* @brief write_all
* @param filePath
* @param data
* @return
*/
inline bool write_all(const QString& filePath, const QByteArray& data)
{
QFile f(filePath);
return write_all(&f, data);
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
/**
* Copies an entire directory to another location.
*
* @brief copy_path
* @param src
* @param dst
* @return
*/
bool copy_path(const QString& src, const QString& dst)
{
QDir sourceDir(src);
if (!sourceDir.exists())
return false;
QDir dstDir(dst);
if (dstDir.exists())
dstDir = QDir(lqt::path_combine({ dst, sourceDir.dirName() }));
if (dstDir.exists()) {
qWarning() << "Destination directory exists";
return false;
}
if (!dstDir.mkpath(".")) {
qWarning() << "Failed to create dir" << dstDir.absolutePath();
return false;
}
for (const auto& entry : QDirListing(src, QDirListing::IteratorFlag::Recursive)) {
const QString filePath = entry.absoluteFilePath();
const QString relativePath = sourceDir.relativeFilePath(filePath);
const QString destPath = lqt::path_combine({dstDir.absolutePath(), relativePath});
if (QFile(destPath).exists()) {
qWarning() << "Destination exists:" << destPath;
continue;
}
if (entry.isDir()) {
if (!QDir(destPath).mkpath(".")) {
qWarning() << "Failed to create dir:" << destPath;
continue;
}
}
else {
if (!QFile(filePath).copy(destPath)) {
qWarning() << "Failed to copy" << filePath << "->" << destPath;
continue;
}
}
}
return true;
}
#endif
} // namespace