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