home *** CD-ROM | disk | FTP | other *** search
- /* PROGRAM pfxcmprs;
- 1982/09/25 20:34
-
- Prefix compression of a sorted line-format file.
-
- Copyright (C) 1982 William G. Hutchison, Jr.
- P.O. Box 278
- Exton, PA 19341-0278
- U.S.A.
-
- CompuServe 70665,1307
- (215) 363-7028
-
-
- Prefix compression is a simple technique for removing
- redundant prefixes from a sequential line-format file. By
- line-format file, I mean a file created by a text editor,
- with from 0 to 132 characters on a line, followed by
- carriage return and line feed. Pfxcmprs reads lines from the
- file, and compares each line with its predecessor. If the
- lines have from 1 to 9 characters in common at the front of
- the line (the prefix) pfxcmprs writes an ASCII digit at the
- front of the output line with the number of common digits.
- Otherwise, it simply writes the line with no preceding
- digit. This is based on the assumption that the input file
- has no digits at the front of its lines.
- This program works best with a sorted file, for example,
- a spelling word list.
- It is particularly good with a word list, because most
- of the redundancy in a dictionary occurs at the front of the
- words.
-
-
- This program may be used freely for any non-commercial
- purpose, provided that the user does not remove or alter
- this notice or the copyright statement.
- Those who wish to sell or lease this program, or to
- incorporate it into a product for sale or lease, should
- apply to the author (above) for licensing information.
-
-
- Disclaimer:
-
- This program is not covered by a warranty, either express
- or implied. The author shall not be responsible for any
- damages (including consequential) caused by reliance on the
- materials presented, including but not limited to
- typographical errors or arithmetic errors.
-
-
- Super Disclaimer:
-
- This program was rejected for publication in Dr. Dobb's
- Journal by Assistant Editor Fritzi Lareau on the grounds
- that "the algorithm was in a language which might limit its
- usefulness to a narrow segment of our readers". You have
- been warned! Use this program at your own risk!!
- */
-
- #include "c80def.h"
- #define MAXCOMP 9
-
- #ifdef CP_M
- extern FILE *STDIN, *STDOUT;
- static FILE *STDERR;
- #endif
-
- static char pline[MAXLINE]= EOS;
- static char qline[MAXLINE]= EOS;
-
- static char *p= pline;
- static char *q= qline;
-
- main() {
- char *t;
-
- #ifdef CP_M
- STDERR= open("CON:", WRITE);
- #endif
-
- putline("PFXCMPRS Copyright (C) 1982 William G. Hutchison, Jr.",
- STDERR);
- putchar(NEWLINE);
- while (getline(p)) {
- put_prefix_compress(p, q);
- t= p; p= q; q= t; /* swap line pointers */
- }
- } /* end of main */
-
-
- getline(s)
- char s[];
- {
- int i;
-
- for (i= 0; i < MAXLINE-3; i++)
- if ((s[i]= getchar()) == EOF)
- return 0;
- else if (s[i] == NEWLINE)
- break;
-
- if (s[i++] != NEWLINE)
- s[i++]= NEWLINE;
- s[i]= EOS;
- return i;
- } /* end of getline */
-
- put_prefix_compress(p, q)
- char *p, *q;
- {
- char i;
-
- for (i= 0; i < MAXCOMP && *p == *q; i++) {
- p++; q++ /* keep scanning prefix */;
- }
- if (i)
- putchar(i+'0') /* compressed, put the digit */;
- putline(p, STDOUT) /* put the line, after the prefix */;
- } /* end of put_prefix_compress */
-
- putline(s, ofd)
- char *s;
- FILE *ofd;
- {
-
- while (*s)
- putc(*s++, ofd);
- } /* end of putline */