-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.cpp
More file actions
316 lines (274 loc) · 9.57 KB
/
Copy pathexecution.cpp
File metadata and controls
316 lines (274 loc) · 9.57 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "execution.h"
#include <iostream>
#include <filesystem>
#include <string>
#include <sstream>
#include <fstream>
#include <zip.h>
#include <cstring>
#include <sys/stat.h>
#include <sys/types.h>
#include "utility.h"
using namespace std;
execution::execution() {
unzipDB();
if (!(std::filesystem::exists(path) && std::filesystem::is_directory(path))) {
throw string ("No data base to load data.");
}
for (const auto& entry : filesystem::directory_iterator(path)) {
string name = entry.path().filename().string();
airport* ap = new airport(name);
airports[name] = ap;
}
}
execution::~execution() {
zipDB();
for (const auto& pair : airports) {
delete pair.second;
}
}
string execution::getAllArrivalFlightsDetails(vector<string> icoas){
string res;
ostringstream oss;
vector<string> lines;
for(const auto& icoa:icoas){
try{
if(airports.count(icoa) <= 0)
throw string("The airpot " + icoa + " does not exist in the data base.\n");
for (const auto& f : airports[icoa]->get_arv_flights())
{
std::ostringstream oss;
oss << "Flight #" << f.callsign << " arriving from " << f.dpt << ", took off at " << airport::convertLocalTime(f.dpt_time) << " landed at " << airport::convertLocalTime(f.arv_time) << std::endl;
lines.push_back(oss.str());
}
}
catch(string exepction_icoa)
{
res += exepction_icoa;
}
}
for (const std::string& line : lines) {
res += line;
}
return res;
}
string execution::getFull_schedule(vector<string> icoas){
string res;
vector<string> lines;
for (string icoa:icoas)
{
vector<pair<Flight,bool>> sortSche;
string name = icoa;
//loop sorted by time using func form utilty
try {
if(airports.count(icoa) <= 0)
throw string("The airpot " + icoa + " does not exist in the data base.\n");
sortSche = airports[name]->sort_full_sche();
for (const auto& f : sortSche) {
std::ostringstream oss;
if(f.second == IS_ARV)
oss << "Flight #" << f.first.callsign << " arriving from " << f.first.dpt << ", at " << airport::convertLocalTime(f.first.arv_time) << endl;
else
oss << "Flight #" << f.first.callsign << " departing to " << f.first.arv << ", at " << airport::convertLocalTime(f.first.dpt_time) << endl;
lines.push_back(oss.str());
}
}
catch (string exepction_icoa)
{
res+=exepction_icoa;
}
}
for (const std::string& line : lines)
{
res += line;
}
return res;
}
string execution::getAirplaneFlight(string icoa24){
string res;
ostringstream oss;
vector<Flight> flights;
vector<Flight> temp_flights;
vector<Flight> temp;
vector<string> lines;
//Here we check for evrey arg in evrey icoa if found we print.
for(auto const& airp : airports)
{
temp = airp.second->getFlightsByicoa24(icoa24);
temp_flights.insert(temp_flights.end(),temp.begin(),temp.end());
}
if(temp_flights.size() == 0)
{
oss << "Airplane \"" << icoa24 << "\" have no data" << endl;
lines.push_back(oss.str());
}
flights.insert(flights.end(),temp_flights.begin(),temp_flights.end());
temp_flights.clear();
for(Flight& f : flights)
{
oss << f.icoa24 << " departed from " << f.dpt << " at " << airport::convertLocalTime(f.dpt_time) << " arrived in " << f.arv << " at " << airport::convertLocalTime(f.arv_time) << endl;
lines.push_back(oss.str());
}
for (const std::string& line : lines) {
res += line;
}
return res;
}
string execution::updateDB(){
int counter = 0;
string res;
//Here we check if there is DB to update
if(!(filesystem::exists(path) && filesystem::is_directory(path)))
return "No data base dir \"flightDB\" found";
else if(!(filesystem::exists(cmd) && !filesystem::is_directory(cmd)))
return "No bash script \"flightScanner.sh\" found";
else{
for (const auto & entry : filesystem::directory_iterator(path)) {
cmd += " " + entry.path().filename().string();
counter++;
}
}
if(counter > 0)
{
system("rm -r flightDB");
int result = system(cmd.c_str());
if (result != 0)
return"Error occured";
res += "DB updated succesfuly";
}
else{
res += "Flight data base is empty.\n";
}
return res;
}
bool execution::zipDB(){
bool res = true;
const string libraryPath = path;
const string zipFilename = "DB.zip";
int error;
zip* archive = zip_open(zipFilename.c_str(), ZIP_CREATE | ZIP_TRUNCATE, &error);
if (!archive) {
//std::cerr << "Failed to create ZIP archive: " << zip_strerror(archive) << std::endl;
return false;
}
filesystem::path libraryDirectory(libraryPath);
if (filesystem::exists(libraryDirectory) && filesystem::is_directory(libraryDirectory)) {
addDirectoryToZip(archive, libraryDirectory, filesystem::path(""));
} else {
cerr << "Invalid library path: " << libraryPath << endl;
zip_close(archive);
return 1;
}
if (zip_close(archive) == -1) {
//std::cerr << "Failed to close ZIP archive: " << zip_strerror(archive) << std::endl;
return false;
}
return res;
}
void execution::addFileToZip(zip* archive, const filesystem::path& filePath, const filesystem::path& relativePath) {
std::string fileStr = filePath.string();
zip_source* source = zip_source_file(archive, fileStr.c_str(), 0, -1);
if (!source) {
std::cerr << "Failed to add file to ZIP archive: " << zip_strerror(archive) << std::endl;
return;
}
std::string relativePathStr = relativePath.string();
zip_int64_t index = zip_file_add(archive, relativePathStr.c_str(), source, ZIP_FL_OVERWRITE);
if (index == -1) {
//std::cerr << "Failed to add file to ZIP archive: " << zip_strerror(archive) << std::endl;
zip_source_free(source);
return;
}
}
void execution::addDirectoryToZip(zip* archive, const filesystem::path& directoryPath, const filesystem::path& relativePath) {
for (const auto& entry : fs::directory_iterator(directoryPath)) {
const auto& path = entry.path();
const auto& relativeSubPath = relativePath / path.filename();
if (filesystem::is_directory(path)) {
addDirectoryToZip(archive, path, relativeSubPath);
} else if (filesystem::is_regular_file(path)) {
addFileToZip(archive, path, relativeSubPath);
}
}
}
bool execution::createDirectory(const std::string& directoryPath) {
int status = mkdir(directoryPath.c_str(), 0755);
if (status == 0) {
return true;
} else {
return errno == EEXIST; // Directory already exists
}
}
bool execution::extractEntry(zip* archive, int index, const std::string& outputDirectory) {
zip_stat_t stat;
if (zip_stat_index(archive, index, 0, &stat) != 0) {
return false;
}
std::string entryName = stat.name;
std::string outputPath = outputDirectory + "/" + entryName;
if (stat.name[strlen(stat.name) - 1] == '/') {
// Entry is a directory
if (!createDirectory(outputPath)) {
return false;
}
} else {
// Entry is a file
std::size_t found = entryName.find_last_of("/");
if (found != std::string::npos) {
// Extract the directory path from the entry name
std::string directoryPath = outputDirectory + "/" + entryName.substr(0, found);
if (!createDirectory(directoryPath)) {
return false;
}
}
zip_file_t* file = zip_fopen_index(archive, index, 0);
if (!file) {
return false;
}
std::ofstream outputFile(outputPath, std::ios::binary);
if (!outputFile) {
zip_fclose(file);
return false;
}
static const size_t bufferSize = 8192;
char buffer[bufferSize];
zip_int64_t bytesRead = 0;
while ((bytesRead = zip_fread(file, buffer, bufferSize)) > 0) {
outputFile.write(buffer, bytesRead);
}
zip_fclose(file);
}
return true;
}
bool execution::unzipFile(const std::string& zipFilename, const std::string& outputDirectory) {
zip* archive = zip_open(zipFilename.c_str(), 0, NULL);
if (!archive) {
return false;
}
int numEntries = zip_get_num_entries(archive, 0);
for (int i = 0; i < numEntries; ++i) {
if (!extractEntry(archive, i, outputDirectory)) {
zip_close(archive);
return false;
}
}
zip_close(archive);
return true;
}
bool execution::unzipDB(){
bool hasDir = true;
const std::string zipFilename = "DB.zip";
const std::string outputDirectory = "flightDB";
if (!(std::filesystem::exists(path) && std::filesystem::is_directory(path))) {
mkdir(path.c_str(), 0755);
hasDir = false;
}
mkdir(path.c_str(), 0755);
if (unzipFile(zipFilename, outputDirectory)) {
return true;
} else {
if(!hasDir)
std::filesystem::remove(path);
return false;
}
}