1 /* $Id: scan.c,v 1.19 2005-01-15 19:38:24 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 int dict_scan_trav (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 (*userfunc)((char*) str, info+(j+1)*sizeof(Dict_char),
67 *count * dir, client);
76 /* Dict_char sub char */
77 /* unsigned char length of information */
78 /* char * information */
80 info = (char*)p - indxp[-lo];
81 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
83 memcpy (&subptr, info, sizeof(Dict_ptr));
84 if (dir>0 && info[sizeof(Dict_ptr)+sizeof(Dict_char)])
86 str[pos+1] = DICT_EOS;
87 if ((*userfunc)((char*) str,
88 info+sizeof(Dict_ptr)+sizeof(Dict_char),
89 *count * dir, client))
93 if (*count>0 && subptr)
95 dict_scan_trav (dict, subptr, pos+1, str, -1, count,
96 client, userfunc, dir);
97 dict_bf_readp (dict->dbf, ptr, &p);
98 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
100 if (*count>0 && dir<0 && info[sizeof(Dict_ptr)+sizeof(Dict_char)])
102 str[pos+1] = DICT_EOS;
103 if ((*userfunc)((char*) str,
104 info+sizeof(Dict_ptr)+sizeof(Dict_char),
105 *count * dir, client))
115 int dict_scan_r (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
116 int *before, int *after, void *client,
117 int (*userfunc)(char *, const char *, int, void *))
119 int cmp = 0, mid, lo, hi;
124 dict_bf_readp (dict->dbf, ptr, &p);
128 hi = DICT_nodir(p)-1;
129 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
135 /* string (Dict_char *) DICT_EOS terminated */
136 /* unsigned char length of information */
137 /* char * information */
138 info = (char*)p + indxp[-mid];
139 cmp = dict_strcmp ((Dict_char*) info, str + pos);
144 (*userfunc)((char *) str, info+
145 (dict_strlen((Dict_char*) info)+1)
158 /* Dict_ptr subptr */
159 /* Dict_char sub char */
160 /* unsigned char length of information */
161 /* char * information */
162 info = (char*)p - indxp[-mid];
163 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
167 memcpy (&subptr, info, sizeof(Dict_ptr));
168 if (str[pos+1] == DICT_EOS)
170 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
174 (*userfunc)((char*) str,
175 info+sizeof(Dict_ptr)+
181 if (*after && subptr)
182 if (dict_scan_trav (dict, subptr, pos+1, str, -1,
183 after, client, userfunc, 1))
188 if (dict_scan_r (dict, subptr, pos+1, str, before, after,
200 if (lo>hi && cmp < 0)
203 if (dict_scan_trav (dict, ptr, pos, str, cmp ? mid : mid+1, after,
204 client, userfunc, 1))
206 if (*before && mid > 0)
207 if (dict_scan_trav (dict, ptr, pos, str, mid-1, before,
208 client, userfunc, -1))
213 int dict_scan (Dict dict, char *str, int *before, int *after, void *client,
214 int (*f)(char *name, const char *info, int pos, void *client))
218 yaz_log (YLOG_DEBUG, "dict_scan");
219 for (i = 0; str[i]; i++)
221 yaz_log (YLOG_DEBUG, " %3d %c", str[i],
222 (str[i] > ' ' && str[i] < 127) ? str[i] : '?');
224 if (!dict->head.root)
226 return dict_scan_r (dict, dict->head.root, 0, (Dict_char *) str,
227 before, after, client, f);