Repository navigation
Expand file tree
/
Copy pathfileio.cpp
More file actions
89 lines (70 loc) · 2.64 KB
/
Copy pathfileio.cpp
File metadata and controls
89 lines (70 loc) · 2.64 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
89
#include "fileio.h"
#include <vector>
#include <cmath>
quint32 fileio::chunkAmount(quint32 unCompressedSize, quint32 chunkSize) {
if (chunkSize == 0) {
throw std::invalid_argument("chunkSize must be greater than 0");
}
// Calculate the number of chunks needed
quint32 chunkCount = unCompressedSize / chunkSize;
// If there is any remainder, we need an additional chunk
if (unCompressedSize % chunkSize != 0) {
chunkCount++;
}
return (unCompressedSize == 0) ? 0 : chunkCount; // Return 0 for zero size input
}
std::vector<quint64> fileio::chunkSizes(quint32 unCompressedSize, qint32 chunkAmount, quint32 chunkSize) {
std::vector<quint64> chunkS(chunkAmount, 0); // Initialize with zeros
// Handle case where the uncompressed size is less than the chunk size
if (unCompressedSize < chunkSize) {
chunkS[0] = unCompressedSize;
return chunkS; // Early return
}
// Calculate the number of full chunks that can be created
int fullChunks = unCompressedSize / chunkSize;
int remainder = unCompressedSize % chunkSize;
// If the number of chunks is equal to the full chunks, fill them
if (fullChunks >= chunkAmount) {
for (int i = 0; i < chunkAmount; ++i) {
chunkS[i] = chunkSize;
}
return chunkS; // Early return
}
// Fill the full chunks
for (int i = 0; i < fullChunks; ++i) {
chunkS[i] = chunkSize;
}
// Handle the last chunk
if (fullChunks < chunkAmount) {
chunkS[fullChunks] = remainder; // Assign remainder to the last chunk
}
return chunkS;
}
QString fileio::resolveFolderPath(const QString& inputPath) {
QString resolvedPath;
// Check if it is already a full absolute path
if (QFileInfo(inputPath).isAbsolute()) {
resolvedPath = QDir::cleanPath(inputPath);
} else {
// Resolves - example: ".\\bank" to a full absolute folder path
resolvedPath = QDir::current().absoluteFilePath(inputPath);
}
return QDir::cleanPath(resolvedPath);
}
QStringList fileio::readTextFileToQStringList(const QString& filePath) {
QStringList stringList;
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw std::invalid_argument("Could not open txt file").what();
return stringList; // Return empty list if file cannot be opened
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
stringList.append(line);
}
if (stringList.isEmpty()) // prevent's application crash if password is empty.
stringList.append("");
file.close();
return stringList;
}