Fast.xs

filter: Statementstmt 56.0% 331 / 591 Branchbran 41.4% 248 / 598 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 48.7% 579 / 1189 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/XML/Fast.pm ↑ Summary xmlfast.c »
1 #include "EXTERN.h"
2  
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 #define XML_DEBUG 0
9 #define XML_DEVEL 0
10  
11 #include "xmlfast.h"
12  
13 #if XML_DEBUG
14 #define WHERESTR " at %s line %d.\n"
15 #define WHEREARG __FILE__, __LINE__
16 #define debug(...) do{ fprintf(stderr, __VA_ARGS__); fprintf(stderr, WHERESTR, WHEREARG); } while(0)
17 #else
18 #define debug(...)
19 #endif
20  
21  
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <strings.h>
26  
27 #ifndef ptr_t
28 typedef void * ptr_t;
29 #endif
30  
31 #ifndef PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8
32 #define PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8
33 #endif
34  
35 typedef struct {
36 char *name;
37 unsigned int len;
38 char *fullname;
39 unsigned int fulllen;
40 } xml_node;
41  
42 /*
43 commit 30866c9f74d890c45e8da27ea855468a314a59cf
44 xmlbare 1785/s -- -19%
45 xmlfast 2209/s 24% --
46  
47 */
48  
49 #define UTF8_BYTES 1
50 #define UTF8_UPGRADE 2
51 #define UTF8_DECODE 3
52 #define UTF8_EDECODE 4
53  
54 #define EMIT_WARNS 0x0001
55 #define TAG_MATCH 0x0002
56  
57 #define MODE_ORDER 0x1000
58 #define MODE_TRIM 0x2000
59 #define MODE_ARRAYS 0x4000
60  
61 typedef struct {
62 // config
63 unsigned int flags;
64 unsigned int bytes;
65 unsigned int utf8;
66 SV * attr;
67 SV * text;
68 SV * join;
69 SV * cdata;
70 SV * comm;
71 HV * array;
72  
73 // state
74 char *encoding;
75 SV *encode;
76 int depth;
77 unsigned int chainsize;
78 xml_node * chain;
79 HV ** hchain;
80 HV * hcurrent; //just a pointer
81  
82 SV * pi;
83 SV * attrname;
84 SV * textval;
85
86 SV * error;
87 parser_state * state;
88
89 } parsestate;
90  
91 typedef struct {
92 // config
93 unsigned int flags;
94 unsigned int bytes;
95 unsigned int utf8;
96
97 char * attr; STRLEN attl;
98 char * text;
99 char * join;
100 char * cdata;
101 char * comm;
102
103
104 HV * array;
105  
106 // state
107 char *encoding;
108 SV *encode;
109 unsigned int chainsize;
110 xml_node * chain;
111 HV ** hchain;
112 HV * hcurrent; //just a pointer
113  
114 SV * pi;
115 SV * attrname;
116 SV * textval;
117
118 SV * error;
119
120 /// new
121 I32 depth;
122 I32 ix;
123 SV * result;
124 SV * nested;
125
126
127 } compstate;
128  
129 // hv_store to array if already have non-array value
130 #define hv_store_a( hv, key, sv ) \
131 STMT_START { \
132 SV **exists; \
133 char *kv = SvPV_nolen(key); \
134 int kl = SvCUR(key); \
135 if( exists = hv_fetch(hv, kv, kl, 0) ) { \
136 if ( SvROK(*exists) && SvTYPE( SvRV(*exists) ) == SVt_PVAV) { \
137 AV *av = (AV *) SvRV( *exists ); \
138 av_push( av, sv ); \
139 } \
140 else { \
141 AV *av = newAV(); \
142 if (SvROK(*exists)) { \
143 SvREFCNT_inc(*exists); \
144 av_push( av, *exists ); \
145 } else { \
146 SV *old = newSV(0); \
147 sv_copypv(old, *exists); \
148 av_push( av, old ); \
149 } \
150 av_push( av, sv ); \
151 (void) hv_store( hv, kv, kl, newRV_noinc( (SV *) av ), 0 ); \
152 } \
153 } else { \
154 (void) hv_store(hv, kv, kl, sv, 0); \
155 } \
156 } STMT_END
157  
158 // hv_store to array, create if not exists
159 #define hv_store_aa( hv, key, sv ) \
160 STMT_START { \
161 SV **exists; \
162 char *kv = SvPV_nolen(key); \
163 int kl = SvCUR(key); \
164 if( ( exists = hv_fetch(hv, kv, kl, 0) ) && SvROK(*exists) && (SvTYPE( SvRV(*exists) ) == SVt_PVAV) ) { \
165 AV *av = (AV *) SvRV( *exists ); \
166 av_push( av, sv ); \
167 } \
168 else { \
169 AV *av = newAV(); \
170 av_push( av, sv ); \
171 (void) hv_store( hv, kv, kl, newRV_noinc( (SV *) av ), 0 ); \
172 } \
173 } STMT_END
174  
175 #define xml_sv_decode(ctx, sv) \
176 STMT_START { \
177 if (!ctx->bytes && !SvUTF8(sv)) {\
178 if (ctx->utf8 == UTF8_UPGRADE) { \
179 SvUTF8_on(sv); \
180 } \
181 else if (ctx->utf8 == UTF8_DECODE) { \
182 sv_utf8_decode(sv); \
183 } \
184 else if (ctx->encode) { \
185 (void) sv_recode_to_utf8(sv, ctx->encode); \
186 } \
187 }\
188 } STMT_END
189  
190 void on_bytes_charset_part(void * pctx, char * data, unsigned int length);
191 void on_bytes_charset(void * pctx, char * data, unsigned int length);
192 void on_tag_close(void * pctx, char * data, unsigned int length);
193  
194 38 static inline void my_warn(parsestate *ctx, char * format, ...) {
195 //TODO: free all
196 38 ▶ if(!(ctx->flags & EMIT_WARNS)) return;
Branch: 1 branch
BranchTrueFalseTotal
if(!(ctx->flags & EMIT_WARNS)) return; 1 37 38
197 va_list va;
198 37 va_start(va,format);
199 37 SV *text = sv_2mortal(newSVpvn("",0));
200 37 sv_vcatpvf(text, format, &va);
201 37 va_end(va);
202 37 warn("%s",SvPV_nolen(text));
203 }
204  
205 52 static inline void DESTROY (parsestate *ctx) {
206 52 ▶ if(ctx->encode) { SvREFCNT_dec(ctx->encode); ctx->encode = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->encode) { SvREFCNT_dec(ctx->encode); ctx->encode = 0; } 6 46 52
207 52 ▶ if(ctx->textval) { SvREFCNT_dec(ctx->textval); ctx->textval = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->textval) { SvREFCNT_dec(ctx->textval); ctx->textval = 0; } 0 52 52
208 52 ▶ if(ctx->pi) { SvREFCNT_dec(ctx->pi); ctx->pi = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->pi) { SvREFCNT_dec(ctx->pi); ctx->pi = 0; } 2 50 52
209 52 ▶ if(ctx->attrname) { SvREFCNT_dec(ctx->attrname); ctx->attrname = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->attrname) { SvREFCNT_dec(ctx->attrname); ctx->attrname = 0; } 2 50 52
210 52 ▶ if(ctx->array) { SvREFCNT_dec(ctx->array); ctx->array = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->array) { SvREFCNT_dec(ctx->array); ctx->array = 0; } 2 50 52
211 52 ▶ if(ctx->depth > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->depth > -1) { 3 49 52
212 //fprintf(stderr,"DESTROY Free depth %d\n",ctx->depth);
213 3 int currdepth = ctx->depth;
214 6 ▶ while(ctx->depth > -1) {
Branch: 1 branch
BranchTrueFalseTotal
while(ctx->depth > -1) { 3 3 6
215 //fprintf(stderr,"Free depth %d\n",ctx->depth);
216 3 on_tag_close(ctx,ctx->chain->name,ctx->chain->len);
217 3 ▶ if (currdepth == ctx->depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (currdepth == ctx->depth) { 0 3 3
218 0 my_warn(ctx,"Recursion during autoclose tags. depth=%d\n",ctx->depth);
219 0 break;
220 }
221 }
222 }
223 52 ▶ if (ctx->hchain) { Safefree(ctx->hchain); ctx->hchain = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->hchain) { Safefree(ctx->hchain); ctx->hchain = 0; } 52 0 52
224 52 ▶ if (ctx->chain) { Safefree(ctx->chain); ctx->chain = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->chain) { Safefree(ctx->chain); ctx->chain = 0; } 52 0 52
225 52 }
226  
227 0 static inline void my_croak(parsestate *ctx, char * format, ...) {
228 //TODO: free all
229 0 DESTROY(ctx);
230 va_list va;
231 0 va_start(va,format);
232 0 SV *text = sv_2mortal(newSVpvn("",0));
233 0 sv_vcatpvf(text, format, &va);
234 0 va_end(va);
235 0 croak("%s",SvPV_nolen(text));
236 }
237  
238 6 SV * find_encoding(char * encoding) {
239 6 dSP;
240 int count;
241 //require_pv("Encode.pm");
242
243 6 ENTER;
244 6 SAVETMPS;
245
246 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
247 6 ▶ XPUSHs(sv_2mortal(newSVpv(encoding, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(encoding, 0))); 0 6 6
248 6 PUTBACK;
249
250 6 count = call_pv("Encode::find_encoding",G_SCALAR);
251
252 6 SPAGAIN;
253 6 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 6 0 6
if (SvTRUE(ERRSV)) { 0 6 6
254 0 ▶ warn("Shit happens: %s\n", SvPV_nolen(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
warn("Shit happens: %s\n", SvPV_nolen(ERRSV)); 0 0 0
255 0 (void) POPs;
256 }
257
258 6 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 6 6
259 0 croak("find_encoding fault: bad number of returned values: %d",count);
260
261 6 SV *encode = POPs;
262 6 SvREFCNT_inc(encode);
263
264 6 PUTBACK;
265
266 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
267 6 LEAVE;
268
269 6 return encode;
270 }
271  
272 0 SV * get_constant(char * name) {
273 0 dSP;
274 int count;
275 //require_pv("Encode.pm");
276
277 0 ENTER;
278 0 SAVETMPS;
279
280 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
281 0 PUTBACK;
282
283 0 count = call_pv(name,G_SCALAR);
284
285 0 SPAGAIN;
286
287 0 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 0 0
288 0 croak("Bad number of returned values: %d",count);
289
290 0 SV *value = POPs;
291 0 sv_dump(value);
292 0 SvREFCNT_inc(value);
293
294 0 PUTBACK;
295
296 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
297 0 LEAVE;
298
299 0 return value;
300 }
301  
302 2 SV * sv_recode_from_utf8(pTHX_ SV *sv, SV *encoding) {
303 dVAR;
304  
305 PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
306 2 ▶ if (SvPOK(sv) && SvUTF8(sv) && SvROK(encoding)) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvPOK(sv) && SvUTF8(sv) && SvROK(encoding)) { 2 0 2
if (SvPOK(sv) && SvUTF8(sv) && SvROK(encoding)) { 2 0 2
if (SvPOK(sv) && SvUTF8(sv) && SvROK(encoding)) { 2 0 2
307 SV *bytes;
308 2 dSP;
309 2 ENTER;
310 2 SAVETMPS;
311 2 save_re_context();
312 2 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 2 2
313 2 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 2 2
314 2 ▶ XPUSHs(encoding);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(encoding); 0 2 2
315 2 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 2 2
316 2 ▶ XPUSHs(sv_2mortal(newSViv(4)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(4))); 0 2 2
317
318 2 PUTBACK;
319 2 call_method("encode", G_SCALAR);
320 2 SPAGAIN;
321 2 bytes = POPs;
322 2 SvREFCNT_inc(bytes);
323 2 PUTBACK;
324
325 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
326 2 LEAVE;
327 2 return bytes;
328 }
329 0 ▶ return SvPOKp(sv) ? sv : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvPOKp(sv) ? sv : NULL; 0 0 0
330 }
331  
332 0 void on_comment(void * pctx, char * data,unsigned int length) {
333 #if XML_DEVEL
334 if (!pctx) croak("Context not passed to on_comment");
335 #endif
336 0 parsestate *ctx = pctx;
337 0 SV *sv = newSVpvn(data, length);
338 0 ▶ hv_store_a(ctx->hcurrent, ctx->comm, sv );
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, ctx->comm, sv ); 0 0 0
hv_store_a(ctx->hcurrent, ctx->comm, sv ); 0 0 0
hv_store_a(ctx->hcurrent, ctx->comm, sv ); 0 0 0
hv_store_a(ctx->hcurrent, ctx->comm, sv ); 0 0 0
339 0 }
340  
341 //TODO: Separate on_bytes/on_uchar for non-utf mode
342  
343 17 void on_pi_attr(parsestate *ctx) {
344 //warn("pi attr '%-.*s'", SvCUR(ctx->attrname), SvPVX(ctx->attrname));
345 17 if (
346 17 ▶ (SvCUR(ctx->attrname) == 8) &&
Branch: 1 branch
BranchTrueFalseTotal
(SvCUR(ctx->attrname) == 8) && 12 5 17
347 24 ▶ (memcmp(SvPV_nolen(ctx->attrname), "encoding", 8) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
(memcmp(SvPV_nolen(ctx->attrname), "encoding", 8) == 0 ) 12 0 12
348 ) {
349 12 ctx->encoding = (char *) SvPV_nolen(ctx->textval);
350 //printf("Noticed encoding %s\n",ctx->encoding);
351 12 ▶ if ( ( SvCUR(ctx->textval) == 5 ) && ( strncasecmp( ctx->encoding, "utf-8",5 ) == 0 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( ( SvCUR(ctx->textval) == 5 ) && ( strncasecmp( ctx->encoding, "utf-8",5 ) == 0 ) ) { 6 6 12
if ( ( SvCUR(ctx->textval) == 5 ) && ( strncasecmp( ctx->encoding, "utf-8",5 ) == 0 ) ) { 6 0 6
352 6 ▶ if (ctx->bytes) ctx->utf8 = UTF8_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->bytes) ctx->utf8 = UTF8_BYTES; 1 5 6
353 //printf("Native utf-8 mode (%d)\n",ctx->utf8);
354 } else {
355 6 ctx->encode = find_encoding(ctx->encoding);
356 6 ctx->utf8 = 0;
357 6 ▶ if(ctx->text) {
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->text) { 6 0 6
358 //printf("Switch text mode to charset\n");
359 6 ctx->state->cb.bytes = on_bytes_charset;
360 6 ctx->state->cb.bytespart = on_bytes_charset_part;
361 }
362 }
363 } else {
364 //printf("PI %s, attr %s='%s'\n",SvPV_nolen(ctx->pi), SvPV_nolen(ctx->attrname),SvPV_nolen(ctx->textval) );
365 }
366 17 sv_2mortal(ctx->attrname);
367 17 sv_2mortal(ctx->textval);
368 17 ctx->attrname = ctx->textval = 0;
369 17 }
370  
371 2 static inline SV * mkchr(UV chr) {
372 char *end, utf[UTF8_MAXBYTES + 1];
373 /*
374 if (UTF8_IS_INVARIANT(chr)) {
375 utf[0] = (char)chr;
376 utf[1] = 0;
377 return newSVpvn(utf,2);
378 }
379 */
380 SV *tmp;
381 2 end = uvchr_to_utf8(utf, chr);
382 2 *end = '\0';
383 2 tmp = newSVpvn(utf, end-utf);
384 2 SvUTF8_on(tmp);
385 2 return tmp;
386 }
387  
388 5 void on_uchar(void * pctx, wchar_t chr) {
389 #if XML_DEVEL
390 if (!pctx) croak("Context not passed to on_text_part");
391 #endif
392 5 parsestate *ctx = pctx;
393 dTHX;
394 5 ▶ if ( !( ctx->attrname || ctx->text ) ) return;
Branch: 2 branches
BranchTrueFalseTotal
if ( !( ctx->attrname || ctx->text ) ) return; 5 0 5
if ( !( ctx->attrname || ctx->text ) ) return; 0 5 5
395
396 5 ▶ if (!ctx->utf8 && ctx->bytes && !UTF8_IS_INVARIANT(chr) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (!ctx->utf8 && ctx->bytes && !UTF8_IS_INVARIANT(chr) ) { 4 1 5
if (!ctx->utf8 && ctx->bytes && !UTF8_IS_INVARIANT(chr) ) { 2 2 4
if (!ctx->utf8 && ctx->bytes && !UTF8_IS_INVARIANT(chr) ) { 2 0 2
397 2 ▶ if (!ctx->encode)
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->encode) 0 2 2
398 0 my_croak(ctx,"Can't decode entities in non-utf8, bytes mode");
399 2 SV *tmp = mkchr(chr);
400 2 SV *bytes = sv_recode_from_utf8(aTHX_ tmp, ctx->encode);
401 2 ▶ if (SvCUR(bytes) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(bytes) == 0) { 1 1 2
402 1 my_warn(ctx,"Can't recode U+%04d entity into %s in bytes mode", chr, ctx->encoding);
403 1 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 1 0 1
404 1 sv_catpvn(ctx->textval,"?",1);
405 } else {
406 0 ctx->textval = newSVpvn("?",1);
407 }
408 1 sv_2mortal(tmp);
409 1 sv_2mortal(bytes);
410 1 return;
411 }
412 //printf("Created char %s / %s / bytes = %s\n",utf, SvPV_nolen(tmp),SvPV_nolen(bytes));
413 1 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 1 0 1
414 1 sv_catsv(ctx->textval,bytes);
415 1 sv_2mortal(bytes);
416 } else {
417 0 ctx->textval = bytes;
418 }
419 1 return;
420 } else {
421 char *start, *end;
422 3 STRLEN len = 0;
423 3 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 3 0 3
424 3 len = SvCUR(ctx->textval);
425 } else {
426 0 ctx->textval = newSVpvn("",0);
427 }
428 3 sv_grow(ctx->textval, len + UTF8_MAXBYTES + 1 );
429 3 start = end = SvEND(ctx->textval);
430 3 end = uvchr_to_utf8(start, chr);*end = '\0';
431 3 SvCUR_set(ctx->textval,len + end - start);
432 }
433 }
434  
435  
436 6 void on_bytes_charset_part(void * pctx, char * data, unsigned int length) {
437 #if XML_DEVEL
438 if (!pctx) croak("Context not passed to on_bytes_charset_part");
439 #endif
440 6 parsestate *ctx = pctx;
441 6 ▶ if ( !( ctx->attrname || ctx->text ) ) return;
Branch: 2 branches
BranchTrueFalseTotal
if ( !( ctx->attrname || ctx->text ) ) return; 6 0 6
if ( !( ctx->attrname || ctx->text ) ) return; 0 6 6
442 6 ▶ if (!length) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!length) return; 0 6 6
443 6 SV *tmp = newSVpvn(data, length);
444 6 ▶ xml_sv_decode(ctx,tmp);
Branch: 5 branches
BranchTrueFalseTotal
xml_sv_decode(ctx,tmp); 4 2 6
xml_sv_decode(ctx,tmp); 4 0 4
xml_sv_decode(ctx,tmp); 0 4 4
xml_sv_decode(ctx,tmp); 0 4 4
xml_sv_decode(ctx,tmp); 4 0 4
445 6 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 2 4 6
446 2 sv_catsv(ctx->textval, tmp);
447 2 sv_2mortal(tmp);
448 } else {
449 4 ctx->textval = tmp;
450 }
451 }
452  
453 6 void on_bytes_charset(void * pctx, char * data, unsigned int length) {
454 #if XML_DEVEL
455 if (!pctx) croak("Context not passed to on_bytes");
456 #endif
457 6 parsestate *ctx = pctx;
458 //if (!ctx->textval && !length) {
459 // my_warn(ctx,"Called on_bytes with empty text and empty body");
460 //}
461 6 ▶ if ( !( ctx->attrname || ctx->text ) ) return;
Branch: 2 branches
BranchTrueFalseTotal
if ( !( ctx->attrname || ctx->text ) ) return; 6 0 6
if ( !( ctx->attrname || ctx->text ) ) return; 0 6 6
462 6 SV *tmp = newSVpvn(data, length);
463 6 ▶ xml_sv_decode(ctx,tmp);
Branch: 5 branches
BranchTrueFalseTotal
xml_sv_decode(ctx,tmp); 3 3 6
xml_sv_decode(ctx,tmp); 3 0 3
xml_sv_decode(ctx,tmp); 0 3 3
xml_sv_decode(ctx,tmp); 0 3 3
xml_sv_decode(ctx,tmp); 3 0 3
464 6 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 4 2 6
465 4 sv_catsv(ctx->textval, tmp);
466 4 sv_2mortal(tmp);
467 } else {
468 2 ctx->textval = tmp;
469 }
470 6 ▶ if (ctx->attrname) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->attrname) { 0 6 6
471 0 ▶ if (ctx->pi) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->pi) { 0 0 0
472 0 on_pi_attr(ctx);
473 } else {
474 0 ▶ hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval); 0 0 0
475 }
476 0 sv_2mortal(ctx->attrname);
477 0 ctx->attrname = 0;
478 0 ctx->textval = 0;
479 }
480 else {
481 12 ▶ hv_store_a(ctx->hcurrent, ctx->text, ctx->textval);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 6 6
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
482 }
483 6 ctx->textval = 0;
484 }
485  
486 16233 void on_bytes_part(void * pctx, char * data, unsigned int length) {
487 #if XML_DEVEL
488 if (!pctx) croak("Context not passed to on_bytes_part");
489 #endif
490 16233 parsestate *ctx = pctx;
491
492 16233 ▶ if ( !( ctx->attrname || ctx->text ) ) return;
Branch: 2 branches
BranchTrueFalseTotal
if ( !( ctx->attrname || ctx->text ) ) return; 16233 0 16233
if ( !( ctx->attrname || ctx->text ) ) return; 0 16233 16233
493
494 16233 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 16116 117 16233
495 16116 ▶ if (length > 0) { sv_catpvn(ctx->textval, data, length); }
Branch: 1 branch
BranchTrueFalseTotal
if (length > 0) { sv_catpvn(ctx->textval, data, length); } 16116 0 16116
496 } else {
497 117 ctx->textval = newSVpvn(data, length);
498 }
499 }
500  
501 2372 void on_bytes(void * pctx, char * data, unsigned int length) {
502 #if XML_DEVEL
503 if (!pctx) croak("Context not passed to on_bytes");
504 #endif
505 2372 parsestate *ctx = pctx;
506 //if (!ctx->textval && !length) {
507 // my_warn(ctx,"Called on_bytes with empty text and empty body");
508 //}
509 2372 ▶ if ( !( ctx->attrname || ctx->text ) ) return;
Branch: 2 branches
BranchTrueFalseTotal
if ( !( ctx->attrname || ctx->text ) ) return; 2205 167 2372
if ( !( ctx->attrname || ctx->text ) ) return; 0 2205 2205
510
511 2372 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 117 2255 2372
512 117 ▶ if (length > 0) { sv_catpvn(ctx->textval, data, length); }
Branch: 1 branch
BranchTrueFalseTotal
if (length > 0) { sv_catpvn(ctx->textval, data, length); } 49 68 117
513 } else {
514 2255 ctx->textval = newSVpvn(data, length);
515 }
516 2372 ▶ xml_sv_decode(ctx,ctx->textval);
Branch: 5 branches
BranchTrueFalseTotal
xml_sv_decode(ctx,ctx->textval); 2367 5 2372
xml_sv_decode(ctx,ctx->textval); 2367 0 2367
xml_sv_decode(ctx,ctx->textval); 2367 0 2367
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
517 2372 ▶ if (ctx->attrname) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->attrname) { 167 2205 2372
518 167 ▶ if (ctx->pi) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->pi) { 17 150 167
519 17 on_pi_attr(ctx);
520 } else {
521 300 ▶ hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval); 0 150 150
hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->attrname, ctx->textval); 0 0 0
522 }
523 167 sv_2mortal(ctx->attrname);
524 167 ctx->attrname = 0;
525 167 ctx->textval = 0;
526 }
527 else {
528 //printf("text close, store %s\n",SvPV_nolen(ctx->textval));
529 4410 ▶ hv_store_a(ctx->hcurrent, ctx->text, ctx->textval);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 13 2192 2205
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 5 8 13
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 5 0 5
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 8 8
530 }
531 2372 ctx->textval = 0;
532 }
533  
534  
535 1 void on_cdata(void * pctx, char * data,unsigned int length) {
536 #if XML_DEVEL
537 if (!pctx) croak("Context not passed to on_cdata");
538 #endif
539 1 parsestate *ctx = pctx;
540 1 SV *sv = newSVpvn(data, length);
541 1 ▶ xml_sv_decode(ctx,sv);
Branch: 5 branches
BranchTrueFalseTotal
xml_sv_decode(ctx,sv); 1 0 1
xml_sv_decode(ctx,sv); 1 0 1
xml_sv_decode(ctx,sv); 1 0 1
xml_sv_decode(ctx,sv); 0 0 0
xml_sv_decode(ctx,sv); 0 0 0
542 2 ▶ hv_store_a(ctx->hcurrent, ctx->cdata, sv );
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, ctx->cdata, sv ); 0 1 1
hv_store_a(ctx->hcurrent, ctx->cdata, sv ); 0 0 0
hv_store_a(ctx->hcurrent, ctx->cdata, sv ); 0 0 0
hv_store_a(ctx->hcurrent, ctx->cdata, sv ); 0 0 0
543 1 }
544  
545 16 void on_pi_open(void * pctx, char * data, unsigned int length) {
546 #if XML_DEVEL
547 if (!pctx) croak("Context not passed to on_pi_open");
548 #endif
549 16 parsestate *ctx = pctx;
550 16 ctx->pi = newSVpvn(data,length);
551 16 }
552  
553 14 void on_pi_close(void * pctx, char * data, unsigned int length) {
554 #if XML_DEVEL
555 if (!pctx) croak("Context not passed to on_pi_close");
556 #endif
557 14 parsestate *ctx = pctx;
558 14 sv_2mortal(ctx->pi);
559 14 ctx->pi = 0;
560 14 }
561  
562 2371 void on_tag_open(void * pctx, char * data, unsigned int length) {
563 #if XML_DEVEL
564 if (!pctx) croak("Context not passed to on_tag_open");
565 #endif
566 2371 parsestate *ctx = pctx;
567 2371 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 0 2371 2371
568 0 ▶ xml_sv_decode(ctx,ctx->textval);
Branch: 5 branches
BranchTrueFalseTotal
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
569 0 ▶ hv_store_a(ctx->hcurrent, ctx->text, ctx->textval);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
570 0 ctx->textval = 0;
571 }
572 2371 HV * hv = newHV();
573 2371 ctx->depth++;
574 2371 ▶ if (ctx->depth >= ctx->chainsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth >= ctx->chainsize) { 0 2371 2371
575 0 warn("XML depth too high. Consider increasing `_max_depth' to at more than %d to avoid reallocations",ctx->chainsize);
576 0 ctx->chainsize *= 2;
577 0 Renew( ctx->hchain, ctx->chainsize, HV* );
578 0 Renew( ctx->chain, ctx->chainsize, xml_node);
579 }
580 2371 ctx->chain[ctx->depth].len = length;
581 2371 ctx->chain[ctx->depth].name = data;
582 2371 ▶ if (ctx->flags & TAG_MATCH) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->flags & TAG_MATCH) { 0 2371 2371
583 0 ▶ if (ctx->depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth == 0) { 0 0 0
584 0 ctx->chain[ctx->depth].fulllen = length + 1;
585 0 Newx(ctx->chain[ctx->depth].fullname, ctx->chain[ctx->depth].fulllen + 1, char);
586 0 ctx->chain[ctx->depth].fullname[0] = '/';
587 0 memcpy(ctx->chain[ctx->depth].fullname+1,data,length);
588 0 ctx->chain[ctx->depth].fullname[length+1] = 0;
589 //printf("Fullame = %s\n",ctx->chain[ctx->depth].fullname);
590 } else {
591 0 ctx->chain[ctx->depth].fulllen = ctx->chain[ctx->depth - 1].fulllen + length + 1;
592 0 Newx(ctx->chain[ctx->depth].fullname, ctx->chain[ctx->depth].fulllen + 1, char);
593 0 memcpy(
594 0 ctx->chain[ctx->depth].fullname,
595 0 ctx->chain[ctx->depth - 1].fullname,
596 0 ctx->chain[ctx->depth - 1].fulllen
597 );
598 0 ctx->chain[ctx->depth].fullname[ ctx->chain[ctx->depth - 1].fulllen ] = '/';
599 0 memcpy(
600 0 ctx->chain[ctx->depth].fullname + ctx->chain[ctx->depth - 1].fulllen + 1,
601 data,
602 length
603 );
604 0 ctx->chain[ctx->depth].fullname[ ctx->chain[ctx->depth].fulllen ] = 0;
605 //printf("Fullame = %s\n",ctx->chain[ctx->depth].fullname);
606 }
607 }
608 //printf("node name=%s, fullname=%s\n", SvPV_nolen(ctx->name[ ctx->depth ]),SvPV_nolen(fname));
609 2371 ctx->hchain[ ctx->depth ] = ctx->hcurrent;
610 2371 ctx->hcurrent = hv;
611 2371 }
612  
613 2373 void on_tag_close(void * pctx, char * data, unsigned int length) {
614 #if XML_DEVEL
615 if (!pctx) croak("Context not passed to on_tag_close");
616 #endif
617 2373 parsestate *ctx = pctx;
618 2373 SV *tag = sv_2mortal(newSVpvn(data,length));
619
620 SV **text;
621 2373 ▶ I32 keys = HvKEYS(ctx->hcurrent);
Branch: 1 branch
BranchTrueFalseTotal
I32 keys = HvKEYS(ctx->hcurrent); 0 2373 2373
622 2373 SV *svtext = 0;
623 2373 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 0 2373 2373
624 0 ▶ xml_sv_decode(ctx,ctx->textval);
Branch: 5 branches
BranchTrueFalseTotal
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
xml_sv_decode(ctx,ctx->textval); 0 0 0
625 0 ▶ hv_store_a(ctx->hcurrent, ctx->text, ctx->textval);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
hv_store_a(ctx->hcurrent, ctx->text, ctx->textval); 0 0 0
626 0 ctx->textval = 0;
627 }
628 2373 ▶ if (ctx->depth < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth < 0) { 0 2373 2373
629 0 my_warn(ctx,"Ignore unbalanced tag: closed upper than root");
630 0 return;
631 }
632 2373 ▶ if ( (ctx->chain[ctx->depth].len != length) || (memcmp( ctx->chain[ctx->depth].name, data, length) != 0) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( (ctx->chain[ctx->depth].len != length) || (memcmp( ctx->chain[ctx->depth].name, data, length) != 0) ) { 2371 2 2373
if ( (ctx->chain[ctx->depth].len != length) || (memcmp( ctx->chain[ctx->depth].name, data, length) != 0) ) { 0 2371 2371
633 2 int close, depth = ctx->depth;
634 2 my_warn(ctx,"Unbalanced close tag <%s> depth=%d\n", SvPV_nolen(tag),depth);
635 // checkdepth is used to avoid infinite loops on errors with auto_closing
636 2 int checkdepth = depth + 10;
637 35 ▶ while (depth > -1 && checkdepth-- > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (depth > -1 && checkdepth-- > 0) { 35 0 35
while (depth > -1 && checkdepth-- > 0) { 35 0 35
638 35 ▶ if ( (ctx->chain[depth].len == length) && (memcmp( ctx->chain[depth].name, data, length) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ( (ctx->chain[depth].len == length) && (memcmp( ctx->chain[depth].name, data, length) == 0)) { 2 33 35
if ( (ctx->chain[depth].len == length) && (memcmp( ctx->chain[depth].name, data, length) == 0)) { 2 0 2
639 37 ▶ for (close = ctx->depth; close >= depth; close--) {
Branch: 1 branch
BranchTrueFalseTotal
for (close = ctx->depth; close >= depth; close--) { 35 2 37
640 35 my_warn(ctx,"Force tag close <%.*s> at depth %u", ctx->chain[close].len, ctx->chain[close].name, close);
641 35 on_tag_close(pctx, ctx->chain[close].name, (STRLEN)ctx->chain[close].len);
642 }
643 2 depth = -1;
644 2 break;
645 }
646 33 depth--;
647 }
648 2 ▶ if (depth != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth != -1) { 0 2 2
649 0 my_warn(ctx,"Found no open tag for %s. Ignored", SvPV_nolen(tag));
650 }
651 2 return;
652 }
653 // Text joining
654 2371 ▶ if (ctx->text) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->text) { 2371 0 2371
655 // we may have stored text node
656 4742 text = hv_fetch(ctx->hcurrent, SvPV_nolen(ctx->text), SvCUR(ctx->text), 0);
657 2371 ▶ if (text && SvOK(*text)) {
Branch: 2 branches
BranchTrueFalseTotal
if (text && SvOK(*text)) { 2198 173 2371
if (text && SvOK(*text)) { 2198 0 2198
658 2206 ▶ if (SvROK(*text) && SvTYPE( SvRV(*text) ) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*text) && SvTYPE( SvRV(*text) ) == SVt_PVAV) { 8 2190 2198
if (SvROK(*text) && SvTYPE( SvRV(*text) ) == SVt_PVAV) { 8 0 8
659 8 AV *av = (AV *) SvRV( *text );
660 SV **val;
661 8 I32 len = 0, avlen = av_len(av) + 1;
662 8 ▶ if (ctx->join) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->join) { 8 0 8
663 8 svtext = newSVpvn("",0);
664 8 ▶ if (SvCUR(ctx->join)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(ctx->join)) { 1 7 8
665 //printf("Join length = %d, avlen=%d\n",SvCUR(*join),avlen);
666 3 ▶ for ( len = 0; len < avlen; len++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( len = 0; len < avlen; len++ ) { 2 1 3
667 2 ▶ if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) { 2 0 2
if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) { 2 0 2
668 //printf("Join %s with '%s'\n",SvPV_nolen(*val), SvPV_nolen(ctx->join));
669 2 ▶ if(len > 0) { sv_catsv(svtext,ctx->join); }
Branch: 1 branch
BranchTrueFalseTotal
if(len > 0) { sv_catsv(svtext,ctx->join); } 1 1 2
670 //printf("Join %s with '%s'\n",SvPV_nolen(*val), SvPV_nolen(ctx->join));
671 2 sv_catsv(svtext,*val);
672 }
673 }
674 } else {
675 //printf("Optimized join loop\n");
676 26 ▶ for ( len = 0; len < avlen; len++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( len = 0; len < avlen; len++ ) { 19 7 26
677 19 ▶ if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) { 19 0 19
if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) { 19 0 19
678 //printf("Join %s with ''\n",SvPV_nolen(*val));
679 19 sv_catsv(svtext,*val);
680 }
681 }
682 }
683 //printf("Joined: to %s => '%s'\n",SvPV_nolen(ctx->text),SvPV_nolen(svtext));
684 8 SvREFCNT_inc(svtext);
685 16 (void) hv_store(ctx->hcurrent, SvPV_nolen(ctx->text), SvCUR(ctx->text), svtext, 0);
686 }
687 else
688 // currently unreachable, since if we have single element, it is stored as SV value, not AV
689 //if ( avlen == 1 ) {
690 // Perl_warn("# AVlen=1\n");
691 // /* works
692 // svtext = newSVpvn("",0);
693 // val = av_fetch(av,0,0);
694 // if (val && SvOK(*val)) {
695 // //svtext = *val;
696 // //SvREFCNT_inc(svtext);
697 // sv_catsv(svtext,*val);
698 // }
699 // */
700 // val = av_fetch(av,0,0);
701 // if (val) {
702 // svtext = *val;
703 // SvREFCNT_inc(svtext);
704 // hv_store(ctx->hcurrent, SvPV_nolen(ctx->text), SvCUR(ctx->text), svtext, 0);
705 // }
706 //}
707 //else
708 {
709 // Remebmer for use if it is single
710 //warn("# No join\n");
711 0 svtext = newRV( (SV *) av );
712 }
713 } else {
714 2190 svtext = *text;
715 2190 SvREFCNT_inc(svtext);
716 }
717 }
718 }
719 //printf("svtext=(0x%lx) '%s'\n", svtext, svtext ? SvPV_nolen(svtext) : "");
720 // Text joining
721 2371 ▶ if (ctx->depth > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->depth > -1) { 2371 0 2371
722 2371 HV *hv = ctx->hcurrent;
723 2371 ctx->hcurrent = ctx->hchain[ ctx->depth ];
724 2371 ctx->hchain[ ctx->depth ] = (HV *)NULL;
725 2371 ctx->depth--;
726 2371 ▶ if (keys == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys == 0) { 14 2357 2371
727 //printf("Tag %s have no keys\n", SvPV_nolen(tag));
728 14 SvREFCNT_dec(hv);
729 14 SV *sv = newSVpvn("",0);
730 14 ▶ if (ctx->flags & MODE_ARRAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->flags & MODE_ARRAYS) { 2 12 14
731 2 ▶ hv_store_aa(ctx->hcurrent, tag, sv);
Branch: 3 branches
BranchTrueFalseTotal
hv_store_aa(ctx->hcurrent, tag, sv); 0 2 2
hv_store_aa(ctx->hcurrent, tag, sv); 0 0 0
hv_store_aa(ctx->hcurrent, tag, sv); 0 0 0
732 }
733 12 ▶ else if (ctx->array && (hv_exists(ctx->array, data,length ) )) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ctx->array && (hv_exists(ctx->array, data,length ) )) { 2 10 12
else if (ctx->array && (hv_exists(ctx->array, data,length ) )) { 0 2 2
734 0 ▶ hv_store_aa(ctx->hcurrent, tag, sv);
Branch: 3 branches
BranchTrueFalseTotal
hv_store_aa(ctx->hcurrent, tag, sv); 0 0 0
hv_store_aa(ctx->hcurrent, tag, sv); 0 0 0
hv_store_aa(ctx->hcurrent, tag, sv); 0 0 0
735 }
736 else {
737 12 ▶ hv_store_a(ctx->hcurrent, tag, sv);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, tag, sv); 0 12 12
hv_store_a(ctx->hcurrent, tag, sv); 0 0 0
hv_store_a(ctx->hcurrent, tag, sv); 0 0 0
hv_store_a(ctx->hcurrent, tag, sv); 0 0 0
738 }
739 }
740 else
741 2357 ▶ if (keys == 1 && svtext) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys == 1 && svtext) { 2107 250 2357
if (keys == 1 && svtext) { 2074 33 2107
742 //SV *sx = newSVpvn(data, length);sv_2mortal(sx);
743 //printf("Hash in tag '%s' for destruction have refcnt = %d (%lx | %lx)\n",SvPV_nolen(sx),SvREFCNT(hv), hv, ctx->hcurrent);
744 2074 SvREFCNT_inc(svtext);
745 2074 SvREFCNT_dec(hv);
746 //hv_store(ctx->hcurrent, data, length, svtext, 0);
747 2074 ▶ if (ctx->flags & MODE_ARRAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->flags & MODE_ARRAYS) { 2 2072 2074
748 //printf("Cast %s as array (all should be)\n",SvPV_nolen(tag));
749 2 ▶ hv_store_aa(ctx->hcurrent, tag, svtext);
Branch: 3 branches
BranchTrueFalseTotal
hv_store_aa(ctx->hcurrent, tag, svtext); 0 2 2
hv_store_aa(ctx->hcurrent, tag, svtext); 0 0 0
hv_store_aa(ctx->hcurrent, tag, svtext); 0 0 0
750 }
751 2072 ▶ else if (ctx->array && (hv_exists(ctx->array, data,length ) )) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ctx->array && (hv_exists(ctx->array, data,length ) )) { 2 2070 2072
else if (ctx->array && (hv_exists(ctx->array, data,length ) )) { 0 2 2
752 //printf("Cast %s as array\n",SvPV_nolen(tag));
753 0 ▶ hv_store_aa(ctx->hcurrent, tag, svtext);
Branch: 3 branches
BranchTrueFalseTotal
hv_store_aa(ctx->hcurrent, tag, svtext); 0 0 0
hv_store_aa(ctx->hcurrent, tag, svtext); 0 0 0
hv_store_aa(ctx->hcurrent, tag, svtext); 0 0 0
754 }
755 else {
756 2072 ▶ hv_store_a(ctx->hcurrent, tag, svtext);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, tag, svtext); 1219 853 2072
hv_store_a(ctx->hcurrent, tag, svtext); 1171 48 1219
hv_store_a(ctx->hcurrent, tag, svtext); 1171 0 1171
hv_store_a(ctx->hcurrent, tag, svtext); 0 48 48
757 }
758 } else {
759 283 SV *sv = newRV_noinc( (SV *) hv );
760 //printf("Store hash into RV '%lx'\n",sv);
761 //hv_store(ctx->hcurrent, data, length, sv, 0);
762 //printf("Check %s to be array\n",SvPV_nolen(tag));
763 283 ▶ if (ctx->flags & MODE_ARRAYS) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->flags & MODE_ARRAYS) { 6 277 283
764 //printf("Cast %s as array (all should be)\n",SvPV_nolen(tag));
765 6 ▶ hv_store_aa(ctx->hcurrent, tag, sv);
Branch: 3 branches
BranchTrueFalseTotal
hv_store_aa(ctx->hcurrent, tag, sv); 2 4 6
hv_store_aa(ctx->hcurrent, tag, sv); 2 0 2
hv_store_aa(ctx->hcurrent, tag, sv); 2 0 2
766 }
767 277 ▶ else if (ctx->array && ( hv_exists(ctx->array, data,length ) )) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ctx->array && ( hv_exists(ctx->array, data,length ) )) { 6 271 277
else if (ctx->array && ( hv_exists(ctx->array, data,length ) )) { 2 4 6
768 //printf("Cast %s as array\n",SvPV_nolen(tag));
769 2 ▶ hv_store_aa(ctx->hcurrent, tag, sv);
Branch: 3 branches
BranchTrueFalseTotal
hv_store_aa(ctx->hcurrent, tag, sv); 0 2 2
hv_store_aa(ctx->hcurrent, tag, sv); 0 0 0
hv_store_aa(ctx->hcurrent, tag, sv); 0 0 0
770 }
771 else {
772 275 ▶ hv_store_a(ctx->hcurrent, tag, sv);
Branch: 4 branches
BranchTrueFalseTotal
hv_store_a(ctx->hcurrent, tag, sv); 100 175 275
hv_store_a(ctx->hcurrent, tag, sv); 96 4 100
hv_store_a(ctx->hcurrent, tag, sv); 92 4 96
hv_store_a(ctx->hcurrent, tag, sv); 4 4 8
773 }
774 }
775 2371 ▶ if (svtext) SvREFCNT_dec(svtext);
Branch: 1 branch
BranchTrueFalseTotal
if (svtext) SvREFCNT_dec(svtext); 2198 173 2371
776 } else {
777 0 my_croak(ctx,"Bad depth: %d for tag close %s\n",ctx->depth,SvPV_nolen(tag));
778 }
779 }
780  
781 169 void on_attr_name(void * pctx, char * data,unsigned int length) {
782 #if XML_DEVEL
783 if (!pctx) croak("Context not passed to on_attr_name");
784 #endif
785 169 parsestate *ctx = pctx;
786 169 ▶ if (ctx->textval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->textval) { 0 169 169
787 0 my_croak(ctx,"Have textval=%s, while called attrname\n",SvPV_nolen(ctx->textval));
788 }
789 //warn("Called attrname on %s '%-.*s'=",ctx->pi ? "pi" : "common", length,data);
790 169 ▶ if (ctx->attrname) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->attrname) { 0 169 169
791 0 my_croak(ctx,"Called attrname '%-.*s'=, while have attrname='%-.*s'\n",length,data,SvCUR(ctx->attrname),SvPVX(ctx->attrname));
792 }
793 169 ▶ if (ctx->pi) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->pi) { 18 151 169
794 18 ctx->attrname = newSVpvn(data,length);
795 } else {
796 151 ▶ if( ctx->attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if( ctx->attr ) { 151 0 151
797 //warn("What TF is attr??"); attr prefix
798 151 ctx->attrname = newSV(length + SvCUR(ctx->attr));
799 151 sv_copypv(ctx->attrname, ctx->attr);
800 151 sv_catpvn(ctx->attrname, data, length);
801 } else {
802 0 ctx->attrname = newSVpvn(data, length);
803 }
804 }
805 169 }
806  
807 0 void on_warn(void * pctx, char * format, ...) {
808 #if XML_DEVEL
809 if (!pctx) croak("Context not passed to on_warn");
810 #endif
811 //parsestate *ctx = pctx;
812 //if(!(ctx->flags & EMIT_WARNS)) return;
813 va_list va;
814 0 va_start(va,format);
815 0 SV *text = sv_2mortal(newSVpvn("",0));
816 0 sv_vcatpvf(text, format, &va);
817 0 warn("%s",SvPV_nolen(text));
818 0 va_end(va);
819 0 }
820  
821 26 void on_die(void * pctx, char * format, ...) {
822 #if XML_DEVEL
823 if (!pctx) croak("Context not passed to on_die");
824 #endif
825 26 parsestate *ctx = pctx;
826 va_list va;
827 26 va_start(va,format);
828 26 ctx->error = sv_2mortal(newSVpvn("",0));
829 26 sv_vcatpvf(ctx->error, format, &va);
830 //warn("got a die with %s",SvPV_nolen(ctx->error));
831 26 va_end(va);
832 26 }
833  
834  
835  
836 /*
837 #define newRVHV() newRV_noinc((SV *)newHV())
838 #define rv_hv_store(rv,key,len,sv,f) hv_store((HV*)SvRV(rv), key,len,sv,f)
839 #define rv_hv_fetch(rv,key,len,f) hv_fetch((HV*)SvRV(rv), key,len,f)
840 */
841 /*
842 void
843 _test()
844 CODE:
845 SV *sv1 = newRVHV();
846 SV *sv2 = newRVHV();
847 sv_2mortal(sv1);
848 sv_2mortal(sv2);
849 SV *test = newSVpvn("test",4);
850 rv_hv_store(sv1, "test",4,test,0);
851 SvREFCNT_inc(test);
852 rv_hv_store(sv2, "test",4,test,0);
853 */
854  
855 #ifndef HePV_nolen
856 #define HePV_nolen(he) ((HeKLEN(he) == HEf_SVKEY) ? \
857 SvPV_nolen(HeKEY_sv(he)) : \
858 (HeKEY(he)))
859 #endif
860 //#define strcmp(a,b) ( a && b ? strcmp(a,b) : fprintf(stderr, "Failed to compare %s with %s at %s line %d.\n", a,b,__FILE__,__LINE__)+2 )
861 #define mystrcmp(a,b) ( a && b ? strcmp(a,b) : 2 )
862  
863 0 void h2xout( char *data, compstate *p ) {
864 0 printf( "%*s", p->depth * 4,"" );
865 0 printf( "%s\n", data );
866 0 }
867 0 void h2xoute( char *data, compstate *p ) {
868 0 printf( "%*s", p->depth * 4,"" );
869 0 printf("e(%s)\n", data);
870 0 }
871  
872 0 void h2xp( compstate *p, char *f, ... ) {
873 va_list va_args;
874
875 /*
876 printf( "%*s", p->depth * 4,"" );
877 va_start(va_args,f);
878 vfprintf(stdout, f, va_args);
879 va_end(va_args);
880 */
881
882 0 va_start(va_args,f);
883 0 sv_vcatpvf( p->result, f, &va_args );
884 0 va_end(va_args);
885
886 //printf ("\n");
887 0 }
888 0 void h2xpe( compstate *p, char *s ) {
889 0 char *b = s;
890 while (1) {
891 0 switch (*s) {
892 warn("%c", *s);
893 0 case 0:
894 0 ▶ if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b );
Branch: 1 branch
BranchTrueFalseTotal
if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b ); 0 0 0
895 0 return;
896 0 case '<':
897 0 ▶ if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b );
Branch: 1 branch
BranchTrueFalseTotal
if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b ); 0 0 0
898 0 sv_catpvf( p->result, "%s", "&lt;" );
899 0 b = s+1;
900 0 break;
901 0 case '>':
902 0 ▶ if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b );
Branch: 1 branch
BranchTrueFalseTotal
if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b ); 0 0 0
903 0 sv_catpvf( p->result, "%s", "&gt;" );
904 0 b = s+1;
905 0 break;
906 0 case '"':
907 0 ▶ if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b );
Branch: 1 branch
BranchTrueFalseTotal
if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b ); 0 0 0
908 0 sv_catpvf( p->result, "%s", "&quot;" );
909 0 b = s+1;
910 0 break;
911 0 case '\'':
912 0 ▶ if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b );
Branch: 1 branch
BranchTrueFalseTotal
if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b ); 0 0 0
913 0 sv_catpvf( p->result, "%s", "&apos;" );
914 0 b = s+1;
915 0 break;
916 0 case '&':
917 0 ▶ if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b );
Branch: 1 branch
BranchTrueFalseTotal
if (b < s) sv_catpvf( p->result, "%-.*s", (int)(s - b), b ); 0 0 0
918 0 sv_catpvf( p->result, "%s", "&amp;" );
919 0 b = s+1;
920 0 break;
921 0 default:
922 0 break;
923 }
924 0 s++;
925 }
926 }
927  
928  
929 void kv2x ( char *key, SV *val, compstate *p );
930 0 void kv2x ( char *key, SV *val, compstate *p ) {
931 char closed;
932
933 HE* ent;
934
935 char *nkey;
936 STRLEN i, nlen;
937
938 AV *av;
939 SV **avv;
940
941 debug("key=%s, val=%s",key, SvPV_nolen(val));
942 0 ▶ if ( mystrcmp( key, p->text ) == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( mystrcmp( key, p->text ) == 0 ) { 0 0 0
if ( mystrcmp( key, p->text ) == 0 ) { 0 0 0
if ( mystrcmp( key, p->text ) == 0 ) { 0 0 0
943 0 h2xpe(p, SvPV_nolen( val ));
944 }
945 else
946 0 ▶ if ( mystrcmp( key, p->cdata ) == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( mystrcmp( key, p->cdata ) == 0 ) { 0 0 0
if ( mystrcmp( key, p->cdata ) == 0 ) { 0 0 0
if ( mystrcmp( key, p->cdata ) == 0 ) { 0 0 0
947 0 h2xp(p, "<![CDATA[");
948 0 h2xp(p, SvPV_nolen( val ));
949 0 h2xp(p, "]]>");
950 }
951 else
952 0 ▶ if ( p->comm && mystrcmp( key, p->comm) == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ( p->comm && mystrcmp( key, p->comm) == 0 ) { 0 0 0
if ( p->comm && mystrcmp( key, p->comm) == 0 ) { 0 0 0
if ( p->comm && mystrcmp( key, p->comm) == 0 ) { 0 0 0
if ( p->comm && mystrcmp( key, p->comm) == 0 ) { 0 0 0
953 debug("comm: %s", SvPV_nolen( val ));
954 0 h2xp(p, "<!-- ");
955 0 h2xpe(p, SvPV_nolen( val ));
956 0 h2xp(p, " -->");
957 }
958 else {
959 0 ▶ if (SvROK( val )) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK( val )) { 0 0 0
960 0 switch ( SvTYPE( SvRV( val ) ) ) {
961 0 case SVt_PVHV:
962 debug("%s -> hash inside", key);
963 0 (void) hv_iterinit( (HV *) SvRV( val ) );
964 0 h2xp(p, "<%s", key);
965 0 closed = 0;
966 0 ▶ while ((ent = hv_iternext( (HV *) SvRV( val ) ))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext( (HV *) SvRV( val ) ))) { 0 0 0
967 0 ▶ nkey = HePV(ent, nlen);
Branch: 1 branch
BranchTrueFalseTotal
nkey = HePV(ent, nlen); 0 0 0
968 0 ▶ if ( strncmp( nkey, p->attr, p->attl ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strncmp( nkey, p->attr, p->attl ) == 0 ) { 0 0 0
969 0 nkey += p->attl;
970 0 h2xp(p, " %s=\"",nkey);
971 0 h2xpe(p, SvPV_nolen(HeVAL(ent)));
972 0 h2xp(p, "\"");
973 0 continue;
974 }
975 }
976
977 0 (void) hv_iterinit( (HV *) SvRV( val ) );
978 0 ▶ while ((ent = hv_iternext( (HV *) SvRV( val ) ))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext( (HV *) SvRV( val ) ))) { 0 0 0
979 0 ▶ nkey = HePV(ent, nlen);
Branch: 1 branch
BranchTrueFalseTotal
nkey = HePV(ent, nlen); 0 0 0
980 0 ▶ if ( strncmp( nkey, p->attr, p->attl ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strncmp( nkey, p->attr, p->attl ) == 0 ) { 0 0 0
981 0 continue;
982 }
983 debug("Nested %s->%s", nkey, SvPV_nolen(HeVAL(ent)));
984 0 ▶ if (!closed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!closed) { 0 0 0
985 0 closed = 1;
986 0 h2xp(p,">");
987 }
988 0 p->depth++;
989 0 kv2x( nkey, HeVAL(ent), p );
990 0 p->depth--;
991 }
992
993 0 ▶ if (!closed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!closed) { 0 0 0
994 0 h2xp(p, "/>");
995 } else {
996 0 h2xp(p, "</%s>",key);
997 }
998
999
1000 0 break;
1001 0 case SVt_PVAV:
1002 debug("array inside (Multinode)");
1003
1004 0 av = (AV *) SvRV( val );
1005 0 nlen = av_len(av) + 1;
1006
1007 0 ▶ for ( i = 0; i < nlen; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < nlen; i++ ) { 0 0 0
1008 0 ▶ if( ( avv = av_fetch(av,i,0) ) && SvOK(*avv) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( avv = av_fetch(av,i,0) ) && SvOK(*avv) ) { 0 0 0
if( ( avv = av_fetch(av,i,0) ) && SvOK(*avv) ) { 0 0 0
1009 0 kv2x( key, *avv, p );
1010 /*
1011 //printf("AV.V: %s\n",SvPV_nolen(*avv));
1012 if (closed) {
1013 h2xp(p, "<%s", key);
1014 closed = 0;
1015 }
1016 if (SvROK( *avv )) {
1017 switch( SvTYPE( SvRV(*avv) ) ) {
1018 case SVt_PVHV:
1019 //debug ("hv inside av");
1020 (void) hv_iterinit( (HV *) SvRV( *avv ) );
1021 while ((ent = hv_iternext( (HV *) SvRV( *avv ) ))) {
1022 nkey = HePV_nolen(ent);
1023 if ( strncmp( nkey, p->attr, p->attl ) == 0 ) {
1024 nkey += p->attl;
1025 h2xp(p, " %s='%s'",nkey, SvPV_nolen(HeVAL(ent)));
1026 continue;
1027 }
1028 }
1029 break;
1030 case SVt_PVAV:
1031 //Ignore attrs from AV in AV
1032 //debug ("av inside av");
1033 // Prohibited
1034 break;
1035 default:
1036 debug("Unhandled svtype: %s", SvPV_nolen( *avv ));
1037 }
1038 } else {
1039
1040 }
1041
1042 h2xp(p, "</%s>", key);
1043 closed = 1;
1044 */
1045 }
1046 }
1047 /*
1048 debug("array inside. loop 2");
1049
1050 for ( i = 0; i < nlen; i++ ) {
1051 if( ( avv = av_fetch(av,i,0) ) && SvOK(*avv) ) {
1052 if (SvROK( *avv )) {
1053 switch( SvTYPE( SvRV(*avv) ) ) {
1054 case SVt_PVHV:
1055 (void) hv_iterinit( (HV *) SvRV( *avv ) );
1056 while ((ent = hv_iternext( (HV *) SvRV( *avv ) ))) {
1057 nkey = HePV_nolen(ent);
1058 if ( strncmp( nkey, p->attr, p->attl ) == 0 ) {
1059 continue;
1060 }
1061
1062 //
1063 debug("Nested %s->%s", nkey, SvPV_nolen(HeVAL(ent)));
1064 if (!closed) {
1065 closed = 1;
1066 h2xp(p,">");
1067 }
1068 p->depth++;
1069 kv2x( nkey, HeVAL(ent), p );
1070 p->depth--;
1071 }
1072 break;
1073 case SVt_PVAV:
1074 //AV in AV
1075 debug("Array within Array is useless and not supported. Ignoring");
1076 break;
1077 default:
1078 debug("Unhandled svtype: %s", SvPV_nolen( *avv ));
1079 }
1080 } else {
1081
1082 }
1083 }
1084 }
1085 */
1086
1087 0 break;
1088 0 default:
1089 0 warn("Bad reference found: %s", SvPV_nolen( SvRV(val) ));
1090 0 break;
1091 }
1092 } else {
1093 0 ▶ if (SvOK(val) && SvCUR(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(val) && SvCUR(val)) { 0 0 0
if (SvOK(val) && SvCUR(val)) { 0 0 0
1094 0 h2xp(p, "<%s>", key);
1095 0 h2xpe(p, SvPV_nolen(val));
1096 0 h2xp(p, "</%s>", key);
1097 } else {
1098 0 h2xp(p, "<%s/>",key);
1099 }
1100 }
1101 }
1102 0 }
1103  
1104 void h2x ( SV *x, compstate *p );
1105 0 void h2x ( SV *x, compstate *p ) {
1106
1107 HE* ent;
1108 SV *val;
1109 char *key;
1110 STRLEN klen;
1111
1112 0 ▶ if (SvROK (x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK (x)) { 0 0 0
1113 0 ▶ switch( SvTYPE( SvRV(x) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
switch( SvTYPE( SvRV(x) ) ) { 0 0 0
1114 0 case SVt_PVHV:
1115 0 (void) hv_iterinit( (HV *) SvRV( x ) );
1116 0 ▶ while ((ent = hv_iternext( (HV *) SvRV( x ) ))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext( (HV *) SvRV( x ) ))) { 0 0 0
1117 0 ▶ key = HePV(ent, klen);
Branch: 1 branch
BranchTrueFalseTotal
key = HePV(ent, klen); 0 0 0
1118 0 val = HeVAL(ent);
1119 0 ▶ if ( strncmp( key, p->attr, strlen(p->attr) ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strncmp( key, p->attr, strlen(p->attr) ) == 0 ) { 0 0 0
1120 debug("Attribute %s at this level is prohibited", key);
1121 0 continue;
1122 }
1123 0 kv2x( key, val, p );
1124 }
1125 0 break;
1126 0 default:
1127 0 warn("skip %s", SvPV_nolen( SvRV(x) ));
1128 }
1129 } else {
1130 0 warn("skip nonref");
1131 }
1132 0 }
1133  
1134  
1135  
1136 MODULE = XML::Fast PACKAGE = XML::Fast
1137  
1138 void
1139 _test()
1140 CODE:
1141 dTHX;
1142 0 SV * cons = get_constant("Encode::FB_QUIET");
1143 0 SV * test = newSViv(4);
1144 0 sv_dump(test);
1145 0 printf("Got constant %s\n", SvPV_nolen(cons));
1146 //UV chr = 0x2622;
1147 0 UV chr = 0xAB;
1148 char *end, utf[UTF8_MAXBYTES + 1];
1149 0 SV *encode = find_encoding("windows-1251");
1150 0 end = uvchr_to_utf8(utf, chr);
1151 0 *end = '\0';
1152 0 SV *tmp = sv_2mortal(newSVpvn(utf, end-utf));
1153 0 SvUTF8_on(tmp);
1154 0 SV *bytes = sv_recode_from_utf8(aTHX_ tmp, encode);
1155 0 sv_dump(bytes);
1156 0 ▶ printf("Created char %s / %s / bytes = %s\n", utf, SvPV_nolen(tmp), SvPV_nolen(bytes));
Branch: 1 branch
BranchTrueFalseTotal
printf("Created char %s / %s / bytes = %s\n", utf, SvPV_nolen(tmp), SvPV_nolen(bytes)); 0 0 0
1157 //sv_recode_to_utf8(tmp, encode);
1158 //printf("Recoded %s\n",SvPV_nolen(tmp));
1159 0 croak("Force exit");
1160
1161  
1162 void
1163 _xml2hash(xml_sv,conf)
1164 SV *xml_sv;
1165 HV *conf;
1166 PROTOTYPE: $$
1167 PPCODE:
1168 52 SvGETMAGIC(xml_sv);
1169 52 char *xml = SvPVbyte_nolen(xml_sv);
1170 SV * RV;
1171
1172 parser_state state;
1173 52 memset(&state,0,sizeof(state));
1174
1175 parsestate ctx;
1176 52 memset(&ctx,0,sizeof(parsestate));
1177
1178 52 state.ctx = &ctx;
1179 52 ctx.state = &state;
1180
1181 SV **key;
1182 52 ▶ if ((key = hv_fetch(conf, "order", 5, 0)) && SvTRUE(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "order", 5, 0)) && SvTRUE(*key)) { 52 0 52
if ((key = hv_fetch(conf, "order", 5, 0)) && SvTRUE(*key)) { 0 52 52
1183 0 ctx.flags |= MODE_ORDER;
1184 }
1185 52 ▶ if ((key = hv_fetch(conf, "trim", 4, 0)) && SvTRUE(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "trim", 4, 0)) && SvTRUE(*key)) { 52 0 52
if ((key = hv_fetch(conf, "trim", 4, 0)) && SvTRUE(*key)) { 52 0 52
1186 52 ctx.flags |= MODE_TRIM;
1187 }
1188 52 ▶ if ((key = hv_fetch(conf, "bytes", 5, 0)) && SvTRUE(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "bytes", 5, 0)) && SvTRUE(*key)) { 4 48 52
if ((key = hv_fetch(conf, "bytes", 5, 0)) && SvTRUE(*key)) { 4 0 4
1189 4 ctx.bytes = 1;
1190 } else {
1191 48 ▶ if ((key = hv_fetch(conf, "utf8decode", 10, 0)) && SvTRUE(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "utf8decode", 10, 0)) && SvTRUE(*key)) { 0 48 48
if ((key = hv_fetch(conf, "utf8decode", 10, 0)) && SvTRUE(*key)) { 0 0 0
1192 0 ctx.utf8 = UTF8_DECODE;
1193 } else {
1194 48 ctx.utf8 = UTF8_UPGRADE;
1195 }
1196 }
1197
1198 52 ▶ if ((key = hv_fetch(conf, "attr", 4, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "attr", 4, 0))) { 52 0 52
1199 52 ▶ if (SvOK(*key) && SvCUR(*key) > 0 ) { // defined and length
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(*key) && SvCUR(*key) > 0 ) { // defined and length 52 0 52
if (SvOK(*key) && SvCUR(*key) > 0 ) { // defined and length 52 0 52
1200 52 ctx.attr = *key;
1201 }
1202 }
1203 52 ▶ if ((key = hv_fetch(conf, "text", 4, 0)) && SvOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "text", 4, 0)) && SvOK(*key)) { 52 0 52
if ((key = hv_fetch(conf, "text", 4, 0)) && SvOK(*key)) { 52 0 52
1204 52 ctx.text = *key;
1205 }
1206 52 ▶ if ((key = hv_fetch(conf, "join", 4, 0)) && SvPOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "join", 4, 0)) && SvPOK(*key)) { 52 0 52
if ((key = hv_fetch(conf, "join", 4, 0)) && SvPOK(*key)) { 52 0 52
1207 52 ctx.join = *key;
1208 }
1209 52 ▶ if ((key = hv_fetch(conf, "cdata", 5, 0)) && SvPOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "cdata", 5, 0)) && SvPOK(*key)) { 52 0 52
if ((key = hv_fetch(conf, "cdata", 5, 0)) && SvPOK(*key)) { 1 51 52
1210 1 ctx.cdata = *key;
1211 }
1212 52 ▶ if ((key = hv_fetch(conf, "comm", 4, 0)) && SvPOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "comm", 4, 0)) && SvPOK(*key)) { 52 0 52
if ((key = hv_fetch(conf, "comm", 4, 0)) && SvPOK(*key)) { 0 52 52
1213 0 ctx.comm = *key;
1214 }
1215 52 ▶ if ((key = hv_fetch(conf, "array", 5, 0)) && SvOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "array", 5, 0)) && SvOK(*key)) { 4 48 52
if ((key = hv_fetch(conf, "array", 5, 0)) && SvOK(*key)) { 4 0 4
1216 6 ▶ if (SvROK(*key) && SvTYPE( SvRV(*key) ) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*key) && SvTYPE( SvRV(*key) ) == SVt_PVAV) { 2 2 4
if (SvROK(*key) && SvTYPE( SvRV(*key) ) == SVt_PVAV) { 2 0 2
1217 2 AV *av = (AV *) SvRV( *key );
1218 2 ctx.array = newHV();
1219 2 I32 len = 0, avlen = av_len(av) + 1;
1220 SV **val;
1221 4 ▶ for ( len = 0; len < avlen; len++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( len = 0; len < avlen; len++ ) { 2 2 4
1222 2 ▶ if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) {
Branch: 2 branches
BranchTrueFalseTotal
if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) { 2 0 2
if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) { 2 0 2
1223 2 ▶ if(SvPOK(*val)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvPOK(*val)) { 2 0 2
1224 4 (void) hv_store( ctx.array, SvPV_nolen(*val), SvCUR(*val), newSV(0), 0 );
1225 } else {
1226 0 my_croak(&ctx,"Bad enrty in array entry: %s",SvPV_nolen(*val));
1227 }
1228 }
1229 }
1230
1231
1232 }
1233 2 ▶ else if (!SvROK(*key)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!SvROK(*key)) { 2 0 2
1234 //printf("Remember all should be arrays\n");
1235 2 ▶ if (SvTRUE(*key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(*key)) { 2 0 2
1236 2 ctx.flags |= MODE_ARRAYS;
1237 }
1238 }
1239 else {
1240 0 my_croak(&ctx,"Bad entry in array: %s",SvPV_nolen(*key));
1241 }
1242 }
1243
1244 //ctx.flags |= TAG_MATCH;
1245
1246
1247 52 ▶ if ((key = hv_fetch(conf, "_max_depth", 10, 0)) && SvOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "_max_depth", 10, 0)) && SvOK(*key)) { 0 52 52
if ((key = hv_fetch(conf, "_max_depth", 10, 0)) && SvOK(*key)) { 0 0 0
1248 0 ctx.chainsize = SvIV(*key);
1249 0 ▶ if (ctx.chainsize < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.chainsize < 1) { 0 0 0
1250 0 my_croak(&ctx,"_max_depth contains bad value (%d)",ctx.chainsize);
1251 }
1252 } else {
1253 52 ctx.chainsize = 256;
1254 }
1255
1256 52 ▶ if (!ctx.bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx.bytes) { 48 4 52
1257 48 ctx.encoding = "utf8";
1258 }
1259
1260 52 ▶ if (ctx.flags & MODE_ORDER) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.flags & MODE_ORDER) { 0 52 52
1261 0 my_croak(&ctx,"Ordered mode not implemented yet\n");
1262 } else{
1263 52 ctx.hcurrent = newHV();
1264
1265 52 Newx(ctx.chain, ctx.chainsize, xml_node);
1266 52 Newx(ctx.hchain, ctx.chainsize, HV*);
1267 52 ctx.depth = -1;
1268
1269 52 RV = sv_2mortal(newRV_noinc( (SV *) ctx.hcurrent ));
1270
1271 52 state.cb.piopen = on_pi_open;
1272 52 state.cb.piclose = on_pi_close;
1273 52 state.cb.tagopen = on_tag_open;
1274 52 state.cb.tagclose = on_tag_close;
1275
1276 52 state.cb.attrname = on_attr_name;
1277 52 ▶ if ((key = hv_fetch(conf, "nowarn", 6, 0)) && SvTRUE(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "nowarn", 6, 0)) && SvTRUE(*key)) { 1 51 52
if ((key = hv_fetch(conf, "nowarn", 6, 0)) && SvTRUE(*key)) { 1 0 1
1278 //
1279 } else {
1280 51 state.cb.warn = on_warn;
1281 51 ctx.flags |= EMIT_WARNS;
1282 }
1283 52 state.cb.die = on_die;
1284
1285 52 ▶ if(ctx.comm)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.comm) 0 52 52
1286 0 state.cb.comment = on_comment;
1287
1288 52 ▶ if(ctx.cdata)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.cdata) 1 51 52
1289 1 state.cb.cdata = on_cdata;
1290 51 ▶ else if(ctx.text)
Branch: 1 branch
BranchTrueFalseTotal
else if(ctx.text) 51 0 51
1291 51 state.cb.cdata = on_bytes;
1292
1293 52 state.cb.bytes = on_bytes;
1294 52 state.cb.bytespart = on_bytes_part;
1295 52 state.cb.uchar = on_uchar;
1296
1297 52 ▶ if (!(ctx.flags & MODE_TRIM))
Branch: 1 branch
BranchTrueFalseTotal
if (!(ctx.flags & MODE_TRIM)) 0 52 52
1298 0 state.save_wsp = 1;
1299 }
1300 52 parse(xml,&state);
1301
1302 52 ▶ if (ctx.depth > -1 && !ctx.error) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx.depth > -1 && !ctx.error) { 3 49 52
if (ctx.depth > -1 && !ctx.error) { 0 3 3
1303 0 ctx.error = sv_2mortal(newSVpv("Unbalanced tags",0));
1304 }
1305
1306 52 DESTROY(&ctx);
1307
1308 52 ▶ if (ctx.error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.error) { 23 29 52
1309 46 croak("%s", SvPV_nolen(ctx.error));
1310 }
1311 29 ST(0) = RV;
1312 29 XSRETURN(1);
1313  
1314 void
1315 _hash2xml(hash,conf)
1316 SV *hash;
1317 HV *conf;
1318 PROTOTYPE: $$
1319 PPCODE:
1320 compstate ctx;
1321 0 memset(&ctx,0,sizeof(parsestate));
1322
1323 SV **key;
1324 0 ▶ if ((key = hv_fetch(conf, "order", 5, 0)) && SvTRUE(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "order", 5, 0)) && SvTRUE(*key)) { 0 0 0
if ((key = hv_fetch(conf, "order", 5, 0)) && SvTRUE(*key)) { 0 0 0
1325 0 ctx.flags |= MODE_ORDER;
1326 }
1327 0 ▶ if ((key = hv_fetch(conf, "trim", 4, 0)) && SvTRUE(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "trim", 4, 0)) && SvTRUE(*key)) { 0 0 0
if ((key = hv_fetch(conf, "trim", 4, 0)) && SvTRUE(*key)) { 0 0 0
1328 0 ctx.flags |= MODE_TRIM;
1329 }
1330 /*
1331 if ((key = hv_fetch(conf, "bytes", 5, 0)) && SvTRUE(*key)) {
1332 ctx.bytes = 1;
1333 } else {
1334 if ((key = hv_fetch(conf, "utf8decode", 10, 0)) && SvTRUE(*key)) {
1335 ctx.utf8 = UTF8_DECODE;
1336 } else {
1337 ctx.utf8 = UTF8_UPGRADE;
1338 }
1339 }
1340 */
1341
1342 0 ▶ if ((key = hv_fetch(conf, "attr", 4, 0)) && SvPOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "attr", 4, 0)) && SvPOK(*key)) { 0 0 0
if ((key = hv_fetch(conf, "attr", 4, 0)) && SvPOK(*key)) { 0 0 0
1343 0 ctx.attr = SvPV_nolen(*key);
1344 // warn ("Set attr to '%s'", ctx.attr);
1345 } else {
1346 0 ctx.attr = "-";
1347 }
1348 0 ▶ if ((key = hv_fetch(conf, "text", 4, 0)) && SvPOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "text", 4, 0)) && SvPOK(*key)) { 0 0 0
if ((key = hv_fetch(conf, "text", 4, 0)) && SvPOK(*key)) { 0 0 0
1349 0 ctx.text = SvPV_nolen(*key);
1350 } else {
1351 0 ctx.text = "#text";
1352 }
1353 0 ▶ if ((key = hv_fetch(conf, "cdata", 5, 0)) && SvPOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "cdata", 5, 0)) && SvPOK(*key)) { 0 0 0
if ((key = hv_fetch(conf, "cdata", 5, 0)) && SvPOK(*key)) { 0 0 0
1354 0 ctx.cdata = SvPV_nolen(*key);
1355 } else {
1356 0 ctx.cdata = 0;
1357 }
1358 0 ▶ if ((key = hv_fetch(conf, "comm", 4, 0)) && SvPOK(*key)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((key = hv_fetch(conf, "comm", 4, 0)) && SvPOK(*key)) { 0 0 0
if ((key = hv_fetch(conf, "comm", 4, 0)) && SvPOK(*key)) { 0 0 0
1359 0 ctx.comm = SvPV_nolen(*key);
1360 } else {
1361 0 ctx.comm = 0;
1362 }
1363 /*
1364 if ((key = hv_fetch(conf, "array", 5, 0)) && SvOK(*key)) {
1365 if (SvROK(*key) && SvTYPE( SvRV(*key) ) == SVt_PVAV) {
1366 AV *av = (AV *) SvRV( *key );
1367 ctx.array = newHV();
1368 I32 len = 0, avlen = av_len(av) + 1;
1369 SV **val;
1370 for ( len = 0; len < avlen; len++ ) {
1371 if( ( val = av_fetch(av,len,0) ) && SvOK(*val) ) {
1372 if(SvPOK(*val)) {
1373 (void) hv_store( ctx.array, SvPV_nolen(*val), SvCUR(*val), newSV(0), 0 );
1374 } else {
1375 //my_croak(&ctx,"Bad enrty in array entry: %s",SvPV_nolen(*val));
1376 }
1377 }
1378 }
1379
1380
1381 }
1382 else if (!SvROK(*key)) {
1383 //printf("Remember all should be arrays\n");
1384 if (SvTRUE(*key)) {
1385 ctx.flags |= MODE_ARRAYS;
1386 }
1387 }
1388 else {
1389 //my_croak(&ctx,"Bad entry in array: %s",SvPV_nolen(*key));
1390 }
1391 }
1392
1393 //ctx.flags |= TAG_MATCH;
1394
1395 if (!ctx.bytes) {
1396 ctx.encoding = "utf8";
1397 }
1398 */
1399
1400 0 ctx.depth = 0;
1401 0 ctx.ix = 0;
1402 0 ctx.result = sv_2mortal(newSVpv("",0));
1403 //ctx.nested = newSVpv("",0);
1404 0 ctx.attl = strlen(ctx.attr);
1405
1406 0 ▶ SvGROW(ctx.result,1024);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(ctx.result,1024); 0 0 0
SvGROW(ctx.result,1024); 0 0 0
1407 //SvGROW(ctx.nested,1024);
1408 0 h2x( hash, &ctx);
1409
1410 //croak("XXX: %s", SvPV_nolen(ctx.result));
1411
1412 0 ▶ if (ctx.error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.error) { 0 0 0
1413 0 croak("%s", SvPV_nolen(ctx.error));
1414 }
1415
1416 0 ST(0) = ctx.result;
1417 0 XSRETURN(1);
« blib/lib/XML/Fast.pm ↑ Summary xmlfast.c »