1 /* $Id: delete.c,v 1.11 2005-01-15 19:38:21 adam Exp $
2 Copyright (C) 1995-2005
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
30 static void dict_del_subtree (Dict dict, Dict_ptr ptr,
32 int (*f)(const char *, void *))
41 dict_bf_readp (dict->dbf, ptr, &p);
42 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
44 for (i = 0; i <= hi; i++)
48 /* string (Dict_char *) DICT_EOS terminated */
49 /* unsigned char length of information */
50 /* char * information */
51 char *info = (char*)p + indxp[-i];
53 (*f)(info + (dict_strlen((Dict_char*) info)+1)
54 *sizeof(Dict_char), client);
61 /* Dict_char sub char */
62 /* unsigned char length of information */
63 /* char * information */
64 char *info = (char*)p - indxp[-i];
65 memcpy (&subptr, info, sizeof(Dict_ptr));
67 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
70 (*f)(info+sizeof(Dict_ptr)+sizeof(Dict_char), client);
74 dict_del_subtree (dict, subptr, client, f);
76 /* page may be gone. reread it .. */
77 dict_bf_readp (dict->dbf, ptr, &p);
78 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
82 DICT_backptr(p) = dict->head.freelist;
83 dict->head.freelist = ptr;
84 dict_bf_touch (dict->dbf, ptr);
87 static int dict_del_string (Dict dict, const Dict_char *str, Dict_ptr ptr,
88 int sub_flag, void *client,
89 int (*f)(const char *, void *))
99 dict_bf_readp (dict->dbf, ptr, &p);
101 hi = DICT_nodir(p)-1;
102 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
108 /* string (Dict_char *) DICT_EOS terminated */
109 /* unsigned char length of information */
110 /* char * information */
111 info = (char*)p + indxp[-mid];
113 cmp = dict_strcmp((Dict_char*) info, str);
116 /* determine if prefix match */
117 if (!dict_strncmp (str, (Dict_char*) info, dict_strlen(str)))
120 (*f)(info + (dict_strlen((Dict_char*) info)+1)
121 *sizeof(Dict_char), client);
123 hi = DICT_nodir(p)-1;
126 indxp[-mid] = indxp[-mid-1];
131 dict_bf_touch (dict->dbf, ptr);
134 /* start again (may not be the most efficient way to go)*/
140 /* normal delete: delete if equal */
143 hi = DICT_nodir(p)-1;
146 indxp[-mid] = indxp[-mid-1];
151 dict_bf_touch (dict->dbf, ptr);
161 /* Dict_ptr subptr */
162 /* Dict_char sub char */
163 /* unsigned char length of information */
164 /* char * information */
165 info = (char*)p - indxp[-mid];
166 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
170 memcpy (&subptr, info, sizeof(Dict_ptr));
171 if (*++str == DICT_EOS)
173 if (sub_flag && subptr)
176 memcpy (info, &null_ptr, sizeof(Dict_ptr));
178 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
180 info[sizeof(Dict_ptr)+sizeof(Dict_char)] = 0;
182 dict_bf_touch (dict->dbf, ptr);
185 (*f)(info+sizeof(Dict_ptr)+sizeof(Dict_char),
187 if (sub_flag && subptr)
188 dict_del_subtree (dict, subptr, client, f);
191 if (sub_flag && subptr)
194 dict_bf_touch (dict->dbf, ptr);
195 dict_del_subtree (dict, subptr, client, f);
204 dict_bf_readp (dict->dbf, ptr, &p);
206 hi = DICT_nodir(p)-1;
207 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
220 int dict_delete (Dict dict, const char *p)
222 return dict_del_string (dict, (const Dict_char*) p, dict->head.root, 0,
226 int dict_delete_subtree (Dict dict, const char *p, void *client,
227 int (*f)(const char *info, void *client))
229 return dict_del_string (dict, (const Dict_char*) p, dict->head.root, 1,