1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 typedef unsigned MatchWord;
34 #define SH(x) (((x)<<1)+1)
36 static int lookup_ec(Dict dict, Dict_ptr ptr,
37 MatchInfo *mi, MatchWord *ri_base,
38 int pos, int (*userfunc)(char *), int range,
45 MatchWord match_mask = 1<<(mi->m-1);
47 dict_bf_readp (dict->dbf, ptr, &p);
50 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
55 /* string (Dict_char *) DICT_EOS terminated */
56 /* unsigned char length of information */
57 /* char * information */
58 MatchWord *ri = ri_base, sc;
60 info = (char*)p + indxp[-lo];
65 memcpy(&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
69 if (ri[range] & match_mask)
70 (*userfunc)((char*) prefix);
73 if (j+pos >= mi->m+range)
76 ri[1+range] = SH(ri[0]) & sc;
77 for (i=1; i<=range; i++)
78 ri[i+1+range] = (SH(ri[i]) & sc) | SH(ri[i-1])
79 | SH(ri[i+range]) | ri[i-1];
81 if (!(ri[range] & (1<<(pos+j))))
88 MatchWord *ri = ri_base, sc;
92 /* Dict_char sub char */
93 /* unsigned char length of information */
94 /* char * information */
95 info = (char*)p - indxp[-lo];
96 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
100 ri[1+range] = SH(ri[0]) & sc;
101 for (i=1; i<=range; i++)
102 ri[i+1+range] = (SH(ri[i]) & sc) | SH(ri[i-1])
103 | SH(ri[i+range]) | ri[i-1];
105 if (ri[range] & (1<<pos))
108 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)] &&
109 (ri[range] & match_mask))
111 prefix[pos+1] = DICT_EOS;
112 (*userfunc)((char*) prefix);
114 memcpy(&subptr, info, sizeof(Dict_ptr));
117 lookup_ec(dict, subptr, mi, ri, pos+1,
118 userfunc, range, prefix);
119 dict_bf_readp(dict->dbf, ptr, &p);
120 indxp = (short*) ((char*) p +
121 DICT_bsize(p)-sizeof(short));
130 static MatchInfo *prepare_match(Dict_char *pattern)
136 mi = (MatchInfo *) xmalloc(sizeof(*mi));
137 mi->m = dict_strlen (pattern);
138 mi->s = s = (MatchWord *) xmalloc(sizeof(*s)*256); /* 256 !!! */
139 for (i = 0; i < 256; i++)
141 for (i = 0; pattern[i]; i++)
142 s[pattern[i]&255] += 1<<i;
146 int dict_lookup_ec(Dict dict, char *pattern, int range,
147 int (*userfunc)(char *name))
152 Dict_char prefix[2048];
154 if (!dict->head.root)
157 mi = prepare_match((Dict_char*) pattern);
159 ri = (MatchWord *) xmalloc((dict_strlen((Dict_char*) pattern)+range+2)
160 * (range+1)*sizeof(*ri));
161 for (i = 0; i <= range; i++)
164 ret = lookup_ec(dict, dict->head.root, mi, ri, 0, userfunc,
173 * indent-tabs-mode: nil
175 * vim: shiftwidth=4 tabstop=8 expandtab