1 /* $Id: scan.c,v 1.15 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 (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));
106 int dict_scan_r (Dict dict, Dict_ptr ptr, int pos, Dict_char *str,
107 int *before, int *after, void *client,
108 int (*userfunc)(char *, const char *, int, void *))
110 int cmp = 0, mid, lo, hi;
115 dict_bf_readp (dict->dbf, ptr, &p);
119 hi = DICT_nodir(p)-1;
120 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
126 /* string (Dict_char *) DICT_EOS terminated */
127 /* unsigned char length of information */
128 /* char * information */
129 info = (char*)p + indxp[-mid];
130 cmp = dict_strcmp ((Dict_char*) info, str + pos);
135 (*userfunc)((char *) str, info+
136 (dict_strlen((Dict_char*) info)+1)
149 /* Dict_ptr subptr */
150 /* Dict_char sub char */
151 /* unsigned char length of information */
152 /* char * information */
153 info = (char*)p - indxp[-mid];
154 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
158 memcpy (&subptr, info, sizeof(Dict_ptr));
159 if (str[pos+1] == DICT_EOS)
161 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
165 (*userfunc)((char*) str,
166 info+sizeof(Dict_ptr)+
172 if (*after && subptr)
173 if (dict_scan_trav (dict, subptr, pos+1, str, -1,
174 after, client, userfunc, 1))
179 if (dict_scan_r (dict, subptr, pos+1, str, before, after,
191 if (lo>hi && cmp < 0)
194 if (dict_scan_trav (dict, ptr, pos, str, cmp ? mid : mid+1, after,
195 client, userfunc, 1))
197 if (*before && mid > 0)
198 if (dict_scan_trav (dict, ptr, pos, str, mid-1, before,
199 client, userfunc, -1))
204 int dict_scan (Dict dict, char *str, int *before, int *after, void *client,
205 int (*f)(char *name, const char *info, int pos, void *client))
209 logf (LOG_DEBUG, "dict_scan");
210 for (i = 0; str[i]; i++)
212 logf (LOG_DEBUG, " %3d %c", str[i],
213 (str[i] > ' ' && str[i] < 127) ? str[i] : '?');
215 if (!dict->head.root)
217 return dict_scan_r (dict, dict->head.root, 0, (Dict_char *) str,
218 before, after, client, f);