-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
269 lines (226 loc) · 7.98 KB
/
main.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <QtCore>
#include <QCoreApplication>
#include <QSettings>
#include <QDir>
#include <QDate>
#include <QDebug>
#include <QRegularExpression>
#include <QBasicTimer>
#include <QTimer>
#include <iostream>
using namespace std;
struct FileTemplate {
QString filetemplate;
int startdate;
};
QList<FileTemplate> filetemplates;
QList<QString> segmentdirectories;
QStringList completelist;
QList<bool> complete_todelete;
QList<int> complete_startdate;
void CleanupDirectories(bool bListfiles);
void MoveTheFiles(const QString &thedir, bool bListfiles);
void MoveToDirectory(QString filename, int thestartdate, QDir dir);
void MyRun()
{
CleanupDirectories(true);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFileDevice::Permissions p = QFile::permissions("EumetcastWatcherOut.ini");
cout << "start reading permissions" << endl;
if (p & QFileDevice::ReadOwner)
cout << "The file is readable by the owner of the file." << endl;
if (p & QFileDevice::WriteOwner)
cout << "The file is writable by the owner of the file." << endl;
if (p & QFileDevice::ExeOwner)
cout << "The file is executable by the owner of the file." << endl;
if (p & QFileDevice::ReadUser)
cout << "The file is readable by the user." << endl;
if (p & QFileDevice::WriteUser)
cout << "The file is writable by the user." << endl;
if (p & QFileDevice::ExeUser)
cout << "The file is executable by the user." << endl;
if (p & QFileDevice::ReadGroup)
cout << "The file is readable by the group." << endl;
if (p & QFileDevice::WriteGroup)
cout << "The file is writable by the group." << endl;
if (p & QFileDevice::ExeGroup)
cout << "The file is executable by the group." << endl;
if (p & QFileDevice::ReadOther)
cout << "The file is readable by anyone." << endl;
if (p & QFileDevice::WriteOther)
cout << "The file is writable by anyone." << endl;
if (p & QFileDevice::ExeOther)
cout << "The file is executable by anyone." << endl;
// QFile file("EumetcastWatcherOut.ini");
// if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
// {
// cout << "EumetcastWatcherOut.ini not found !" << endl;
// return 0;
// }
// while (!file.atEnd()) {
// QByteArray line = file.readLine();
// cout << line.toStdString() << endl;
// }
// file.close();
QSettings settings("EumetcastWatcherOut.ini", QSettings::IniFormat);
int size = settings.beginReadArray("filetemplates");
if(size == 0)
{
cout << "No filetemplates !" << endl;
return 0;
}
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
FileTemplate mytemplate;
mytemplate.filetemplate = settings.value("template").toString();
mytemplate.startdate = settings.value("startdate").toInt();
filetemplates.append(mytemplate);
}
settings.endArray();
// for(int i = 0; i < filetemplates.size(); i++)
// {
// printf("%d - %s\n", filetemplates.at(i).startdate, filetemplates.at(i).filetemplate.toStdString().c_str());
// }
size = settings.beginReadArray("segmentdirectories");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
QString mydirectory;
mydirectory = settings.value("directory").toString();
segmentdirectories.append(mydirectory);
}
settings.endArray();
// for(int i = 0; i < segmentdirectories.size(); i++)
// {
// printf("%s\n", segmentdirectories.at(i).toStdString().c_str());
// }
CleanupDirectories(false);
QTimer *timer = new QTimer(&a);
QObject::connect(timer, &QTimer::timeout, &a, MyRun);
timer->start(5000);
return a.exec();
}
void CleanupDirectories(bool bListfiles)
{
if (segmentdirectories.count() > 0)
{
QStringList::Iterator its = segmentdirectories.begin();
while( its != segmentdirectories.end() )
{
QString stits = *its;
MoveTheFiles(stits, bListfiles);
++its;
}
}
}
void MoveTheFiles(const QString &thedir, bool bListfiles)
{
QString filename;
QString restfile;
QDir dir( thedir );
QString fileyear;
QString filemonth;
QString fileday;
int thecount = 0;
completelist.clear();
complete_todelete.clear();
complete_startdate.clear();
dir.setFilter(QDir::Files | QDir::NoSymLinks);
completelist = dir.entryList();
for(int i = 0; i < completelist.count(); i++)
{
complete_todelete.append(true);
complete_startdate.append(0);
}
if (filetemplates.count() > 0)
{
QList<FileTemplate>::Iterator itfile = filetemplates.begin();
while( itfile != filetemplates.end() )
{
thecount = 0;
QString wildcard = QRegularExpression::wildcardToRegularExpression((*itfile).filetemplate);
QRegularExpression re(wildcard);
int intfrom = 0;
intfrom = completelist.indexOf(re, intfrom);
while(intfrom != -1)
{
filename = completelist.at(intfrom);
complete_todelete[intfrom] = false;
complete_startdate[intfrom] = (*itfile).startdate;
thecount++;
intfrom = completelist.indexOf(re, intfrom + 1);
}
++itfile;
}
for(int i = 0; i < completelist.count(); i++)
{
if(complete_todelete.at(i) == false)
{
QDateTime local(QDateTime::currentDateTime());
QDateTime UTC(local.toUTC());
QString timeStr = UTC.toString(Qt::ISODate);
filename = completelist.at(i);
if(bListfiles)
cout << timeStr.toStdString() << " File " << filename.toStdString() << " moved" << endl;
MoveToDirectory(filename, complete_startdate.at(i), dir);
}
else
{
QDateTime local(QDateTime::currentDateTime());
QDateTime UTC(local.toUTC());
QString timeStr = UTC.toString(Qt::ISODate);
filename = completelist.at(i);
if(bListfiles)
cout << timeStr.toStdString() << " File " << filename.toStdString() << " deleted" << endl;
dir.remove(filename);
}
}
}
}
void MoveToDirectory(QString filename, int thestartdate, QDir dir)
{
QString fileyear;
QString filemonth;
QString fileday;
bool gelukt;
if(filename.mid(0, 6) == "OR_ABI")
{
int nyear = filename.mid( thestartdate, 4).toInt();
int ndayofyear = filename.mid( thestartdate + 4, 3).toInt();
QDate filedate = QDate(nyear, 1, 1).addDays(ndayofyear - 1);
fileyear = filedate.toString("yyyy");
filemonth = filedate.toString("MM");
fileday = filedate.toString("dd");
}
else
{
fileyear = filename.mid( thestartdate, 4);
filemonth = filename.mid( thestartdate + 4, 2);
fileday = filename.mid( thestartdate + 6, 2);
}
QDir dirdateyear(dir.absolutePath() + "/" + fileyear);
QDir dirdatemonth(dir.absolutePath() + "/" + fileyear + "/" + filemonth);
QDir dirdateday(dir.absolutePath() + "/" + fileyear + "/" + filemonth + "/" + fileday);
gelukt = false;
if (!dirdateyear.exists())
{
gelukt = dir.mkdir(dir.absolutePath() + "/" + fileyear);
}
if (!dirdatemonth.exists())
{
gelukt = dir.mkdir(dir.absolutePath() + "/" + fileyear + "/" + filemonth);
}
if (!dirdateday.exists())
{
gelukt = dir.mkdir(dir.absolutePath() + "/" + fileyear + "/" + filemonth + "/" + fileday);
}
if (!dirdateday.exists(filename))
{
gelukt = dir.rename(dir.absolutePath() + "/" + filename,
dir.absolutePath() + "/" + fileyear + "/" + filemonth + "/" + fileday + "/" + filename);
}
else
dir.remove(filename);
}