XS.xs

filter: Statementstmt 100% 117 / 117 Branchbran 83.3% 70 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.0% 187 / 201 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« toml.c ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 #include <string.h>
9  
10 #include "toml.h"
11 #include "tomlxs.h"
12  
13 #define DOCUMENT_CLASS "TOML::XS::Document"
14 #define TIMESTAMP_CLASS "TOML::XS::Timestamp"
15 #define BOOLEAN_CLASS "TOML::XS"
16  
17 #define CROAK_ERR_MSG_FN "TOML::XS::_croak_err_message_from_pieces"
18  
19 #define PERL_TRUE get_sv(BOOLEAN_CLASS "::true", 0)
20 #define PERL_FALSE get_sv(BOOLEAN_CLASS "::false", 0)
21  
22 #define UNUSED(x) (void)(x)
23  
24 #define ERR_PATH_UNSHIFT(err_path_ptr, sv) STMT_START { \
25 if (NULL == *err_path_ptr) *err_path_ptr = newAV(); \
26 av_unshift(*err_path_ptr, 1); \
27 av_store(*err_path_ptr, 0, sv); \
28 } STMT_END
29  
30 #define _timestamp_to_sv(ts) _ptr_to_svrv(aTHX_ ts, gv_stashpv(TIMESTAMP_CLASS, FALSE))
31  
32 #define _verify_no_null(tomlstr, tomllen) \
33 if (strchr(tomlstr, 0) != (tomlstr + tomllen)) { \
34 croak( \
35 "String contains a NUL at index %" UVf "!", \
36 (UV)(strchr(tomlstr, 0) - tomlstr) \
37 ); \
38 }
39  
40 #define _verify_valid_utf8(tomlstr, tomllen) \
41 if (!is_utf8_string( (const U8*)tomlstr, tomllen )) { \
42 U8* ep; \
43 const U8** epptr = (const U8**) &ep; \
44 is_utf8_string_loc((const U8*)tomlstr, tomllen, epptr); \
45 croak( \
46 "String contains non-UTF-8 at index %" UVf "!", \
47 (UV)((char*) ep - tomlstr) \
48 ); \
49 }
50  
51 #if TOMLXS_SV_CAN_USE_EXTERNAL_STRING
52 /* More efficient: make the SV use the existing string.
53 (Would sv_usepvn() work just as well??)
54 */
55 #define RETURN_IF_DATUM_IS_STRING(d) \
56 if (d.ok) { \
57 SV* ret = newSV(0); \
58 SvUPGRADE(ret, SVt_PV); \
59 SvPV_set(ret, d.u.s); \
60 SvPOK_on(ret); \
61 SvCUR_set(ret, strlen(d.u.s)); \
62 SvLEN_set(ret, SvCUR(ret)); \
63 SvUTF8_on(ret); \
64 return ret; \
65 }
66 #else
67 /* Slow but safe: copy the string into the PV. */
68 #define RETURN_IF_DATUM_IS_STRING(d) \
69 if (d.ok) { \
70 SV* ret = newSVpvn_utf8(d.u.s, strlen(d.u.s), TRUE); \
71 tomlxs_free_string(d.u.s); \
72 return ret; \
73 }
74 #endif
75  
76 #define RETURN_IF_DATUM_IS_BOOLEAN(d) \
77 if (d.ok) { \
78 return SvREFCNT_inc(d.u.b ? PERL_TRUE : PERL_FALSE); \
79 }
80  
81 #define RETURN_IF_DATUM_IS_INTEGER(d) \
82 if (d.ok) { \
83 return newSViv((IV)d.u.i); \
84 }
85  
86 #define RETURN_IF_DATUM_IS_DOUBLE(d) \
87 if (d.ok) { \
88 return newSVnv((NV)d.u.d); \
89 }
90  
91 #define RETURN_IF_DATUM_IS_TIMESTAMP(d) \
92 if (d.ok) { \
93 return _timestamp_to_sv(d.u.ts); \
94 }
95  
96 /* ---------------------------------------------------------------------- */
97  
98 8 SV* _ptr_to_svrv(pTHX_ void* ptr, HV* stash) {
99 8 SV* referent = newSVuv( PTR2UV(ptr) );
100 8 SV* retval = newRV_noinc(referent);
101 8 sv_bless(retval, stash);
102  
103 8 return retval;
104 }
105  
106 8 toml_table_t* _get_toml_table_from_sv(pTHX_ SV *self_sv) {
107 8 SV *referent = SvRV(self_sv);
108 8 return INT2PTR(toml_table_t*, SvUV(referent));
109 }
110  
111 52 toml_timestamp_t* _get_toml_timestamp_from_sv(pTHX_ SV *self_sv) {
112 52 SV *referent = SvRV(self_sv);
113 52 return INT2PTR(toml_timestamp_t*, SvUV(referent));
114 }
115  
116 SV* _toml_table_value_to_sv(pTHX_ toml_table_t* curtab, const char* key, AV** err_path_ptr);
117 SV* _toml_array_value_to_sv(pTHX_ toml_array_t* arr, int i, AV** err_path_ptr);
118  
119 14 SV* _toml_table_to_sv(pTHX_ toml_table_t* tab, AV** err_path_ptr) {
120 int i;
121  
122 /* Doesn’t need to be mortal since this should not throw.
123 Should that ever change this will need to be mortal then
124 de-mortalized.
125 */
126 14 HV* hv = newHV();
127  
128 41 for (i = 0; ; i++) {
129 41 const char* key = toml_key_in(tab, i);
130 41 ▶ if (!key) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!key) break; 11 30 41
131  
132 30 SV* sv = _toml_table_value_to_sv(aTHX_ tab, key, err_path_ptr);
133  
134 30 ▶ if (NULL == sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == sv) { 3 27 30
135 3 SvREFCNT_dec((SV*)hv);
136 3 SV* piece = newSVpv(key, 0);
137 3 sv_utf8_decode(piece);
138 3 ▶ ERR_PATH_UNSHIFT(err_path_ptr, piece);
Branch: 1 branch
BranchTrueFalseTotal
ERR_PATH_UNSHIFT(err_path_ptr, piece); 1 2 3
139 3 return NULL;
140 }
141  
142 27 hv_store(hv, key, strlen(key), sv, 0);
143 }
144  
145 11 return newRV_noinc( (SV *) hv );
146 }
147  
148 7 SV* _toml_array_to_sv(pTHX_ toml_array_t* arr, AV** err_path_ptr) {
149 int i;
150  
151 /* Doesn’t need to be mortal since this should not throw.
152 Should that ever change this will need to be mortal then
153 de-mortalized.
154 */
155 7 AV* av = newAV();
156  
157 7 int size = toml_array_nelem(arr);
158  
159 7 av_extend(av, size - 1);
160  
161 22 ▶ for (i = 0; i<size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i<size; i++) { 17 5 22
162 17 SV* sv = _toml_array_value_to_sv(aTHX_ arr, i, err_path_ptr);
163  
164 17 ▶ if (NULL == sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == sv) { 2 15 17
165 2 SvREFCNT_dec((SV*)av);
166 2 ▶ ERR_PATH_UNSHIFT(err_path_ptr, newSViv(i));
Branch: 1 branch
BranchTrueFalseTotal
ERR_PATH_UNSHIFT(err_path_ptr, newSViv(i)); 1 1 2
167 2 return NULL;
168 }
169  
170 15 av_store(av, i, sv);
171 }
172  
173 5 return newRV( (SV *) av );
174 }
175  
176 30 SV* _toml_table_value_to_sv(pTHX_ toml_table_t* curtab, const char* key, AV** err_path_ptr) {
177 toml_array_t* arr;
178 toml_table_t* tab;
179  
180 30 ▶ if (0 != (arr = toml_array_in(curtab, key))) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != (arr = toml_array_in(curtab, key))) { 5 25 30
181 5 return _toml_array_to_sv(aTHX_ arr, err_path_ptr);
182 }
183  
184 25 ▶ if (0 != (tab = toml_table_in(curtab, key))) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != (tab = toml_table_in(curtab, key))) { 8 17 25
185 8 return _toml_table_to_sv(aTHX_ tab, err_path_ptr);
186 }
187  
188 toml_datum_t d;
189  
190 17 d = toml_string_in(curtab, key);
191 17 ▶ RETURN_IF_DATUM_IS_STRING(d);
Branch: 2 branches
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_STRING(d); 7 10 17
RETURN_IF_DATUM_IS_STRING(d); 7 0 7
192  
193 10 d = toml_bool_in(curtab, key);
194 10 ▶ RETURN_IF_DATUM_IS_BOOLEAN(d);
Branch: 2 branches
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_BOOLEAN(d); 2 8 10
RETURN_IF_DATUM_IS_BOOLEAN(d); 1 1 2
195  
196 8 d = toml_int_in(curtab, key);
197 8 ▶ RETURN_IF_DATUM_IS_INTEGER(d);
Branch: 1 branch
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_INTEGER(d); 1 7 8
198  
199 7 d = toml_double_in(curtab, key);
200 7 ▶ RETURN_IF_DATUM_IS_DOUBLE(d);
Branch: 1 branch
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_DOUBLE(d); 3 4 7
201  
202 4 d = toml_timestamp_in(curtab, key);
203 4 ▶ RETURN_IF_DATUM_IS_TIMESTAMP(d);
Branch: 1 branch
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_TIMESTAMP(d); 3 1 4
204  
205 /* This indicates some unspecified parse error that the initial
206 parse didn’t catch.
207 */
208 1 return NULL;
209 }
210  
211 17 SV* _toml_array_value_to_sv(pTHX_ toml_array_t* curarr, int i, AV** err_path_ptr) {
212 toml_array_t* arr;
213 toml_table_t* tab;
214  
215 17 ▶ if (0 != (arr = toml_array_at(curarr, i))) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != (arr = toml_array_at(curarr, i))) { 2 15 17
216 2 return _toml_array_to_sv(aTHX_ arr, err_path_ptr);
217 }
218  
219 15 ▶ if (0 != (tab = toml_table_at(curarr, i))) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != (tab = toml_table_at(curarr, i))) { 2 13 15
220 2 return _toml_table_to_sv(aTHX_ tab, err_path_ptr);
221 }
222  
223 toml_datum_t d;
224  
225 13 d = toml_string_at(curarr, i);
226 13 ▶ RETURN_IF_DATUM_IS_STRING(d);
Branch: 2 branches
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_STRING(d); 3 10 13
RETURN_IF_DATUM_IS_STRING(d); 3 0 3
227  
228 10 d = toml_bool_at(curarr, i);
229 10 ▶ RETURN_IF_DATUM_IS_BOOLEAN(d);
Branch: 2 branches
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_BOOLEAN(d); 2 8 10
RETURN_IF_DATUM_IS_BOOLEAN(d); 1 1 2
230  
231 8 d = toml_int_at(curarr, i);
232 8 ▶ RETURN_IF_DATUM_IS_INTEGER(d);
Branch: 1 branch
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_INTEGER(d); 4 4 8
233  
234 4 d = toml_double_at(curarr, i);
235 4 ▶ RETURN_IF_DATUM_IS_DOUBLE(d);
Branch: 1 branch
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_DOUBLE(d); 2 2 4
236  
237 2 d = toml_timestamp_at(curarr, i);
238 2 ▶ RETURN_IF_DATUM_IS_TIMESTAMP(d);
Branch: 1 branch
BranchTrueFalseTotal
RETURN_IF_DATUM_IS_TIMESTAMP(d); 1 1 2
239  
240 /* This indicates some unspecified parse error that the initial
241 parse didn’t catch.
242 */
243 1 return NULL;
244 }
245  
246 /* for profiling: */
247 /*
248 #include <sys/time.h>
249  
250 void _print_timeofday(char* label) {
251 struct timeval tp;
252  
253 gettimeofday(&tp, NULL);
254 fprintf(stderr, "%s: %ld.%06d\n", label, tp.tv_sec, tp.tv_usec);
255 }
256 */
257  
258 /* ---------------------------------------------------------------------- */
259  
260 MODULE = TOML::XS PACKAGE = TOML::XS
261  
262 PROTOTYPES: DISABLE
263  
264 SV*
265 from_toml (SV* tomlsv)
266 CODE:
267 STRLEN tomllen;
268 char errbuf[200];
269 8 char* tomlstr = SvPVbyte(tomlsv, tomllen);
270  
271 8 ▶ _verify_no_null(tomlstr, tomllen);
Branch: 1 branch
BranchTrueFalseTotal
_verify_no_null(tomlstr, tomllen); 1 7 8
272  
273 7 ▶ _verify_valid_utf8(tomlstr, tomllen);
Branch: 1 branch
BranchTrueFalseTotal
_verify_valid_utf8(tomlstr, tomllen); 1 6 7
274  
275 6 toml_table_t* tab = toml_parse(tomlstr, errbuf, sizeof(errbuf));
276  
277 6 ▶ if (tab == NULL) croak("%s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (tab == NULL) croak("%s", errbuf); 2 4 6
278  
279 4 RETVAL = _ptr_to_svrv( aTHX_ tab, gv_stashpv(DOCUMENT_CLASS, FALSE) );
280 OUTPUT:
281 RETVAL
282  
283 # ----------------------------------------------------------------------
284  
285 MODULE = TOML::XS PACKAGE = TOML::XS::Document
286  
287 PROTOTYPES: DISABLE
288  
289 SV*
290 to_struct (SV* docsv)
291 CODE:
292 4 toml_table_t* tab = _get_toml_table_from_sv(aTHX_ docsv);
293  
294 4 AV* err_path = NULL;
295  
296 4 RETVAL = _toml_table_to_sv(aTHX_ tab, &err_path);
297  
298 4 ▶ if (NULL == RETVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == RETVAL) { 2 2 4
299 2 dSP;
300  
301 2 ENTER;
302 2 SAVETMPS;
303  
304 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
305 2 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 2 2
306  
307 /* When this mortal reference is reaped it’ll decrement
308 the referent AV’s refcount. */
309 2 mPUSHs(newRV_noinc( (SV*)err_path ));
310  
311 2 PUTBACK;
312  
313 2 call_pv(CROAK_ERR_MSG_FN, G_DISCARD);
314  
315 // Unneeded:
316 // FREETMPS;
317 // LEAVE;
318  
319 assert(0);
320 }
321 OUTPUT:
322 RETVAL
323  
324 void
325 DESTROY (SV* docsv)
326 CODE:
327 4 toml_table_t* tab = _get_toml_table_from_sv(aTHX_ docsv);
328 4 toml_free(tab);
329  
330 # ----------------------------------------------------------------------
331  
332 MODULE = TOML::XS PACKAGE = TOML::XS::Timestamp
333  
334 PROTOTYPES: DISABLE
335  
336 SV*
337 to_string (SV* selfsv)
338 CODE:
339 4 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
340  
341 4 RETVAL = newSVpvs("");
342  
343 4 ▶ if (NULL != ts->year) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != ts->year) { 4 0 4
344 4 sv_catpvf(
345 RETVAL,
346 "%02d-%02d-%02d",
347 *ts->year, *ts->month, *ts->day
348 );
349 }
350  
351 4 ▶ if (NULL != ts->hour) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != ts->hour) { 4 0 4
352 4 sv_catpvf(
353 RETVAL,
354 "T%02d:%02d:%02d",
355 *ts->hour, *ts->minute, *ts->second
356 );
357  
358 4 ▶ if (NULL != ts->millisec) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != ts->millisec) { 1 3 4
359 1 sv_catpvf(
360 RETVAL,
361 ".%03d",
362 *ts->millisec
363 );
364 }
365 }
366  
367 4 ▶ if (NULL != ts->z) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL != ts->z) { 4 0 4
368 4 sv_catpv(RETVAL, ts->z);
369 }
370 OUTPUT:
371 RETVAL
372  
373 SV*
374 year (SV* selfsv)
375 CODE:
376 4 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
377  
378 4 ▶ RETVAL = ts->year ? newSViv(*ts->year) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ts->year ? newSViv(*ts->year) : &PL_sv_undef; 4 0 4
379 OUTPUT:
380 RETVAL
381  
382 SV*
383 month (SV* selfsv)
384 CODE:
385 4 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
386  
387 4 ▶ RETVAL = ts->month ? newSViv(*ts->month) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ts->month ? newSViv(*ts->month) : &PL_sv_undef; 4 0 4
388 OUTPUT:
389 RETVAL
390  
391 SV*
392 day (SV* selfsv)
393 ALIAS:
394 date = 1
395 CODE:
396 UNUSED(ix);
397 8 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
398  
399 8 ▶ RETVAL = ts->day ? newSViv(*ts->day) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ts->day ? newSViv(*ts->day) : &PL_sv_undef; 8 0 8
400 OUTPUT:
401 RETVAL
402  
403 SV*
404 hour (SV* selfsv)
405 ALIAS:
406 hours = 1
407 CODE:
408 UNUSED(ix);
409 8 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
410  
411 8 ▶ RETVAL = ts->hour ? newSViv(*ts->hour) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ts->hour ? newSViv(*ts->hour) : &PL_sv_undef; 8 0 8
412 OUTPUT:
413 RETVAL
414  
415 SV*
416 minute (SV* selfsv)
417 ALIAS:
418 minutes = 1
419 CODE:
420 UNUSED(ix);
421 4 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
422  
423 4 ▶ RETVAL = ts->minute ? newSViv(*ts->minute) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ts->minute ? newSViv(*ts->minute) : &PL_sv_undef; 4 0 4
424 OUTPUT:
425 RETVAL
426  
427 SV*
428 second (SV* selfsv)
429 ALIAS:
430 seconds = 1
431 CODE:
432 UNUSED(ix);
433 4 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
434  
435 4 ▶ RETVAL = ts->second ? newSViv(*ts->second) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ts->second ? newSViv(*ts->second) : &PL_sv_undef; 4 0 4
436 OUTPUT:
437 RETVAL
438  
439 SV*
440 millisecond (SV* selfsv)
441 ALIAS:
442 milliseconds = 1
443 CODE:
444 UNUSED(ix);
445 8 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
446  
447 8 ▶ RETVAL = ts->millisec ? newSViv(*ts->millisec) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ts->millisec ? newSViv(*ts->millisec) : &PL_sv_undef; 2 6 8
448 OUTPUT:
449 RETVAL
450  
451 SV*
452 timezone (SV* selfsv)
453 CODE:
454 4 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
455  
456 4 ▶ RETVAL = ts->z ? newSVpv(ts->z, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ts->z ? newSVpv(ts->z, 0) : &PL_sv_undef; 4 0 4
457 OUTPUT:
458 RETVAL
459  
460 void
461 DESTROY (SV* selfsv)
462 CODE:
463 4 toml_timestamp_t* ts = _get_toml_timestamp_from_sv(aTHX_ selfsv);
464 4 tomlxs_free_timestamp(ts);
« toml.c ↑ Summary