2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1996-02-02 13:43:52 adam
8 * The public functions simply use char instead of Dict_char to represent
9 * search strings. Dict_char is used internally only.
11 * Revision 1.7 1995/12/11 09:04:50 adam
12 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
14 * Revision 1.6 1995/11/20 11:58:04 adam
15 * Support for YAZ in standard located directories, such as /usr/local/..
17 * Revision 1.5 1995/10/09 16:18:32 adam
18 * Function dict_lookup_grep got extra client data parameter.
20 * Revision 1.4 1995/10/06 13:52:00 adam
21 * Bug fixes. Handler may abort further scanning.
23 * Revision 1.3 1995/10/06 11:06:07 adam
26 * Revision 1.2 1995/10/06 10:43:16 adam
29 * Revision 1.1 1995/10/06 09:04:18 adam
30 * First version of scan.
40 int dict_scan_trav (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
41 int start, int *count, void *client,
42 int (*userfunc)(char *, const char *, int, void *),
50 dict_bf_readp (dict->dbf, ptr, &p);
52 if (start == 0 && dir == -1)
56 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
58 while (lo <= hi && lo >= 0 && *count > 0)
62 /* string (Dict_char *) DICT_EOS terminated */
63 /* unsigned char length of information */
64 /* char * information */
66 info = (char*)p + indxp[-lo];
67 for (j = 0; info[j] != DICT_EOS; j++)
69 str[pos+j] = DICT_EOS;
70 if ((*userfunc)((char*) str, info+(j+1)*sizeof(Dict_char),
71 *count * dir, client))
81 /* Dict_char sub char */
82 /* unsigned char length of information */
83 /* char * information */
85 info = (char*)p - indxp[-lo];
86 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
88 memcpy (&subptr, info, sizeof(Dict_ptr));
89 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
91 str[pos+1] = DICT_EOS;
92 if ((*userfunc)((char*) str,
93 info+sizeof(Dict_ptr)+sizeof(Dict_char),
94 *count * dir, client))
98 if (*count > 0 && subptr)
99 dict_scan_trav (dict, subptr, pos+1, str, 0, count,
100 client, userfunc, dir);
107 int dict_scan_r (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
108 int *before, int *after, void *client,
109 int (*userfunc)(char *, const char *, int, void *))
111 int cmp = 0, mid, lo, hi;
116 dict_bf_readp (dict->dbf, ptr, &p);
120 hi = DICT_nodir(p)-1;
121 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
127 /* string (Dict_char *) DICT_EOS terminated */
128 /* unsigned char length of information */
129 /* char * information */
130 info = (char*)p + indxp[-mid];
131 cmp = dict_strcmp ((Dict_char*) info, str + pos);
136 (*userfunc)((char *) str, info+
137 (dict_strlen((Dict_char*) info)+1)
150 /* Dict_ptr subptr */
151 /* Dict_char sub char */
152 /* unsigned char length of information */
153 /* char * information */
154 info = (char*)p - indxp[-mid];
155 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
159 memcpy (&subptr, info, sizeof(Dict_ptr));
160 if (str[pos+1] == DICT_EOS)
162 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
166 (*userfunc)((char*) str,
167 info+sizeof(Dict_ptr)+
173 if (*after && subptr)
174 if (dict_scan_trav (dict, subptr, pos+1, str, 0,
175 after, client, userfunc, 1))
179 if (dict_scan_r (dict, subptr, pos+1, str, before, after,
190 if (lo>hi && cmp < 0)
193 if (dict_scan_trav (dict, ptr, pos, str, cmp ? mid : mid+1, after,
194 client, userfunc, 1))
196 if (*before && mid > 1)
197 if (dict_scan_trav (dict, ptr, pos, str, mid-1, before,
198 client, userfunc, -1))
203 int dict_scan (Dict dict, char *str, int *before, int *after, void *client,
204 int (*f)(char *name, const char *info, int pos, void *client))
206 if (dict->head.last <= 1)
208 return dict_scan_r (dict, 1, 0, (Dict_char *) str, before, after, client,