--- sio_fd.c 2002/10/22 12:57:20 1.1
+++ sio_fd.c 2002/10/23 17:05:10 1.2
@@ -10,10 +10,17 @@
#include "sio_module.h"
typedef struct {
+ char *mem;
+ size_t size;
+} buffer_t;
+
+typedef struct {
int fd;
- size_t buflen;
- char *buf;
- size_t actual;
+ buffer_t input;
+ size_t written;
+ void *label_data;
+ void *label_error;
+ void *label_eof;
} private_t;
/*
@@ -30,9 +37,14 @@
if (mydata == NULL)
return SIO_ERR_MEM;
- mydata->fd = -1;
- mydata->buflen = 0;
- mydata->buf = NULL;
+ mydata->fd = -1;
+
+ mydata->input.mem = NULL;
+ mydata->input.size = 0;
+
+ sio_label(sio, SIO_LN_DATA, &mydata->label_data);
+ sio_label(sio, SIO_LN_ERROR, &mydata->label_error);
+ sio_label(sio, SIO_LN_EOF, &mydata->label_eof);
*u = mydata;
@@ -44,20 +56,15 @@
*
*/
static
-sio_rc_t fd_configure(sio_t *sio, void *u, void *obj, void *value)
+sio_rc_t fd_configure(sio_t *sio, void *u, void *obj, void *val)
{
private_t *mydata = (private_t *)u;
const char *name = (const char *)obj;
- char *newbuf;
if (!strcmp(name, "fd")) {
- mydata->fd = *(int *)value;
+ mydata->fd = *(int *)val;
} else if (!strcmp(name, "buflen")) {
- mydata->buflen = *(int *)value;
- newbuf = realloc(mydata->buf, mydata->buflen);
- if (newbuf == NULL)
- return SIO_ERR_MEM;
- mydata->buf = newbuf;
+ mydata->input.size = *(size_t *)val;
} else {
return SIO_ERR_ARG;
}
@@ -71,22 +78,50 @@
static
sio_rc_t fd_cleanup(sio_t *sio, void *u)
{
+ return SIO_OK;
+}
+
+static
+sio_rc_t fd_openr(sio_t *sio, al_t *al, void *u)
+{
+ private_t *mydata = (private_t *)u;
+ buffer_t *buf = &mydata->input;
+ char *p;
+ size_t n;
+
+ n = buf->size;
+ p = realloc(buf->mem, n);
+ if (p != NULL)
+ buf->mem = p;
+
+ if (buf->mem == NULL)
+ return SIO_ERR_MEM;
+
+ return SIO_OK;
+}
+
+static
+sio_rc_t fd_closer(sio_t *sio, al_t *al, void *u)
+{
private_t *mydata = (private_t *)u;
+ buffer_t *buf = &mydata->input;
- if (mydata->buf) free(mydata->buf);
- free(mydata);
+ if (buf->mem != NULL) {
+ free(buf->mem);
+ buf->mem = NULL;
+ }
return SIO_OK;
}
static
-sio_rc_t fd_open(sio_t *sio, void *u)
+sio_rc_t fd_openw(sio_t *sio, al_t *al, void *u)
{
return SIO_OK;
}
static
-sio_rc_t fd_close(sio_t *sio, void *u)
+sio_rc_t fd_closew(sio_t *sio, al_t *al, void *u)
{
return SIO_OK;
}
@@ -95,18 +130,21 @@
sio_rc_t fd_input(sio_t *sio, al_t *al, void *u)
{
private_t *mydata = (private_t *)u;
+ buffer_t *buf = &mydata->input;
size_t actual;
- if (mydata->buf == NULL)
- return SIO_ERR_ARG;
-
- actual = read(mydata->fd, mydata->buf, mydata->buflen);
- if (actual < 0)
- return SIO_ERR_SYS;
+ actual = read(mydata->fd, buf->mem, buf->size);
+ if (actual < 0) {
+ al_append_bytes(al, NULL, 0, mydata->label_error);
+ return SIO_OK;
+ } else if (actual == 0) {
+ al_append_bytes(al, NULL, 0, mydata->label_eof);
+ return SIO_OK;
+ }
- al_append_bytes(al, mydata->buf, actual);
+ al_append_bytes(al, buf->mem, actual, mydata->label_data);
- return SIO_OK;
+ return SIO_DOWNSTREAM;
}
static
@@ -123,7 +161,7 @@
if (actual < 0)
return AL_ERR_EOF;
- mydata->actual += actual;
+ mydata->written += actual;
return AL_OK;
}
@@ -135,24 +173,27 @@
al_rc_t arc;
size_t n = al_bytes(al);
- mydata->actual = 0;
+ mydata->written = 0;
- arc = al_traverse_cb(al, 0, n, AL_FORWARD, fd_output_chunk, u);
+ arc = al_traverse_cb(al, 0, n, AL_FORWARD, mydata->label_data,
+ fd_output_chunk, u);
if (arc != AL_OK) return SIO_ERR_INT;
- arc = al_splice(al, 0, mydata->actual, NULL, NULL);
+ arc = al_splice(al, 0, mydata->written, NULL, NULL);
if (arc != AL_OK) return SIO_ERR_INT;
- return SIO_OK;
+ return SIO_DOWNSTREAM;
}
-sio_module_t sio_fd_module = {
+sio_module_t sio_module_fd = {
"fd",
fd_init,
fd_configure,
fd_cleanup,
- fd_open,
- fd_close,
+ fd_openr,
+ fd_closer,
+ fd_openw,
+ fd_closew,
fd_input,
fd_output
};
|