| 1 | #ifndef IMAGER_IOLAYERT_H |
| 2 | #define IMAGER_IOLAYERT_H |
| 3 | |
| 4 | #ifndef _MSC_VER |
| 5 | #include <unistd.h> |
| 6 | #endif |
| 7 | #include <sys/types.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdio.h> |
| 10 | |
| 11 | typedef enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type; |
| 12 | |
| 13 | #ifdef _MSC_VER |
| 14 | typedef int ssize_t; |
| 15 | #endif |
| 16 | |
| 17 | typedef struct i_io_glue_t i_io_glue_t; |
| 18 | |
| 19 | /* compatibility for now */ |
| 20 | typedef i_io_glue_t io_glue; |
| 21 | |
| 22 | /* Callbacks we give out */ |
| 23 | |
| 24 | typedef ssize_t(*i_io_readp_t) (io_glue *ig, void *buf, size_t count); |
| 25 | typedef ssize_t(*i_io_writep_t)(io_glue *ig, const void *buf, size_t count); |
| 26 | typedef off_t (*i_io_seekp_t) (io_glue *ig, off_t offset, int whence); |
| 27 | typedef int (*i_io_closep_t)(io_glue *ig); |
| 28 | typedef ssize_t(*i_io_sizep_t) (io_glue *ig); |
| 29 | |
| 30 | typedef void (*i_io_closebufp_t)(void *p); |
| 31 | typedef void (*i_io_destroyp_t)(i_io_glue_t *ig); |
| 32 | |
| 33 | |
| 34 | /* Callbacks we get */ |
| 35 | |
| 36 | typedef ssize_t(*i_io_readl_t) (void *p, void *buf, size_t count); |
| 37 | typedef ssize_t(*i_io_writel_t)(void *p, const void *buf, size_t count); |
| 38 | typedef off_t (*i_io_seekl_t) (void *p, off_t offset, int whence); |
| 39 | typedef int (*i_io_closel_t)(void *p); |
| 40 | typedef void (*i_io_destroyl_t)(void *p); |
| 41 | typedef ssize_t(*i_io_sizel_t) (void *p); |
| 42 | |
| 43 | extern char *io_type_names[]; |
| 44 | |
| 45 | |
| 46 | |
| 47 | /* Structures to describe data sources */ |
| 48 | |
| 49 | struct i_io_glue_t { |
| 50 | io_type type; |
| 51 | void *exdata; |
| 52 | i_io_readp_t readcb; |
| 53 | i_io_writep_t writecb; |
| 54 | i_io_seekp_t seekcb; |
| 55 | i_io_closep_t closecb; |
| 56 | i_io_sizep_t sizecb; |
| 57 | i_io_destroyp_t destroycb; |
| 58 | unsigned char *buffer; |
| 59 | unsigned char *read_ptr; |
| 60 | unsigned char *read_end; |
| 61 | unsigned char *write_ptr; |
| 62 | unsigned char *write_end; |
| 63 | size_t buf_size; |
| 64 | |
| 65 | /* non-zero if we encountered EOF */ |
| 66 | int buf_eof; |
| 67 | |
| 68 | /* non-zero if we've seen an error */ |
| 69 | int error; |
| 70 | |
| 71 | /* if non-zero we do write buffering (enabled by default) */ |
| 72 | int buffered; |
| 73 | |
| 74 | im_context_t context; |
| 75 | }; |
| 76 | |
| 77 | #define I_IO_DUMP_CALLBACKS 1 |
| 78 | #define I_IO_DUMP_BUFFER 2 |
| 79 | #define I_IO_DUMP_STATUS 4 |
| 80 | #define I_IO_DUMP_DEFAULT (I_IO_DUMP_BUFFER | I_IO_DUMP_STATUS) |
| 81 | |
| 82 | #define i_io_type(ig) ((ig)->source.ig_type) |
| 83 | #define i_io_raw_read(ig, buf, size) ((ig)->readcb((ig), (buf), (size))) |
| 84 | #define i_io_raw_write(ig, data, size) ((ig)->writecb((ig), (data), (size))) |
| 85 | #define i_io_raw_seek(ig, offset, whence) ((ig)->seekcb((ig), (offset), (whence))) |
| 86 | #define i_io_raw_close(ig) ((ig)->closecb(ig)) |
| 87 | #define i_io_is_buffered(ig) ((int)((ig)->buffered)) |
| 88 | |
| 89 | #define i_io_getc(ig) \ |
| 90 | ((ig)->read_ptr < (ig)->read_end ? \ |
| 91 | *((ig)->read_ptr++) : \ |
| 92 | i_io_getc_imp(ig)) |
| 93 | #define i_io_nextc(ig) \ |
| 94 | ((void)((ig)->read_ptr < (ig)->read_end ? \ |
| 95 | ((ig)->read_ptr++, 0) : \ |
| 96 | i_io_getc_imp(ig))) |
| 97 | #define i_io_peekc(ig) \ |
| 98 | ((ig)->read_ptr < (ig)->read_end ? \ |
| 99 | *((ig)->read_ptr) : \ |
| 100 | i_io_peekc_imp(ig)) |
| 101 | #define i_io_putc(ig, c) \ |
| 102 | ((ig)->write_ptr < (ig)->write_end && !(ig)->error ? \ |
| 103 | *(ig)->write_ptr++ = (c) : \ |
| 104 | i_io_putc_imp(ig, (c))) |
| 105 | #define i_io_eof(ig) \ |
| 106 | ((ig)->read_ptr == (ig)->read_end && (ig)->buf_eof) |
| 107 | #define i_io_error(ig) \ |
| 108 | ((ig)->read_ptr == (ig)->read_end && (ig)->error) |
| 109 | |
| 110 | #endif |