]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/statichuff/main_bychunk.c
12f8028ef5a06d3d506f1e45753bdc16572df44c
[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,decoded = 0;
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_encoder_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_scanfreq_chunk(shuff,chunk,i);                    
57                 }
58                 /* Le indico al huffman que efectivamente comprima los chunks */
59                 shuff_encode_file(shuff);
60                 
61                 /* De init encoder */
62                 shuff_deinit_encoder(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                 /* Init decoder */
74                 shuff = shuff_init_decoder(argv[optind],NULL);
75                 fp = fopen(argv[optind+1],"w");
76                 
77                 /* Gimme chunks till last one */
78                 while (shuff_decode_chunk(shuff,chunk,4,&decoded))
79                         fwrite(chunk,decoded,1,fp);
80                 
81                 /* Last chunk written alone */
82                 fwrite(chunk,decoded,1,fp);
83                 fclose(fp);
84                 
85                 /* Deinit decoder */
86                 shuff_deinit_decoder(shuff);
87                 free(shuff);
88         }
89                 
90         return 0;
91 }