home *** CD-ROM | disk | FTP | other *** search
- /*
- ** compfile.c
- ** contains: compfile()
- */
-
- #include <stdio.h>
- #include "gfuncts.h"
-
-
-
-
- typedef struct {
- FILE *outhandle;
- unsigned char tempbuf[4];
- unsigned short counter,runcount;
- } _COMPDATA;
-
-
- int GF_CONV _flushco(_COMPDATA *);
-
-
- #ifdef AZTEC
- #define READOPENSTR "r"
- #define WRITEOPENSTR "w"
- #else
- #define READOPENSTR "rb"
- #define WRITEOPENSTR "wb"
- #endif
-
- /*
- ** int
- ** compfile(char *inputname,char *outputname)
- **
- ** ARGUMENT(s)
- ** inputname - Null terminated string for source path & filename.
- ** outputname - Null terminated string for destination path & filename.
- **
- **
- ** DESCRIPTION
- ** Performs a non-repeat compression. Data is passed through normally unless
- ** a run of more than three of a character is detected. A run is encoded
- ** as:
- ** <character> <0x90> <count>
- **
- ** If the character to be written is 0x90 it will be followed by a
- ** count byte of 0.
- **
- ** RETURNS
- ** 0 = successful or CANTOPENFILE
- **
- ** AUTHOR
- ** "" Mon 09-Jan-1989 08:44:12
- ** Copyright (C)1989-1990 Greenleaf Software Inc.x All Rights Reserved.
- **
- ** MODIFICATIONS
- **
- */
- int GF_CONV compfile(inputname,outputname)
- char *inputname,*outputname;
- {
- FILE *inhandle;
- _COMPDATA s;
- int inputchar;
-
- if((inhandle=fopen(inputname,READOPENSTR))==(FILE *)0)
- return(CANTOPENFILE);
- if((s.outhandle=fopen(outputname,WRITEOPENSTR))==(FILE *)0) {
- fclose(inhandle);
- return(CANTOPENFILE);
- }
- s.runcount=s.counter=0;
- while((inputchar=fgetc(inhandle))!=-1) {
- if(inputchar==COMPMARK) { /* Handle special case */
- if(s.counter||s.runcount)
- _flushco(&s); /* flush what is stored */
- fputc((char)COMPMARK,s.outhandle);
- fputc((char)0,s.outhandle);
- } else {
- if(s.runcount) { /* if building a run either add to it or dump it */
- if((unsigned)inputchar==s.tempbuf[0]) { /* if same */
- if(++s.runcount==255)
- _flushco(&s);
- } else { /* Breaking run */
- if(s.counter||s.runcount)
- _flushco(&s);
- s.tempbuf[s.counter++]=(unsigned char)inputchar;
- }
- } else { /* if not building a run */
- s.tempbuf[s.counter++]=(unsigned char)inputchar;
- if(s.counter>1&&inputchar!=(int)s.tempbuf[0]) {
- fwrite((char *)s.tempbuf,s.counter-1,1,s.outhandle);
- s.tempbuf[0]=(unsigned char)inputchar;
- s.counter=1;
- } else if(s.counter==4) {
- s.runcount=4;
- s.counter=1;
- }
- }
- }
- }
- if(s.counter||s.runcount)
- _flushco(&s);
- fclose(inhandle);
- fclose(s.outhandle);
- return(0);
- }
-
-
- int GF_CONV _flushco(s)
- _COMPDATA *s;
- {
- if(s->runcount) {
- fputc(s->tempbuf[0],s->outhandle);
- fputc(COMPMARK,s->outhandle);
- fputc(s->runcount,s->outhandle);
- } else if(s->counter)
- fwrite((char *)s->tempbuf,s->counter,1,s->outhandle);
- s->counter=s->runcount=0;
- return(0);
- }
-