2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1997-10-27 14:33:04 adam
8 * Moved towards generic character mapping depending on "structure"
9 * field in abstract syntax file. Fixed a few memory leaks. Fixed
10 * bug with negative integers when doing searches with relational
13 * Revision 1.8 1996/02/02 13:43:52 adam
14 * The public functions simply use char instead of Dict_char to represent
15 * search strings. Dict_char is used internally only.
17 * Revision 1.7 1995/12/11 09:04:50 adam
18 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
20 * Revision 1.6 1995/11/20 11:58:04 adam
21 * Support for YAZ in standard located directories, such as /usr/local/..
23 * Revision 1.5 1995/10/09 16:18:32 adam
24 * Function dict_lookup_grep got extra client data parameter.
26 * Revision 1.4 1995/10/06 13:52:00 adam
27 * Bug fixes. Handler may abort further scanning.
29 * Revision 1.3 1995/10/06 11:06:07 adam
32 * Revision 1.2 1995/10/06 10:43:16 adam
35 * Revision 1.1 1995/10/06 09:04:18 adam
36 * First version of scan.
46 int dict_scan_trav (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
47 int start, int *count, void *client,
48 int (*userfunc)(char *, const char *, int, void *),
56 dict_bf_readp (dict->dbf, ptr, &p);
58 if (start == 0 && dir == -1)
62 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
64 while (lo <= hi && lo >= 0 && *count > 0)
68 /* string (Dict_char *) DICT_EOS terminated */
69 /* unsigned char length of information */
70 /* char * information */
72 info = (char*)p + indxp[-lo];
73 for (j = 0; info[j] != DICT_EOS; j++)
75 str[pos+j] = DICT_EOS;
76 if ((*userfunc)((char*) str, info+(j+1)*sizeof(Dict_char),
77 *count * dir, client))
87 /* Dict_char sub char */
88 /* unsigned char length of information */
89 /* char * information */
91 info = (char*)p - indxp[-lo];
92 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
94 memcpy (&subptr, info, sizeof(Dict_ptr));
95 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
97 str[pos+1] = DICT_EOS;
98 if ((*userfunc)((char*) str,
99 info+sizeof(Dict_ptr)+sizeof(Dict_char),
100 *count * dir, client))
104 if (*count > 0 && subptr)
105 dict_scan_trav (dict, subptr, pos+1, str, 0, count,
106 client, userfunc, dir);
113 int dict_scan_r (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
114 int *before, int *after, void *client,
115 int (*userfunc)(char *, const char *, int, void *))
117 int cmp = 0, mid, lo, hi;
122 dict_bf_readp (dict->dbf, ptr, &p);
126 hi = DICT_nodir(p)-1;
127 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
133 /* string (Dict_char *) DICT_EOS terminated */
134 /* unsigned char length of information */
135 /* char * information */
136 info = (char*)p + indxp[-mid];
137 cmp = dict_strcmp ((Dict_char*) info, str + pos);
142 (*userfunc)((char *) str, info+
143 (dict_strlen((Dict_char*) info)+1)
156 /* Dict_ptr subptr */
157 /* Dict_char sub char */
158 /* unsigned char length of information */
159 /* char * information */
160 info = (char*)p - indxp[-mid];
161 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
165 memcpy (&subptr, info, sizeof(Dict_ptr));
166 if (str[pos+1] == DICT_EOS)
168 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
172 (*userfunc)((char*) str,
173 info+sizeof(Dict_ptr)+
179 if (*after && subptr)
180 if (dict_scan_trav (dict, subptr, pos+1, str, 0,
181 after, client, userfunc, 1))
185 if (dict_scan_r (dict, subptr, pos+1, str, before, after,
196 if (lo>hi && cmp < 0)
199 if (dict_scan_trav (dict, ptr, pos, str, cmp ? mid : mid+1, after,
200 client, userfunc, 1))
202 if (*before && mid > 1)
203 if (dict_scan_trav (dict, ptr, pos, str, mid-1, before,
204 client, userfunc, -1))
209 int dict_scan (Dict dict, char *str, int *before, int *after, void *client,
210 int (*f)(char *name, const char *info, int pos, void *client))
214 logf (LOG_DEBUG, "dict_scan");
215 for (i = 0; str[i]; i++)
217 logf (LOG_DEBUG, " %3d %c", str[i],
218 (str[i] > ' ' && str[i] < 127) ? str[i] : '?');
220 if (dict->head.last <= 1)
222 return dict_scan_r (dict, 1, 0, (Dict_char *) str, before, after, client,