1 /* $NetBSD: citrus_lookup.c,v 1.5 2006/03/27 01:09:11 christos Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_lookup.c,v 1.3 2008/04/10 10:21:01 hasso Exp $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
42 #include "citrus_namespace.h"
43 #include "citrus_bcs.h"
44 #include "citrus_region.h"
45 #include "citrus_memstream.h"
46 #include "citrus_mmap.h"
47 #include "citrus_db.h"
48 #include "citrus_db_hash.h"
49 #include "citrus_lookup.h"
50 #include "citrus_lookup_file.h"
52 struct _citrus_lookup {
55 struct _citrus_db *db;
56 struct _citrus_region file;
58 struct _db_locator locator;
66 #define cl_dbidx u.db.idx
67 #define cl_dbfile u.db.file
68 #define cl_dbnum u.db.num
69 #define cl_dblocator u.db.locator
70 #define cl_plainr u.plain.r
71 #define cl_plainms u.plain.ms
76 int (*cl_next)(struct _citrus_lookup *, struct _region *,
78 int (*cl_lookup)(struct _citrus_lookup *, const char *,
80 int (*cl_num_entries)(struct _citrus_lookup *);
81 void (*cl_close)(struct _citrus_lookup *);
85 seq_get_num_entries_db(struct _citrus_lookup *cl)
91 seq_next_db(struct _citrus_lookup *cl,
92 struct _region *key, struct _region *data)
97 _region_init(key, cl->cl_key, cl->cl_keylen);
98 return _db_lookup_by_s(cl->cl_db, cl->cl_key, data,
106 if (cl->cl_dbidx >= cl->cl_dbnum)
109 return _db_get_entry(cl->cl_db, cl->cl_dbidx++, key, data);
113 seq_lookup_db(struct _citrus_lookup *cl, const char *key,
114 struct _region *data)
118 cl->cl_key = strdup(key);
119 if (cl->cl_ignore_case)
120 _bcs_convert_to_lower(cl->cl_key);
121 cl->cl_keylen = strlen(cl->cl_key);
122 _db_locator_init(&cl->cl_dblocator);
123 return _db_lookup_by_s(cl->cl_db, cl->cl_key, data, &cl->cl_dblocator);
127 seq_close_db(struct _citrus_lookup *cl)
129 _db_close(cl->cl_db);
130 _unmap_file(&cl->cl_dbfile);
134 seq_open_db(struct _citrus_lookup *cl, const char *name)
140 snprintf(path, sizeof(path), "%s.db", name);
141 ret = _map_file(&r, path);
145 ret = _db_open(&cl->cl_db, &r, _CITRUS_LOOKUP_MAGIC,
153 cl->cl_dbnum = _db_get_num_entries(cl->cl_db);
156 cl->cl_lookup = &seq_lookup_db;
157 cl->cl_next = &seq_next_db;
158 cl->cl_num_entries = &seq_get_num_entries_db;
159 cl->cl_close = &seq_close_db;
166 seq_next_plain(struct _citrus_lookup *cl, struct _region *key,
167 struct _region *data)
173 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
177 p = _memstream_getln(&cl->cl_plainms, &len);
181 q = memchr(p, T_COMM, len);
185 /* ignore trailing spaces */
186 _bcs_trunc_rws_len(p, &len);
187 p = _bcs_skip_ws_len(p, &len);
188 q = _bcs_skip_nonws_len(p, &len);
191 if (cl->cl_key && ((size_t)(q-p) != cl->cl_keylen ||
192 memcmp(p, cl->cl_key, (size_t)(q-p)) != 0))
197 _region_init(key, __DECONST(char *, p), q-p);
198 p = _bcs_skip_ws_len(q, &len);
200 _region_init(data, len ? __DECONST(char *, p) : NULL, len);
206 seq_get_num_entries_plain(struct _citrus_lookup *cl)
211 while (seq_next_plain(cl, NULL, NULL) == 0)
218 seq_lookup_plain(struct _citrus_lookup *cl, const char *key,
219 struct _region *data)
226 cl->cl_key = strdup(key);
227 if (cl->cl_ignore_case)
228 _bcs_convert_to_lower(cl->cl_key);
229 cl->cl_keylen = strlen(cl->cl_key);
230 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
231 p = _memstream_matchline(&cl->cl_plainms, cl->cl_key, &len, 0);
235 _region_init(data, __DECONST(char *, p), len);
241 seq_close_plain(struct _citrus_lookup *cl)
243 _unmap_file(&cl->cl_plainr);
247 seq_open_plain(struct _citrus_lookup *cl, const char *name)
251 /* open read stream */
252 ret = _map_file(&cl->cl_plainr, name);
257 cl->cl_next = &seq_next_plain;
258 cl->cl_lookup = &seq_lookup_plain;
259 cl->cl_num_entries = &seq_get_num_entries_plain;
260 cl->cl_close = &seq_close_plain;
266 _citrus_lookup_seq_open(struct _citrus_lookup **rcl, const char *name,
270 struct _citrus_lookup *cl;
272 cl = malloc(sizeof(*cl));
278 cl->cl_ignore_case = ignore_case;
279 ret = seq_open_db(cl, name);
281 ret = seq_open_plain(cl, name);
291 _citrus_lookup_seq_rewind(struct _citrus_lookup *cl)
300 _citrus_lookup_seq_next(struct _citrus_lookup *cl,
301 struct _region *key, struct _region *data)
303 return (*cl->cl_next)(cl, key, data);
307 _citrus_lookup_seq_lookup(struct _citrus_lookup *cl, const char *key,
308 struct _region *data)
310 return (*cl->cl_lookup)(cl, key, data);
314 _citrus_lookup_get_number_of_entries(struct _citrus_lookup *cl)
316 return (*cl->cl_num_entries)(cl);
320 _citrus_lookup_seq_close(struct _citrus_lookup *cl)
328 _citrus_lookup_simple(const char *name, const char *key,
329 char *linebuf, size_t linebufsize, int ignore_case)
332 struct _citrus_lookup *cl;
335 ret = _citrus_lookup_seq_open(&cl, name, ignore_case);
339 ret = _citrus_lookup_seq_lookup(cl, key, &data);
341 _citrus_lookup_seq_close(cl);
345 snprintf(linebuf, linebufsize, "%.*s",
346 (int)_region_size(&data), (const char *)_region_head(&data));
348 _citrus_lookup_seq_close(cl);