]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
d31c648505d8bbd2a63d52335df36992d5f85e46
[z.facultad/75.06/jacu.git] / src / jacu.c
1
2 #include "blocksorting/bs.h"
3 #include "mtf/mtf.h"
4 #include "zerogrouping/zerogrouping.h"
5 #include "statichuff/statichuff.h"
6 #include "vfile/vfile.h"
7 #include "vfile/common.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <unistd.h>
11
12 long get_file_size(const char* filename);
13
14 int main(int argc, char* argv[])
15 {       
16         int cflag = 0;
17         int dflag = 0;
18         int zflag = 0;
19         int tflag = 0;
20         int qflag = 0;
21         long int volumesize = 0;
22         size_t pagesize = 32768; /* 32KB */
23         int ch;
24         t_BlockSort *bs;
25                         
26         while ((ch = getopt(argc, argv, "cdzt:q:")) != -1) { 
27                  
28                 switch (ch) { 
29                         case 'c': cflag = 1; 
30                                           break;
31
32                         case 'd': dflag = 1; 
33                                           break; 
34
35                         case 'z': zflag = 1; 
36                                           break; 
37
38                         case 't': tflag = 1; 
39                                 volumesize = atol(optarg);
40                                 break; 
41
42                         case 'q': qflag = 1; 
43                                 switch (atoi(optarg))
44                                 {
45                                         case 0: pagesize = 1024; /* 1K */
46                                                 break;
47                                         case 1: pagesize = 2048; /* 2K */
48                                                 break;
49                                         case 2: pagesize = 4096; /* 4K */
50                                                 break;
51                                         case 3: pagesize = 8192; /* 8K */
52                                                 break;
53                                         case 4: pagesize = 16384; /* 16K */
54                                                 break;
55                                         case 5: pagesize = 32768; /* 32K */
56                                                 break;
57                                         case 6: pagesize = 65536; /* 64K */
58                                                 break;
59                                         case 7: pagesize = 131072; /* 128K */
60                                                 break;
61                                         case 8: pagesize = 262144; /* 256K */
62                                                 break;
63                                         case 9: pagesize = 524288; /* 512K */
64                                                 break;
65                                         default: pagesize = 0; /* error */
66                                 }
67                                 break; 
68
69                         default: fprintf(stderr, "Usage: %s [-cdpt] sourcefile targetfile\n", argv[0]); 
70                                          return(2);
71                 }
72         }
73                 
74         if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
75                 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); 
76                 return (3);
77         }
78         if ((tflag) && (volumesize <= 0l)) {
79                 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
80                 return (4);
81         }
82         if ((qflag) && (pagesize <= 1u)) {
83                 fprintf(stderr,"Error: El nivel de compresión debe ser entre 0 (menor) y 9 (mayor).\n");
84                 return (5);
85         }
86                 
87         if (cflag == 1) {
88                 /* Comprimo */
89                 /* No me gusta el tmpfile ... es para probar como anda todo junto */
90                 FILE *fp, *fp_out;
91                 Uint32 i, j, total, k;
92                 char *mtf;
93                 char *salida, *data, c;
94                 char *z;
95                 int z_len;
96
97                 data = malloc(sizeof(char)*pagesize);
98                 /* Reservo lugar tambien para guardar el k y el tamaño  */
99                 salida = malloc(sizeof(char)*(pagesize)+sizeof(Uint32)*2);
100                 bs = bs_create(pagesize);
101
102                 fp = fopen(argv[optind], "rb");
103                 fp_out = fopen("tmp.comp", "wb");
104
105                 c = fgetc(fp);
106                 total = 0;
107                 while (!feof(fp)) {
108                         i = 0;
109                         while ((!feof(fp)) && (i < pagesize)) {
110                                 data[i++] = c;
111                                 c = fgetc(fp);
112                                 total++;
113                         }
114
115                         /* Hago el BS */
116                         bs_solve(data, salida, bs, &k, i);
117
118                         printf("BS k=%ld\n", k);
119                         printf("PageSize = %ld\n", i);
120
121                         /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
122                         mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
123
124                         printf("MTF Z (len=%d) = [", z_len);
125                         {
126                                 int ii;
127                                 for(ii=0; ii<z_len; ii++)
128                                         printf("%c", z[ii]);
129                                 printf("]\n");
130
131                         }
132                         /* Si me lo piden, aplico ZG. */
133                         if (zflag) {
134                                 size_t len;
135                                 char buff[2];
136                                 ZG zg;
137                                 zg_init(&zg);
138                                 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
139                                 for (j = 0; j < i; ++j)
140                                         if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
141                         } else {
142                                 /* Guardo el PageSize */
143                                 //for(j=0; j<sizeof(Uint32); j++)
144                                 //      fputc(mtf[j], fp_out);
145                                 fwrite(&i, sizeof(Uint32), 1, fp_out);
146
147                                 /* Guardo el Z len y el Z */
148                                 fwrite(&z_len, sizeof(int), 1, fp_out);
149                                 fwrite(z, z_len, sizeof(char), fp_out);
150
151                                 /* Guardo la salida del MTF */
152                                 for(j=sizeof(Uint32); j<i; j++)
153                                         fputc(mtf[j], fp_out);
154                         }
155                         free(mtf);
156                         free(z);
157                 }
158
159                 /* Limpiando */
160                 fclose(fp);
161                 fclose(fp_out);
162                 bs_destroy(bs);
163
164                 /* Comprimo con huffman */
165                 i = shuff_encode_file("tmp.comp", argv[optind+1], volumesize);
166
167                 /* borro el temporal */
168                 remove("tmp.comp");
169
170                 /* Muestro bpb */
171                 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
172                 return i;
173         }
174         
175         if (dflag == 1) { 
176                 /* Descomprimo */
177                 FILE *fp_out;
178                 FILE *fp_in;
179                 Uint32 block_size, k;
180                 char *block, *mtf, *orig;
181                 char *z;
182                 int z_len;
183
184                 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
185                 fp_in = fopen("tmp.comp", "rb");
186                 fp_out = fopen(argv[optind+1], "wb");
187
188                 while (!feof(fp_in)) {
189                         block_size = 0;
190                         PERR("Leo bloque");
191                         fread(&block_size, sizeof(Uint32), 1, fp_in);
192                         printf("PageSize = %ld\n", block_size);
193                         fread(&z_len, sizeof(int), 1, fp_in);
194                         z = malloc(sizeof(char)*z_len);
195                         fread(z, z_len, sizeof(char), fp_in);
196
197                         printf("MTF Z (len=%d) = [", z_len);
198                         {
199                                 int ii;
200                                 for(ii=0; ii<z_len; ii++)
201                                         printf("%c", z[ii]);
202                                 printf("]\n");
203
204                         }
205                         if (block_size > 0) {
206                                 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
207                                 orig = malloc(block_size*sizeof(char));
208                                 fread(block, block_size, sizeof(char), fp_in);
209
210                                 /* Hago el MTF inverso */
211                                 mtf = jacu_mtf_inv(z, block, block_size);
212
213                                 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
214                                 memcpy(&k, block, sizeof(Uint32));
215
216                                 printf("Restored : k=%ld\n", k);
217                                 bs_restore(orig, block+sizeof(Uint32), k, block_size);
218
219                                 fwrite(orig, block_size, sizeof(char), fp_out);
220                                 free(block);
221                                 free(orig);
222                                 free(mtf);
223                         }
224                 }
225                 fclose(fp_in);
226                 fclose(fp_out);
227         }
228
229         return 0;
230 }
231
232 long get_file_size(const char* filename)
233 {
234         FILE* file;
235         long  file_size;
236
237         if (!(file = fopen(filename, "ab"))) return -1;
238         file_size = ftell(file);
239         fclose(file);
240         return file_size;
241 }
242