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 24 frx_stream_free(pTHX_ frx_stream_state *st)
54 {
55 24 ▶ if (!st) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) return; 0 24 24
56 24 frx_reader_xs_free(aTHX_ st->x);
57 24 free(st->rec_ns);
58 24 free(st->rec_local);
59 24 free(st);
60 }
61  
62 static frx_stream_state *
63 27 frx_stream_new(pTHX_ FilePluginContext *ctx)
64 {
65 frx_stream_state *st;
66 27 SV **rec = hv_fetchs(ctx->options, "record", 0);
67 AV *av;
68 SV **e;
69 27 ▶ 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) 25 2 27
if (!rec || !SvROK(*rec) || SvTYPE(SvRV(*rec)) != SVt_PVAV || av_len((AV *)SvRV(*rec)) != 1) 25 0 25
if (!rec || !SvROK(*rec) || SvTYPE(SvRV(*rec)) != SVt_PVAV || av_len((AV *)SvRV(*rec)) != 1) 25 0 25
if (!rec || !SvROK(*rec) || SvTYPE(SvRV(*rec)) != SVt_PVAV || av_len((AV *)SvRV(*rec)) != 1) 0 25 25
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 25 av = (AV *)SvRV(*rec);
73 25 st = (frx_stream_state *)malloc(sizeof *st);
74 25 ▶ if (!st) croak("File::Raw::XML: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!st) croak("File::Raw::XML: out of memory"); 0 25 25
75 25 memset(st, 0, sizeof *st);
76 25 e = av_fetch(av, 0, 0);
77 49 ▶ 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)); 25 0 25
if (e && SvOK(*e)) st->rec_ns = frx_strdup(SvPV_nolen(*e)); 24 1 25
78 25 e = av_fetch(av, 1, 0);
79 25 ▶ 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"); } 25 0 25
if (!e || !SvOK(*e)) { free(st->rec_ns); free(st); croak("File::Raw::XML: record needs a local name"); } 0 25 25
80 50 st->rec_local = frx_strdup(SvPV_nolen(*e));
81 25 st->x = frx_reader_xs_new(aTHX_ ctx->options, ctx->path);
82 24 return st;
83 }
84  
85 static int
86 1441530 frx_stream_matches(const frx_stream_state *st, const frx_event *e)
87 {
88 1441530 ▶ if (e->kind != FRX_START) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e->kind != FRX_START) return 0; 720753 720777 1441530
89 720777 ▶ 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; 28 720749 720777
90 720749 ▶ 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; 720747 2 720749
if (st->rec_ns && !frx_str_eq(&e->ns, st->rec_ns, strlen(st->rec_ns))) return 0; 0 720747 720747
91 720749 return 1;
92 }
93  
94 /* a finished record to the callback; a die inside it ends the stream */
95 static void
96 720749 frx_stream_emit(pTHX_ FilePluginContext *ctx, frx_stream_state *st, frx_doc *d)
97 {
98 720749 dSP;
99 720749 ENTER;
100 720749 SAVETMPS;
101 720749 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 720749 720749
102 720749 ▶ XPUSHs(sv_2mortal(frx_doc_bless(aTHX_ d)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(frx_doc_bless(aTHX_ d))); 0 720749 720749
103 720749 PUTBACK;
104 720749 call_sv(ctx->callback, G_VOID | G_DISCARD | G_EVAL);
105 720749 SPAGAIN;
106 720749 PUTBACK;
107 720749 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 720749 0 720749
108 720749 LEAVE;
109 720749 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 720749 0 720749
if (SvTRUE(ERRSV)) { 22 720727 720749
110 22 ▶ SV *err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *err = sv_2mortal(newSVsv(ERRSV)); 22 0 22
111 22 ctx->call_state = NULL;
112 22 frx_stream_free(aTHX_ st);
113 22 croak_sv(err);
114 }
115 720727 }
116  
117 static int
118 1631 frx_stream(pTHX_ FilePluginContext *ctx, const char *chunk, size_t len, int eof)
119 {
120 1631 frx_stream_state *st = (frx_stream_state *)ctx->call_state;
121 frx_reader *r;
122 1631 ▶ if (!st) {
Branch: 1 branch
BranchTrueFalseTotal
if (!st) { 27 1604 1631
123 27 st = frx_stream_new(aTHX_ ctx);
124 24 ctx->call_state = st;
125 }
126 1628 r = &st->x->r;
127 1628 ▶ if (!frx_reader_feed(r, chunk ? chunk : "", len, eof)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!frx_reader_feed(r, chunk ? chunk : "", len, eof)) { 1626 2 1628
if (!frx_reader_feed(r, chunk ? chunk : "", len, eof)) { 0 1628 1628
128 0 ctx->call_state = NULL;
129 0 frx_stream_free(aTHX_ st);
130 0 frx_reader_croak(aTHX_ st->x);
131 }
132 1441508 for (;;) {
133 frx_event ev;
134 int rc;
135 1443136 ▶ if (r->capturing) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->capturing) { 1193 1441943 1443136
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 1441943 rc = frx_reader_next(r, &ev);
145 1441943 ▶ 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 1441943 1441943
146 1441943 ▶ if (rc == 0) return 0; /* more bytes wanted */
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; /* more bytes wanted */ 411 1441532 1441943
147 1441532 ▶ if (rc < 0) break; /* the document ended */
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) break; /* the document ended */ 2 1441530 1441532
148 1441530 ▶ if (frx_stream_matches(st, &ev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (frx_stream_matches(st, &ev)) { 720749 720781 1441530
149 720749 frx_doc *d = NULL;
150 720749 rc = frx_reader_subtree(r, &d);
151 720749 ▶ 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 720749 720749
152 720749 ▶ if (!rc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!rc) return 0; 1193 719556 720749
153 719556 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 »