| 1 |
|
| #ifndef _XH_WRITER_H_ |
| 2 |
|
| #define _XH_WRITER_H_ |
| 3 |
|
| |
| 4 |
|
| #include "xh_config.h" |
| 5 |
|
| #include "xh_core.h" |
| 6 |
|
| |
| 7 |
|
| #define XH_WRITER_RESIZE_BUFFER(w, b, l) \ |
| 8 |
|
| if (((l) + 1) > (size_t) (b->end - b->cur)) { \ |
| 9 |
|
| xh_writer_resize_buffer(w, (l) + 1); \ |
| 10 |
|
| } |
| 11 |
|
| |
| 12 |
|
| typedef struct _xh_writer_t xh_writer_t; |
| 13 |
|
| struct _xh_writer_t { |
| 14 |
|
| #ifdef XH_HAVE_ENCODER |
| 15 |
|
| xh_encoder_t *encoder; |
| 16 |
|
| xh_perl_buffer_t enc_buf; |
| 17 |
|
| #endif |
| 18 |
|
| PerlIO *perl_io; |
| 19 |
|
| SV *perl_obj; |
| 20 |
|
| SV *perl_cb; |
| 21 |
|
| xh_perl_buffer_t main_buf; |
| 22 |
|
| xh_int_t indent; |
| 23 |
|
| xh_int_t indent_count; |
| 24 |
|
| xh_bool_t trim; |
| 25 |
|
| xh_bool_t utf8; |
| 26 |
|
| }; |
| 27 |
|
| |
| 28 |
|
| SV *xh_writer_flush_buffer(xh_writer_t *writer, xh_perl_buffer_t *buf); |
| 29 |
|
| SV *xh_writer_flush(xh_writer_t *writer); |
| 30 |
|
| void xh_writer_resize_buffer(xh_writer_t *writer, size_t inc); |
| 31 |
|
| void xh_writer_destroy(xh_writer_t *writer); |
| 32 |
|
| void xh_writer_init(xh_writer_t *writer, xh_char_t *encoding, SV *output, SV *output_cb, size_t size, xh_uint_t indent, xh_bool_t trim, xh_bool_t utf8); |
| 33 |
|
| |
| 34 |
|
| XH_INLINE void |
| 35 |
40 |
| xh_writer_write_to_perl_cb(xh_perl_buffer_t *buf, SV *perl_cb, xh_bool_t utf8) |
| 36 |
|
| { |
| 37 |
40 |
| size_t len = buf->cur - buf->start; |
| 38 |
|
| |
| 39 |
40 |
▶ | if (len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 0) { |
30 |
10 |
40 |
|
| 40 |
|
| SV *chunk; |
| 41 |
30 |
| dSP; |
| 42 |
|
| |
| 43 |
|
| /* The callback owns a stable copy; the writer buffer can be reused |
| 44 |
|
| * immediately, including when the callback throws an exception. */ |
| 45 |
30 |
| buf->cur = buf->start; |
| 46 |
|
| |
| 47 |
30 |
| ENTER; |
| 48 |
30 |
| SAVETMPS; |
| 49 |
|
| |
| 50 |
30 |
| chunk = sv_2mortal(newSVpvn((const char *) buf->start, len)); |
| 51 |
30 |
▶ | if (utf8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (utf8) |
26 |
4 |
30 |
|
| 52 |
26 |
| SvUTF8_on(chunk); |
| 53 |
|
| |
| 54 |
30 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
30 |
30 |
|
| 55 |
30 |
▶ | XPUSHs(chunk); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(chunk); |
0 |
30 |
30 |
|
| 56 |
30 |
| PUTBACK; |
| 57 |
|
| |
| 58 |
30 |
| call_sv(perl_cb, G_DISCARD); |
| 59 |
|
| |
| 60 |
28 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
28 |
0 |
28 |
|
| 61 |
28 |
| LEAVE; |
| 62 |
|
| } |
| 63 |
38 |
| } |
| 64 |
|
| |
| 65 |
|
| XH_INLINE void |
| 66 |
1 |
| xh_writer_write_to_perl_obj(xh_perl_buffer_t *buf, SV *perl_obj) |
| 67 |
|
| { |
| 68 |
1 |
| size_t len = buf->cur - buf->start; |
| 69 |
|
| |
| 70 |
1 |
▶ | if (len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 0) { |
1 |
0 |
1 |
|
| 71 |
1 |
| dSP; |
| 72 |
|
| |
| 73 |
1 |
| *buf->cur = '\0'; |
| 74 |
1 |
| SvCUR_set(buf->scalar, len); |
| 75 |
|
| |
| 76 |
1 |
| ENTER; |
| 77 |
1 |
| SAVETMPS; |
| 78 |
|
| |
| 79 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 80 |
1 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
1 |
1 |
|
| 81 |
1 |
| PUSHs((SV *) perl_obj); |
| 82 |
1 |
| PUSHs(buf->scalar); |
| 83 |
1 |
| PUTBACK; |
| 84 |
|
| |
| 85 |
1 |
| call_method("PRINT", G_DISCARD); |
| 86 |
|
| |
| 87 |
1 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
1 |
0 |
1 |
|
| 88 |
1 |
| LEAVE; |
| 89 |
|
| |
| 90 |
1 |
| buf->cur = buf->start; |
| 91 |
|
| } |
| 92 |
1 |
| } |
| 93 |
|
| |
| 94 |
|
| XH_INLINE void |
| 95 |
3 |
| xh_writer_write_to_perl_io(xh_perl_buffer_t *buf, PerlIO *perl_io) |
| 96 |
|
| { |
| 97 |
3 |
| size_t len = buf->cur - buf->start; |
| 98 |
|
| |
| 99 |
3 |
▶ | if (len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 0) { |
3 |
0 |
3 |
|
| 100 |
3 |
| *buf->cur = '\0'; |
| 101 |
3 |
| SvCUR_set(buf->scalar, len); |
| 102 |
|
| |
| 103 |
3 |
| PerlIO_write(perl_io, buf->start, len); |
| 104 |
|
| |
| 105 |
3 |
| buf->cur = buf->start; |
| 106 |
|
| } |
| 107 |
3 |
| } |
| 108 |
|
| |
| 109 |
|
| XH_INLINE SV * |
| 110 |
46 |
| xh_writer_write_to_perl_scalar(xh_perl_buffer_t *buf) |
| 111 |
|
| { |
| 112 |
46 |
| *buf->cur = '\0'; |
| 113 |
46 |
| SvCUR_set(buf->scalar, buf->cur - buf->start); |
| 114 |
|
| |
| 115 |
46 |
| return buf->scalar; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| #endif /* _XH_WRITER_H_ */ |