home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / sq < prev    next >
Encoding:
Internet Message Format  |  1991-08-07  |  5.5 KB

  1. From: mikew@wyse1.wyse.com (Mike Wexler)
  2. Newsgroups: comp.sources.misc
  3. Subject: v02i070: sq - squeeze a dictionary
  4. Message-ID: <7473@ncoast.UUCP>
  5. Date: 5 Mar 88 23:51:26 GMT
  6. Approved: allbery@ncoast.UUCP
  7.  
  8. Comp.sources.misc: Volume 2, Issue 70
  9. Submitted-By: "Mike Wexler" <mikew@wyse1.wyse.com>
  10. Archive-Name: sq
  11.  
  12. [A couple programs that sat around until I had time to fix 'em up.  ++bsa]
  13.  
  14. The following program compresses dictionaries.  It works in combination
  15. with compress and sort.  You should have both of these for optimal use.
  16. I have included a simple demo.  Have fun...
  17. #--------------------------------CUT HERE-------------------------------------
  18. #! /bin/sh
  19. #
  20. # This is a shell archive.  Save this into a file, edit it
  21. # and delete all lines above this comment.  Then give this
  22. # file to sh by executing the command "sh file".  The files
  23. # will be extracted into the current directory owned by
  24. # you with default permissions.
  25. #
  26. # The files contained herein are:
  27. #
  28. # -rw-r--r--   1 allbery  System       449 Mar  5 18:43 Makefile
  29. # -rw-r--r--   1 allbery  System       451 Mar  5 18:43 Makefile.orig
  30. # -rw-r--r--   1 allbery  System       546 Mar  5 18:43 sq.c
  31. # -rw-r--r--   1 allbery  System       606 Mar  5 18:43 sq.l
  32. # -rw-r--r--   1 allbery  System       468 Mar  5 18:43 unsq.c
  33. #
  34. echo 'x - Makefile'
  35. if test -f Makefile; then echo 'shar: not overwriting Makefile'; else
  36. sed 's/^X//' << '________This_Is_The_END________' > Makefile
  37. XCFLAGS=-O
  38. XBINDIR=/usr/new
  39. XMANDIR=/usr/man/manl
  40. XDICT=/usr/dict/words
  41. Xall: sq unsq
  42. Xsq: sq.c
  43. X    $(CC) $(CFLAGS) -o sq sq.c
  44. Xunsq: unsq.c
  45. X    $(CC) $(CFLAGS) -o unsq unsq.c
  46. Xinstall:
  47. X    cp sq unsq $(BINDIR)
  48. X    cp sq.l $(MANDIR)
  49. Xshar:
  50. X    shar sq.l sq.c unsq.c Makefile > sq.shar
  51. Xdemo:
  52. X    sort $(DICT) | sq | compress > dict.sq.Z
  53. X    compress < $(DICT) > dict.Z
  54. X    ls -l $(DICT) dict.Z dict.sq.Z
  55. X    compress -d < dict.sq.Z | unsq | sort -df > dict.copy
  56. X    diff $(DICT) dict.copy
  57. ________This_Is_The_END________
  58. if test `wc -c < Makefile` -ne 449; then
  59.     echo 'shar: Makefile was damaged during transit (should have been 449 bytes)'
  60. fi
  61. fi        ; : end of overwriting check
  62. echo 'x - Makefile.orig'
  63. if test -f Makefile.orig; then echo 'shar: not overwriting Makefile.orig'; else
  64. sed 's/^X//' << '________This_Is_The_END________' > Makefile.orig
  65. XCFLAGS=-O
  66. XBINDIR=/usr/local/bin
  67. XMANDIR=/usr/man/manl
  68. XDICT=/usr/dict/words
  69. Xall: sq unsq
  70. Xsq: sq.c
  71. X    $(CC) $(CFLAGS) -o sq sq.c
  72. Xunsq: unsq.c
  73. X    $(CC) $(CFLAGS) -o unsq unsq.c
  74. Xinstall:
  75. X    cp sq unsq $BINDIR
  76. X    cp sq.l $MANDIR
  77. Xshar:
  78. X    shar sq.l sq.c unsq.c Makefile > sq.shar
  79. Xdemo:
  80. X    sort $(DICT) | sq | compress > dict.sq.Z
  81. X    compress < $(DICT) > dict.Z
  82. X    ls -l $(DICT) dict.Z dict.sq.Z
  83. X    compress -d < dict.sq.Z | unsq | sort -df > dict.copy
  84. X    diff $(DICT) dict.copy
  85. ________This_Is_The_END________
  86. if test `wc -c < Makefile.orig` -ne 451; then
  87.     echo 'shar: Makefile.orig was damaged during transit (should have been 451 bytes)'
  88. fi
  89. fi        ; : end of overwriting check
  90. echo 'x - sq.c'
  91. if test -f sq.c; then echo 'shar: not overwriting sq.c'; else
  92. sed 's/^X//' << '________This_Is_The_END________' > sq.c
  93. X#include <stdio.h>
  94. X
  95. Xmain()
  96. X{
  97. X  char word[257];
  98. X  static char prev[257]="";
  99. X  char outword[258];
  100. X
  101. X  while (gets(word)!=NULL) {
  102. X    trunc(word,prev);
  103. X  }
  104. Xexit(0);
  105. X}
  106. X
  107. Xtrunc(word,prev) 
  108. Xchar *word;
  109. Xchar *prev;
  110. X{
  111. X  unsigned char same_count;
  112. X  char *wordp;
  113. X  char *prevp;
  114. X
  115. X  wordp=word;
  116. X  prevp=prev;
  117. X  for (same_count=0;*wordp==*prevp++;++wordp,++same_count);
  118. X  if (same_count>255) {
  119. X    fprintf(stderr,"same count exceeded 255 characters, aborted");
  120. X    exit(1);
  121. X  }
  122. X  fwrite(&same_count,1,1,stdout);
  123. X  puts(wordp);
  124. X  strcpy(prev,word);
  125. X  return;
  126. X}
  127. X
  128. ________This_Is_The_END________
  129. if test `wc -c < sq.c` -ne 546; then
  130.     echo 'shar: sq.c was damaged during transit (should have been 546 bytes)'
  131. fi
  132. fi        ; : end of overwriting check
  133. echo 'x - sq.l'
  134. if test -f sq.l; then echo 'shar: not overwriting sq.l'; else
  135. sed 's/^X//' << '________This_Is_The_END________' > sq.l
  136. X.TH SQ 1 LOCAL
  137. X.\" $Header: unshar.man,v 1.1 87/02/27 13:45:34 rs Exp $
  138. X.SH NAME
  139. Xsq \- squeeze a sorted word list
  140. Xunsq \- unsqueeze a sorted word list
  141. X.SH SYNOPSIS
  142. X.B sq
  143. X< infile > outfile
  144. X.PP
  145. X.B unsq
  146. X< infile > outfile
  147. X.SH DESCRIPTION
  148. X.I sq
  149. Xcompresses a sorted list of words(a dictionary). 
  150. XFor example:
  151. X.RS
  152. Xsort /usr/dict/words | sq | compress > words.sq.Z
  153. X.RE
  154. Xwill compress dict by about a factor of 4.
  155. X.PP
  156. X.I unsq
  157. Xuncompress the output of
  158. X.I sq.
  159. XFor example:
  160. X.RS
  161. Xcompress -d < words.sq.Z | unsq | sort -f -o words
  162. X.RE
  163. Xwill uncompress a dictionary compressed with
  164. X.I sq.
  165. X.SH SEE ALSO
  166. Xcompress(1), sort.
  167. ________This_Is_The_END________
  168. if test `wc -c < sq.l` -ne 606; then
  169.     echo 'shar: sq.l was damaged during transit (should have been 606 bytes)'
  170. fi
  171. fi        ; : end of overwriting check
  172. echo 'x - unsq.c'
  173. if test -f unsq.c; then echo 'shar: not overwriting unsq.c'; else
  174. sed 's/^X//' << '________This_Is_The_END________' > unsq.c
  175. X#include <stdio.h>
  176. X
  177. Xmain()
  178. X{
  179. X  char word[257];
  180. X  static char prev[257]="";
  181. X  unsigned char count;
  182. X  char outword[258];
  183. X
  184. X  while (!expand(word,prev)) {
  185. X    puts(word);
  186. X  }
  187. Xexit(0);
  188. X}
  189. X
  190. Xexpand(word,prev) 
  191. Xchar *word;
  192. Xchar *prev;
  193. X{
  194. X  unsigned char same_count;
  195. X  char *wordp;
  196. X  char *prevp;
  197. X
  198. X  fread(&same_count,1,1,stdin);
  199. X  prevp=prev;
  200. X  wordp=word;
  201. X  while (same_count--)
  202. X    *wordp++=(*prevp++);
  203. X  if (gets(wordp)==NULL) return(1);
  204. X  strcpy(prev,word);
  205. X  return(0);
  206. X}
  207. X
  208. ________This_Is_The_END________
  209. if test `wc -c < unsq.c` -ne 468; then
  210.     echo 'shar: unsq.c was damaged during transit (should have been 468 bytes)'
  211. fi
  212. fi        ; : end of overwriting check
  213. exit 0
  214.