-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
482 lines (424 loc) · 15 KB
/
utils.cpp
File metadata and controls
482 lines (424 loc) · 15 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>
#include <fstream>
#include "utils.h"
//h aplh periptwsh me char *
//vasismeno se Marc J. Rochkind - Advanced UNIX Programming (2004, Addison-Wesley Professional) - selida 97
int send_string(int fd, char * str, int b){
ssize_t nwritten = 0, n;
int size = strlen(str) +1; //to mhkos poy prepei na steilei prwta
int bwrit = write(fd, &size, sizeof(int));
//stelnoyme twra to string
//prosexw na mh grapsw perissotero ap oso xreiazetai
//grafw b bytes kathe fora mexri na parw to mhnyma me swstous elegxous
int to_write=0;
do {
if(size-nwritten <= b)
to_write = size-nwritten;
else
{to_write = b;}
//error handling gia th write
if ((n = write(fd, &((const char *)str)[nwritten], to_write)) == -1) {
if (errno == EINTR) //an diakopei apo signal
continue;
else
return -1;
}
nwritten += n;
} while (nwritten < size); //an osa exw grapsei mexri twra ftasan to swsto megethos, stamata
return nwritten;
}
//to stelnei apo string gia anti gia char array
//vasismeno se Marc J. Rochkind - Advanced UNIX Programming (2004, Addison-Wesley Professional) - selida 97
int send_string(int fd, std::string * str, int b){
ssize_t nwritten = 0, n;
int size = str->length() +1; //to mhkos poy prepei na steilei prwta
char a[size]; //gia na ginei low level I/O xreiazomai anagkastika char *
strcpy(a, str->c_str()); //ara pernaw to std::string se ena proswrino char * kai stelnw auto
//std::cout << *str;
int bwrit = write(fd, &size, sizeof(int));
//stelnoyme twra to string
//prosexw na mh grapsw perissotero ap oso xreiazetai
//grafw b bytes kathe fora mexri na parw to mhnyma me swstous elegxous
int to_write=0;
do {
//apofasisw an me pairnei na grapsw b h to mhnyma teleiwnei se ligotera
if(size-nwritten <= b)
to_write = size-nwritten;
else
{to_write = b;}
//error handling gia th write
if ((n = write(fd, &((const char *)a)[nwritten], to_write)) == -1) {
if (errno == EINTR) //an diakopei apo signal
continue;
else
return -1;
}
nwritten += n;
} while (nwritten < size); //an osa exw grapsei mexri twra ftasan to swsto megethos, stamata
return nwritten;
}
//h aplh periptwsh me char *
//vasismeno se Marc J. Rochkind - Advanced UNIX Programming (2004, Addison-Wesley Professional) - selida 97
int receive_string(int fd, char * buf, int b){
ssize_t nread = 0, n;
strcpy(buf, "");
int size =0;
//pare mhkos erxomenhs sumvoloseiras
int brd = read(fd, &size, sizeof(int));
int to_read=0;
do {
//apofasisw an me pairnei na diabasw b h to mhnyma teleiwnei se ligotera
if(size-nread <= b)
to_read = size-nread;
else
to_read = b;
//error handling ths read
if ((n = read(fd, &((char *)buf)[nread], to_read)) == -1) {
if (errno == EINTR) //an diakopei apo signal
continue;
else
return -1;
}
if (n == 0)
return nread;
nread += n;
} while (nread < size); //stamata otan diabaseis to akribes megethos mhnymatos se bytes
return nread;
}
//vasismeno se Marc J. Rochkind - Advanced UNIX Programming (2004, Addison-Wesley Professional) - selida 97
//to krataei se string anti gia gia char array
int receive_string(int fd, std::string * str, int b){
ssize_t nread = 0, n;
char tool[300];
strcpy(tool, "");
int size =0;
//pare mhkos erxomenhs sumvoloseiras
int brd = read(fd, &size, sizeof(int));
int to_read=0;
do {
//apofasisw an me pairnei na diabasw b h to mhnyma teleiwnei se ligotera
if(size-nread <= b)
to_read = size-nread;
else
{to_read = b;}
//error handling ths read
if ((n = read(fd, &((char *)tool)[nread], to_read)) == -1) {
if (errno == EINTR) //an diakopei apo signal
continue;
else
return -1;
}
if (n == 0)
return nread;
nread += n;
*str = std::string(tool);
} while (nread < size); //stamata otan diabaseis to akribes megethos mhnymatos se bytes
return nread;
}
//gia na pairnw ta files poy exoun mesa ta countries-dirs
int extract_files(char * inpdir, int * fleft, std::string ** fls){
DIR *dp;
struct dirent *dirp;
//anoigw kai tsekarw oti einai ok
dp = opendir(inpdir);
if(dp == NULL)
{
std::cout << "Error opening " << inpdir << "\n";
return errno;
}
//metraw ta directories poy exei mesa
while ((dirp = readdir(dp)) != NULL){
if((strcmp(dirp->d_name, ".") == 0)||(strcmp(dirp->d_name, "..") == 0))
continue;
*(fleft) += 1;
}
closedir(dp);
if(*fleft > 0){
dp = opendir(inpdir);
if(dp == NULL)
{
std::cout << "Error opening " << inpdir << "\n";
return errno;
}
//metraw ta directories poy exei mesa
*fls = new std::string[*fleft];
int in =0;
while ((dirp = readdir(dp)) != NULL){ //krata to dir name
if((strcmp(dirp->d_name, ".") == 0)||(strcmp(dirp->d_name, "..") == 0))
continue;
(*fls)[in] = std::string(dirp->d_name);
in++;
}
closedir(dp);
}
return 0;
}
//hash gia strings
//https://stackoverflow.com/questions/16075271/hashing-a-string-to-an-integer-in-c - hash function gia strings
//prosarmosmenh sta dika moy dedomena
unsigned hash_str(std::string str)
{
uint32_t hash = 0x811c9dc5;
uint32_t prime = 0x1000193;
for(int i = 0; i < str.size(); ++i) {
uint8_t value = str[i];
hash = hash ^ value;
hash *= prime;
}
return hash;
}
//h sunarthsh epistrefei to katallhlo apotelesma gia to an to date1 einai megalutero, iso h mikrotero tou date2
std::string dates_compare(std::string date1, std::string date2){
if(is_date_ok(date1) == false)
return "problem";
if(is_date_ok(date2) == false)
return "problem";
if(date1 == "-") //de ginetai na mhn exei entry date
return "problem";
int params_count =0;
std::string intermediate;
std::stringstream check1(date1);
int date1_parts[3]; //mia thesh gia mera, mia gia mhna mia gia xronia.
while(getline(check1, intermediate, '-')) {
date1_parts[params_count] = stoi(intermediate);
params_count++;
} //telos while eksagwghs gnwrismatwn apo date1
//date2
if(date2 == "-")
return "smaller"; //den exei bgei akoma, eimaste ok me thn paula.
int params_count2 =0;
std::stringstream check2(date2);
int date2_parts[3]; //mia thesh gia mera, mia gia mhna mia gia xronia.
while(getline(check2, intermediate, '-')) {
date2_parts[params_count2] = stoi(intermediate);
params_count2++;
}//telos while eksagwghs gnwrismatwn apo date2
if(params_count2 != params_count)//problhmatiko input. de tha eprepe na dothei etsi sumfwna me ekfwnhsh
return "kakws orismena dates. shouldn't happen kata ekfwnhsh";
if(date1_parts[2] > date2_parts[2]) //megaluterh xronia
return "bigger";
if(date1_parts[2] == date2_parts[2]){ //ish xronia
if(date1_parts[1] > date2_parts[1]) //megaluteros mhnas me ish xronia
return "bigger";
if(date1_parts[1] == date2_parts[1]){ //isos mhnas me ish xronia
if(date1_parts[0] > date2_parts[0]) //megaluterh mera me iso mhna kai xronia
return "bigger";
if(date1_parts[0] == date2_parts[0]) //ola isa
return "equal";
if(date1_parts[0] < date2_parts[0]) //ish xronia isos mhnas mikroterh mera
return "smaller";
}
if(date1_parts[1] < date2_parts[1]) //mikroteros mhnas me ish xronia
return "smaller";
}
if(date1_parts[2] < date2_parts[2]) //mikroterh xronia
return "smaller";
}
//elegxw an ena date einai ok
bool is_date_ok(std::string dato){
if(dato == "-")
return true;
int params_count =0;
std::string intermediate;
std::stringstream check1(dato);
int date1_parts[3]; //mia thesh gia mera, mia gia mhna mia gia xronia.
while(getline(check1, intermediate, '-')) {
//std::cout << intermediate;
date1_parts[params_count] = stoi(intermediate);
params_count++;
} //telos while eksagwghs gnwrismatwn apo dato
if(params_count != 3) //den to zorizoume. einai lathos
return false;
if((date1_parts[1] == 1) || (date1_parts[1] == 3) || (date1_parts[1] == 5) || (date1_parts[1] == 7) || (date1_parts[1] == 8) || (date1_parts[1] == 10) || (date1_parts[1] == 12)){
if((date1_parts[0] <= 31)&&(date1_parts[0] >= 1))
return true;
}
else if((date1_parts[1] == 4) || (date1_parts[1] == 6) || (date1_parts[1] == 9) || (date1_parts[1] == 11) ){
if((date1_parts[0] <= 30)&&(date1_parts[0] >= 1))
return true;
}
else if(date1_parts[1] == 2){
if((date1_parts[0] <= 29)&&(date1_parts[0] >= 1))
return true;
}
return false;
}
//sortarisma arxeiwn me quicksort, prosarmosmenh sthn askhsh (strings)
//https://www.geeksforgeeks.org/quick-sort/
int partition (std::string * filen, int low, int high)
{
std::string pivot = filen[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than the pivot
if(dates_compare(filen[j], pivot) == "smaller")
{
i++; // increment index of smaller element, swap
std::string temp = filen[i];
filen[i] = filen[j];
filen[j] = temp;
}
}
std::string temp = filen[i+1];
filen[i+1] = filen[high];
filen[high] = temp;
return (i + 1);
}
void sort_files(std::string * filesn , int low, int high){
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(filesn, low, high);
// Separately sort elements before
// partition and after partition
sort_files(filesn, low, pi - 1);
sort_files(filesn, pi + 1, high);
}
}
//moy leei an einai sthn 1h, 2h, 3h h 4h orismenh kathgoria hlikias
int get_age_category(int age){
//error, should never happen
if(age <0)
return -1;
if((age >= 0)&&(age <= 20))
{return 0;} //ekei anhkei
else if((age >= 21)&&(age <= 40))
{return 1;} //ekei anhkei
else if((age >= 41)&&(age <= 60))
{return 2;} //ekei anhkei
else //61+
{return 3;} //ekei anhkei
}
//steile se kapoion (gonio) ta periexomena tou file summary
void send_file_summary(int wfd, int summ_entries, std::string filename, std::string country, file_summary * fsm, int bsize){
write(wfd, &summ_entries, sizeof(int));
if(summ_entries == 0)
return; //mh grapseis tipota allo
//paw na grapsw
send_string(wfd, &filename, bsize);
send_string(wfd, &country, bsize);
file_summary * currptr = fsm;
for(int i=0; i<summ_entries; i++){
//steile onoma iou
send_string(wfd, &(currptr->diseasename), bsize);
//grapse arithmo krousmatwn kathe kathgorias hlikiakhs
for(int j=0; j<4; j++)
write(wfd, &(currptr->age_cats[j]), sizeof(int));
currptr = currptr->next; //h parametros summ entries einai tetoia poy de tha prokalesei problhma
}//telos for periexomena tou summary
}//telos sunarthshs
//diabase apo kapoion (paidi) ta periexomena tou summary
void receive_and_print_file_summary(int rfd, int bsize){
int summ_entries =0;
read(rfd, &summ_entries, sizeof(int));
if(summ_entries == 0)
return; //mhn kaneis tpt allo
//paw na diabasw
std::string filename;
std::string country;
std::string dis_name;
receive_string(rfd, &filename, bsize);
receive_string(rfd, &country, bsize);
//AKOLOYTHW FORMAT EKTYPWSHS EKFWNHSHS
std::cout << filename << "\n";
std::cout << country << "\n";
for(int i=0; i<summ_entries; i++){
//diabase k printare onoma iou
receive_string(rfd, &dis_name, bsize);
std::cout << dis_name << "\n";
//diabase k deikse arithmo krousmatwn kathe kathgorias hlikiakhs
int krousm=0;
read(rfd, &krousm, sizeof(int));
std::cout << "Age range 0-20 years: " << krousm << " cases\n";
read(rfd, &krousm, sizeof(int));
std::cout << "Age range 21-40 years: " << krousm << " cases\n";
read(rfd, &krousm, sizeof(int));
std::cout << "Age range 41-60 years: " << krousm << " cases\n";
read(rfd, &krousm, sizeof(int));
std::cout << "Age range 60+ years: " << krousm << " cases\n";
//afhne kenh seira metaksu iwn opws fainetai na kanei h ekfwnhsh
std::cout << "\n";
}//telos for entries enos summary
}//telos sunarthshs
//gia th boh8htikh klash gia ta summaries ana io arxeiou
file_summary::file_summary(){
diseasename = "";
for(int i=0; i<4; i++)
age_cats[i] = 0; //arxikopoiei se 0
next = NULL;
}
//me tous kala orismenous destructors ths c++ arkei! h katastrofh ginetai anadromika se olous!!
file_summary::~file_summary(){
delete next;
}
int file_summary::insert_data(std::string * record_parts){
if(record_parts[5] != "-"){ //koitame MONO tis enter gia ta summaries
if(diseasename == ""){ //mono thn prwth fora
diseasename = record_parts[3]; //pare thn astheneia
if((std::stoi(record_parts[7]) >= 0)&&(std::stoi(record_parts[7]) <= 20))
{age_cats[0] += 1; return 1;} //ekei anhkei
else if((std::stoi(record_parts[7]) >= 21)&&(std::stoi(record_parts[7]) <= 40))
{age_cats[1] += 1; return 1;} //ekei anhkei
else if((std::stoi(record_parts[7]) >= 41)&&(std::stoi(record_parts[7]) <= 60))
{age_cats[2] += 1; return 1;} //ekei anhkei
else
{age_cats[3] += 1; return 1;} //ekei anhkei
}
if(diseasename == record_parts[3]){ //bre8hke h as8eneia, kanoyme enhmerwsh
if((std::stoi(record_parts[7]) >= 0)&&(std::stoi(record_parts[7]) <= 20))
{age_cats[0] += 1; return 0;} //ekei anhkei
else if((std::stoi(record_parts[7]) >= 21)&&(std::stoi(record_parts[7]) <= 40))
{age_cats[1] += 1; return 0;} //ekei anhkei
else if((std::stoi(record_parts[7]) >= 41)&&(std::stoi(record_parts[7]) <= 60))
{age_cats[2] += 1; return 0;} //ekei anhkei
else
{age_cats[3] += 1; return 0;} //ekei anhkei
}//telos if bre8hke astehneia
if(next == NULL){ //to vazoume ston epomeno adeio
next = new file_summary;
return next->insert_data(record_parts);
}
else //an o epomenos den einai adeios, tha krinei autos
return next->insert_data(record_parts);
}//telos if einai entry eggrafh
return -1;
}
//h 2h boh8htikh klash gia summaries
directory_summary::directory_summary(int filesn, std::string cnt){
nfiles = filesn;
countryname = cnt;
filenames = new std::string[nfiles];
nodes_per_file = new int[nfiles];
tfile_sums = new file_summary*[nfiles];
}
directory_summary::~directory_summary(){
for(int i=0; i<nfiles; i++)
delete tfile_sums[i];
delete[] tfile_sums;
delete[] nodes_per_file;
delete[] filenames;
}
//gia poll
void reset_poll_parameters(struct pollfd * pollfds, int length){
for(int i=0; i<length; i++)
pollfds[i].events = POLLIN;
}
//gia th dhimourgia logfile
int create_logfile(int succ, int fail, std::string * countries, int length){
std::ofstream myfile;
std::string fname = "log_file." + std::to_string(getpid());
myfile.open(fname);
for(int i=0; i<length; i++){
myfile << countries[i] << "\n";
}
myfile << "TOTAL " << succ+fail << "\n";
myfile << "SUCCESS " << succ << "\n";
myfile << "FAIL " << fail << "\n";
myfile.close();
return 0;
}