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