00001
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <sys/mman.h>
00022 #include <sys/stat.h>
00023 #include <unistd.h>
00024 #include <errno.h>
00025 #include <ctype.h>
00026 #include <math.h>
00027
00028
00029
00030 #include "HuffwordLib.h"
00031
00032
00033 int main(int argc, char *argv[])
00034 {
00035 int getopt(int argc, char * const *argv, const char *options);
00036 extern char *optarg;
00037 extern int optind, opterr, optopt;
00038 int Is_compression,Verbose,Parsing_rule,Ave_jumper,c;
00039 struct stat sbuf;
00040 FILE *Input_File, *Output_File;
00041 char Input_File_name[255], Output_File_name[255];
00042 char *Input_string, *Output_string;
00043 int Input_size, Output_size, l;
00044
00045
00046
00047 Is_compression = 1;
00048 Verbose = 0;
00049 Parsing_rule = 0;
00050 Ave_jumper = 0;
00051
00052 opterr = 0;
00053 while ((c=getopt(argc, argv, "dvp:j:")) != -1)
00054 {
00055 switch (c)
00056 {
00057 case 'd':
00058 Is_compression=0; break;
00059 case 'p':
00060 Parsing_rule = atoi(optarg); break;
00061 case 'v':
00062 Verbose++; break;
00063 case 'j':
00064 Ave_jumper = atoi(optarg);
00065 if(Ave_jumper < 0) {
00066 fprintf(stderr,"\n\n Error: Average jump value must be >= 0\n");
00067 exit(1); }
00068 break;
00069 case '?':
00070 fprintf(stderr,"Unknown option: %c -main-\n", optopt);
00071 exit(1);
00072 }
00073 }
00074
00075 if ((Ave_jumper != 0) && (Parsing_rule != 0)) {
00076 fprintf(stderr,"\n\n Error: -j option can be used only with -p 0\n");
00077 exit(1); }
00078
00079
00080 if (optind == argc){
00081 fprintf(stderr,
00082 "This is a Huffword, byte-aligned, and tagged compressor.\n\n");
00083 fprintf(stderr, "Usage: huffw [-dv -j A -p N] infile\n");
00084 fprintf(stderr,
00085 "Output: a file with extension .hwz\n\n");
00086 fprintf(stderr," -d\tdecompress\n");
00087 fprintf(stderr," -j\taverage distance of the auxiliary text pointers for faster random access (use only with p = 0)\n");
00088 fprintf(stderr," -p N\tparsing rule: 0 = token is (Lett+Numb)+ or singletons,\n ");
00089 fprintf(stderr,"\t\t 1 = like 0 but dropping spaces (Spaceless model)\n");
00090 fprintf(stderr," -v\tdetails of the computation\n\n");
00091 exit(0);
00092 }
00093
00094
00095 strcpy(Input_File_name,argv[optind]);
00096 if(Input_File_name == NULL) {
00097 fprintf(stderr,"\n\n Error: input file name empty\n");
00098 exit(1); }
00099
00100
00101 if (Is_compression){
00102
00103 strcpy(Output_File_name,Input_File_name);
00104 strcat(Output_File_name, ".hwz");
00105
00106 } else {
00107
00108 l=strlen(Input_File_name);
00109 if (strcmp(strdup(Input_File_name + l - 4),".hwz") != 0) {
00110 fprintf(stderr,"Fatal Error: Input file name does not end for \".hwz\"\n");
00111 exit(-1);
00112
00113 }
00114
00115
00116 strncpy(Output_File_name,Input_File_name,l-4);
00117 Output_File_name[l-4] = '\0';
00118 }
00119
00120
00121
00122 if ((Input_File=fopen(Input_File_name,"r"))==NULL) {
00123 fprintf(stderr,"Fatal Error: Input file does not exist\n");
00124 exit(-1);
00125 }
00126
00127 if ((Output_File=fopen(Output_File_name,"w"))==NULL) {
00128 fprintf(stderr,"Fatal Error: Output file does exist\n");
00129 exit(-1);
00130 }
00131
00132
00133 stat(Input_File_name,&sbuf);
00134 Input_size = (int)sbuf.st_size;
00135 if (Input_size == 0){
00136 fprintf(stderr,"Fatal Error: Input file empty\n");
00137 exit(-1);
00138 }
00139 Input_string = (char *) mmap(NULL,Input_size,PROT_READ,MAP_SHARED,
00140 fileno(Input_File),0);
00141
00142
00143 if (Is_compression){
00144
00145 if (Parsing_rule == 0)
00146 Huffw_compress(Input_string,Input_size,Ave_jumper,&Output_string,&Output_size,Verbose);
00147 else
00148 Huffw_spaceless_compress(Input_string,Input_size,&Output_string,&Output_size,Verbose);
00149
00150 } else {
00151
00152
00153 Parsing_rule = (int) *Input_string;
00154
00155 if (Parsing_rule == 0)
00156 Huffw_decompress(Input_string,Input_size,&Output_string,&Output_size,Verbose);
00157 else
00158 Huffw_spaceless_decompress(Input_string,Input_size,&Output_string,&Output_size,Verbose);
00159
00160 }
00161
00162
00163 fwrite(Output_string,1,Output_size,Output_File);
00164
00165
00166 munmap(Input_string,Input_size);
00167 fclose(Input_File);
00168 fclose(Output_File);
00169
00170 return 0;
00171
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192