home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-2.ZIP / GFUNC / COMPFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  2.8 KB  |  121 lines

  1. /*
  2. ** compfile.c
  3. ** contains: compfile()
  4. */
  5.  
  6. #include <stdio.h>
  7. #include "gfuncts.h"
  8.  
  9.  
  10.  
  11.  
  12. typedef struct {
  13.     FILE *outhandle;
  14.     unsigned char tempbuf[4];
  15.     unsigned short counter,runcount;
  16. } _COMPDATA;
  17.  
  18.  
  19. int GF_CONV _flushco(_COMPDATA *);
  20.  
  21.  
  22. #ifdef    AZTEC
  23. #define READOPENSTR    "r"
  24. #define WRITEOPENSTR    "w"
  25. #else
  26. #define READOPENSTR    "rb"
  27. #define WRITEOPENSTR    "wb"
  28. #endif
  29.  
  30. /*
  31. **  int
  32. ** compfile(char *inputname,char *outputname)
  33. **
  34. ** ARGUMENT(s)
  35. **    inputname    -    Null terminated string for source path & filename.
  36. **    outputname    -    Null terminated string for destination path & filename.
  37. **
  38. **
  39. ** DESCRIPTION
  40. **  Performs a non-repeat compression.    Data is passed through normally unless
  41. **  a run of more than three of a character is detected.  A run is encoded
  42. **  as:
  43. **        <character> <0x90> <count>
  44. **
  45. **    If the character to be written is 0x90 it will be followed by a
  46. **    count byte of 0.
  47. **
  48. ** RETURNS
  49. **    0 = successful or CANTOPENFILE
  50. **
  51. ** AUTHOR
  52. **        ""        Mon 09-Jan-1989  08:44:12
  53. **   Copyright (C)1989-1990  Greenleaf Software Inc.x    All Rights Reserved.
  54. **
  55. ** MODIFICATIONS
  56. **
  57. */
  58. int GF_CONV compfile(inputname,outputname)
  59. char *inputname,*outputname;
  60. {
  61.     FILE *inhandle;
  62.     _COMPDATA s;
  63.     int inputchar;
  64.  
  65.     if((inhandle=fopen(inputname,READOPENSTR))==(FILE *)0)
  66.         return(CANTOPENFILE);
  67.     if((s.outhandle=fopen(outputname,WRITEOPENSTR))==(FILE *)0) {
  68.         fclose(inhandle);
  69.         return(CANTOPENFILE);
  70.     }
  71.     s.runcount=s.counter=0;
  72.     while((inputchar=fgetc(inhandle))!=-1) {
  73.         if(inputchar==COMPMARK) {       /* Handle special case */
  74.             if(s.counter||s.runcount)
  75.                 _flushco(&s);     /* flush what is stored */
  76.             fputc((char)COMPMARK,s.outhandle);
  77.             fputc((char)0,s.outhandle);
  78.         } else {
  79.             if(s.runcount) { /* if building a run either add to it or dump it */
  80.                 if((unsigned)inputchar==s.tempbuf[0]) { /* if same */
  81.                     if(++s.runcount==255)
  82.                         _flushco(&s);
  83.                 } else {              /* Breaking run */
  84.                     if(s.counter||s.runcount)
  85.                         _flushco(&s);
  86.                     s.tempbuf[s.counter++]=(unsigned char)inputchar;
  87.                 }
  88.             } else {  /* if not building a run */
  89.                 s.tempbuf[s.counter++]=(unsigned char)inputchar;
  90.                 if(s.counter>1&&inputchar!=(int)s.tempbuf[0]) {
  91.                     fwrite((char *)s.tempbuf,s.counter-1,1,s.outhandle);
  92.                     s.tempbuf[0]=(unsigned char)inputchar;
  93.                     s.counter=1;
  94.                 } else if(s.counter==4) {
  95.                     s.runcount=4;
  96.                     s.counter=1;
  97.                 }
  98.             }
  99.         }
  100.     }
  101.     if(s.counter||s.runcount)
  102.         _flushco(&s);
  103.     fclose(inhandle);
  104.     fclose(s.outhandle);
  105.     return(0);
  106. }
  107.  
  108.  
  109. int GF_CONV _flushco(s)
  110. _COMPDATA *s;
  111. {
  112.     if(s->runcount) {
  113.         fputc(s->tempbuf[0],s->outhandle);
  114.         fputc(COMPMARK,s->outhandle);
  115.         fputc(s->runcount,s->outhandle);
  116.     } else if(s->counter)
  117.         fwrite((char *)s->tempbuf,s->counter,1,s->outhandle);
  118.     s->counter=s->runcount=0;
  119.     return(0);
  120. }
  121.