]> mj.ucw.cz Git - libucw.git/blob - xml/common.c
Renamed uns -> uint
[libucw.git] / xml / common.c
1 /*
2  *      Sherlock Library -- A simple XML parser
3  *
4  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include <ucw/lib.h>
13 #include <xml/xml.h>
14 #include <xml/dtd.h>
15 #include <xml/internals.h>
16 #include <ucw/stkstring.h>
17 #include <ucw/ff-unicode.h>
18
19 #include <setjmp.h>
20
21 /*** Error handling ***/
22
23 void NONRET
24 xml_throw(struct xml_context *ctx)
25 {
26   ASSERT(ctx->err_code && ctx->throw_buf);
27   longjmp(*(jmp_buf *)ctx->throw_buf, ctx->err_code);
28 }
29
30 void
31 xml_warn(struct xml_context *ctx, const char *format, ...)
32 {
33   if (ctx->h_warn)
34     {
35       va_list args;
36       va_start(args, format);
37       ctx->err_msg = stk_vprintf(format, args);
38       ctx->err_code = XML_ERR_WARN;
39       va_end(args);
40       ctx->h_warn(ctx);
41       ctx->err_msg = NULL;
42       ctx->err_code = XML_ERR_OK;
43     }
44 }
45
46 void
47 xml_error(struct xml_context *ctx, const char *format, ...)
48 {
49   if (ctx->h_error)
50     {
51       va_list args;
52       va_start(args, format);
53       ctx->err_msg = stk_vprintf(format, args);
54       ctx->err_code = XML_ERR_ERROR;
55       va_end(args);
56       ctx->h_error(ctx);
57       ctx->err_msg = NULL;
58       ctx->err_code = XML_ERR_OK;
59     }
60 }
61
62 void NONRET
63 xml_fatal(struct xml_context *ctx, const char *format, ...)
64 {
65   va_list args;
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;
70   va_end(args);
71   if (ctx->h_fatal)
72     ctx->h_fatal(ctx);
73   xml_throw(ctx);
74 }
75
76 /*** Memory management ***/
77
78 void *
79 xml_hash_new(struct mempool *pool, uint size)
80 {
81   void *tab = mp_alloc_zero(pool, size + XML_HASH_HDR_SIZE);
82   *(void **)tab = pool;
83   return tab + XML_HASH_HDR_SIZE;
84 }
85
86 /*** Initialization ***/
87
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,
92   .chars = {
93     .name = "<xml_chars>",
94     .spout = xml_spout_chars,
95     .can_overwrite_buffer = 1,
96   },
97 };
98
99 static void
100 xml_do_init(struct xml_context *ctx)
101 {
102   xml_attrs_table_init(ctx);
103 }
104
105 void
106 xml_init(struct xml_context *ctx)
107 {
108   *ctx = xml_defaults;
109   ctx->pool = mp_new(65536);
110   ctx->stack = mp_new(65536);
111   xml_do_init(ctx);
112   TRACE(ctx, "init");
113 }
114
115 void
116 xml_cleanup(struct xml_context *ctx)
117 {
118   TRACE(ctx, "cleanup");
119   xml_attrs_table_cleanup(ctx);
120   xml_dtd_cleanup(ctx);
121   xml_sources_cleanup(ctx);
122   mp_delete(ctx->pool);
123   mp_delete(ctx->stack);
124 }
125
126 void
127 xml_reset(struct xml_context *ctx)
128 {
129   TRACE(ctx, "reset");
130   struct mempool *pool = ctx->pool, *stack = ctx->stack;
131   xml_attrs_table_cleanup(ctx);
132   xml_dtd_cleanup(ctx);
133   xml_sources_cleanup(ctx);
134   mp_flush(pool);
135   mp_flush(stack);
136   *ctx = xml_defaults;
137   ctx->pool = pool;
138   ctx->stack = stack;
139   xml_do_init(ctx);
140 }