1 /* $Id: scan.c,v 1.25 2007-01-15 15:10:15 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 static void scan_direction(Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
32 int start, int *count, void *client,
33 int (*userfunc)(char *, const char *, int, void *),
41 dict_bf_readp (dict->dbf, ptr, &p);
52 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
54 while (lo <= hi && lo >= 0 && *count > 0)
58 /* string (Dict_char *) DICT_EOS terminated */
59 /* unsigned char length of information */
60 /* char * information */
62 info = (char*)p + indxp[-lo];
63 for (j = 0; info[j] != DICT_EOS; j++)
65 str[pos+j] = DICT_EOS;
66 if ((*userfunc)((char*) str, info+(j+1)*sizeof(Dict_char),
67 *count * dir, client))
80 /* Dict_char sub char */
81 /* unsigned char length of information */
82 /* char * information */
84 info = (char*)p - indxp[-lo];
85 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
87 memcpy (&subptr, info, sizeof(Dict_ptr));
88 if (dir>0 && info[sizeof(Dict_ptr)+sizeof(Dict_char)])
90 str[pos+1] = DICT_EOS;
91 if ((*userfunc)((char*) str,
92 info+sizeof(Dict_ptr)+sizeof(Dict_char),
93 *count * dir, client))
100 if (*count>0 && subptr)
102 scan_direction (dict, subptr, pos+1, str, -1, count,
103 client, userfunc, dir);
104 dict_bf_readp (dict->dbf, ptr, &p);
105 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
107 if (*count>0 && dir<0 && info[sizeof(Dict_ptr)+sizeof(Dict_char)])
109 str[pos+1] = DICT_EOS;
110 if ((*userfunc)((char*) str,
111 info+sizeof(Dict_ptr)+sizeof(Dict_char),
112 *count * dir, client))
124 void dict_scan_r(Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
125 int *before, int *after, void *client,
126 int (*userfunc)(char *, const char *, int, void *))
128 int cmp = 0, mid, lo, hi;
133 dict_bf_readp (dict->dbf, ptr, &p);
137 hi = DICT_nodir(p)-1;
138 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
144 /* string (Dict_char *) DICT_EOS terminated */
145 /* unsigned char length of information */
146 /* char * information */
147 info = (char*)p + indxp[-mid];
148 cmp = dict_strcmp ((Dict_char*) info, str + pos);
153 if ((*userfunc)((char *) str, info+
154 (dict_strlen((Dict_char*) info)+1)
171 /* Dict_ptr subptr */
172 /* Dict_char sub char */
173 /* unsigned char length of information */
174 /* char * information */
175 info = (char*)p - indxp[-mid];
176 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
180 memcpy (&subptr, info, sizeof(Dict_ptr));
181 if (str[pos+1] == DICT_EOS)
183 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
187 if ((*userfunc)((char*) str,
188 info+sizeof(Dict_ptr)+
198 if (*after && subptr)
199 scan_direction(dict, subptr, pos+1, str, -1,
200 after, client, userfunc, 1);
204 dict_scan_r(dict, subptr, pos+1, str, before, after,
215 if (lo>hi && cmp < 0)
218 scan_direction(dict, ptr, pos, str, cmp ? mid : mid+1, after,
219 client, userfunc, 1);
220 if (*before && mid > 0)
221 scan_direction(dict, ptr, pos, str, mid-1, before,
222 client, userfunc, -1);
225 int dict_scan(Dict dict, char *str, int *before, int *after, void *client,
226 int (*f)(char *name, const char *info, int pos, void *client))
230 yaz_log(YLOG_DEBUG, "dict_scan");
231 for (i = 0; str[i]; i++)
233 yaz_log(YLOG_DEBUG, "start_term pos %d %3d %c", i, str[i],
234 (str[i] > ' ' && str[i] < 127) ? str[i] : '?');
236 if (!dict->head.root)
238 dict_scan_r(dict, dict->head.root, 0, (Dict_char *) str,
239 before, after, client, f);
245 * indent-tabs-mode: nil
247 * vim: shiftwidth=4 tabstop=8 expandtab