Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

example.c

Go to the documentation of this file.
00001 
00017 // Include the Library
00018 #include "HuffwordLib.h"
00019 
00020 
00021 int main()
00022 {
00023 
00024   FILE *textfile;
00025   struct stat sbuf;
00026   char *text,*cbody,*cstring,*dstring,*dblock;
00027   int tlen,clen,dlen,dblocklen,cbodylen,dbytes,tmp;
00028   Console console;
00029 
00030   textfile = fopen("example.dat","r");
00031   stat("example.dat",&sbuf);
00032   tlen = (int)sbuf.st_size;
00033   text = (char *) mmap(NULL,tlen,PROT_READ,MAP_SHARED,fileno(textfile),0);
00034 
00035   // Compresses the text file, and computes jumper pointers with
00036   // average distance about 10. Uses the verbose mode (1).
00037   Huffw_compress(text,tlen,10,&cstring,&clen,1);
00038 
00039   // Decompresses the string s, and initializes the console c. 
00040   // Uses the verbose mode (1).
00041   Huffw_decompress(cstring,clen,&dstring,&dlen,0);
00042 
00043   // Print some debugging infos
00044   printf("Comparing the compressed and decompressed files = %d (0 is OK)\n",
00045          strncmp(dstring,text,dlen));
00046 
00047 
00048 
00049   /* -------------------------------------------------------------- */
00050   // ---------- A test of decompression and random access ---------
00051   /* -------------------------------------------------------------- */
00052 
00053   printf("\n---- Some test ----\n");
00054 
00055   // Initialize all the data structures: Dictionary, Hash Table, Canonical Info and Jumpers (if any).
00056   // Note cbody is the pointer to the compressed body, and cbodylen is its length.
00057   HDecompress_getBodyAndConsole(cstring,clen,&dlen,&cbody,&cbodylen,&console);
00058 
00059   // Decompresses the first 10 words, d = #bytes decompressed
00060   HDecompress_nextBlock_tokens(cbody,cbodylen,10,&dblock,&dblocklen,
00061                                    &dbytes,&console); 
00062 
00063   // Prints some debugging infos
00064   printf("\n10 words from the beginning:\n\"");
00065   HPrint_string(dblock,dblocklen);
00066   printf("\"\n");
00067 
00068   // Decompresses the 5 words which precede the byte reached by the previous Decompress
00069   HDecompress_previousBlock_tokens(cbody+dbytes,dbytes,5,&dblock,
00070                                        &dblocklen,&tmp,&console); 
00071 
00072   // Prints some debugging infos
00073   printf("\nLast 5 words of the previous phrase:\n\"");
00074   HPrint_string(dblock,dblocklen);
00075   printf("\"\n");
00076 
00077   printf("\nDo you wish to see the dictionary content ? (y/n) ");
00078   if (getchar() == 'y')
00079     HDictionary_print(&(console.dictionary),&(console.hashtable),1);
00080 
00081 
00082   return 0;
00083 }

Generated on Mon Mar 31 14:44:30 2003 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002