1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 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
28 static void scan_direction(Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
29 int start, int *count, void *client,
30 int (*userfunc)(char *, const char *, int, void *),
38 dict_bf_readp(dict->dbf, ptr, &p);
49 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
51 while (lo <= hi && lo >= 0 && *count > 0)
55 /* string (Dict_char *) DICT_EOS terminated */
56 /* unsigned char length of information */
57 /* char * information */
59 info = (char*)p + indxp[-lo];
60 for (j = 0; info[j] != DICT_EOS; j++)
62 str[pos+j] = DICT_EOS;
63 if ((*userfunc)((char*) str, info+(j+1)*sizeof(Dict_char),
64 *count * dir, client))
77 /* Dict_char sub char */
78 /* unsigned char length of information */
79 /* char * information */
81 info = (char*)p - indxp[-lo];
82 memcpy(&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
84 memcpy(&subptr, info, sizeof(Dict_ptr));
85 if (dir>0 && info[sizeof(Dict_ptr)+sizeof(Dict_char)])
87 str[pos+1] = DICT_EOS;
88 if ((*userfunc)((char*) str,
89 info+sizeof(Dict_ptr)+sizeof(Dict_char),
90 *count * dir, client))
97 if (*count>0 && subptr)
99 scan_direction(dict, subptr, pos+1, str, -1, count,
100 client, userfunc, dir);
101 dict_bf_readp(dict->dbf, ptr, &p);
102 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
104 if (*count>0 && dir<0 && info[sizeof(Dict_ptr)+sizeof(Dict_char)])
106 str[pos+1] = DICT_EOS;
107 if ((*userfunc)((char*) str,
108 info+sizeof(Dict_ptr)+sizeof(Dict_char),
109 *count * dir, client))
121 void dict_scan_r(Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
122 int *before, int *after, void *client,
123 int (*userfunc)(char *, const char *, int, void *))
125 int cmp = 0, mid, lo, hi;
130 dict_bf_readp(dict->dbf, ptr, &p);
134 hi = DICT_nodir(p)-1;
135 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
141 /* string (Dict_char *) DICT_EOS terminated */
142 /* unsigned char length of information */
143 /* char * information */
144 info = (char*)p + indxp[-mid];
145 cmp = dict_strcmp((Dict_char*) info, str + pos);
150 if ((*userfunc)((char *) str, info+
151 (dict_strlen((Dict_char*) info)+1)
168 /* Dict_ptr subptr */
169 /* Dict_char sub char */
170 /* unsigned char length of information */
171 /* char * information */
172 info = (char*)p - indxp[-mid];
173 memcpy(&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
177 memcpy(&subptr, info, sizeof(Dict_ptr));
178 if (str[pos+1] == DICT_EOS)
180 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
184 if ((*userfunc)((char*) str,
185 info+sizeof(Dict_ptr)+
195 if (*after && subptr)
196 scan_direction(dict, subptr, pos+1, str, -1,
197 after, client, userfunc, 1);
201 dict_scan_r(dict, subptr, pos+1, str, before, after,
212 if (lo>hi && cmp < 0)
215 scan_direction(dict, ptr, pos, str, cmp ? mid : mid+1, after,
216 client, userfunc, 1);
217 if (*before && mid > 0)
218 scan_direction(dict, ptr, pos, str, mid-1, before,
219 client, userfunc, -1);
222 int dict_scan(Dict dict, char *str, int *before, int *after, void *client,
223 int (*f)(char *name, const char *info, int pos, void *client))
227 yaz_log(YLOG_DEBUG, "dict_scan");
228 for (i = 0; str[i]; i++)
230 yaz_log(YLOG_DEBUG, "start_term pos %d %3d %c", i, str[i],
231 (str[i] > ' ' && str[i] < 127) ? str[i] : '?');
233 if (!dict->head.root)
235 dict_scan_r(dict, dict->head.root, 0, (Dict_char *) str,
236 before, after, client, f);
242 * c-file-style: "Stroustrup"
243 * indent-tabs-mode: nil
245 * vim: shiftwidth=4 tabstop=8 expandtab