]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/statichuff/main_bychunk.c
Cambios minimos, no se si entraran en la impresion :(
[z.facultad/75.06/jacu.git] / src / statichuff / main_bychunk.c
1 /*----------------------------------------------------------------------------
2  *                   jacu - Just Another Compression Utility
3  *----------------------------------------------------------------------------
4  * This file is part of jacu.
5  *
6  * jacu is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * jacu is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with jacu; if not, write to the Free Software Foundation, Inc., 59 Temple
18  * Place, Suite 330, Boston, MA  02111-1307  USA
19  *----------------------------------------------------------------------------
20  */
21
22
23 #include "statichuff.h"
24 #include <stdlib.h>
25
26 int main(int argc, char* argv[])
27 {       
28         HUFF_STATE *shuff;
29         FILE *fp;
30         char *chunk = (char*)malloc(sizeof(char)*4);
31         int cflag = 0;
32         int dflag = 0;
33         int tflag = 0;
34         int sflag = 0;
35         int mflag = 0;
36         long int volumesize = 0;
37         int lastchunk,i,j,ch,decoded = 0;
38         char *staticmodel;
39                         
40         while ((ch = getopt(argc, argv, "scdm:t:")) != -1) { 
41                  
42                 switch (ch) { 
43                         case 'c': cflag = 1; 
44                                           break;
45                         
46                         case 'd': dflag = 1; 
47                                           break; 
48                         
49                         case 't': tflag = 1; 
50                                           volumesize = atoi(optarg);                                      
51                                           break; 
52                         
53                         case 'm': mflag = 1;
54                                           staticmodel = optarg;
55                                           break; 
56                         
57                         case 's': sflag = 1;                                      
58                                           break;
59                         
60                         default: fprintf(stderr, "Usage: %s [-cds] [-t volsizekb] sourcefile targetfile [-m modeldumpfile]\n", argv[0]); 
61                                          return(2);
62                 }
63         }
64                 
65         if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) || (mflag & sflag)) {
66                 fprintf(stderr, "Usage: %s [-cds] [-t volsizekb] sourcefile targetfile [-m modeldumpfile]\n", argv[0]); 
67                 if ((tflag == 1) && (volumesize < 0)) fprintf(stderr,"Error: The volume size must be a non-zero value\n");
68                 return (2);             
69         }
70                 
71         if (cflag == 1) {
72                 /* Inicio un compresor huffman estatico por chunks */
73                 if ((shuff = shuff_init_encoder_bychunk(argv[optind+1],volumesize*1024)) == NULL) return 0;
74                 if (mflag == 1) shuff_loadmodel(shuff,staticmodel);
75                 
76                 /* Comprimo por chunks */               
77                 if ((fp = fopen(argv[optind],"rb")) == NULL) return 1;          
78                 while (!feof(fp)) {
79                         i = 0;
80                         while (!feof(fp) && (i < 4))
81                         {
82                           ch = fgetc(fp);
83                           if (feof(fp)) continue;
84                           chunk[i] = ch;                          
85                           i++;
86                         }                       
87                         /* Comprimo el chunk con huffman estatico */                                            
88                         shuff_scanfreq_chunk(shuff,chunk,i);                    
89                 }
90                 /* Le indico al huffman que efectivamente comprima los chunks */
91                 shuff_encode_file(shuff);               
92                 if (sflag == 1) shuff_savemodel(shuff);         
93                 
94                 /* De init encoder */
95                 shuff_deinit_encoder(shuff);
96                 
97                 /* Free mem allocated by main */
98                 free(shuff);            
99                 
100                 /* Close files opened by main */
101                 fclose(fp);
102         }
103         
104         if (dflag == 1) { 
105                 /* Init decoder */
106                 shuff = shuff_init_decoder(argv[optind],NULL);
107                 fp = fopen(argv[optind+1],"w");
108                 
109                 /* Gimme chunks till last one */
110                 while (shuff_decode_chunk(shuff,chunk,4,&decoded))
111                         fwrite(chunk,decoded,1,fp);
112                 
113                 /* Last chunk written alone */
114                 fwrite(chunk,decoded,1,fp);
115                 fclose(fp);
116                 
117                 /* Deinit decoder */
118                 shuff_deinit_decoder(shuff);
119                 free(shuff);            
120         }
121         
122         /* Free mem */
123         free(chunk);            
124         return 0;
125 }