XS.xs

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