2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1998-06-22 11:34:45 adam
8 * Changed scan callback function so it doesn't stop further scanning.
10 * Revision 1.10 1998/03/06 16:58:04 adam
11 * Fixed bug which related to scanning of large indexes.
13 * Revision 1.9 1997/10/27 14:33:04 adam
14 * Moved towards generic character mapping depending on "structure"
15 * field in abstract syntax file. Fixed a few memory leaks. Fixed
16 * bug with negative integers when doing searches with relational
19 * Revision 1.8 1996/02/02 13:43:52 adam
20 * The public functions simply use char instead of Dict_char to represent
21 * search strings. Dict_char is used internally only.
23 * Revision 1.7 1995/12/11 09:04:50 adam
24 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
26 * Revision 1.6 1995/11/20 11:58:04 adam
27 * Support for YAZ in standard located directories, such as /usr/local/..
29 * Revision 1.5 1995/10/09 16:18:32 adam
30 * Function dict_lookup_grep got extra client data parameter.
32 * Revision 1.4 1995/10/06 13:52:00 adam
33 * Bug fixes. Handler may abort further scanning.
35 * Revision 1.3 1995/10/06 11:06:07 adam
38 * Revision 1.2 1995/10/06 10:43:16 adam
41 * Revision 1.1 1995/10/06 09:04:18 adam
42 * First version of scan.
52 int dict_scan_trav (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
53 int start, int *count, void *client,
54 int (*userfunc)(char *, const char *, int, void *),
62 dict_bf_readp (dict->dbf, ptr, &p);
64 if (start == 0 && dir == -1)
68 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
70 while (lo <= hi && lo >= 0 && *count > 0)
74 /* string (Dict_char *) DICT_EOS terminated */
75 /* unsigned char length of information */
76 /* char * information */
78 info = (char*)p + indxp[-lo];
79 for (j = 0; info[j] != DICT_EOS; j++)
81 str[pos+j] = DICT_EOS;
82 (*userfunc)((char*) str, info+(j+1)*sizeof(Dict_char),
83 *count * dir, client);
92 /* Dict_char sub char */
93 /* unsigned char length of information */
94 /* char * information */
96 info = (char*)p - indxp[-lo];
97 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
99 memcpy (&subptr, info, sizeof(Dict_ptr));
100 if (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))
109 if (*count > 0 && subptr)
111 dict_scan_trav (dict, subptr, pos+1, str, 0, count,
112 client, userfunc, dir);
113 dict_bf_readp (dict->dbf, ptr, &p);
114 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
122 int dict_scan_r (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
123 int *before, int *after, void *client,
124 int (*userfunc)(char *, const char *, int, void *))
126 int cmp = 0, mid, lo, hi;
131 dict_bf_readp (dict->dbf, ptr, &p);
135 hi = DICT_nodir(p)-1;
136 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
142 /* string (Dict_char *) DICT_EOS terminated */
143 /* unsigned char length of information */
144 /* char * information */
145 info = (char*)p + indxp[-mid];
146 cmp = dict_strcmp ((Dict_char*) info, str + pos);
151 (*userfunc)((char *) str, info+
152 (dict_strlen((Dict_char*) info)+1)
165 /* Dict_ptr subptr */
166 /* Dict_char sub char */
167 /* unsigned char length of information */
168 /* char * information */
169 info = (char*)p - indxp[-mid];
170 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
174 memcpy (&subptr, info, sizeof(Dict_ptr));
175 if (str[pos+1] == DICT_EOS)
177 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
181 (*userfunc)((char*) str,
182 info+sizeof(Dict_ptr)+
188 if (*after && subptr)
189 if (dict_scan_trav (dict, subptr, pos+1, str, 0,
190 after, client, userfunc, 1))
195 if (dict_scan_r (dict, subptr, pos+1, str, before, after,
207 if (lo>hi && cmp < 0)
210 if (dict_scan_trav (dict, ptr, pos, str, cmp ? mid : mid+1, after,
211 client, userfunc, 1))
213 if (*before && mid > 1)
214 if (dict_scan_trav (dict, ptr, pos, str, mid-1, before,
215 client, userfunc, -1))
220 int dict_scan (Dict dict, char *str, int *before, int *after, void *client,
221 int (*f)(char *name, const char *info, int pos, void *client))
225 logf (LOG_DEBUG, "dict_scan");
226 for (i = 0; str[i]; i++)
228 logf (LOG_DEBUG, " %3d %c", str[i],
229 (str[i] > ' ' && str[i] < 127) ? str[i] : '?');
231 if (dict->head.last <= 1)
233 return dict_scan_r (dict, 1, 0, (Dict_char *) str, before, after, client,