Debian Codegraph @

src/XS.xs

raw
1 #include "xh_config.h"
2 #include "xh_core.h"
3
4 MODULE = XML::Hash::XS PACKAGE = XML::Hash::XS
5
6 PROTOTYPES: DISABLE
7
8 xh_opts_t *
9 new(CLASS,...)
10 PREINIT:
11 xh_opts_t *opts;
12 CODE:
13 dXCPT;
14
15 if ((opts = xh_create_opts()) == NULL)
16 croak("Malloc error in new()");
17
18 XCPT_TRY_START
19 {
20 xh_parse_param(opts, 1, ax, items);
21 } XCPT_TRY_END
22
23 XCPT_CATCH
24 {
25 xh_destroy_opts(opts);
26 XCPT_RETHROW;
27 }
28
29 RETVAL = opts;
30 OUTPUT:
31 RETVAL
32
33 SV *
34 hash2xml(...)
35 PREINIT:
36 xh_h2x_ctx_t ctx;
37 SV *result;
38 CODE:
39 dXCPT;
40 XCPT_TRY_START
41 {
42 xh_h2x_init_ctx(&ctx, ax, items);
43
44 /* hack */
45 #ifdef XH_HAVE_DOM
46 if (ctx.opts.doc) {
47 result = xh_h2d(&ctx);
48 }
49 else {
50 result = xh_h2x(&ctx);
51 }
52 #else
53 result = xh_h2x(&ctx);
54 #endif
55 } XCPT_TRY_END
56
57 XCPT_CATCH
58 {
59 xh_h2x_destroy_ctx(&ctx);
60 XCPT_RETHROW;
61 }
62
63 if (ctx.opts.output != NULL || ctx.opts.output_cb != NULL) result = NULL;
64
65 xh_h2x_destroy_ctx(&ctx);
66
67 if (result == NULL) XSRETURN_UNDEF;
68
69 RETVAL = result;
70
71 OUTPUT:
72 RETVAL
73
74 SV *
75 xml2hash(...)
76 PREINIT:
77 xh_x2h_ctx_t ctx;
78 SV *result;
79 CODE:
80 dXCPT;
81 XCPT_TRY_START
82 {
83 xh_x2h_init_ctx(&ctx, ax, items);
84
85 result = xh_x2h(&ctx);
86 } XCPT_TRY_END
87
88 XCPT_CATCH
89 {
90 xh_x2h_destroy_ctx(&ctx);
91 XCPT_RETHROW;
92 }
93
94 if (ctx.opts.cb != NULL) result = NULL;
95
96 xh_x2h_destroy_ctx(&ctx);
97
98 if (result == NULL) XSRETURN_UNDEF;
99
100 RETVAL = result;
101
102 OUTPUT:
103 RETVAL
104
105 void
106 DESTROY(opts)
107 xh_opts_t *opts;
108 CODE:
109 xh_destroy_opts(opts);
110 free(opts);
111
112 MODULE = XML::Hash::XS PACKAGE = XML::Hash::XS::Parser
113
114 SV *
115 new(CLASS,...)
116 SV *CLASS;
117 PREINIT:
118 xh_x2h_stream_t *stream;
119 CODE:
120 dXCPT;
121
122 if ((stream = malloc(sizeof(*stream))) == NULL)
123 croak("Malloc error in XML::Hash::XS::Parser->new()");
124 memset(stream, 0, sizeof(*stream));
125
126 XCPT_TRY_START
127 {
128 xh_x2h_stream_init(stream, NULL, ax, items);
129 } XCPT_TRY_END
130
131 XCPT_CATCH
132 {
133 xh_x2h_stream_destroy(stream);
134 free(stream);
135 XCPT_RETHROW;
136 }
137
138 RETVAL = newRV_noinc(newSViv(PTR2IV(stream)));
139 sv_bless(RETVAL, gv_stashsv(CLASS, GV_ADD));
140 OUTPUT:
141 RETVAL
142
143 void
144 feed(parser, input)
145 SV *parser;
146 SV *input;
147 PREINIT:
148 xh_x2h_stream_t *stream;
149 xh_char_t *data;
150 STRLEN len;
151 CODE:
152 dXCPT;
153 if (!sv_isa(parser, "XML::Hash::XS::Parser"))
154 croak("feed: parser is not of type XML::Hash::XS::Parser");
155 stream = INT2PTR(xh_x2h_stream_t *, SvIV(SvRV(parser)));
156 if (stream == NULL)
157 croak("feed: parser is already destroyed");
158 if (SvROK(input)) input = SvRV(input);
159 data = XH_CHAR_CAST SvPV(input, len);
160 SvREFCNT_inc(parser);
161 XCPT_TRY_START
162 {
163 xh_x2h_stream_feed(stream, data, (size_t) len, FALSE);
164 } XCPT_TRY_END
165 XCPT_CATCH
166 {
167 stream->busy = FALSE;
168 stream->failed = TRUE;
169 SvREFCNT_dec(parser);
170 XCPT_RETHROW;
171 }
172 SvREFCNT_dec(parser);
173
174 SV *
175 finish(parser)
176 SV *parser;
177 PREINIT:
178 xh_x2h_stream_t *stream;
179 CODE:
180 dXCPT;
181 if (!sv_isa(parser, "XML::Hash::XS::Parser"))
182 croak("finish: parser is not of type XML::Hash::XS::Parser");
183 stream = INT2PTR(xh_x2h_stream_t *, SvIV(SvRV(parser)));
184 if (stream == NULL)
185 croak("finish: parser is already destroyed");
186 SvREFCNT_inc(parser);
187 XCPT_TRY_START
188 {
189 RETVAL = xh_x2h_stream_finish(stream);
190 } XCPT_TRY_END
191 XCPT_CATCH
192 {
193 stream->busy = FALSE;
194 stream->failed = TRUE;
195 SvREFCNT_dec(parser);
196 XCPT_RETHROW;
197 }
198 SvREFCNT_dec(parser);
199 OUTPUT:
200 RETVAL
201
202 void
203 DESTROY(parser)
204 SV *parser;
205 PREINIT:
206 xh_x2h_stream_t *stream;
207 CODE:
208 stream = INT2PTR(xh_x2h_stream_t *, SvIV(SvRV(parser)));
209 if (stream != NULL) {
210 xh_x2h_stream_destroy(stream);
211 free(stream);
212 sv_setiv(SvRV(parser), 0);
213 }