2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-12-11 09:04:50 adam
8 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
10 * Revision 1.6 1995/11/20 11:58:04 adam
11 * Support for YAZ in standard located directories, such as /usr/local/..
13 * Revision 1.5 1995/10/09 16:18:32 adam
14 * Function dict_lookup_grep got extra client data parameter.
16 * Revision 1.4 1995/10/06 13:52:00 adam
17 * Bug fixes. Handler may abort further scanning.
19 * Revision 1.3 1995/10/06 11:06:07 adam
22 * Revision 1.2 1995/10/06 10:43:16 adam
25 * Revision 1.1 1995/10/06 09:04:18 adam
26 * First version of scan.
36 int dict_scan_trav (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
37 int start, int *count, void *client,
38 int (*userfunc)(Dict_char *, const char *, int, void *),
46 dict_bf_readp (dict->dbf, ptr, &p);
48 if (start == 0 && dir == -1)
52 indxp = (short*) ((char*) p+DICT_pagesize(dict)-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 if ((*userfunc)(str, info+(j+1)*sizeof(Dict_char), *count * dir,
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 (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
87 str[pos+1] = DICT_EOS;
88 if ((*userfunc)(str, info+sizeof(Dict_ptr)+sizeof(Dict_char),
89 *count * dir, client))
93 if (*count > 0 && subptr)
94 dict_scan_trav (dict, subptr, pos+1, str, 0, count,
95 client, userfunc, dir);
102 int dict_scan_r (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
103 int *before, int *after, void *client,
104 int (*userfunc)(Dict_char *, const char *, int, void *))
106 int cmp = 0, mid, lo, hi;
111 dict_bf_readp (dict->dbf, ptr, &p);
115 hi = DICT_nodir(p)-1;
116 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
122 /* string (Dict_char *) DICT_EOS terminated */
123 /* unsigned char length of information */
124 /* char * information */
125 info = (char*)p + indxp[-mid];
126 cmp = dict_strcmp ((Dict_char*) info, str + pos);
131 (*userfunc)(str, info+
132 (dict_strlen(info)+1)*sizeof(Dict_char),
144 /* Dict_ptr subptr */
145 /* Dict_char sub char */
146 /* unsigned char length of information */
147 /* char * information */
148 info = (char*)p - indxp[-mid];
149 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
153 memcpy (&subptr, info, sizeof(Dict_ptr));
154 if (str[pos+1] == DICT_EOS)
156 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
161 info+sizeof(Dict_ptr)+
167 if (*after && subptr)
168 if (dict_scan_trav (dict, subptr, pos+1, str, 0,
169 after, client, userfunc, 1))
173 if (dict_scan_r (dict, subptr, pos+1, str, before, after,
184 if (lo>hi && cmp < 0)
187 if (dict_scan_trav (dict, ptr, pos, str, cmp ? mid : mid+1, after,
188 client, userfunc, 1))
190 if (*before && mid > 1)
191 if (dict_scan_trav (dict, ptr, pos, str, mid-1, before,
192 client, userfunc, -1))
197 int dict_scan (Dict dict, Dict_char *str, int *before, int *after,
199 int (*f)(Dict_char *name, const char *info, int pos,
202 if (dict->head.last <= 1)
204 return dict_scan_r (dict, 1, 0, str, before, after, client, f);