]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
borro el archivo temporal cuando no lo uso mas.
[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 <stdlib.h>
6 #include <stdio.h>
7 #include <unistd.h>
8
9 int main(int argc, char* argv[])
10 {       
11         int cflag = 0;
12         int dflag = 0;
13         int tflag = 0;
14         long int volumesize = 0;
15         int ch;
16         t_BlockSort *bs;
17                         
18         while ((ch = getopt(argc, argv, "cdt:")) != -1) { 
19                  
20                 switch (ch) { 
21                         case 'c': cflag = 1; 
22                                           break;
23                         
24                         case 'd': dflag = 1; 
25                                           break; 
26                         
27                         case 't': tflag = 1; 
28                                           volumesize = atoi(optarg);                                      
29                                           break; 
30                         
31                         default: fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); 
32                                          return(2);
33                 }
34         }
35                 
36         if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
37                 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); 
38                 if ((tflag == 1) && (volumesize <= 0)) fprintf(stderr,"Error: The volume size must be a non-zero value\n");
39                 return (2);             
40         }
41                 
42         if (cflag == 1) {
43                 /* Comprimo */
44                 /* No me gusta el tmpfile ... es para probar como anda todo junto */
45                 FILE *fp, *fp_out;
46                 unsigned long int len, i, j, total, k;
47                 int *mtf;
48                 char *salida, *data, c;
49                 len = 4096;
50                 data = malloc(sizeof(char)*len);
51                 salida = malloc(sizeof(char)*(len));
52                 bs = bs_create(len);
53                 fp = fopen(argv[optind], "rb");
54                 fp_out = fopen("tmp.comp", "wb");
55                 c = fgetc(fp);
56                 total = 0;
57                 while (!feof(fp)) {
58                         i = 0;
59                         while ((!feof(fp)) && (i < len)) {
60                                 data[i++] = c;
61                                 c = fgetc(fp);
62                                 total++;
63                         }
64                         /* Hago el BS */
65                         bs_solve(data, salida, bs, &k, i);
66                         /* Le aplico el MTF */
67                         mtf = jacu_mtf(salida, i);
68                         for(j=0; j<i; j++)
69                                 fputc(mtf[j], fp_out);
70                 }
71                 fclose(fp);
72                 fclose(fp_out);
73                 bs_destroy(bs);
74                 i = shuff_encode_file("tmp.comp",argv[optind+1]);
75                 /* borro el temporal */
76                 unlink("tmp.comp");
77                 return i;
78         }
79         
80         if (dflag == 1) { 
81                 /* Descomprimo */
82                 return shuff_decode_file(argv[optind],argv[optind+1]);
83         }
84                 
85         return 0;
86 }