v02i070: sq - squeeze a dictionary
Mike Wexler
mikew at wyse1.wyse.com
Sun Mar 6 09:51:26 AEST 1988
Submitted-By: "Mike Wexler" <mikew at wyse1.wyse.com>
Archive-Name: sq
Comp.sources.misc: Volume 2, Issue 70
Submitted-By: "Mike Wexler" <mikew at wyse1.wyse.com>
Archive-Name: sq
[A couple programs that sat around until I had time to fix 'em up. ++bsa]
The following program compresses dictionaries. It works in combination
with compress and sort. You should have both of these for optimal use.
I have included a simple demo. Have fun...
#--------------------------------CUT HERE-------------------------------------
#! /bin/sh
#
# This is a shell archive. Save this into a file, edit it
# and delete all lines above this comment. Then give this
# file to sh by executing the command "sh file". The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
# -rw-r--r-- 1 allbery System 449 Mar 5 18:43 Makefile
# -rw-r--r-- 1 allbery System 451 Mar 5 18:43 Makefile.orig
# -rw-r--r-- 1 allbery System 546 Mar 5 18:43 sq.c
# -rw-r--r-- 1 allbery System 606 Mar 5 18:43 sq.l
# -rw-r--r-- 1 allbery System 468 Mar 5 18:43 unsq.c
#
echo 'x - Makefile'
if test -f Makefile; then echo 'shar: not overwriting Makefile'; else
sed 's/^X//' << '________This_Is_The_END________' > Makefile
XCFLAGS=-O
XBINDIR=/usr/new
XMANDIR=/usr/man/manl
XDICT=/usr/dict/words
Xall: sq unsq
Xsq: sq.c
X $(CC) $(CFLAGS) -o sq sq.c
Xunsq: unsq.c
X $(CC) $(CFLAGS) -o unsq unsq.c
Xinstall:
X cp sq unsq $(BINDIR)
X cp sq.l $(MANDIR)
Xshar:
X shar sq.l sq.c unsq.c Makefile > sq.shar
Xdemo:
X sort $(DICT) | sq | compress > dict.sq.Z
X compress < $(DICT) > dict.Z
X ls -l $(DICT) dict.Z dict.sq.Z
X compress -d < dict.sq.Z | unsq | sort -df > dict.copy
X diff $(DICT) dict.copy
________This_Is_The_END________
if test `wc -c < Makefile` -ne 449; then
echo 'shar: Makefile was damaged during transit (should have been 449 bytes)'
fi
fi ; : end of overwriting check
echo 'x - Makefile.orig'
if test -f Makefile.orig; then echo 'shar: not overwriting Makefile.orig'; else
sed 's/^X//' << '________This_Is_The_END________' > Makefile.orig
XCFLAGS=-O
XBINDIR=/usr/local/bin
XMANDIR=/usr/man/manl
XDICT=/usr/dict/words
Xall: sq unsq
Xsq: sq.c
X $(CC) $(CFLAGS) -o sq sq.c
Xunsq: unsq.c
X $(CC) $(CFLAGS) -o unsq unsq.c
Xinstall:
X cp sq unsq $BINDIR
X cp sq.l $MANDIR
Xshar:
X shar sq.l sq.c unsq.c Makefile > sq.shar
Xdemo:
X sort $(DICT) | sq | compress > dict.sq.Z
X compress < $(DICT) > dict.Z
X ls -l $(DICT) dict.Z dict.sq.Z
X compress -d < dict.sq.Z | unsq | sort -df > dict.copy
X diff $(DICT) dict.copy
________This_Is_The_END________
if test `wc -c < Makefile.orig` -ne 451; then
echo 'shar: Makefile.orig was damaged during transit (should have been 451 bytes)'
fi
fi ; : end of overwriting check
echo 'x - sq.c'
if test -f sq.c; then echo 'shar: not overwriting sq.c'; else
sed 's/^X//' << '________This_Is_The_END________' > sq.c
X#include <stdio.h>
X
Xmain()
X{
X char word[257];
X static char prev[257]="";
X char outword[258];
X
X while (gets(word)!=NULL) {
X trunc(word,prev);
X }
Xexit(0);
X}
X
Xtrunc(word,prev)
Xchar *word;
Xchar *prev;
X{
X unsigned char same_count;
X char *wordp;
X char *prevp;
X
X wordp=word;
X prevp=prev;
X for (same_count=0;*wordp==*prevp++;++wordp,++same_count);
X if (same_count>255) {
X fprintf(stderr,"same count exceeded 255 characters, aborted");
X exit(1);
X }
X fwrite(&same_count,1,1,stdout);
X puts(wordp);
X strcpy(prev,word);
X return;
X}
X
________This_Is_The_END________
if test `wc -c < sq.c` -ne 546; then
echo 'shar: sq.c was damaged during transit (should have been 546 bytes)'
fi
fi ; : end of overwriting check
echo 'x - sq.l'
if test -f sq.l; then echo 'shar: not overwriting sq.l'; else
sed 's/^X//' << '________This_Is_The_END________' > sq.l
X.TH SQ 1 LOCAL
X.\" $Header: unshar.man,v 1.1 87/02/27 13:45:34 rs Exp $
X.SH NAME
Xsq \- squeeze a sorted word list
Xunsq \- unsqueeze a sorted word list
X.SH SYNOPSIS
X.B sq
X< infile > outfile
X.PP
X.B unsq
X< infile > outfile
X.SH DESCRIPTION
X.I sq
Xcompresses a sorted list of words(a dictionary).
XFor example:
X.RS
Xsort /usr/dict/words | sq | compress > words.sq.Z
X.RE
Xwill compress dict by about a factor of 4.
X.PP
X.I unsq
Xuncompress the output of
X.I sq.
XFor example:
X.RS
Xcompress -d < words.sq.Z | unsq | sort -f -o words
X.RE
Xwill uncompress a dictionary compressed with
X.I sq.
X.SH SEE ALSO
Xcompress(1), sort.
________This_Is_The_END________
if test `wc -c < sq.l` -ne 606; then
echo 'shar: sq.l was damaged during transit (should have been 606 bytes)'
fi
fi ; : end of overwriting check
echo 'x - unsq.c'
if test -f unsq.c; then echo 'shar: not overwriting unsq.c'; else
sed 's/^X//' << '________This_Is_The_END________' > unsq.c
X#include <stdio.h>
X
Xmain()
X{
X char word[257];
X static char prev[257]="";
X unsigned char count;
X char outword[258];
X
X while (!expand(word,prev)) {
X puts(word);
X }
Xexit(0);
X}
X
Xexpand(word,prev)
Xchar *word;
Xchar *prev;
X{
X unsigned char same_count;
X char *wordp;
X char *prevp;
X
X fread(&same_count,1,1,stdin);
X prevp=prev;
X wordp=word;
X while (same_count--)
X *wordp++=(*prevp++);
X if (gets(wordp)==NULL) return(1);
X strcpy(prev,word);
X return(0);
X}
X
________This_Is_The_END________
if test `wc -c < unsq.c` -ne 468; then
echo 'shar: unsq.c was damaged during transit (should have been 468 bytes)'
fi
fi ; : end of overwriting check
exit 0
More information about the Comp.sources.misc
mailing list