forked from antoniolm23/lz78
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
144 lines (128 loc) · 4.15 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
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
/***********************************************
*Copyright La Marra Antonio Rotili Giacomo
* (c) 2013
**********************************************/
//#include "everything.h"
#include "comp.h"
//#include "dict.h"
//#include "hash.h"
#include "everything.h"
int main(int argc, char* argv[]) {
//array to keep int account the options passed:
/*0: c 1: v 2: i 3: o 4: d 5: s */
bool option[7];
char* input_filename=NULL;
char* output_filename=NULL;
int size=0;
//int input_fd;
//int output_fd;
/* RAW Performance evaluation */
//clock_t begin, end;
//double time_spent;
//used to address the parameters passed to the call
//int optindex[6]={0, 0, 0, 0, 0, 0};
//inizialization of option array
int i;
//char* namefile;
for(i=0; i<7; i++) option[i]=false;
//checking of number of arguments passed
if(argc<2) { //this means that the program was called without arguments
fprintf(stderr, "Missing arguments\n");//print message on stderror
exit(-1);//exit from the program
}
//checking the type of arguments passed
int actopt;//takes into account the character corresponding to actual option
while((actopt=getopt(argc, argv, "cdvi:o:s:k:"))!=-1) {
switch(actopt) {
//compression command
case 'c':
option[0]=true;
break;
//verbose output
case 'd':
option[1]=true;
//optindex[1]=optind;
break;
//input filename
case 'i':
option[2]=true;
input_filename=optarg;
if(optarg==NULL) {
fprintf(stderr, "error, no filename passed\n");
return -1;
}
fprintf(stderr, "%s\n", input_filename);
break;
//output filename
case 'o':
option[3]=true;
output_filename=optarg;
if(optarg==NULL) {
fprintf(stderr, "error, no filename passed\n");
return -1;
}
fprintf(stderr, "%s\n", output_filename);
//optindex[3]=optind;
break;
//decompression command
case 'v':
option[4]=true;
//optindex[4]=optind;
break;
//dictionary size
case 's':
option[5]=true;
size=atoi(optarg);
//optindex[5]=optind;
break;
//input string
case 'k':
option[6]=true;
//here we have to write the passed string to the stdin manually?
break;
default:
break;
}
}
//managing options
if(size<900) size=1000;
//compression
if(option[0]==true) {
if(option[6]==true) {
input_filename=NULL;
}
else {
if(input_filename==NULL) input_filename="file.txt";
//input_fd=open(input_filename, 'r');
}
if(output_filename==NULL) output_filename="compressed.lz78";
//begin = clock();
compress(input_filename, output_filename, size);
//end = clock();
//time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
//if verbose print the time spent
//if(option[4]==true) fprintf(stderr, "time spent: %f\n", time_spent);
//if verbose print the compression ratio
//if(option[4]==true) {
//off_t i_fsize, o_fsize;
//input_fd=open(input_filename, 'r');
//i_fsize = lseek(input_fd, 0, SEEK_END);
//output_fd=open(output_filename, 'r');
//o_fsize = lseek(output_fd, 0, SEEK_END);
//fprintf(stderr, "compression ratio: %f\n", (double)o_fsize/i_fsize);
//}
}
/*//decompression
if(option[1]==true) {
if(input_filename==NULL) input_filename="compressed.lz78";
if(output_filename==NULL) output_filename="decomp.txt";
//better managing file extensions automatically or having the possibility of printing the
//out of the decompressor to the standard output?
begin = clock();
decompress(input_filename, output_filename);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
if(option[4]==true) fprintf(stderr, "time spent: %f\n", time_spent);
}*/
return 0;
}