include/frx/frx_plugin.h

filter: Statementstmt 96.0% 96 / 100 Branchbran 69.2% 54 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.2% 150 / 178 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_sink.h ↑ Summary include/frx/frx_entity.h »
1 #ifndef FRX_PLUGIN_H
2 #define FRX_PLUGIN_H
3  
4 /* The File::Raw plugin: file_slurp($path, plugin => 'xml', %opts) and
5 * each_line($path, $cb, plugin => 'xml', record => [$ns, $local], %opts).
6 *
7 * READ parses the file's bytes under the per-call options and returns
8 * the same blessed document the direct codec returns. STREAM feeds each
9 * chunk File::Raw reads to a reader (frx_reader.h) and, for every element
10 * whose namespace and local name match `record`, cuts that element out
11 * as a Document of its own and calls the callback with it, so a file of
12 * any size is read at the memory cost of one record. An element whose
13 * end the chunk does not reach stays in capture across chunks. `record`
14 * is required for the stream phase: without one there is nothing to
15 * emit, and the refusal says so. WRITE serialises a Document with
16 * frx_write.h under the same options to_string takes. RECORD is NULL.
17 *
18 * The FilePlugin is a file-scope static: File::Raw's registry stores the
19 * pointer, not a copy (file_plugin.h). Its fields carry the _fn suffix
20 * because plain read and write collide with Perl's host-IO macros on
21 * Win32, which is why the struct is spelled that way upstream.
22 *
23 * Ownership, from file.c's dispatch: a plugin that replaces the bytes
24 * returns a fresh SV at +1 and File::Raw mortalises it; NULL is "no
25 * result" and becomes undef without being mortalised, which is why the
26 * no-data case returns NULL rather than the immortal undef. The stream
27 * state lives in ctx->call_state for the dispatch and is freed here at
28 * the end or before any croak, since File::Raw does not free it.
29 *
30 * ctx->data is the file's bytes, raw; ctx->options is the per-call HV,
31 * which File::Raw has built with `plugin` in it - frx_opts_from_hv knows
32 * to skip that key, and `record` too when there is a path.
33 *
34 * Needs file_plugin.h, frx_obj.h. */
35  
36 static SV *
37 23 frx_read(pTHX_ FilePluginContext *ctx)
38 {
39 STRLEN len;
40 const char *in;
41 23 ▶ if (!ctx->data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->data) return NULL; 0 23 23
42 23 in = SvPV(ctx->data, len);
43 23 return frx_parse_to_sv(aTHX_ in, len, ctx->options, ctx->path);
44 }
45  
46 typedef struct frx_stream_state {
47 frx_reader_xs *x;
48 char *rec_ns; /* NULL: any namespace */
49 char *rec_local;
50 } frx_stream_state;
51  
52 static void
53 4 frx_stream_free(pTHX_ frx_stream_state *st)
54 {
55 4 ▶ if (!st) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) return; 0 4 4
56 4 frx_reader_xs_free(aTHX_ st->x);
57 4 free(st->rec_ns);
58 4 free(st->rec_local);
59 4 free(st);
60 }
61  
62 static frx_stream_state *
63 7 frx_stream_new(pTHX_ FilePluginContext *ctx)
64 {
65 frx_stream_state *st;
66 7 SV **rec = hv_fetchs(ctx->options, "record", 0);
67 AV *av;
68 SV **e;
69 7 ▶ if (!rec || !SvROK(*rec) || SvTYPE(SvRV(*rec)) != SVt_PVAV || av_len((AV *)SvRV(*rec)) != 1)
Branch: 4 branches
BranchTrueFalseTotal
if (!rec || !SvROK(*rec) || SvTYPE(SvRV(*rec)) != SVt_PVAV || av_len((AV *)SvRV(*rec)) != 1) 5 2 7
if (!rec || !SvROK(*rec) || SvTYPE(SvRV(*rec)) != SVt_PVAV || av_len((AV *)SvRV(*rec)) != 1) 5 0 5
if (!rec || !SvROK(*rec) || SvTYPE(SvRV(*rec)) != SVt_PVAV || av_len((AV *)SvRV(*rec)) != 1) 5 0 5
if (!rec || !SvROK(*rec) || SvTYPE(SvRV(*rec)) != SVt_PVAV || av_len((AV *)SvRV(*rec)) != 1) 0 5 5
70 2 croak("File::Raw::XML: the xml plugin streams records: pass record => [$ns, $local], "
71 "with undef for any namespace and '' for none");
72 5 av = (AV *)SvRV(*rec);
73 5 st = (frx_stream_state *)malloc(sizeof *st);
74 5 ▶ if (!st) croak("File::Raw::XML: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!st) croak("File::Raw::XML: out of memory"); 0 5 5
75 5 memset(st, 0, sizeof *st);
76 5 e = av_fetch(av, 0, 0);
77 9 ▶ if (e && SvOK(*e)) st->rec_ns = frx_strdup(SvPV_nolen(*e));
Branch: 2 branches
BranchTrueFalseTotal
if (e && SvOK(*e)) st->rec_ns = frx_strdup(SvPV_nolen(*e)); 5 0 5
if (e && SvOK(*e)) st->rec_ns = frx_strdup(SvPV_nolen(*e)); 4 1 5
78 5 e = av_fetch(av, 1, 0);
79 5 ▶ if (!e || !SvOK(*e)) { free(st->rec_ns); free(st); croak("File::Raw::XML: record needs a local name"); }
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) { free(st->rec_ns); free(st); croak("File::Raw::XML: record needs a local name"); } 5 0 5
if (!e || !SvOK(*e)) { free(st->rec_ns); free(st); croak("File::Raw::XML: record needs a local name"); } 0 5 5
80 10 st->rec_local = frx_strdup(SvPV_nolen(*e));
81 5 st->x = frx_reader_xs_new(aTHX_ ctx->options, ctx->path);
82 4 return st;
83 }
84  
85 static int
86 1441470 frx_stream_matches(const frx_stream_state *st, const frx_event *e)
87 {
88 1441470 ▶ if (e->kind != FRX_START) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e->kind != FRX_START) return 0; 720733 720737 1441470
89 720737 ▶ if (!frx_str_eq(&e->local, st->rec_local, strlen(st->rec_local))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_str_eq(&e->local, st->rec_local, strlen(st->rec_local))) return 0; 8 720729 720737
90 720729 ▶ if (st->rec_ns && !frx_str_eq(&e->ns, st->rec_ns, strlen(st->rec_ns))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (st->rec_ns && !frx_str_eq(&e->ns, st->rec_ns, strlen(st->rec_ns))) return 0; 720727 2 720729
if (st->rec_ns && !frx_str_eq(&e->ns, st->rec_ns, strlen(st->rec_ns))) return 0; 0 720727 720727
91 720729 return 1;
92 }
93  
94 /* a finished record to the callback; a die inside it ends the stream */
95 static void
96 720729 frx_stream_emit(pTHX_ FilePluginContext *ctx, frx_stream_state *st, frx_doc *d)
97 {
98 720729 dSP;
99 720729 ENTER;
100 720729 SAVETMPS;
101 720729 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 720729 720729
102 720729 ▶ XPUSHs(sv_2mortal(frx_doc_bless(aTHX_ d)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(frx_doc_bless(aTHX_ d))); 0 720729 720729
103 720729 PUTBACK;
104 720729 call_sv(ctx->callback, G_VOID | G_DISCARD | G_EVAL);
105 720729 SPAGAIN;
106 720729 PUTBACK;
107 720729 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 720729 0 720729
108 720729 LEAVE;
109 720729 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 720729 0 720729
if (SvTRUE(ERRSV)) { 2 720727 720729
110 2 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 2 0 2
111 2 ctx->call_state = NULL;
112 2 frx_stream_free(aTHX_ st);
113 2 croak_sv(err);
114 }
115 720727 }
116  
117 static int
118 1611 frx_stream(pTHX_ FilePluginContext *ctx, const char *chunk, size_t len, int eof)
119 {
120 1611 frx_stream_state *st = (frx_stream_state *)ctx->call_state;
121 frx_reader *r;
122 1611 ▶ if (!st) {
Branch: 1 branch
BranchTrueFalseTotal
if (!st) { 7 1604 1611
123 7 st = frx_stream_new(aTHX_ ctx);
124 4 ctx->call_state = st;
125 }
126 1608 r = &st->x->r;
127 1608 ▶ if (!frx_reader_feed(r, chunk ? chunk : "", len, eof)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_reader_feed(r, chunk ? chunk : "", len, eof)) { 1606 2 1608
if (!frx_reader_feed(r, chunk ? chunk : "", len, eof)) { 0 1608 1608
128 0 ctx->call_state = NULL;
129 0 frx_stream_free(aTHX_ st);
130 0 frx_reader_croak(aTHX_ st->x);
131 }
132 1441468 for (;;) {
133 frx_event ev;
134 int rc;
135 1443076 ▶ if (r->capturing) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->capturing) { 1193 1441883 1443076
136 1193 frx_doc *d = NULL;
137 1193 rc = frx_reader_subtree(r, &d);
138 1193 ▶ if (r->failed) { ctx->call_state = NULL; frx_stream_free(aTHX_ st); frx_reader_croak(aTHX_ st->x); }
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) { ctx->call_state = NULL; frx_stream_free(aTHX_ st); frx_reader_croak(aTHX_ st->x); } 0 1193 1193
139 1193 ▶ if (!rc) return 0; /* more bytes wanted */
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return 0; /* more bytes wanted */ 0 1193 1193
140 1193 frx_stream_emit(aTHX_ ctx, st, d);
141 1193 ▶ if (ctx->cancel) break;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cancel) break; 0 1193 1193
142 1193 continue;
143 }
144 1441883 rc = frx_reader_next(r, &ev);
145 1441883 ▶ if (r->failed) { ctx->call_state = NULL; frx_stream_free(aTHX_ st); frx_reader_croak(aTHX_ st->x); }
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) { ctx->call_state = NULL; frx_stream_free(aTHX_ st); frx_reader_croak(aTHX_ st->x); } 0 1441883 1441883
146 1441883 ▶ if (rc == 0) return 0; /* more bytes wanted */
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; /* more bytes wanted */ 411 1441472 1441883
147 1441472 ▶ if (rc < 0) break; /* the document ended */
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) break; /* the document ended */ 2 1441470 1441472
148 1441470 ▶ if (frx_stream_matches(st, &ev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_stream_matches(st, &ev)) { 720729 720741 1441470
149 720729 frx_doc *d = NULL;
150 720729 rc = frx_reader_subtree(r, &d);
151 720729 ▶ if (r->failed) { ctx->call_state = NULL; frx_stream_free(aTHX_ st); frx_reader_croak(aTHX_ st->x); }
Branch: 1 branch
BranchTrueFalseTotal
if (r->failed) { ctx->call_state = NULL; frx_stream_free(aTHX_ st); frx_reader_croak(aTHX_ st->x); } 0 720729 720729
152 720729 ▶ if (!rc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return 0; 1193 719536 720729
153 719536 frx_stream_emit(aTHX_ ctx, st, d);
154 719534 ▶ if (ctx->cancel) break;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->cancel) break; 0 719534 719534
155 }
156 }
157 2 ctx->call_state = NULL;
158 2 frx_stream_free(aTHX_ st);
159 2 return 0;
160 }
161  
162 /* WRITE: file_spew($path, $doc, plugin => 'xml', %opts): the document's
163 * markup under the writer's options; File::Raw writes the bytes to a
164 * temporary file beside the path and renames it into place */
165 static SV *
166 3 frx_write_hook(pTHX_ FilePluginContext *ctx)
167 {
168 frx_doc *d;
169 frx_write_opts o;
170 3 int perl_chars = 0;
171 3 ▶ if (!ctx->data || !SvROK(ctx->data))
Branch: 2 branches
BranchTrueFalseTotal
if (!ctx->data || !SvROK(ctx->data)) 3 0 3
if (!ctx->data || !SvROK(ctx->data)) 1 2 3
172 1 croak("File::Raw::XML: file_spew with the xml plugin takes a File::Raw::XML::Document");
173 2 d = frx_doc_from_sv(aTHX_ ctx->data, "file_spew");
174 2 frx_write_opts_from_hv(aTHX_ ctx->options, &o, &perl_chars, "file_spew", 1);
175 2 ▶ if (perl_chars)
Branch: 1 branch
BranchTrueFalseTotal
if (perl_chars) 1 1 2
176 1 croak("File::Raw::XML: file_spew: encoding => 'perl' is for to_string; a file takes bytes");
177 1 return frx_write_sv(aTHX_ d->document, d, &o, 0, "file_spew");
178 }
179  
180 static FilePlugin frx_plugin; /* filled at BOOT; the registry keeps the pointer */
181  
182 static void
183 37 frx_plugin_register(pTHX)
184 {
185 37 frx_plugin.name = "xml";
186 37 frx_plugin.read_fn = frx_read;
187 37 frx_plugin.write_fn = frx_write_hook;
188 37 frx_plugin.record_fn = NULL;
189 37 frx_plugin.stream_fn = frx_stream;
190 37 frx_plugin.state = NULL;
191 37 ▶ if (file_register_plugin(aTHX_ &frx_plugin) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (file_register_plugin(aTHX_ &frx_plugin) != 1) 0 37 37
192 0 warn("File::Raw::XML: failed to register the 'xml' plugin with File::Raw");
193 37 }
194  
195 #endif /* FRX_PLUGIN_H */
« include/frx/frx_sink.h ↑ Summary include/frx/frx_entity.h »