-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelping_funs.cpp
441 lines (360 loc) · 9.32 KB
/
helping_funs.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
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
#include "helping_funs.h"
unsigned int get_number_of_digits(int num)
{
if (num == 0)
{
return 1;
}
else
{
return floor(log10(abs(num))) + 1; //https://stackoverflow.com/questions/3068397/finding-the-length-of-an-integer-in-c
}
}
FILE * open_file(char * fn)
{
FILE* random_file;
random_file = fopen(fn, "r");
if (!random_file)
{
fprintf(stderr, "-! ERROR - docfile does not exist !-\n");
return NULL;
}
return random_file;
}
unsigned int get_number_of_lines(FILE * fp)
{
unsigned int lines = 0;
int ch ;
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
lines++;
}
}
rewind(fp);
return lines ;
}
char * read_str(unsigned int str_len)
{ //reads and returns string of unknown length
char * str = (char *) malloc((str_len) * sizeof(char));
int ch;
unsigned int i = 0;
while(((ch = getchar()) != '\n') && (ch != EOF))
{
str[i] = ch;
i++;
if(i == str_len)
{
str_len++;
str = (char *) realloc(str, str_len * sizeof(char));
}
}
str[i] = '\0'; // add \0 at the end of the string
return str;
}
char ** strip_line_into_words(char * cmd, unsigned int & cmd_nwords)
{//returns a char * array which contains each cmd's word
char ** cmd_words = NULL;
char * word = strtok (cmd, " \t");
unsigned int word_counter = 0;
while (word != NULL)
{ //for each cmd's word
cmd_words = (char **) realloc(cmd_words, (word_counter + 1) * sizeof(char *));
cmd_words[word_counter] = (char *) malloc((strlen(word) + 1) * sizeof(char)); // plus one for \0
strcpy(cmd_words[word_counter], word);
word_counter++;
word = strtok (NULL, " \t");
}
cmd_nwords = word_counter;
return cmd_words;
}
void delete_str_array(char ** str_array, unsigned int array_size)
{
for(unsigned int i = 0; i < array_size; i++)
{
free(str_array[i]);
}
free(str_array);
}
bool parse_docs(char **docs, FILE * fp)
{// storing docfile's lines into docs array - checking docfile for errors
char * line = NULL;
size_t len = 0;
ssize_t nchars;
int line_index = 0;
while ((nchars = getline(&line, &len, fp)) != -1)
{
if ( line[nchars - 1] == '\n')
{ // removing newline character from string
line[nchars - 1] = '\0';
nchars--;
}
DIR * dir = opendir(line);
if (dir)
{
/* Directory exists. */
closedir(dir);
}
else if (ENOENT == errno)
{
/* Directory does not exist. */
cout<<"-! ERROR !- Directory with path:'"<<line<<"' does not exist."<<endl;
free(line);
return 1;
}
else
{
/* opendir() failed for some other reason. */
cout<<"-! ERROR !- Directory with path:'"<<line<<"' cannot be opened."<<endl;
free(line);
return 1;
}
docs[line_index] = (char *) malloc((strlen(line) + 1) * sizeof(char)); //plus one for \0
strcpy(docs[line_index], line); //storing docfile's line into doc array
line_index++;
}
free(line);
return 0;
}
char ** parse_text_file(FILE * fp, unsigned int & n_lines, unsigned int & n_chars)
{// storing docfile's lines into docs array
char * line = NULL;
size_t len = 0;
ssize_t nchars;
n_lines = 0;
n_chars = 0;
char ** lines = NULL;
while ((nchars = getline(&line, &len, fp)) != -1)
{
if ( line[nchars - 1] == '\n')
{ // removing newline character from string
line[nchars - 1] = '\0';
nchars--;
}
n_chars += nchars + 1;
lines = (char **) realloc(lines, (n_lines + 1) * sizeof(char *));
lines[n_lines] = (char *) malloc((strlen(line) + 1) * sizeof(char)); //plus one for \0
strcpy(lines[n_lines], line); //storing docfile's line into doc array
n_lines++;
}
free(line);
if (n_chars > 0)
{
n_chars--;
}
return lines;
}
int scan_dir_for_files(char * in_dir)
{//checking that text files at every dir exist
DIR * FD;
struct dirent* in_file;
/* Scanning directory */
if (NULL == (FD = opendir(in_dir)))
{
fprintf(stderr, "Error : Failed to open input directory - %s\n", strerror(errno));
return 1;
}
while ((in_file = readdir(FD)))
{
if (!strcmp (in_file->d_name, "."))
continue;
if (!strcmp (in_file->d_name, ".."))
continue;
FILE * in_dir_file = open_file(in_file->d_name);
if (in_dir_file == NULL)
{
fprintf(stderr, "Error : Failed to open directory file - %s\n", strerror(errno));
return 1;
}
fclose(in_dir_file);
}
return 0;
}
bool is_number(const char *str)
{
unsigned int dot_counter = 0;
for(unsigned int i=0; i < strlen(str); i++)
{ //iterating the whole string character by character
if(str[i] == '.')
{
dot_counter++;
if((i == 0) || (i == strlen(str) - 1))
{
return 0;
}
else
{
if(dot_counter == 2)
{
return 0;
}
}
}
else if(!isdigit(str[i]))
{
return 0;
}
}
return 1;
}
bool is_unsigned_int_number(const char *str)
{
for(unsigned int i=0; i < strlen(str); i++)
{ //iterating the whole string character by character
if(!isdigit(str[i]))
{// and checking if every character is a number
return 0;
}
}
return 1;
}
char * make_full_file_path(char * path, char * text_file_name)
{
char * full_file_path = (char *)malloc((strlen(text_file_name) + strlen(path) + 1 + 1) * sizeof(char));
strcpy(full_file_path, path);
strcat(full_file_path, "/");
strcat(full_file_path, text_file_name);
return full_file_path;
}
int send_msg(const char * msg, unsigned int fd)
{//send a string of uknown legth through fifo
int buf_size = strlen(msg) + 1;
if (write(fd, &buf_size, sizeof(int)) < 0)
{
perror("problem in writing fifo");
exit(4);
}
if (write(fd, msg, buf_size) < 0)
{
perror("problem in writing fifo");
exit(4);
}
return 0;
}
char * read_msg(unsigned int fd)
{//read a string of uknown legth from fifo
int buf_size;
if (read(fd, &buf_size, sizeof(int)) < 0)
{
perror("problem in reading fifo");
exit(5);
}
char * inc_msg = (char *)malloc(buf_size);
if (read(fd, inc_msg, buf_size) < 0)
{
perror("problem in reading fifo");
exit(5);
}
return inc_msg;
}
int send_int_number(unsigned int fd, int number)
{
if (write(fd, &number, sizeof(int)) < 0)
{
perror("problem in writing fifo");
exit(4);
}
return 0;
}
int read_int_number(unsigned int fd)
{
int number;
if (read(fd, &number, sizeof(int)) < 0)
{
perror("problem in reading fifo");
exit(5);
}
return number;
}
char * check_program_args(char ** argv, int argc, unsigned int & N)
{
if (argc != 5)
{
cout<<"-! ERROR - Wrong number of arguments is given !-"<<endl;
exit(1);
}
char * docfile = NULL;
bool N_found = 0;
for(int i = 1; i < argc; i++)
{
if(strcmp(argv[i], "-d") == 0)
{// if -d parameter found the next argument must be docfile's name
if((i + 1) == argc)
{
cout<<"-! ERROR - Docfile's name not found !-"<<endl;
exit(1);
}
else
{
docfile = argv[i + 1];
i++;
}
}
else if(strcmp(argv[i], "-w") == 0)
{
if((i + 1) == argc)
{
cout<<"-! ERROR - w's value not found !-"<<endl;
exit(1);
}
else
{
if(!is_unsigned_int_number(argv[i + 1]))
{
cout<<"-> ERROR - w's value is not an unsigned int number !-"<<endl;
exit(1);
}
else
{
N = atoi(argv[i + 1]);
N_found = 1;
i++;
}
}
}
else
{
cout<<"-! ERROR - invalid parameters given !-"<<endl;
exit(1);
}
}
if(docfile == NULL)
{
cout<<"-! ERROR - Docfile's -d parameter not found !-"<<endl;
exit(1);
}
if(N_found == 0)
{
cout<<"-! ERROR - numWorkers' -w parameter not found !-"<<endl;
exit(1);
}
else
{
if (N < 1)
{
cout<<"-> ERROR - numWorkers' value is 0 !-"<<endl;
exit(1);
}
}
return docfile;
}
char * get_timestamp()
{
time_t now = time(NULL);
char* dt = ctime(&now);
dt[strlen(dt) - 1] = '\0';
return dt;
}
void sep_term_line()
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
unsigned int width = w.ws_col;
for(unsigned int i = 0; i < width; i++)
{
cout<<"-";
}
cout<<endl;
}