/
main.c
67 lines (54 loc) · 1.3 KB
/
main.c
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
/* MARDALE Andrei-Iulian - 311CB */
#include "dns.h"
#define BSIZE 1000
int main (int argc, char *argv[]) {
if (argc != 4) return 3;
int M = strtol(argv[1], NULL, 10); //get numerical value from string
//open input file
FILE *in = fopen(argv[2], "r");
if (!in) return 2;
//open output file
FILE *out = fopen(argv[3], "w");
if (!out) {
fclose(in);
return 2;
}
//buffer input which will be used to parse commands
//aux is used to check return value of function fgets
//aux tells us when to stop reading
char *aux = NULL, *buffer = NULL;
buffer = malloc(BSIZE * sizeof(char));
if (!buffer) {
fclose(in);
fclose(out);
return 2;
}
THash *H = initHash(M);
if (!H) {
fclose(in);
fclose(out);
free(buffer);
return 2;
}
aux = fgets(buffer, BSIZE, in);
while (aux) {
if (buffer[0] == '\n') break; //limit case when end of file reached (almost) and last line is '\n'
buffer[strlen(buffer) - 1] = '\0';
//try to parse commands
//if command is put and memory cannot be allocated then exit program (clean)
if (parse(H, buffer, out) == -1) {
fclose(in);
fclose(out);
free(buffer);
destroyHash(&H, freeInf);
return 2;
}
aux = fgets(buffer, BSIZE, in);
}
//free everything
destroyHash(&H, freeInf);
fclose(in);
fclose(out);
free(buffer);
return 0;
}