--- sio.c 2002/11/05 16:12:39 1.7
+++ sio.c 2002/11/05 16:53:55 1.8
@@ -30,6 +30,7 @@
#include <stddef.h>
#include <stdlib.h>
+#include <string.h>
#include "al.h"
#include "sio.h"
@@ -73,9 +74,12 @@
LIST(sio_halfduplex_t) hd;
al_t *al;
} writers;
- sio_labelnum_t label_data;
- sio_labelnum_t label_error;
- sio_labelnum_t label_eof;
+ sio_labelnum_t label_data;
+ sio_labelnum_t label_error;
+ sio_labelnum_t label_eof;
+ int eof_flag;
+ int error_flag;
+ char *dst;
};
#define SIO_LABEL_DATA(sio) ((al_label_t)&(sio)->label_data)
#define SIO_LABEL_ERROR(sio) ((al_label_t)&(sio)->label_error)
@@ -484,6 +488,24 @@
return SIO_RC(rc);
}
+al_rc_t sio_readchunk(al_chunk_t *alc, void *u)
+{
+ sio_t *sio = (sio_t *)u;
+ size_t len;
+
+ if (al_same_label(alc, SIO_LABEL_DATA(sio))) {
+ len = al_chunk_len(alc);
+ memcpy(sio->dst, al_chunk_ptr(alc,0), len);
+ sio->dst += len;
+ } else if (al_same_label(alc, SIO_LABEL_ERROR(sio))) {
+ sio->error_flag = 1;
+ } else if (al_same_label(alc, SIO_LABEL_EOF(sio))) {
+ sio->eof_flag = 1;
+ }
+
+ return AL_OK;
+}
+
sio_rc_t sio_read(sio_t *sio, char *dst, size_t n, size_t *actualp)
{
al_rc_t arc;
@@ -497,9 +519,11 @@
if (arc != AL_OK) return SIO_RC(SIO_ERR_INT);
rc = sio_input(sio, al, n);
- if (rc == SIO_OK)
- arc = al_flatten(al, 0, n, AL_FORWARD_SPAN, SIO_LABEL_DATA(sio),
- dst, actualp);
+ if (rc == SIO_OK) {
+ sio->dst = dst;
+ al_traverse_cb(al, 0, n, AL_FORWARD, NULL, sio_readchunk, (void*)sio);
+ *actualp = sio->dst - dst;
+ }
arc = al_destroy(al);
if (arc != AL_OK) return SIO_RC(SIO_ERR_INT);
@@ -531,6 +555,46 @@
return SIO_RC(rc);
}
+int sio_flag(sio_t *sio, sio_flag_t fl)
+{
+ int rc;
+
+ switch (fl) {
+ case SIO_FLAG_ERROR:
+ rc = sio->error_flag;
+ break;
+ case SIO_FLAG_EOF:
+ rc = sio->eof_flag;
+ break;
+ default:
+ rc = 0;
+ break;
+ }
+
+ return rc;
+}
+
+int sio_clearflag(sio_t *sio, sio_flag_t fl)
+{
+ int rc;
+
+ switch (fl) {
+ case SIO_FLAG_ERROR:
+ rc = sio->error_flag;
+ sio->error_flag = 0;
+ break;
+ case SIO_FLAG_EOF:
+ rc = sio->eof_flag;
+ sio->eof_flag = 0;
+ break;
+ default:
+ rc = 0;
+ break;
+ }
+
+ return rc;
+}
+
const char *sio_error(sio_rc_t rc)
{
const char *mess;
|