| 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 |
xh_writer_write_to_perl_cb(xh_perl_buffer_t *buf, SV *perl_cb, xh_bool_t utf8) |
| 36 |
{ |
| 37 |
size_t len = buf->cur - buf->start; |
| 38 |
|
| 39 |
if (len > 0) { |
| 40 |
SV *chunk; |
| 41 |
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 |
buf->cur = buf->start; |
| 46 |
|
| 47 |
ENTER; |
| 48 |
SAVETMPS; |
| 49 |
|
| 50 |
chunk = sv_2mortal(newSVpvn((const char *) buf->start, len)); |
| 51 |
if (utf8) |
| 52 |
SvUTF8_on(chunk); |
| 53 |
|
| 54 |
PUSHMARK(SP); |
| 55 |
XPUSHs(chunk); |
| 56 |
PUTBACK; |
| 57 |
|
| 58 |
call_sv(perl_cb, G_DISCARD); |
| 59 |
|
| 60 |
FREETMPS; |
| 61 |
LEAVE; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
XH_INLINE void |
| 66 |
xh_writer_write_to_perl_obj(xh_perl_buffer_t *buf, SV *perl_obj) |
| 67 |
{ |
| 68 |
size_t len = buf->cur - buf->start; |
| 69 |
|
| 70 |
if (len > 0) { |
| 71 |
dSP; |
| 72 |
|
| 73 |
*buf->cur = '\0'; |
| 74 |
SvCUR_set(buf->scalar, len); |
| 75 |
|
| 76 |
ENTER; |
| 77 |
SAVETMPS; |
| 78 |
|
| 79 |
PUSHMARK(SP); |
| 80 |
EXTEND(SP, 2); |
| 81 |
PUSHs((SV *) perl_obj); |
| 82 |
PUSHs(buf->scalar); |
| 83 |
PUTBACK; |
| 84 |
|
| 85 |
call_method("PRINT", G_DISCARD); |
| 86 |
|
| 87 |
FREETMPS; |
| 88 |
LEAVE; |
| 89 |
|
| 90 |
buf->cur = buf->start; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
XH_INLINE void |
| 95 |
xh_writer_write_to_perl_io(xh_perl_buffer_t *buf, PerlIO *perl_io) |
| 96 |
{ |
| 97 |
size_t len = buf->cur - buf->start; |
| 98 |
|
| 99 |
if (len > 0) { |
| 100 |
*buf->cur = '\0'; |
| 101 |
SvCUR_set(buf->scalar, len); |
| 102 |
|
| 103 |
PerlIO_write(perl_io, buf->start, len); |
| 104 |
|
| 105 |
buf->cur = buf->start; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
XH_INLINE SV * |
| 110 |
xh_writer_write_to_perl_scalar(xh_perl_buffer_t *buf) |
| 111 |
{ |
| 112 |
*buf->cur = '\0'; |
| 113 |
SvCUR_set(buf->scalar, buf->cur - buf->start); |
| 114 |
|
| 115 |
return buf->scalar; |
| 116 |
} |
| 117 |
|
| 118 |
#endif /* _XH_WRITER_H_ */ |