]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
aad39e267f1a4a145a9995b6624a4bed4d52e0f0
[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 pflag = 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:p:")) != -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 'p': pflag = 1; 
43                                           pagesize = atoi(optarg);
44                                           break; 
45
46                         default: fprintf(stderr, "Usage: %s [-cdpt] sourcefile targetfile\n", argv[0]); 
47                                          return(2);
48                 }
49         }
50                 
51         if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
52                 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); 
53                 return (3);
54         }
55         if ((tflag) && (volumesize <= 0l)) {
56                 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
57                 return (4);
58         }
59         if ((pflag) && (pagesize <= 1u)) {
60                 fprintf(stderr,"Error: El tamaño de página debe ser mayor a 1 byte.\n");
61                 return (5);
62         }
63                 
64         if (cflag == 1) {
65                 /* Comprimo */
66                 /* No me gusta el tmpfile ... es para probar como anda todo junto */
67                 FILE *fp, *fp_out;
68                 Uint32 i, j, total, k;
69                 char *mtf;
70                 char *salida, *data, c;
71                 char *z;
72                 int z_len;
73
74                 data = malloc(sizeof(char)*pagesize);
75                 /* Reservo lugar tambien para guardar el k y el tamaño  */
76                 salida = malloc(sizeof(char)*(pagesize)+sizeof(Uint32)*2);
77                 bs = bs_create(pagesize);
78
79                 fp = fopen(argv[optind], "rb");
80                 fp_out = fopen("tmp.comp", "wb");
81
82                 c = fgetc(fp);
83                 total = 0;
84                 while (!feof(fp)) {
85                         i = 0;
86                         while ((!feof(fp)) && (i < pagesize)) {
87                                 data[i++] = c;
88                                 c = fgetc(fp);
89                                 total++;
90                         }
91
92                         /* Hago el BS */
93                         bs_solve(data, salida, bs, &k, i);
94
95                         /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
96                         mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
97
98                         /* Si me lo piden, aplico ZG. */
99                         if (zflag) {
100                                 size_t len;
101                                 char buff[2];
102                                 ZG zg;
103                                 zg_init(&zg);
104                                 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
105                                 for (j = 0; j < i; ++j)
106                                         if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
107                         } else {
108                                 /* Guardo el PageSize */
109                                 //for(j=0; j<sizeof(Uint32); j++)
110                                 //      fputc(mtf[j], fp_out);
111                                 fwrite(mtf, sizeof(Uint32), 1, fp_out);
112
113                                 /* Guardo el Z len y el Z */
114                                 fwrite(&z_len, sizeof(int), 1, fp_out);
115                                 fwrite(z, z_len, sizeof(char), fp_out);
116
117                                 /* Guardo la salida del MTF */
118                                 for(j=sizeof(Uint32); j<i; j++)
119                                         fputc(mtf[j], fp_out);
120                         }
121                         free(mtf);
122                         free(z);
123                 }
124
125                 /* Limpiando */
126                 fclose(fp);
127                 fclose(fp_out);
128                 bs_destroy(bs);
129
130                 /* Comprimo con huffman */
131                 i = shuff_encode_file("tmp.comp",argv[optind+1], volumesize);
132
133                 /* borro el temporal */
134                 unlink("tmp.comp");
135
136                 /* Muestro bpb */
137                 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
138                 return i;
139         }
140         
141         if (dflag == 1) { 
142                 /* Descomprimo */
143                 FILE *fp_out;
144                 FILE *fp_in;
145                 Uint32 block_size, k;
146                 char *block, *mtf, *orig;
147                 char *z;
148                 int z_len;
149
150                 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
151                 fp_in = fopen("tmp.comp", "rb");
152                 fp_out = fopen(argv[optind+1], "wb");
153
154                 while (!feof(fp_in)) {
155                         block_size = 0;
156                         PERR("Leo bloque");
157                         fread(&block_size, sizeof(Uint32), 1, fp_in);
158                         fread(&z_len, sizeof(int), 1, fp_in);
159                         z = malloc(sizeof(char)*z_len);
160                         fread(z, z_len, sizeof(char), fp_in);
161
162                         if (block_size > 0) {
163                                 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
164                                 orig = malloc(block_size*sizeof(char));
165                                 fread(block, block_size, sizeof(char), fp_in);
166
167                                 /* Hago el MTF inverso */
168                                 mtf = jacu_mtf_inv(z, block, block_size);
169
170                                 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
171                                 memcpy(&k, block, sizeof(Uint32));
172
173                                 bs_restore(orig, block+sizeof(Uint32), k, block_size);
174
175                                 fwrite(orig, block_size, sizeof(char), fp_out);
176                                 free(block);
177                                 free(orig);
178                                 free(mtf);
179                         }
180                 }
181                 fclose(fp_in);
182                 fclose(fp_out);
183         }
184
185         return 0;
186 }
187
188 long get_file_size(const char* filename)
189 {
190         FILE* file;
191         long  file_size;
192
193         if (!(file = fopen(filename, "ab"))) return -1;
194         file_size = ftell(file);
195         fclose(file);
196         return file_size;
197 }
198