1 /* $NetBSD: citrus_lookup.c,v 1.6 2009/02/03 04:58:38 lukem Exp $ */
4 * Copyright (c)2003 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_lookup.c,v 1.6 2009/02/03 04:58:38 lukem Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
45 #include <sys/types.h>
47 #include "citrus_namespace.h"
48 #include "citrus_bcs.h"
49 #include "citrus_region.h"
50 #include "citrus_memstream.h"
51 #include "citrus_mmap.h"
52 #include "citrus_db.h"
53 #include "citrus_db_hash.h"
54 #include "citrus_lookup.h"
55 #include "citrus_lookup_file.h"
57 struct _citrus_lookup
{
60 struct _citrus_db
*db
;
61 struct _citrus_region file
;
63 struct _db_locator locator
;
71 #define cl_dbidx u.db.idx
72 #define cl_dbfile u.db.file
73 #define cl_dbnum u.db.num
74 #define cl_dblocator u.db.locator
75 #define cl_plainr u.plain.r
76 #define cl_plainms u.plain.ms
81 int (*cl_next
)(struct _citrus_lookup
*, struct _region
*,
83 int (*cl_lookup
)(struct _citrus_lookup
*, const char *,
85 int (*cl_num_entries
)(struct _citrus_lookup
*);
86 void (*cl_close
)(struct _citrus_lookup
*);
90 seq_get_num_entries_db(struct _citrus_lookup
*cl
)
96 seq_next_db(struct _citrus_lookup
*cl
,
97 struct _region
*key
, struct _region
*data
)
102 _region_init(key
, cl
->cl_key
, cl
->cl_keylen
);
103 return _db_lookup_by_s(cl
->cl_db
, cl
->cl_key
, data
,
111 if (cl
->cl_dbidx
>= cl
->cl_dbnum
)
114 return _db_get_entry(cl
->cl_db
, cl
->cl_dbidx
++, key
, data
);
118 seq_lookup_db(struct _citrus_lookup
*cl
, const char *key
,
119 struct _region
*data
)
123 cl
->cl_key
= strdup(key
);
124 if (cl
->cl_ignore_case
)
125 _bcs_convert_to_lower(cl
->cl_key
);
126 cl
->cl_keylen
= strlen(cl
->cl_key
);
127 _db_locator_init(&cl
->cl_dblocator
);
128 return _db_lookup_by_s(cl
->cl_db
, cl
->cl_key
, data
, &cl
->cl_dblocator
);
132 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
, __UNCONST(p
), (size_t)(q
-p
));
203 p
= _bcs_skip_ws_len(q
, &len
);
205 _region_init(data
, len
? __UNCONST(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
, __UNCONST(p
), len
);
246 seq_close_plain(struct _citrus_lookup
*cl
)
248 _unmap_file(&cl
->cl_plainr
);
252 seq_open_plain(struct _citrus_lookup
*cl
, const char *name
)
256 /* open read stream */
257 ret
= _map_file(&cl
->cl_plainr
, name
);
262 cl
->cl_next
= &seq_next_plain
;
263 cl
->cl_lookup
= &seq_lookup_plain
;
264 cl
->cl_num_entries
= &seq_get_num_entries_plain
;
265 cl
->cl_close
= &seq_close_plain
;
271 _citrus_lookup_seq_open(struct _citrus_lookup
**rcl
, const char *name
,
275 struct _citrus_lookup
*cl
;
277 cl
= malloc(sizeof(*cl
));
283 cl
->cl_ignore_case
= ignore_case
;
284 ret
= seq_open_db(cl
, name
);
286 ret
= seq_open_plain(cl
, name
);
296 _citrus_lookup_seq_rewind(struct _citrus_lookup
*cl
)
305 _citrus_lookup_seq_next(struct _citrus_lookup
*cl
,
306 struct _region
*key
, struct _region
*data
)
308 return (*cl
->cl_next
)(cl
, key
, data
);
312 _citrus_lookup_seq_lookup(struct _citrus_lookup
*cl
, const char *key
,
313 struct _region
*data
)
315 return (*cl
->cl_lookup
)(cl
, key
, data
);
319 _citrus_lookup_get_number_of_entries(struct _citrus_lookup
*cl
)
321 return (*cl
->cl_num_entries
)(cl
);
325 _citrus_lookup_seq_close(struct _citrus_lookup
*cl
)
333 _citrus_lookup_simple(const char *name
, const char *key
,
334 char *linebuf
, size_t linebufsize
, int ignore_case
)
337 struct _citrus_lookup
*cl
;
340 ret
= _citrus_lookup_seq_open(&cl
, name
, ignore_case
);
344 ret
= _citrus_lookup_seq_lookup(cl
, key
, &data
);
346 _citrus_lookup_seq_close(cl
);
350 snprintf(linebuf
, linebufsize
, "%.*s",
351 (int)_region_size(&data
), (const char *)_region_head(&data
));
353 _citrus_lookup_seq_close(cl
);