1 /* perlio.c - Imager's interface to PerlIO
4 #define IMAGER_NO_CONTEXT
12 perlio_reader(void *handle, void *buf, size_t count);
14 perlio_writer(void *handle, const void *buf, size_t count);
16 perlio_seeker(void *handle, off_t offset, int whence);
18 perlio_closer(void *handle);
20 perlio_destroy(void *handle);
21 static const char *my_strerror(int err);
31 #define dIMCTXperlio(state) dIMCTXctx(state->aIMCTX)
34 =item im_io_new_perlio(PerlIO *)
36 Create a new perl I/O object that reads/writes/seeks on a PerlIO
39 The close() handle flushes output but does not close the handle.
45 im_io_new_perlio(pTHX_ PerlIO *handle) {
46 im_perlio *state = mymalloc(sizeof(im_perlio));
49 state->handle = handle;
53 state->aIMCTX = aIMCTX;
55 return io_new_cb(state, perlio_reader, perlio_writer,
56 perlio_seeker, perlio_closer, perlio_destroy);
60 perlio_reader(void *ctx, void *buf, size_t count) {
61 im_perlio *state = ctx;
62 dTHXa(state->my_perl);
65 ssize_t result = PerlIO_read(state->handle, buf, count);
66 if (result == 0 && PerlIO_error(state->handle)) {
67 im_push_errorf(aIMCTX, errno, "read() failure (%s)", my_strerror(errno));
75 perlio_writer(void *ctx, const void *buf, size_t count) {
76 im_perlio *state = ctx;
77 dTHXa(state->my_perl);
81 result = PerlIO_write(state->handle, buf, count);
84 im_push_errorf(aIMCTX, errno, "write() failure (%s)", my_strerror(errno));
91 perlio_seeker(void *ctx, off_t offset, int whence) {
92 im_perlio *state = ctx;
93 dTHXa(state->my_perl);
96 if (whence != SEEK_CUR || offset != 0) {
97 if (PerlIO_seek(state->handle, offset, whence) < 0) {
98 im_push_errorf(aIMCTX, errno, "seek() failure (%s)", my_strerror(errno));
103 return PerlIO_tell(state->handle);
107 perlio_closer(void *ctx) {
108 im_perlio *state = ctx;
109 dTHXa(state->my_perl);
112 if (PerlIO_flush(state->handle) < 0) {
113 im_push_errorf(aIMCTX, errno, "flush() failure (%s)", my_strerror(errno));
120 perlio_destroy(void *ctx) {
125 const char *my_strerror(int err) {
126 const char *result = strerror(err);
129 result = "Unknown error";