File Coverage

src/xh_h2x.c
Criterion Covered Total %
statement 28 36 77.7
branch 10 22 45.4
condition n/a
subroutine n/a
pod n/a
total 38 58 65.5


line stmt bran cond sub pod time code
1             #include "xh_config.h"
2             #include "xh_core.h"
3              
4             SV *
5 42           xh_h2x(xh_h2x_ctx_t *ctx)
6             {
7             SV *result;
8              
9             /* run */
10 42           dXCPT;
11 42 50         XCPT_TRY_START
12             {
13 42           xh_stack_init(&ctx->stash, XH_H2X_STASH_SIZE, sizeof(SV *));
14 42           xh_writer_init(&ctx->writer, ctx->opts.encoding, ctx->opts.output, ctx->opts.buf_size, ctx->opts.indent, ctx->opts.trim);
15              
16 42 100         if (ctx->opts.xml_decl) {
17 38           xh_xml_write_xml_declaration(&ctx->writer, ctx->opts.version, ctx->opts.encoding);
18             }
19              
20 42           switch (ctx->opts.method) {
21 22           case XH_METHOD_NATIVE:
22 22 100         if (ctx->opts.keep_root) {
23 21           xh_h2x_native(ctx, ctx->opts.root, xh_strlen(ctx->opts.root), SvRV(ctx->hash));
24             }
25             else
26             {
27 1           xh_h2x_native(ctx, NULL, 0, SvRV(ctx->hash));
28             }
29 22           break;
30 5           case XH_METHOD_NATIVE_ATTR_MODE:
31 5           (void) xh_h2x_native_attr(ctx, ctx->opts.root, xh_strlen(ctx->opts.root), SvRV(ctx->hash), XH_H2X_F_COMPLEX);
32 5           break;
33 15           case XH_METHOD_LX:
34 15           xh_h2x_lx(ctx, ctx->hash, NULL, 0, XH_H2X_F_NONE);
35 15           break;
36 0           default:
37 0           croak("Invalid method");
38             }
39 42           } XCPT_TRY_END
40              
41 42 50         XCPT_CATCH
42             {
43 0           xh_stash_clean(&ctx->stash);
44 0           result = xh_writer_flush(&ctx->writer);
45 0 0         if (result != NULL && result != &PL_sv_undef) {
    0          
46 0           SvREFCNT_dec(result);
47             }
48 0           xh_writer_destroy(&ctx->writer);
49 0 0         XCPT_RETHROW;
    0          
50             }
51              
52 42           xh_stash_clean(&ctx->stash);
53 42           result = xh_writer_flush(&ctx->writer);
54 42 50         if (result != NULL && ctx->opts.utf8) {
    50          
55             #ifdef XH_HAVE_ENCODER
56 42 100         if (ctx->writer.encoder == NULL) {
57 40           SvUTF8_on(result);
58             }
59             #else
60             SvUTF8_on(result);
61             #endif
62             }
63 42           xh_writer_destroy(&ctx->writer);
64              
65 42           return result;
66             }
67              
68             #ifdef XH_HAVE_DOM
69             SV *
70             xh_h2d(xh_h2x_ctx_t *ctx)
71             {
72             dXCPT;
73              
74             xmlDocPtr doc = xmlNewDoc(BAD_CAST ctx->opts.version);
75             if (doc == NULL) {
76             croak("Can't create new document");
77             }
78             if (ctx->opts.encoding[0] == '\0') {
79             doc->encoding = (const xmlChar*) xmlStrdup((const xmlChar*) XH_INTERNAL_ENCODING);
80             }
81             else {
82             doc->encoding = (const xmlChar*) xmlStrdup((const xmlChar*) ctx->opts.encoding);
83             }
84              
85             XCPT_TRY_START
86             {
87             xh_stack_init(&ctx->stash, XH_H2X_STASH_SIZE, sizeof(SV *));
88             switch (ctx->opts.method) {
89             case XH_METHOD_NATIVE:
90             xh_h2d_native(ctx, (xmlNodePtr) doc, ctx->opts.root, xh_strlen(ctx->opts.root), SvRV(ctx->hash));
91             break;
92             case XH_METHOD_NATIVE_ATTR_MODE:
93             (void) xh_h2d_native_attr(ctx, (xmlNodePtr) doc, ctx->opts.root, xh_strlen(ctx->opts.root), SvRV(ctx->hash), XH_H2X_F_COMPLEX);
94             break;
95             case XH_METHOD_LX:
96             xh_h2d_lx(ctx, (xmlNodePtr) doc, ctx->hash, NULL, 0, XH_H2X_F_NONE);
97             break;
98             default:
99             croak("Invalid method");
100             }
101             } XCPT_TRY_END
102              
103             XCPT_CATCH
104             {
105             xh_stash_clean(&ctx->stash);
106             XCPT_RETHROW;
107             }
108              
109             xh_stash_clean(&ctx->stash);
110              
111             return x_PmmNodeToSv((xmlNodePtr) doc, NULL);
112             }
113             #endif