| 1 |
|
| |
| 2 |
|
| # The direct codec and the importer. |
| 3 |
|
| |
| 4 |
|
| MODULE = File::Raw::XML PACKAGE = File::Raw::XML |
| 5 |
|
| |
| 6 |
|
| void |
| 7 |
|
| import(...) |
| 8 |
|
| PREINIT: |
| 9 |
|
| HV *caller; |
| 10 |
|
| const char *pkg; |
| 11 |
|
| I32 i; |
| 12 |
43 |
| int want_codec = 0, want_const = 0, want_events = 0; |
| 13 |
|
| PPCODE: |
| 14 |
|
| /* `use File::Raw::XML qw(file_xml_decode file_xml_events)`, |
| 15 |
|
| * `:codec`, `:all`, `:const`. Anything else warns and is skipped, |
| 16 |
|
| * as File::Raw::JSON does; a bare `use` exports nothing. */ |
| 17 |
43 |
| caller = CopSTASH(PL_curcop); |
| 18 |
43 |
▶ | pkg = HvNAME(caller); |
Branch: 6 branches
| Branch | True | False | Total |
| pkg = HvNAME(caller); |
43 |
0 |
43 |
| pkg = HvNAME(caller); |
43 |
0 |
43 |
| pkg = HvNAME(caller); |
0 |
43 |
43 |
| pkg = HvNAME(caller); |
0 |
0 |
0 |
| pkg = HvNAME(caller); |
43 |
0 |
43 |
| pkg = HvNAME(caller); |
0 |
43 |
43 |
|
| 19 |
76 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
33 |
43 |
76 |
|
| 20 |
|
| STRLEN n; |
| 21 |
33 |
| const char *a = SvPV(ST(i), n); |
| 22 |
33 |
▶ | if ((n == 6 && memcmp(a, ":codec", 6) == 0) || (n == 4 && memcmp(a, ":all", 4) == 0)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if ((n == 6 && memcmp(a, ":codec", 6) == 0) || (n == 4 && memcmp(a, ":all", 4) == 0)) { |
2 |
31 |
33 |
| if ((n == 6 && memcmp(a, ":codec", 6) == 0) || (n == 4 && memcmp(a, ":all", 4) == 0)) { |
1 |
1 |
2 |
| if ((n == 6 && memcmp(a, ":codec", 6) == 0) || (n == 4 && memcmp(a, ":all", 4) == 0)) { |
7 |
25 |
32 |
| if ((n == 6 && memcmp(a, ":codec", 6) == 0) || (n == 4 && memcmp(a, ":all", 4) == 0)) { |
7 |
0 |
7 |
|
| 23 |
8 |
| want_codec = want_events = 1; |
| 24 |
8 |
▶ | if (n == 4) want_const = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 4) want_const = 1; |
7 |
1 |
8 |
|
| 25 |
25 |
▶ | } else if (n == 6 && memcmp(a, ":const", 6) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (n == 6 && memcmp(a, ":const", 6) == 0) { |
1 |
24 |
25 |
| } else if (n == 6 && memcmp(a, ":const", 6) == 0) { |
1 |
0 |
1 |
|
| 26 |
1 |
| want_const = 1; |
| 27 |
24 |
▶ | } else if (n == 15 && memcmp(a, "file_xml_decode", 15) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (n == 15 && memcmp(a, "file_xml_decode", 15) == 0) { |
24 |
0 |
24 |
| } else if (n == 15 && memcmp(a, "file_xml_decode", 15) == 0) { |
22 |
2 |
24 |
|
| 28 |
22 |
| want_codec = 1; |
| 29 |
2 |
▶ | } else if (n == 15 && memcmp(a, "file_xml_events", 15) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (n == 15 && memcmp(a, "file_xml_events", 15) == 0) { |
2 |
0 |
2 |
| } else if (n == 15 && memcmp(a, "file_xml_events", 15) == 0) { |
1 |
1 |
2 |
|
| 30 |
1 |
| want_events = 1; |
| 31 |
|
| } else { |
| 32 |
1 |
| warn("File::Raw::XML: %.*s is not exported", (int)n, a); |
| 33 |
|
| } |
| 34 |
|
| } |
| 35 |
43 |
▶ | if (want_codec) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (want_codec) { |
30 |
13 |
43 |
|
| 36 |
30 |
| CV *src = get_cv("File::Raw::XML::file_xml_decode", 0); |
| 37 |
30 |
| SV *dst = sv_2mortal(newSVpvf("%s::file_xml_decode", pkg)); |
| 38 |
30 |
▶ | if (!src) croak("File::Raw::XML: file_xml_decode is not loaded"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) croak("File::Raw::XML: file_xml_decode is not loaded"); |
0 |
30 |
30 |
|
| 39 |
|
| /* newXS overwrites an existing destination CV */ |
| 40 |
60 |
| newXS(SvPV_nolen(dst), CvXSUB(src), __FILE__); |
| 41 |
|
| } |
| 42 |
43 |
▶ | if (want_events) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (want_events) { |
9 |
34 |
43 |
|
| 43 |
9 |
| CV *src = get_cv("File::Raw::XML::file_xml_events", 0); |
| 44 |
9 |
| SV *dst = sv_2mortal(newSVpvf("%s::file_xml_events", pkg)); |
| 45 |
9 |
▶ | if (!src) croak("File::Raw::XML: file_xml_events is not loaded"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) croak("File::Raw::XML: file_xml_events is not loaded"); |
0 |
9 |
9 |
|
| 46 |
18 |
| newXS(SvPV_nolen(dst), CvXSUB(src), __FILE__); |
| 47 |
|
| } |
| 48 |
43 |
▶ | if (want_const) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (want_const) { |
8 |
35 |
43 |
|
| 49 |
8 |
| newCONSTSUB(caller, "FRX_ELEMENT", newSViv(FRX_ELEMENT)); |
| 50 |
8 |
| newCONSTSUB(caller, "FRX_TEXT", newSViv(FRX_TEXT)); |
| 51 |
8 |
| newCONSTSUB(caller, "FRX_COMMENT", newSViv(FRX_COMMENT)); |
| 52 |
8 |
| newCONSTSUB(caller, "FRX_PI", newSViv(FRX_PI)); |
| 53 |
8 |
| newCONSTSUB(caller, "FRX_DOCUMENT", newSViv(FRX_DOCUMENT)); |
| 54 |
8 |
| newCONSTSUB(caller, "FRX_START", newSViv(FRX_START)); |
| 55 |
8 |
| newCONSTSUB(caller, "FRX_END", newSViv(FRX_END)); |
| 56 |
8 |
| newCONSTSUB(caller, "FRX_DOCTYPE", newSViv(FRX_DOCTYPE)); |
| 57 |
|
| } |
| 58 |
43 |
| XSRETURN_EMPTY; |
| 59 |
|
| |
| 60 |
|
| SV * |
| 61 |
|
| file_xml_decode(bytes, ...) |
| 62 |
|
| SV *bytes |
| 63 |
|
| PREINIT: |
| 64 |
|
| STRLEN len; |
| 65 |
|
| const char *in; |
| 66 |
|
| HV *opts; |
| 67 |
|
| CODE: |
| 68 |
27973 |
| in = frx_input(aTHX_ bytes, &len); |
| 69 |
27973 |
| opts = frx_opts_hv(aTHX_ &ST(0), 1, items, "file_xml_decode"); |
| 70 |
27972 |
| RETVAL = frx_parse_to_sv(aTHX_ in, len, opts, NULL); |
| 71 |
|
| OUTPUT: |
| 72 |
|
| RETVAL |
| 73 |
|
| |
| 74 |
|
| # The push form over the same reader: the option tail is split into the |
| 75 |
|
| # callbacks, indexed by the event kind they answer to, and the rest, |
| 76 |
|
| # which are the reader's options. The dispatch is an array index and a |
| 77 |
|
| # call_sv - no method call and no hash lookup per event. |
| 78 |
|
| void |
| 79 |
|
| file_xml_events(bytes, ...) |
| 80 |
|
| SV *bytes |
| 81 |
|
| PREINIT: |
| 82 |
|
| STRLEN len; |
| 83 |
|
| const char *in; |
| 84 |
|
| HV *opts; |
| 85 |
|
| SV *cb[FRX_EVENT_KINDS]; |
| 86 |
|
| frx_reader_xs *x; |
| 87 |
|
| SV *rsv; |
| 88 |
|
| frx_event ev; |
| 89 |
|
| int rc, k; |
| 90 |
|
| I32 i; |
| 91 |
|
| CODE: |
| 92 |
171 |
▶ | for (k = 0; k < FRX_EVENT_KINDS; k++) cb[k] = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < FRX_EVENT_KINDS; k++) cb[k] = NULL; |
152 |
19 |
171 |
|
| 93 |
|
| |
| 94 |
19 |
▶ | if (((items - 1) & 1) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (((items - 1) & 1) != 0) |
2 |
17 |
19 |
|
| 95 |
2 |
| croak("File::Raw::XML: file_xml_events: options must be key/value pairs"); |
| 96 |
|
| |
| 97 |
17 |
| opts = (HV *)sv_2mortal((SV *)newHV()); |
| 98 |
61 |
▶ | for (i = 1; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i += 2) { |
46 |
15 |
61 |
|
| 99 |
|
| STRLEN klen; |
| 100 |
46 |
| const char *key = SvPV(ST(i), klen); |
| 101 |
46 |
| int kind = frx_event_kind_named(key, klen); |
| 102 |
|
| |
| 103 |
46 |
▶ | if (kind < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind < 0) { |
8 |
38 |
46 |
|
| 104 |
8 |
| (void)hv_store(opts, key, (I32)klen, newSVsv(ST(i + 1)), 0); |
| 105 |
8 |
| continue; |
| 106 |
|
| } |
| 107 |
38 |
▶ | if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVCV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVCV) |
36 |
2 |
38 |
| if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVCV) |
0 |
36 |
36 |
|
| 108 |
2 |
| croak("File::Raw::XML: file_xml_events: the %.*s callback is not a code reference", |
| 109 |
|
| (int)klen, key); |
| 110 |
36 |
| cb[kind] = ST(i + 1); |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
15 |
| in = frx_input(aTHX_ bytes, &len); |
| 114 |
15 |
| x = frx_reader_xs_new(aTHX_ opts, NULL); |
| 115 |
13 |
| rsv = sv_2mortal(frx_reader_bless(aTHX_ x, FRX_READER_CLASS)); |
| 116 |
|
| |
| 117 |
13 |
▶ | if (!frx_reader_feed(&x->r, in, len, 1)) frx_reader_croak(aTHX_ x); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!frx_reader_feed(&x->r, in, len, 1)) frx_reader_croak(aTHX_ x); |
0 |
13 |
13 |
|
| 118 |
|
| |
| 119 |
|
| for (;;) { |
| 120 |
152 |
| rc = frx_reader_next(&x->r, &ev); |
| 121 |
152 |
▶ | if (x->r.failed) frx_reader_croak(aTHX_ x); |
Branch: 1 branch
| Branch | True | False | Total |
| if (x->r.failed) frx_reader_croak(aTHX_ x); |
2 |
150 |
152 |
|
| 122 |
150 |
▶ | if (rc <= 0) break; /* the document ended; every byte was fed */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) break; /* the document ended; every byte was fed */ |
9 |
141 |
150 |
|
| 123 |
141 |
▶ | if (ev.kind < 0 || ev.kind >= FRX_EVENT_KINDS || !cb[ev.kind]) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (ev.kind < 0 || ev.kind >= FRX_EVENT_KINDS || !cb[ev.kind]) continue; |
141 |
0 |
141 |
| if (ev.kind < 0 || ev.kind >= FRX_EVENT_KINDS || !cb[ev.kind]) continue; |
141 |
0 |
141 |
| if (ev.kind < 0 || ev.kind >= FRX_EVENT_KINDS || !cb[ev.kind]) continue; |
36 |
105 |
141 |
|
| 124 |
|
| |
| 125 |
105 |
| ENTER; |
| 126 |
105 |
| SAVETMPS; |
| 127 |
105 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
105 |
105 |
|
| 128 |
105 |
▶ | XPUSHs(rsv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(rsv); |
0 |
105 |
105 |
|
| 129 |
105 |
| PUTBACK; |
| 130 |
105 |
| call_sv(cb[ev.kind], G_VOID | G_DISCARD); |
| 131 |
103 |
| SPAGAIN; |
| 132 |
103 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
0 |
103 |
103 |
|
| 133 |
103 |
| LEAVE; |
| 134 |
|
| } |