2 * Sherlock Library -- Generating Buckets from Objects
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
5 * (c) 2005, Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
11 #include "sherlock/sherlock.h"
12 #include "ucw/fastbuf.h"
13 #include "ucw/ff-unicode.h"
14 #include "sherlock/object.h"
24 put_attr_set_type(uns type)
28 case BUCKET_TYPE_PLAIN:
37 case BUCKET_TYPE_V33_LIZARD:
42 die("Don't know how to generate buckets of type %08x", type);
52 return len + utf8_space(len);
59 size_object(struct odes *d)
62 for (struct oattr *a=d->attrs; a; a=a->next)
63 for (struct oattr *b=a; b; b=b->same)
64 if (a->attr >= OBJ_ATTR_SON)
65 sz += 3 + size_object(b->son) + 2;
67 sz += size_attr(strlen(b->val));
72 put_attr(byte *ptr, uns type, byte *val, uns len)
76 ptr = utf8_32_put(ptr, len+1);
77 memcpy(ptr, val, len);
84 memcpy(ptr, val, len);
92 put_attr_str(byte *ptr, uns type, byte *val)
94 return put_attr(ptr, type, val, strlen(val));
98 put_attr_vformat(byte *ptr, uns type, byte *mask, va_list va)
103 len = vsprintf(ptr+1, mask, va);
106 byte tmp[6], *tmp_end = tmp;
107 tmp_end = utf8_32_put(tmp_end, len+1);
108 uns l = tmp_end - tmp;
109 memmove(ptr+l, ptr+1, len);
123 len = vsprintf(ptr, mask, va);
131 put_attr_format(byte *ptr, uns type, char *mask, ...)
135 byte *ret = put_attr_vformat(ptr, type, mask, va);
141 put_attr_num(byte *ptr, uns type, uns val)
145 uns len = sprintf(ptr+1, "%d", val) + 1;
151 ptr += sprintf(ptr, "%c%d\n", type, val);
156 put_attr_separator(byte *ptr)
164 put_attr_push(byte *ptr, uns type)
167 return put_attr(ptr, '(', &name, 1);
171 put_attr_pop(byte *ptr)
173 return put_attr(ptr, ')', NULL, 0);
177 put_object(byte *t, struct odes *d)
179 for (struct oattr *a=d->attrs; a; a=a->next)
180 for (struct oattr *b=a; b; b=b->same)
181 if (a->attr >= OBJ_ATTR_SON)
183 t = put_attr_push(t, a->attr - OBJ_ATTR_SON);
184 t = put_object(t, b->son);
188 t = put_attr_str(t, a->attr, b->val);
193 bput_attr_large(struct fastbuf *b, uns type, byte *val, uns len)
197 bput_utf8_32(b, len+1);
210 bput_attr(struct fastbuf *b, uns type, byte *val, uns len)
212 bput_attr_large(b, type, val, len);
216 bput_attr_str(struct fastbuf *b, uns type, byte *val)
218 bput_attr(b, type, val, strlen(val));
222 bput_attr_vformat(struct fastbuf *b, uns type, byte *mask, va_list va)
229 len = vsnprintf(NULL, 0, mask, va2);
232 die("vsnprintf() does not support size=0");
233 bput_utf8_32(b, len+1);
234 vbprintf(b, mask, va);
240 len = vbprintf(b, mask, va);
246 bput_attr_format(struct fastbuf *b, uns type, char *mask, ...)
250 bput_attr_vformat(b, type, mask, va);
255 bput_attr_num(struct fastbuf *b, uns type, uns val)
260 uns len = sprintf(tmp, "%d", val);
266 bprintf(b, "%c%d\n", type, val);
270 bput_attr_separator(struct fastbuf *b)
277 bput_attr_push(struct fastbuf *b, uns type)
280 bput_attr(b, '(', &name, 1);
284 bput_attr_pop(struct fastbuf *b)
286 bput_attr(b, ')', NULL, 0);
290 do_bput_oattr(struct fastbuf *f, struct oattr *a)
292 for(struct oattr *b=a; b; b=b->same)
293 if (a->attr >= OBJ_ATTR_SON)
295 bput_attr_push(f, a->attr - OBJ_ATTR_SON);
296 bput_object(f, b->son);
303 for (z = b->val; *z; z++)
304 if (*z < ' ' && *z != '\t')
306 log(L_ERROR, "obj_write: Found non-ASCII character %02x in %c%s", *z, a->attr, b->val);
310 bput_attr_str(f, a->attr, b->val);
315 bput_oattr(struct fastbuf *f, struct oattr *a)
322 bput_object(struct fastbuf *f, struct odes *d)
324 for(struct oattr *a=d->attrs; a; a=a->next)
329 do_bput_oattr_nocheck(struct fastbuf *f, struct oattr *a)
331 for(struct oattr *b=a; b; b=b->same)
332 if (a->attr >= OBJ_ATTR_SON)
334 bput_attr_push(f, a->attr - OBJ_ATTR_SON);
335 bput_object_nocheck(f, b->son);
339 bput_attr_large(f, a->attr, b->val, strlen(b->val));
343 bput_oattr_nocheck(struct fastbuf *f, struct oattr *a)
346 do_bput_oattr_nocheck(f, a);
350 bput_object_nocheck(struct fastbuf *f, struct odes *d)
352 for(struct oattr *a=d->attrs; a; a=a->next)
353 do_bput_oattr_nocheck(f, a);
357 obj_write(struct fastbuf *b, struct odes *o, uns bucket_type)
359 put_attr_set_type(bucket_type);
364 obj_write_nocheck(struct fastbuf *b, struct odes *o, uns bucket_type)
366 put_attr_set_type(bucket_type);
367 bput_object_nocheck(b, o);