2 * UCW 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.
13 #include <ucw-xml/xml.h>
14 #include <ucw-xml/dtd.h>
15 #include <ucw-xml/internals.h>
16 #include <ucw/stkstring.h>
17 #include <ucw/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, uint size)
81 void *tab = mp_alloc_zero(pool, size + XML_HASH_HDR_SIZE);
83 return tab + XML_HASH_HDR_SIZE;
86 /*** Initialization ***/
88 static struct xml_context xml_defaults = {
89 .flags = XML_SRC_EOF | XML_REPORT_ALL,
90 .state = XML_STATE_START,
91 .h_resolve_entity = xml_def_resolve_entity,
93 .name = "<xml_chars>",
94 .spout = xml_spout_chars,
95 .can_overwrite_buffer = 1,
100 xml_init(struct xml_context *ctx)
103 ctx->pool = mp_new(65536);
104 ctx->stack = mp_new(65536);
109 xml_cleanup(struct xml_context *ctx)
111 TRACE(ctx, "cleanup");
112 xml_dtd_cleanup(ctx);
113 xml_sources_cleanup(ctx);
115 mp_delete(ctx->pool);
116 mp_delete(ctx->stack);
120 xml_reset(struct xml_context *ctx)
123 struct mempool *pool = ctx->pool, *stack = ctx->stack, *ns_pool = ctx->ns_pool;
124 const char **ns_by_id = ctx->ns_by_id;
125 xml_dtd_cleanup(ctx);
126 xml_sources_cleanup(ctx);
132 ctx->ns_pool = ns_pool;
133 ctx->ns_by_id = ns_by_id;