2 * Sherlock Library -- A simple XML parser
4 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 #include "sherlock/sherlock.h"
13 #include "sherlock/xml/xml.h"
14 #include "sherlock/xml/dtd.h"
15 #include "sherlock/xml/common.h"
16 #include "lib/stkstring.h"
17 #include "lib/ff-unicode.h"
21 /*** Error handling ***/
24 xml_throw(struct xml_context *ctx)
26 ASSERT(ctx->err_code && ctx->throw_buf);
27 longjmp(*(jmp_buf *)ctx->throw_buf, ctx->err_code);
31 xml_warn(struct xml_context *ctx, const char *format, ...)
36 va_start(args, format);
37 ctx->err_msg = stk_vprintf(format, args);
38 ctx->err_code = XML_ERR_WARN;
42 ctx->err_code = XML_ERR_OK;
47 xml_error(struct xml_context *ctx, const char *format, ...)
52 va_start(args, format);
53 ctx->err_msg = stk_vprintf(format, args);
54 ctx->err_code = XML_ERR_ERROR;
58 ctx->err_code = XML_ERR_OK;
63 xml_fatal(struct xml_context *ctx, const char *format, ...)
66 va_start(args, format);
67 ctx->err_msg = mp_vprintf(ctx->stack, format, args);
68 ctx->err_code = XML_ERR_FATAL;
69 ctx->state = XML_STATE_EOF;
76 /*** Memory management ***/
79 xml_hash_new(struct mempool *pool, uns size)
81 void *tab = mp_alloc_zero(pool, size + XML_HASH_HDR_SIZE);
83 return tab + XML_HASH_HDR_SIZE;
87 xml_chars_spout(struct fastbuf *fb)
89 if (fb->bptr >= fb->bufend)
91 struct xml_context *ctx = SKIP_BACK(struct xml_context, chars, fb);
92 struct mempool *pool = ctx->pool;
93 if (fb->bufend != fb->buffer)
95 uns len = fb->bufend - fb->buffer;
96 TRACE(ctx, "grow_chars");
97 fb->buffer = mp_expand(pool);
98 fb->bufend = fb->buffer + mp_avail(pool);
99 fb->bstop = fb->buffer;
100 fb->bptr = fb->buffer + len;
104 TRACE(ctx, "push_chars");
105 struct xml_node *n = xml_push_dom(ctx);
106 n->type = XML_NODE_CHARS;
107 xml_start_chars(ctx);
113 xml_init_chars(struct xml_context *ctx)
115 struct fastbuf *fb = &ctx->chars;
116 fb->name = "<xml-chars>";
117 fb->spout = xml_chars_spout;
118 fb->can_overwrite_buffer = 1;
119 fb->bptr = fb->bstop = fb->buffer = fb->bufend = NULL;
122 /*** Initialization ***/
125 xml_do_init(struct xml_context *ctx)
127 ctx->flags = XML_REPORT_ALL;
129 xml_attrs_table_init(ctx);
133 xml_init(struct xml_context *ctx)
135 bzero(ctx, sizeof(*ctx));
136 ctx->pool = mp_new(65536);
137 ctx->stack = mp_new(65536);
143 xml_cleanup(struct xml_context *ctx)
145 TRACE(ctx, "cleanup");
146 xml_attrs_table_cleanup(ctx);
147 xml_dtd_cleanup(ctx);
148 xml_sources_cleanup(ctx);
149 mp_delete(ctx->pool);
150 mp_delete(ctx->stack);
154 xml_reset(struct xml_context *ctx)
157 struct mempool *pool = ctx->pool, *stack = ctx->stack;
158 xml_attrs_table_cleanup(ctx);
159 xml_dtd_cleanup(ctx);
160 xml_sources_cleanup(ctx);
163 bzero(ctx, sizeof(*ctx));