/* How the IO layer works:
*
- * Start by getting an io_glue object. Then define its
- * datasource via io_obj_setp_buffer or io_obj_setp_cb. Before
- * using the io_glue object be sure to call io_glue_commit_types().
- * After that data can be read via the io_glue->readcb() method.
+ * Start by getting an io_glue object by calling the appropriate
+ * io_new...() function. After that data can be read via the
+ * io_glue->readcb() method.
*
*/
-#include <stdio.h>
-#ifndef _MSC_VER
-#include <unistd.h>
-#endif
-#include <sys/types.h>
+#include "iolayert.h"
/* #define BBSIZ 1096 */
-#define BBSIZ 1096
+#define BBSIZ 16384
#define IO_FAKE_SEEK 1<<0L
#define IO_TEMP_SEEK 1<<1L
-typedef union { int i; void *p; } iorp;
-
-typedef enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type;
-
-#ifdef _MSC_VER
-typedef int ssize_t;
-#endif
-
-struct _io_glue;
-
-/* Callbacks we give out */
-
-typedef ssize_t(*readp) (struct _io_glue *ig, void *buf, size_t count);
-typedef ssize_t(*writep)(struct _io_glue *ig, const void *buf, size_t count);
-typedef off_t (*seekp) (struct _io_glue *ig, off_t offset, int whence);
-typedef void (*closep)(struct _io_glue *ig);
-typedef ssize_t(*sizep) (struct _io_glue *ig);
-
-
-
-/* Callbacks we get */
-
-typedef ssize_t(*readl) (int fd, void *buf, size_t count);
-typedef ssize_t(*writel)(int fd, const void *buf, size_t count);
-typedef off_t (*seekl) (int fd, off_t offset, int whence);
-typedef ssize_t(*sizel) (int fd);
-
-extern char *io_type_names[];
-
-
-typedef struct _io_blink {
- char buf[BBSIZ];
- /* size_t cnt; */
- size_t len; /* How large is this buffer = BBZIS for now */
- struct _io_blink *next;
- struct _io_blink *prev;
-} io_blink;
-
-
-/* Structures that describe callback interfaces */
-
-typedef struct {
- off_t offset;
- off_t cpos;
-} io_ex_rseek;
-
-
-typedef struct {
- off_t offset;
- off_t cpos;
- io_blink *head;
- io_blink *tail;
- io_blink *cp;
-} io_ex_fseek;
-
-
-typedef struct {
- off_t offset; /* Offset of the source - not used */
- off_t length; /* Total length of chain in bytes */
- io_blink *head; /* Start of chain */
- io_blink *tail; /* End of chain */
- off_t tfill; /* End of stream in last link */
- io_blink *cp; /* Current element of list */
- off_t cpos; /* Offset within the current */
- off_t gpos; /* Global position in stream */
-} io_ex_bchain;
-
-
-/* Structures to describe data sources */
-
-typedef struct {
- io_type type;
- int fd;
-} io_fdseek;
-
-typedef struct {
- io_type type; /* Must be first parameter */
- char *name; /* Data source name */
- char *c;
- size_t len;
-} io_buffer;
-
-typedef struct {
- io_type type; /* Must be first parameter */
- char *name; /* Data source name */
- void *p; /* Callback data */
- readl readcb;
- writel writecb;
- seekl seekcb;
-} io_cb;
-
-typedef union {
- io_type type;
- io_fdseek fdseek;
- io_buffer buffer;
- io_cb cb;
-} io_obj;
-
-typedef struct _io_glue {
- io_obj source;
- int flags; /* Flags */
- void *exdata; /* Pair specific data */
- readp readcb;
- writep writecb;
- seekp seekcb;
- closep closecb;
- sizep sizecb;
-} io_glue;
-
-void io_obj_setp_buffer (io_obj *io, void *p, size_t len);
-void io_obj_setp_cb (io_obj *io, void *p, readl readcb, writel writecb, seekl seekcb);
-void io_glue_commit_types(io_glue *ig);
void io_glue_gettypes (io_glue *ig, int reqmeth);
-
/* XS functions */
io_glue *io_new_fd(int fd);
io_glue *io_new_bufchain(void);
+io_glue *io_new_buffer(const char *data, size_t len, i_io_closebufp_t closecb, void *closedata);
+io_glue *io_new_cb(void *p, i_io_readl_t readcb, i_io_writel_t writecb, i_io_seekl_t seekcb, i_io_closel_t closecb, i_io_destroyl_t destroycb);
size_t io_slurp(io_glue *ig, unsigned char **c);
-void io_glue_DESTROY(io_glue *ig);
+void io_glue_destroy(io_glue *ig);
+
+void i_io_dump(io_glue *ig, int flags);
+
+/* Buffered I/O */
+extern int i_io_getc_imp(io_glue *ig);
+extern int i_io_peekc_imp(io_glue *ig);
+extern ssize_t i_io_peekn(io_glue *ig, void *buf, size_t size);
+extern int i_io_putc_imp(io_glue *ig, int c);
+extern ssize_t i_io_read(io_glue *ig, void *buf, size_t size);
+extern ssize_t i_io_write(io_glue *ig, const void *buf, size_t size);
+extern off_t i_io_seek(io_glue *ig, off_t offset, int whence);
+extern int i_io_flush(io_glue *ig);
+extern int i_io_close(io_glue *ig);
+extern int i_io_set_buffered(io_glue *ig, int buffered);
+extern ssize_t i_io_gets(io_glue *ig, char *, size_t, int);
#endif /* _IOLAYER_H_ */