Rename argument to zebra_rec_keys_set_buf
[idzebra-moved-to-github.git] / index / reckeys.c
1 /* $Id: reckeys.c,v 1.2 2005-11-09 08:27:28 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <ctype.h>
27
28 #include "index.h"
29 #include "reckeys.h"
30
31 struct zebra_rec_keys_t_ {
32     size_t buf_used;
33     size_t buf_max;
34     size_t fetch_offset;
35     char *buf;
36     void *encode_handle;
37     void *decode_handle;
38     char owner_of_buffer;
39 };
40
41 zebra_rec_keys_t zebra_rec_keys_open()
42 {
43     zebra_rec_keys_t p = xmalloc(sizeof(*p));
44     p->buf_used = 0;
45     p->buf_max = 0;
46     p->fetch_offset = 0;
47     p->buf = 0;
48     p->owner_of_buffer = 1;
49     p->encode_handle = iscz1_start();
50     p->decode_handle = iscz1_start(); 
51     return p;
52 }
53     
54 void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz,
55                             int copy_buf)
56 {
57     if (p->owner_of_buffer)
58         xfree(p->buf);
59     p->buf_used = sz;
60     p->buf_max = sz;
61     if (!copy_buf)
62     {
63         p->buf = buf;
64     }
65     else
66     {
67         if (!sz)
68             p->buf = 0;
69         else
70         {
71             p->buf = xmalloc(sz);
72             memcpy(p->buf, buf, sz);
73         }
74     }
75     p->owner_of_buffer = copy_buf;
76 }
77         
78 void zebra_rec_keys_get_buf(zebra_rec_keys_t p, char **buf, size_t *sz)
79 {
80     *buf = p->buf;
81     *sz = p->buf_used;
82
83     p->buf = 0;
84     p->buf_max = 0;
85     p->buf_used = 0;
86 }
87
88 void zebra_rec_keys_close(zebra_rec_keys_t p)
89 {
90     if (!p)
91         return;
92     
93     if (p->owner_of_buffer)
94         xfree(p->buf);
95     if (p->encode_handle)
96         iscz1_stop(p->encode_handle);
97     if (p->decode_handle)
98         iscz1_stop(p->decode_handle);
99     xfree(p);
100 }
101
102 void zebra_rec_keys_write(zebra_rec_keys_t keys, 
103                           int reg_type,
104                           const char *str, size_t slen,
105                           const struct it_key *key)
106 {
107     char *dst;
108     const char *src = (char*) key;
109     
110     assert(keys->owner_of_buffer);
111
112     if (keys->buf_used+1024 > keys->buf_max)
113     {
114         char *b = (char *) xmalloc (keys->buf_max += 128000);
115         if (keys->buf_used > 0)
116             memcpy (b, keys->buf, keys->buf_used);
117         xfree (keys->buf);
118         keys->buf = b;
119     }
120     dst = keys->buf + keys->buf_used;
121
122     iscz1_encode(keys->encode_handle, &dst, &src);
123
124 #if REG_TYPE_PREFIX
125     *dst++ = reg_type;
126 #endif
127     memcpy (dst, str, slen);
128     dst += slen;
129     *dst++ = '\0';
130     keys->buf_used = dst - keys->buf;
131 }
132
133 void zebra_rec_keys_reset(zebra_rec_keys_t keys)
134 {
135     assert(keys);
136     keys->buf_used = 0;
137     
138     iscz1_reset(keys->encode_handle);
139 }
140
141 int zebra_rec_keys_rewind(zebra_rec_keys_t keys)
142 {
143     assert(keys);
144     iscz1_reset(keys->decode_handle);
145     keys->fetch_offset = 0;
146     if (keys->buf_used == 0)
147         return 0;
148     return 1;
149 }
150
151 int zebra_rec_keys_empty(zebra_rec_keys_t keys)
152 {
153     if (keys->buf_used == 0)
154         return 1;
155     return 0;
156 }
157
158 int zebra_rec_keys_read(zebra_rec_keys_t keys,
159                         const char **str, size_t *slen,
160                         struct it_key *key)
161 {
162     assert(keys);
163     if (keys->fetch_offset == keys->buf_used)
164         return 0;
165     else
166     {
167         const char *src = keys->buf + keys->fetch_offset;
168         char *dst = (char*) key;
169         
170         assert (keys->fetch_offset < keys->buf_used);
171
172         /* store the destination key */
173         iscz1_decode(keys->decode_handle, &dst, &src);
174         
175         /* store pointer to string and length of it */
176         *str = src;
177         *slen = strlen(src);
178         src += *slen + 1;
179         
180         keys->fetch_offset = src - keys->buf;
181     }
182     return 1;
183 }