1 /* code -- bigram- and front-encode filenames for locate
2 Copyright (C) 1994 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 9 Temple Place - Suite 330, Boston, MA 02111-1307,
20 /* Compress a sorted list.
21 Works with `find' to encode a filename database to save space
26 bigram < file_list > bigrams
27 process-bigrams > most_common_bigrams
28 code most_common_bigrams < file_list > squeezed_list
30 Uses `front compression' (see ";login:", March 1983, p. 8).
31 The output begins with the 128 most common bigrams.
32 After that, the output format is, for each line,
33 an offset (from the previous line) differential count byte
34 followed by a (partially bigram-encoded) ASCII remainder.
35 The output lines have no terminating byte; the start of the next line
36 is indicated by its first byte having a value <= 30.
38 The encoding of the output bytes is:
40 0-28 likeliest differential counts + offset (14) to make nonnegative
41 30 escape code for out-of-range count to follow in next halfword
42 128-255 bigram codes (the 128 most common, as determined by `updatedb')
43 32-127 single character (printable) ASCII remainder
45 Written by James A. Woods <jwoods@adobe.com>.
46 Modified by David MacKenzie <djm@gnu.ai.mit.edu>. */
50 #include <sys/types.h>
52 #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
64 # define _(Text) gettext (Text)
67 #define textdomain(Domain)
68 #define bindtextdomain(Package, Directory)
71 # define N_(String) gettext_noop (String)
73 /* See locate.c for explanation as to why not use (String) */
74 # define N_(String) String
80 char *xmalloc
PARAMS((size_t));
82 /* The name this program was run with. */
85 /* The 128 most common bigrams in the file list, padded with NULs
86 if there are fewer. */
87 static char bigrams
[257] = {0};
89 /* Return the offset of PATTERN in STRING, or -1 if not found. */
92 strindex (char *string
, char *pattern
)
96 for (s
= string
; *s
!= '\0'; s
++)
97 /* Fast first char check. */
100 register char *p2
= pattern
+ 1, *s2
= s
+ 1;
101 while (*p2
!= '\0' && *p2
== *s2
)
104 return s2
- strlen (pattern
) - string
;
109 /* Return the length of the longest common prefix of strings S1 and S2. */
112 prefix_length (char *s1
, char *s2
)
114 register char *start
;
116 for (start
= s1
; *s1
== *s2
&& *s1
!= '\0'; s1
++, s2
++)
122 main (int argc
, char **argv
)
124 char *path
; /* The current input entry. */
125 char *oldpath
; /* The previous input entry. */
126 size_t pathsize
, oldpathsize
; /* Amounts allocated for them. */
127 int count
, oldcount
, diffcount
; /* Their prefix lengths & the difference. */
128 char bigram
[3]; /* Bigram to search for in table. */
129 int code
; /* Index of `bigram' in bigrams table. */
130 FILE *fp
; /* Most common bigrams file. */
131 int line_len
; /* Length of input line. */
133 program_name
= argv
[0];
139 fprintf (stderr
, _("Usage: %s most_common_bigrams < list > coded_list\n"),
144 fp
= fopen (argv
[1], "r");
147 fprintf (stderr
, "%s: ", argv
[0]);
152 pathsize
= oldpathsize
= 1026; /* Increased as necessary by getline. */
153 path
= xmalloc (pathsize
);
154 oldpath
= xmalloc (oldpathsize
);
156 /* Set to anything not starting with a slash, to force the first
157 prefix count to 0. */
158 strcpy (oldpath
, " ");
161 /* Copy the list of most common bigrams to the output,
162 padding with NULs if there are <128 of them. */
163 fgets (bigrams
, 257, fp
);
164 fwrite (bigrams
, 1, 256, stdout
);
167 while ((line_len
= getline (&path
, &pathsize
, stdin
)) > 0)
171 path
[line_len
- 1] = '\0'; /* Remove newline. */
173 /* Squelch unprintable chars in path so as not to botch decoding. */
174 for (pp
= path
; *pp
!= '\0'; pp
++)
176 if (!(*pp
>= 040 && *pp
< 0177))
180 count
= prefix_length (oldpath
, path
);
181 diffcount
= count
- oldcount
;
183 /* If the difference is small, it fits in one byte;
184 otherwise, two bytes plus a marker noting that fact. */
185 if (diffcount
< -LOCATEDB_OLD_OFFSET
|| diffcount
> LOCATEDB_OLD_OFFSET
)
187 putc (LOCATEDB_OLD_ESCAPE
, stdout
);
188 putw (diffcount
+ LOCATEDB_OLD_OFFSET
, stdout
);
191 putc (diffcount
+ LOCATEDB_OLD_OFFSET
, stdout
);
193 /* Look for bigrams in the remainder of the path. */
194 for (pp
= path
+ count
; *pp
!= '\0'; pp
+= 2)
198 /* No bigram is possible; only one char is left. */
204 /* Linear search for specific bigram in string table. */
205 code
= strindex (bigrams
, bigram
);
207 putchar ((code
/ 2) | 0200); /* It's a common bigram. */
209 fputs (bigram
, stdout
); /* Write the text as printable ASCII. */
213 /* Swap path and oldpath and their sizes. */
214 char *tmppath
= oldpath
;
215 size_t tmppathsize
= oldpathsize
;
217 oldpathsize
= pathsize
;
219 pathsize
= tmppathsize
;