home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 181_01 / lzw.c < prev    next >
Text File  |  1985-03-10  |  2KB  |  79 lines

  1. /* LZWCOM - FILE COMPRESSOR UTILITY                    */
  2. #include "sgtty.h"
  3. #include "stdio.h"
  4. #include "fcntl.h"
  5. #define FALSE    0
  6. #define TRUE     !FALSE
  7. #define TABSIZE  4096
  8. #define NO_PRED  -1
  9. #define EMPTY    -1
  10. #define NOT_FND  -1
  11. struct entry {
  12.   char used;
  13.   int next;        /* hi bit is 'used' flag            */
  14.   int predecessor;            /* 12 bit code            */
  15.   char follower;
  16. } string_tab[TABSIZE];
  17.  
  18. char is_a_con = FALSE;        /* flag to suppress 'dots' in writec        */
  19.  
  20. /*   routines common to compress and decompress, contained in CommLZW.c    */
  21. hash();
  22. unhash();
  23. getcode();
  24. putcode();
  25. init_tab();
  26. upd_tab();
  27.  
  28.  
  29. main(argc,argv)
  30. int argc; char *argv[];
  31. {
  32.   int c, code, localcode;
  33.   int code_count = TABSIZE - 256;
  34.   int infd, outfd;
  35.   if (3 != argc) {
  36.     printf("Usage : lzwcom oldfilename squeezefilename\n");
  37.     exit(0);
  38.   }
  39.   if ( -1 == (infd = open( *++argv, O_RDONLY )) ) {
  40.     printf("Cant open %s\n", *argv);
  41.     exit(0);
  42.   }
  43.   if ( -1 == (outfd = creat(*++argv,0666)) ) {
  44.     printf("Cant create %s\n",*argv);
  45.     exit(0);
  46.   }
  47.   init_tab();                /* initialize code table    */
  48.   c = readc(infd);
  49.   code = unhash(NO_PRED,c);        /* initial code for table     */
  50. #ifdef DEBUG
  51.   putchar(c);
  52.   printf( "\n%x\n",code);
  53. #endif
  54.   while ( EOF != (c = readc(infd)) ) {
  55. #ifdef DEBUG
  56.   putchar(c);
  57. #endif
  58.     if ( NOT_FND != (localcode = unhash(code,c)) ) {
  59.       code = localcode;
  60.       continue;
  61.     }
  62. /* when the above clause comes false, you have found the last known code */
  63.     putcode(outfd,code);    /* only update table if table isn't full */
  64. #ifdef DEBUG
  65.   printf( "\n%x\n",code);
  66. #endif
  67.     if ( code_count ) {
  68.       upd_tab(code,c);
  69.       --code_count;
  70.     }
  71. /* start loop again with the char that didn't fit into last string    */
  72.     code = unhash(NO_PRED,c);
  73.   }
  74.   putcode(outfd,code);            /* once EOF reached, always     */
  75.                     /* one code left unsent        */
  76.   flushout(outfd);            /* make sure everything's written */
  77.   exit(0);                /* make sure everything gets closed */
  78. }
  79.