]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/statichuff/main_bychunk.c
cosas y mas cosas de prueba
[z.facultad/75.06/jacu.git] / src / statichuff / main_bychunk.c
1
2 #include "statichuff.h"
3 #include <stdlib.h>
4
5 int main(int argc, char* argv[])
6 {       
7         HUFF_STATE *shuff;
8         FILE *fp;
9         char *chunk = (char*)malloc(sizeof(char)*4);
10         int cflag = 0;
11         int dflag = 0;
12         int tflag = 0;
13         long int volumesize = 0;
14         int lastchunk,i,j,ch;
15                         
16         while ((ch = getopt(argc, argv, "cdt:")) != -1) { 
17                  
18                 switch (ch) { 
19                         case 'c': cflag = 1; 
20                                           break;
21                         
22                         case 'd': dflag = 1; 
23                                           break; 
24                         
25                         case 't': tflag = 1; 
26                                           volumesize = atoi(optarg);                                      
27                                           break; 
28                         
29                         default: fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); 
30                                          return(2);
31                 }
32         }
33                 
34         if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
35                 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); 
36                 if ((tflag == 1) && (volumesize < 0)) fprintf(stderr,"Error: The volume size must be a non-zero value\n");
37                 return (2);             
38         }
39                 
40         if (cflag == 1) {
41                 /* Inicio un compresor huffman estatico por chunks */
42                 if ((shuff = shuff_init_static_bychunk(argv[optind+1],volumesize*1024)) == NULL) return 0;
43                 
44                 /* Comprimo por chunks */               
45                 if ((fp = fopen(argv[optind],"rb")) == NULL) return 1;          
46                 while (!feof(fp)) {
47                         i = 0;
48                         while (!feof(fp) && (i < 4))
49                         {
50                           ch = fgetc(fp);
51                           if (feof(fp)) continue;
52                           chunk[i] = ch;                          
53                           i++;
54                         }                       
55                         /* Comprimo el chunk con huffman estatico */                                            
56                         shuff_encode_chunk(shuff,chunk,i,0);
57                 }
58                 /* Le indico al huffman que efectivamente comprima los chunks */
59                 shuff_encode_file(shuff);
60                 
61                 /* De init shuffman by chunks */
62                 shuff_deinit_static_bychunk(shuff);
63                 
64                 /* Free mem allocated by main */
65                 free(shuff);
66                 free(chunk);
67                 
68                 /* Close files opened by main */
69                 fclose(fp);
70         }
71         
72         if (dflag == 1) { 
73                 /* Descomprimo */
74                 return shuff_decode_file(argv[optind],argv[optind+1]);
75         }
76                 
77         return 0;
78 }