2 /* $NetBSD: citrus_lookup.c,v 1.6 2009/02/03 04:58:38 lukem 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
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
44 #include "citrus_namespace.h"
45 #include "citrus_bcs.h"
46 #include "citrus_region.h"
47 #include "citrus_memstream.h"
48 #include "citrus_mmap.h"
49 #include "citrus_db.h"
50 #include "citrus_db_hash.h"
51 #include "citrus_lookup.h"
52 #include "citrus_lookup_file.h"
54 struct _citrus_lookup {
57 struct _citrus_db *db;
58 struct _citrus_region file;
60 struct _db_locator locator;
68 #define cl_dbidx u.db.idx
69 #define cl_dbfile u.db.file
70 #define cl_dbnum u.db.num
71 #define cl_dblocator u.db.locator
72 #define cl_plainr u.plain.r
73 #define cl_plainms u.plain.ms
78 int (*cl_next)(struct _citrus_lookup *, struct _region *,
80 int (*cl_lookup)(struct _citrus_lookup *, const char *,
82 int (*cl_num_entries)(struct _citrus_lookup *);
83 void (*cl_close)(struct _citrus_lookup *);
87 seq_get_num_entries_db(struct _citrus_lookup *cl)
90 return (cl->cl_dbnum);
94 seq_next_db(struct _citrus_lookup *cl, struct _region *key,
100 _region_init(key, cl->cl_key, cl->cl_keylen);
101 return (_db_lookup_by_s(cl->cl_db, cl->cl_key, data,
109 if (cl->cl_dbidx >= cl->cl_dbnum)
112 return (_db_get_entry(cl->cl_db, cl->cl_dbidx++, key, data));
116 seq_lookup_db(struct _citrus_lookup *cl, const char *key, struct _region *data)
121 cl->cl_key = strdup(key);
122 if (cl->cl_ignore_case)
123 _bcs_convert_to_lower(cl->cl_key);
124 cl->cl_keylen = strlen(cl->cl_key);
125 _db_locator_init(&cl->cl_dblocator);
126 return (_db_lookup_by_s(cl->cl_db, cl->cl_key, data,
131 seq_close_db(struct _citrus_lookup *cl)
134 _db_close(cl->cl_db);
135 _unmap_file(&cl->cl_dbfile);
139 seq_open_db(struct _citrus_lookup *cl, const char *name)
145 snprintf(path, sizeof(path), "%s.db", name);
146 ret = _map_file(&r, path);
150 ret = _db_open(&cl->cl_db, &r, _CITRUS_LOOKUP_MAGIC,
158 cl->cl_dbnum = _db_get_num_entries(cl->cl_db);
161 cl->cl_lookup = &seq_lookup_db;
162 cl->cl_next = &seq_next_db;
163 cl->cl_num_entries = &seq_get_num_entries_db;
164 cl->cl_close = &seq_close_db;
171 seq_next_plain(struct _citrus_lookup *cl, struct _region *key,
172 struct _region *data)
178 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
182 p = _memstream_getln(&cl->cl_plainms, &len);
186 q = memchr(p, T_COMM, len);
190 /* ignore trailing spaces */
191 _bcs_trunc_rws_len(p, &len);
192 p = _bcs_skip_ws_len(p, &len);
193 q = _bcs_skip_nonws_len(p, &len);
196 if (cl->cl_key && ((size_t)(q - p) != cl->cl_keylen ||
197 memcmp(p, cl->cl_key, (size_t)(q - p)) != 0))
202 _region_init(key, __DECONST(void *, p), (size_t)(q - p));
203 p = _bcs_skip_ws_len(q, &len);
205 _region_init(data, len ? __DECONST(void *, p) : NULL, len);
211 seq_get_num_entries_plain(struct _citrus_lookup *cl)
216 while (seq_next_plain(cl, NULL, NULL) == 0)
223 seq_lookup_plain(struct _citrus_lookup *cl, const char *key,
224 struct _region *data)
231 cl->cl_key = strdup(key);
232 if (cl->cl_ignore_case)
233 _bcs_convert_to_lower(cl->cl_key);
234 cl->cl_keylen = strlen(cl->cl_key);
235 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
236 p = _memstream_matchline(&cl->cl_plainms, cl->cl_key, &len, 0);
240 _region_init(data, __DECONST(void *, p), len);
246 seq_close_plain(struct _citrus_lookup *cl)
249 _unmap_file(&cl->cl_plainr);
253 seq_open_plain(struct _citrus_lookup *cl, const char *name)
257 /* open read stream */
258 ret = _map_file(&cl->cl_plainr, name);
263 cl->cl_next = &seq_next_plain;
264 cl->cl_lookup = &seq_lookup_plain;
265 cl->cl_num_entries = &seq_get_num_entries_plain;
266 cl->cl_close = &seq_close_plain;
272 _citrus_lookup_seq_open(struct _citrus_lookup **rcl, const char *name,
276 struct _citrus_lookup *cl;
278 cl = malloc(sizeof(*cl));
284 cl->cl_ignore_case = ignore_case;
285 ret = seq_open_db(cl, name);
287 ret = seq_open_plain(cl, name);
297 _citrus_lookup_seq_rewind(struct _citrus_lookup *cl)
307 _citrus_lookup_seq_next(struct _citrus_lookup *cl,
308 struct _region *key, struct _region *data)
311 return ((*cl->cl_next)(cl, key, data));
315 _citrus_lookup_seq_lookup(struct _citrus_lookup *cl, const char *key,
316 struct _region *data)
319 return ((*cl->cl_lookup)(cl, key, data));
323 _citrus_lookup_get_number_of_entries(struct _citrus_lookup *cl)
326 return ((*cl->cl_num_entries)(cl));
330 _citrus_lookup_seq_close(struct _citrus_lookup *cl)
339 _citrus_lookup_simple(const char *name, const char *key,
340 char *linebuf, size_t linebufsize, int ignore_case)
342 struct _citrus_lookup *cl;
346 ret = _citrus_lookup_seq_open(&cl, name, ignore_case);
350 ret = _citrus_lookup_seq_lookup(cl, key, &data);
352 _citrus_lookup_seq_close(cl);
356 snprintf(linebuf, linebufsize, "%.*s", (int)_region_size(&data),
357 (const char *)_region_head(&data));
359 _citrus_lookup_seq_close(cl);