-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFiler.cpp
183 lines (167 loc) · 4.48 KB
/
Filer.cpp
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
#include "Filer.h"
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QDateTime>
Filer::Filer(QObject *parent) :
QObject(parent)
{
fileName = dirName = patchedFileName = "";
}
Filer::FileError Filer::setName(const QString &aFileName)
{
fileName.clear();
dirName.clear();
patchedFileName.clear();
// File Name
if (aFileName.isNull()) {
toLogArea(Error, tr("File name is empty!"));
return FIsEmpty;
}
fileName = aFileName;
QFileInfo fileInfo(fileName);
FileError error = checkFileInfo(fileName);
if (error != AllOk) {
return error;
}
fileName = fileInfo.fileName();
// Directory Name
dirName = fileInfo.absolutePath();
QFileInfo dirInfo(dirName);
if (dirName.isNull()) {
toLogArea(Error, tr("Directory name is empty!"));
return DIsEmpty;
}
if (!dirInfo.isDir()) {
toLogArea(Error, tr("This is not directory!"));
return DIsFileIsNotDir;
}
if (!dirInfo.isReadable()) {
toLogArea(Error, tr("Directory is not readable!"));
return DIsNotReadable;
}
if (!dirInfo.isWritable()) {
toLogArea(Error, tr("Directory is not writable!"));
return DIsNotWritable;
}
dirName += QDir::separator();
// Patched File Name
QString date = QString("_") + QDateTime::currentDateTime().toString("dd-MM-yy_HH-mm-ss");
QString patched = "_patched";
if (!fileName.contains(patched)) {
QString origFileName = fileInfo.fileName();
int _size = origFileName.size() - 1;
if (origFileName.contains('.')) {
// Find last dot
int i;
for (i = _size; i >= 0; --i) {
if (origFileName.at(i) == '.') {
break;
}
}
// Insert string
patchedFileName = origFileName.insert(i, date + patched);
} else {
patchedFileName = origFileName + date + patched;
}
} else {
patchedFileName = fileName;
if (patchedFileName.endsWith(".smg")) {
generateName(true);
} else {
generateName(false);
}
}
QFileInfo patchedFileInfo(dirName + QDir::separator() + patchedFileName);
if (patchedFileInfo.exists()) {
toLogArea(Error, tr("File exists!"));
return FIsExists;
}
return AllOk;
}
Filer::FileError Filer::checkFileInfo(const QFileInfo &aFileInfo)
{
if (aFileInfo.isDir()) {
toLogArea(Error, tr("This is directory, not file!"));
return FIsDirIsNotFile;
}
if (!aFileInfo.isFile()) {
toLogArea(Error, tr("This is not file!"));
return FIsNotFile;
}
if (!aFileInfo.isReadable()) {
toLogArea(Error, tr("File is not readable!"));
return FIsNotReadable;
}
if (!aFileInfo.isWritable()) {
toLogArea(Error, tr("File is not writable!"));
return FIsNotWritable;
}
if (aFileInfo.size() >= 4063232 * 2) {
toLogArea(Error, tr("File is too big!"));
return FIsTooBig;
}
return AllOk;
}
void Filer::generateName(bool aSmgExt)
{
QString s = patchedFileName;
if (aSmgExt) {
s.remove(".smg");
}
int endString = s.size() - 1;
QChar ch = patchedFileName.at(endString);
if (!(ch.isDigit())) {
patchedFileName = s + QString::number(generateNum(s, 0, aSmgExt));
} else {
QString num;
int i;
for (i = endString; i >= 0; --i) {
if (s.at(i).isDigit()) {
num.insert(0, s.at(i));
} else {
break;
}
}
s = s.left(i + 1);
patchedFileName = s + QString::number(generateNum(s, num.toInt(), aSmgExt));
}
if (aSmgExt) {
patchedFileName += ".smg";
}
}
int Filer::generateNum(const QString &aName, int aStart, bool aSmgExt) const
{
QString begin = dirName + QDir::separator() + aName;
QString end = QString::number(aStart);
if (aSmgExt) {
end += ".smg";
}
if (!QFileInfo(begin + end).exists()) {
return aStart;
}
int i;
for (i = aStart; QFileInfo(begin + end).exists(); ++i) {
end = QString::number(i);
if (aSmgExt) {
end += ".smg";
}
}
return i - 1;
}
QString Filer::getDirName() const
{
return dirName;
}
QString Filer::getFileName() const
{
return fileName;
}
QString Filer::getPatchedFileName() const
{
return patchedFileName;
}
Filer::~Filer()
{
/* Empty Destructor */
}