2 * Sherlock Library -- Nested Object Reading Functions
4 * (c) 2005 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #ifndef _SHERLOCK_OBJREAD_H
11 #define _SHERLOCK_OBJREAD_H
13 #include "sherlock/object.h"
15 struct obj_read_state {
16 struct odes *root, *obj;
17 void (*error_callback)(struct obj_read_state *st, char *msg);
22 void default_obj_read_error(struct obj_read_state *st, char *msg);
25 obj_read_start(struct obj_read_state *st, struct odes *obj)
27 st->root = st->obj = obj;
29 st->error_callback = default_obj_read_error;
33 obj_read_push(struct obj_read_state *st, byte *ptr)
35 if (unlikely(!ptr[0] || ptr[1]))
38 st->error_callback(st, "Malformed object: bad `(' attribute");
41 st->obj = obj_add_son(st->obj, ptr[0] + OBJ_ATTR_SON);
45 obj_read_pop(struct obj_read_state *st, byte *ptr)
50 st->error_callback(st, "Malformed object: bad ')' attribute");
52 else if (unlikely(!st->obj->parent))
55 st->error_callback(st, "Malformed object: improper nesting of `( ... )' blocks");
58 st->obj = st->obj->parent;
62 obj_read_attr(struct obj_read_state *st, uns type, byte *val)
65 obj_read_push(st, val);
67 obj_read_pop(st, val);
69 obj_add_attr(st->obj, type, val);
73 obj_read_attr_ref(struct obj_read_state *st, uns type, byte *val)
76 obj_read_push(st, val);
78 obj_read_pop(st, val);
80 obj_add_attr_ref(st->obj, type, val);
84 obj_read_end(struct obj_read_state *st)
86 if (unlikely(st->obj != st->root))
89 st->error_callback(st, "Malformed object: truncated `( ... )' block");