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