]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/statichuff/main_bychunk.c
Intento de poner a andar el ZG.
[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                 free(chunk);
79                 
80                 /* Close files opened by main */
81                 fclose(fp);
82         }
83         
84         if (dflag == 1) { 
85                 /* Init decoder */
86                 shuff = shuff_init_decoder(argv[optind],NULL);
87                 fp = fopen(argv[optind+1],"w");
88                 
89                 /* Gimme chunks till last one */
90                 while (shuff_decode_chunk(shuff,chunk,4,&decoded))
91                         fwrite(chunk,decoded,1,fp);
92                 
93                 /* Last chunk written alone */
94                 fwrite(chunk,decoded,1,fp);
95                 fclose(fp);
96                 
97                 /* Deinit decoder */
98                 shuff_deinit_decoder(shuff);
99                 free(shuff);
100         }
101                 
102         return 0;
103 }