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