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

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