]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/statichuff/main_bychunk.c
e0f1e2850601f4d4a9f6b436cee2f8046c872023
[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         int sflag = 0;
14         int mflag = 0;
15         long int volumesize = 0;
16         int lastchunk,i,j,ch,decoded = 0;
17         char *staticmodel;
18                         
19         while ((ch = getopt(argc, argv, "scdm:t:")) != -1) { 
20                  
21                 switch (ch) { 
22                         case 'c': cflag = 1; 
23                                           break;
24                         
25                         case 'd': dflag = 1; 
26                                           break; 
27                         
28                         case 't': tflag = 1; 
29                                           volumesize = atoi(optarg);                                      
30                                           break; 
31                         
32                         case 'm': mflag = 1;
33                                           staticmodel = optarg;
34                                           break; 
35                         
36                         case 's': sflag = 1;                                      
37                                           break;
38                         
39                         default: fprintf(stderr, "Usage: %s [-cds] [-t volsizekb] sourcefile targetfile [-m modeldumpfile]\n", argv[0]); 
40                                          return(2);
41                 }
42         }
43                 
44         if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) || (mflag & sflag)) {
45                 fprintf(stderr, "Usage: %s [-cds] [-t volsizekb] sourcefile targetfile [-m modeldumpfile]\n", argv[0]); 
46                 if ((tflag == 1) && (volumesize < 0)) fprintf(stderr,"Error: The volume size must be a non-zero value\n");
47                 return (2);             
48         }
49                 
50         if (cflag == 1) {
51                 /* Inicio un compresor huffman estatico por chunks */
52                 if ((shuff = shuff_init_encoder_bychunk(argv[optind+1],volumesize*1024)) == NULL) return 0;
53                 if (mflag == 1) shuff_loadmodel(shuff,staticmodel);
54                 
55                 /* Comprimo por chunks */               
56                 if ((fp = fopen(argv[optind],"rb")) == NULL) return 1;          
57                 while (!feof(fp)) {
58                         i = 0;
59                         while (!feof(fp) && (i < 4))
60                         {
61                           ch = fgetc(fp);
62                           if (feof(fp)) continue;
63                           chunk[i] = ch;                          
64                           i++;
65                         }                       
66                         /* Comprimo el chunk con huffman estatico */                                            
67                         shuff_scanfreq_chunk(shuff,chunk,i);                    
68                 }
69                 /* Le indico al huffman que efectivamente comprima los chunks */
70                 shuff_encode_file(shuff);               
71                 if (sflag == 1) shuff_savemodel(shuff);         
72                 
73                 /* De init encoder */
74                 shuff_deinit_encoder(shuff);
75                 
76                 /* Free mem allocated by main */
77                 free(shuff);            
78                 
79                 /* Close files opened by main */
80                 fclose(fp);
81         }
82         
83         if (dflag == 1) { 
84                 /* Init decoder */
85                 shuff = shuff_init_decoder(argv[optind],NULL);
86                 fp = fopen(argv[optind+1],"w");
87                 
88                 /* Gimme chunks till last one */
89                 while (shuff_decode_chunk(shuff,chunk,4,&decoded))
90                         fwrite(chunk,decoded,1,fp);
91                 
92                 /* Last chunk written alone */
93                 fwrite(chunk,decoded,1,fp);
94                 fclose(fp);
95                 
96                 /* Deinit decoder */
97                 shuff_deinit_decoder(shuff);
98                 free(shuff);            
99         }
100         
101         /* Free mem */
102         free(chunk);            
103         return 0;
104 }