1 /* $Id: reckeys.c,v 1.1 2005-10-28 09:22:50 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
31 struct zebra_rec_keys_t_ {
41 zebra_rec_keys_t zebra_rec_keys_open()
43 zebra_rec_keys_t p = xmalloc(sizeof(*p));
48 p->owner_of_buffer = 1;
49 p->encode_handle = iscz1_start();
50 p->decode_handle = iscz1_start();
54 void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz,
57 if (p->owner_of_buffer)
72 memcpy(p->buf, buf, sz);
75 p->owner_of_buffer = owner;
78 void zebra_rec_keys_get_buf(zebra_rec_keys_t p, char **buf, size_t *sz)
88 void zebra_rec_keys_close(zebra_rec_keys_t p)
93 if (p->owner_of_buffer)
96 iscz1_stop(p->encode_handle);
98 iscz1_stop(p->decode_handle);
102 void zebra_rec_keys_write(zebra_rec_keys_t keys,
104 const char *str, size_t slen,
105 const struct it_key *key)
108 const char *src = (char*) key;
110 assert(keys->owner_of_buffer);
112 if (keys->buf_used+1024 > keys->buf_max)
114 char *b = (char *) xmalloc (keys->buf_max += 128000);
115 if (keys->buf_used > 0)
116 memcpy (b, keys->buf, keys->buf_used);
120 dst = keys->buf + keys->buf_used;
122 iscz1_encode(keys->encode_handle, &dst, &src);
127 memcpy (dst, str, slen);
130 keys->buf_used = dst - keys->buf;
133 void zebra_rec_keys_reset(zebra_rec_keys_t keys)
138 iscz1_reset(keys->encode_handle);
141 int zebra_rec_keys_rewind(zebra_rec_keys_t keys)
144 iscz1_reset(keys->decode_handle);
145 keys->fetch_offset = 0;
146 if (keys->buf_used == 0)
151 int zebra_rec_keys_empty(zebra_rec_keys_t keys)
153 if (keys->buf_used == 0)
158 int zebra_rec_keys_read(zebra_rec_keys_t keys,
159 const char **str, size_t *slen,
163 if (keys->fetch_offset == keys->buf_used)
167 const char *src = keys->buf + keys->fetch_offset;
168 char *dst = (char*) key;
170 assert (keys->fetch_offset < keys->buf_used);
172 /* store the destination key */
173 iscz1_decode(keys->decode_handle, &dst, &src);
175 /* store pointer to string and length of it */
180 keys->fetch_offset = src - keys->buf;