xs/reader.xs

filter: Statementstmt 78.8% 82 / 104 Branchbran 68.4% 63 / 92 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.9% 145 / 196 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_abi_impl.h ↑ Summary xs/xpath.xs »
1  
2 # File::Raw::XML::Reader: the pull reader. Bytes in through feed or from
3 # the file from_file opened, one event at a time out through next, the
4 # accessors over the current event.
5  
6 MODULE = File::Raw::XML PACKAGE = File::Raw::XML::Reader
7  
8 SV *
9 new(class, ...)
10 const char *class
11 PREINIT:
12 HV *opts;
13 frx_reader_xs *x;
14 CODE:
15 38 opts = frx_opts_hv(aTHX_ &ST(0), 1, items, "new");
16 38 x = frx_reader_xs_new(aTHX_ opts, NULL);
17 36 RETVAL = frx_reader_bless(aTHX_ x, class);
18 OUTPUT:
19 RETVAL
20  
21 void
22 feed(self, bytes, ...)
23 SV *self
24 SV *bytes
25 PREINIT:
26 frx_reader_xs *x;
27 STRLEN len;
28 const char *in;
29 int eof;
30 PPCODE:
31 1720 x = frx_reader_from_sv(aTHX_ self, "feed");
32 1720 in = frx_input(aTHX_ bytes, &len);
33 1720 ▶ eof = items > 2 && SvTRUE(ST(2));
Branch: 2 branches
BranchTrueFalseTotal
eof = items > 2 && SvTRUE(ST(2)); 1016 704 1720
eof = items > 2 && SvTRUE(ST(2)); 32 984 1016
34 1720 ▶ if (!frx_reader_feed(&x->r, in, (size_t)len, eof)) frx_reader_croak(aTHX_ x);
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_reader_feed(&x->r, in, (size_t)len, eof)) frx_reader_croak(aTHX_ x); 2 1718 1720
35 1718 XSRETURN_EMPTY;
36  
37 SV *
38 from_file(class, path, ...)
39 const char *class
40 const char *path
41 PREINIT:
42 HV *opts;
43 frx_reader_xs *x;
44 IV h;
45 CODE:
46 810 opts = frx_opts_hv(aTHX_ &ST(0), 2, items, "from_file");
47 810 x = frx_reader_xs_new(aTHX_ opts, NULL);
48 810 h = file_chunk_open(aTHX_ path, FRX_READER_CHUNK);
49 810 ▶ if (h < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (h < 0) { 2 808 810
50 2 int e = errno;
51 2 frx_reader_xs_free(aTHX_ x);
52 2 croak("File::Raw::XML::Reader: cannot open %s: %s", path, Strerror(e));
53 }
54 808 x->chunk = h;
55 808 RETVAL = frx_reader_bless(aTHX_ x, class);
56 OUTPUT:
57 RETVAL
58  
59 SV *
60 next(self)
61 SV *self
62 PREINIT:
63 frx_reader_xs *x;
64 frx_event ev;
65 int rc;
66 CODE:
67 342868 x = frx_reader_from_sv(aTHX_ self, "next");
68 /* An event, or the end of the document, or "more bytes". A reader
69 * with a file behind it answers the third itself: it pulls the
70 * next chunk through File::Raw and feeds it, here, without going
71 * back out to Perl. A reader fed by hand returns 0 and its caller
72 * feeds. */
73 842 for (;;) {
74 const char *buf;
75 IV n;
76  
77 343710 rc = frx_reader_next(&x->r, &ev);
78 343710 ▶ if (x->r.failed) frx_reader_croak(aTHX_ x);
Branch: 1 branch
BranchTrueFalseTotal
if (x->r.failed) frx_reader_croak(aTHX_ x); 6 343704 343710
79 343704 ▶ if (rc == -2) croak("File::Raw::XML: next: a subtree is being captured; feed and call subtree again");
Branch: 1 branch
BranchTrueFalseTotal
if (rc == -2) croak("File::Raw::XML: next: a subtree is being captured; feed and call subtree again"); 2 343702 343704
80 343702 ▶ if (rc != 0 || x->chunk < 0) break;
Branch: 2 branches
BranchTrueFalseTotal
if (rc != 0 || x->chunk < 0) break; 2527 341175 343702
if (rc != 0 || x->chunk < 0) break; 842 1685 2527
81  
82 842 n = file_chunk_read(aTHX_ x->chunk, &buf);
83 842 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 842 842
84 0 int e = errno;
85 0 file_chunk_close(aTHX_ x->chunk);
86 0 x->chunk = -1;
87 0 croak("File::Raw::XML::Reader: read failed: %s", Strerror(e));
88 }
89 842 ▶ if (!frx_reader_feed(&x->r, n ? buf : "", (size_t)n, n == 0))
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_reader_feed(&x->r, n ? buf : "", (size_t)n, n == 0)) 836 6 842
if (!frx_reader_feed(&x->r, n ? buf : "", (size_t)n, n == 0)) 0 842 842
90 0 frx_reader_croak(aTHX_ x);
91 842 ▶ if (n == 0) { /* the file is spent */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { /* the file is spent */ 6 836 842
92 6 file_chunk_close(aTHX_ x->chunk);
93 6 x->chunk = -1;
94 }
95 }
96 342860 ▶ RETVAL = rc > 0 ? newSViv(ev.kind) : rc == 0 ? newSViv(0) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = rc > 0 ? newSViv(ev.kind) : rc == 0 ? newSViv(0) : newSV(0); 341147 1713 342860
RETVAL = rc > 0 ? newSViv(ev.kind) : rc == 0 ? newSViv(0) : newSV(0); 1685 28 1713
97 OUTPUT:
98 RETVAL
99  
100 SV *
101 kind(self)
102 SV *self
103 ALIAS:
104 depth = 1
105 offset = 2
106 empty = 3
107 done = 4
108 PREINIT:
109 frx_reader_xs *x;
110 CODE:
111 635 x = frx_reader_from_sv(aTHX_ self, "kind");
112 635 ▶ if (ix == 4) { RETVAL = newSViv(x->r.done ? 1 : 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 4) { RETVAL = newSViv(x->r.done ? 1 : 0); } 7 628 635
113 628 ▶ else if (!x->r.have_cur) { RETVAL = newSV(0); }
Branch: 1 branch
BranchTrueFalseTotal
else if (!x->r.have_cur) { RETVAL = newSV(0); } 0 628 628
114 628 ▶ else if (ix == 0) RETVAL = newSViv(x->r.cur.kind);
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 0) RETVAL = newSViv(x->r.cur.kind); 0 628 628
115 628 ▶ else if (ix == 1) RETVAL = newSViv(x->r.cur.kind == FRX_START ? x->r.cur.depth + 1 : x->r.cur.depth);
Branch: 2 branches
BranchTrueFalseTotal
else if (ix == 1) RETVAL = newSViv(x->r.cur.kind == FRX_START ? x->r.cur.depth + 1 : x->r.cur.depth); 280 348 628
else if (ix == 1) RETVAL = newSViv(x->r.cur.kind == FRX_START ? x->r.cur.depth + 1 : x->r.cur.depth); 68 212 280
116 348 ▶ else if (ix == 2) RETVAL = newSVuv((UV)x->r.cur.offset);
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 2) RETVAL = newSVuv((UV)x->r.cur.offset); 280 68 348
117 68 else RETVAL = newSViv(x->r.cur.empty ? 1 : 0);
118 OUTPUT:
119 RETVAL
120  
121 SV *
122 name(self)
123 SV *self
124 ALIAS:
125 local = 1
126 ns = 2
127 prefix = 3
128 value = 4
129 target = 5
130 entity = 6
131 PREINIT:
132 frx_reader_xs *x;
133 const frx_event *e;
134 CODE:
135 101555 x = frx_reader_from_sv(aTHX_ self, "name");
136 101555 e = &x->r.cur;
137 101555 ▶ if (!x->r.have_cur) RETVAL = newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!x->r.have_cur) RETVAL = newSV(0); 0 101555 101555
138 101555 else switch (ix) {
139 280 case 0: RETVAL = frx_str_sv(aTHX_ &e->qname); break;
140 100303 case 1: RETVAL = frx_str_sv(aTHX_ &e->local); break;
141 280 case 2: RETVAL = frx_str_sv(aTHX_ &e->ns); break;
142 280 case 3: RETVAL = frx_str_sv(aTHX_ &e->prefix); break;
143 296 case 4: RETVAL = frx_str_sv(aTHX_ &e->value); break;
144 24 ▶ case 5: RETVAL = e->kind == FRX_PI ? frx_str_sv(aTHX_ &e->local) : newSV(0); break;
Branch: 1 branch
BranchTrueFalseTotal
case 5: RETVAL = e->kind == FRX_PI ? frx_str_sv(aTHX_ &e->local) : newSV(0); break; 24 0 24
145 92 ▶ default: RETVAL = e->entity.len ? frx_str_sv(aTHX_ &e->entity) : newSV(0); break;
Branch: 1 branch
BranchTrueFalseTotal
default: RETVAL = e->entity.len ? frx_str_sv(aTHX_ &e->entity) : newSV(0); break; 24 68 92
146 }
147 OUTPUT:
148 RETVAL
149  
150 SV *
151 attrs(self)
152 SV *self
153 PREINIT:
154 frx_reader_xs *x;
155 AV *out;
156 int i;
157 CODE:
158 68 x = frx_reader_from_sv(aTHX_ self, "attrs");
159 68 out = newAV();
160 68 ▶ if (x->r.have_cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->r.have_cur) { 68 0 68
161 120 ▶ for (i = 0; i < x->r.cur.n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->r.cur.n_attrs; i++) { 52 68 120
162 52 const frx_attr *a = &x->r.cur.attrs[i];
163 52 AV *row = newAV();
164 52 av_push(row, frx_str_sv(aTHX_ &a->ns));
165 52 av_push(row, frx_str_sv(aTHX_ &a->prefix));
166 52 av_push(row, frx_str_sv(aTHX_ &a->local));
167 52 av_push(row, frx_str_sv(aTHX_ &a->value));
168 52 av_push(out, newRV_noinc((SV *)row));
169 }
170 }
171 68 RETVAL = newRV_noinc((SV *)out);
172 OUTPUT:
173 RETVAL
174  
175 SV *
176 attr(self, local)
177 SV *self
178 const char *local
179 PREINIT:
180 frx_reader_xs *x;
181 int i;
182 CODE:
183 2 x = frx_reader_from_sv(aTHX_ self, "attr");
184 2 RETVAL = newSV(0);
185 2 ▶ if (x->r.have_cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->r.have_cur) { 2 0 2
186 2 ▶ for (i = 0; i < x->r.cur.n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->r.cur.n_attrs; i++) { 2 0 2
187 2 const frx_attr *a = &x->r.cur.attrs[i];
188 2 ▶ if (frx_str_eq(&a->local, local, strlen(local))) { RETVAL = frx_str_sv(aTHX_ &a->value); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (frx_str_eq(&a->local, local, strlen(local))) { RETVAL = frx_str_sv(aTHX_ &a->value); break; } 2 0 2
189 }
190 }
191 OUTPUT:
192 RETVAL
193  
194 SV *
195 attr_ns(self, ns, local)
196 SV *self
197 SV *ns
198 const char *local
199 PREINIT:
200 frx_reader_xs *x;
201 const char *nsp;
202 int i;
203 CODE:
204 0 x = frx_reader_from_sv(aTHX_ self, "attr_ns");
205 0 nsp = frx_ns_arg(aTHX_ ns);
206 0 RETVAL = newSV(0);
207 0 ▶ if (x->r.have_cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->r.have_cur) { 0 0 0
208 0 ▶ for (i = 0; i < x->r.cur.n_attrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->r.cur.n_attrs; i++) { 0 0 0
209 0 const frx_attr *a = &x->r.cur.attrs[i];
210 0 ▶ if (!frx_str_eq(&a->local, local, strlen(local))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_str_eq(&a->local, local, strlen(local))) continue; 0 0 0
211 0 ▶ if (nsp && !frx_str_eq(&a->ns, nsp, strlen(nsp))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (nsp && !frx_str_eq(&a->ns, nsp, strlen(nsp))) continue; 0 0 0
if (nsp && !frx_str_eq(&a->ns, nsp, strlen(nsp))) continue; 0 0 0
212 0 RETVAL = frx_str_sv(aTHX_ &a->value);
213 0 break;
214 }
215 }
216 OUTPUT:
217 RETVAL
218  
219 SV *
220 doctype(self)
221 SV *self
222 PREINIT:
223 frx_reader_xs *x;
224 CODE:
225 14 x = frx_reader_from_sv(aTHX_ self, "doctype");
226 14 ▶ RETVAL = frx_doctype_sv(aTHX_ x->r.have_cur && x->r.cur.kind == FRX_DOCTYPE ? x->r.cur.doctype : x->r.p.doctype);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = frx_doctype_sv(aTHX_ x->r.have_cur && x->r.cur.kind == FRX_DOCTYPE ? x->r.cur.doctype : x->r.p.doctype); 14 0 14
RETVAL = frx_doctype_sv(aTHX_ x->r.have_cur && x->r.cur.kind == FRX_DOCTYPE ? x->r.cur.doctype : x->r.p.doctype); 14 0 14
227 OUTPUT:
228 RETVAL
229  
230 SV *
231 subtree(self)
232 SV *self
233 PREINIT:
234 frx_reader_xs *x;
235 40010 frx_doc *d = NULL;
236 int rc;
237 CODE:
238 40010 x = frx_reader_from_sv(aTHX_ self, "subtree");
239 /* The same pull loop next has, for the same reason. A reader with
240 * a file behind it answers "more bytes" itself: its caller has no
241 * feed to call, and next refuses while a capture is open, so
242 * without this an element that straddles a chunk boundary could
243 * never complete - subtree would answer undef for ever. */
244 48 for (;;) {
245 const char *buf;
246 IV n;
247  
248 40058 rc = frx_reader_subtree(&x->r, &d);
249 40058 ▶ if (x->r.failed) frx_reader_croak(aTHX_ x);
Branch: 1 branch
BranchTrueFalseTotal
if (x->r.failed) frx_reader_croak(aTHX_ x); 0 40058 40058
250 40058 ▶ if (rc == -2) croak("File::Raw::XML: subtree needs a start event");
Branch: 1 branch
BranchTrueFalseTotal
if (rc == -2) croak("File::Raw::XML: subtree needs a start event"); 2 40056 40058
251 40056 ▶ if (rc != 0 || x->chunk < 0) break;
Branch: 2 branches
BranchTrueFalseTotal
if (rc != 0 || x->chunk < 0) break; 50 40006 40056
if (rc != 0 || x->chunk < 0) break; 48 2 50
252  
253 48 n = file_chunk_read(aTHX_ x->chunk, &buf);
254 48 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 48 48
255 0 int e = errno;
256 0 file_chunk_close(aTHX_ x->chunk);
257 0 x->chunk = -1;
258 0 croak("File::Raw::XML::Reader: read failed: %s", Strerror(e));
259 }
260 48 ▶ if (!frx_reader_feed(&x->r, n ? buf : "", (size_t)n, n == 0))
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_reader_feed(&x->r, n ? buf : "", (size_t)n, n == 0)) 48 0 48
if (!frx_reader_feed(&x->r, n ? buf : "", (size_t)n, n == 0)) 0 48 48
261 0 frx_reader_croak(aTHX_ x);
262 48 ▶ if (n == 0) { /* the file is spent */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { /* the file is spent */ 0 48 48
263 0 file_chunk_close(aTHX_ x->chunk);
264 0 x->chunk = -1;
265 }
266 }
267 40008 ▶ RETVAL = rc > 0 && d ? frx_doc_bless(aTHX_ d) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = rc > 0 && d ? frx_doc_bless(aTHX_ d) : newSV(0); 40006 2 40008
RETVAL = rc > 0 && d ? frx_doc_bless(aTHX_ d) : newSV(0); 40006 0 40006
268 OUTPUT:
269 RETVAL
270  
271 IV
272 capturing(self)
273 SV *self
274 PREINIT:
275 frx_reader_xs *x;
276 CODE:
277 2 x = frx_reader_from_sv(aTHX_ self, "capturing");
278 2 ▶ RETVAL = x->r.capturing ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = x->r.capturing ? 1 : 0; 0 2 2
279 OUTPUT:
280 RETVAL
« include/frx/frx_abi_impl.h ↑ Summary xs/xpath.xs »