--- l2_ch_buffer.c 2001/05/11 17:07:52 1.2
+++ l2_ch_buffer.c 2001/05/22 20:00:12 1.3
@@ -30,39 +30,126 @@
#include "l2.h"
#include "l2_p.h"
+#include <string.h>
+
+typedef struct {
+ char *buf;
+ int bufpos;
+ int bufsize;
+} l2_ch_buffer_t;
+
static int hook_create(l2_context_t *ctx)
{
+ l2_ch_buffer_t *cfg;
+
+ if ((cfg = (l2_ch_buffer_t *)malloc(sizeof(l2_ch_buffer_t))) == NULL)
+ return L2_ERROR;
+ cfg->buf = NULL;
+ cfg->bufpos = 0;
+ cfg->bufsize = 4096;
+ ctx->vp = cfg;
return L2_OK;
}
static int hook_configure(l2_context_t *ctx, const char *fmt, va_list ap)
{
- return L2_OK;
+ l2_ch_buffer_t *cfg;
+ l2_param_t pa[3];
+ l2_error_t rv;
+
+ if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
+ return L2_ERROR;
+ L2_PARAM_SET(pa[0], size, INT, &cfg->bufsize);
+ L2_PARAM_END(pa[2]);
+ rv = l2_channel_setparams(pa, fmt, ap);
+ if (cfg->bufsize < 0)
+ return L2_ERROR;
+ return rv;
}
-static int hook_open(l2_context_t *ctx, l2_channel_t *below)
+static int hook_open(l2_context_t *ctx, l2_channel_t *downstream)
{
+ l2_ch_buffer_t *cfg;
+
+ if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
+ return L2_ERROR;
+ if (cfg->bufsize > 0) {
+ if ((cfg->buf = malloc(cfg->bufsize)) == NULL)
+ return L2_ERROR;
+ cfg->bufpos = 0;
+ }
+ if (downstream != NULL)
+ if (l2_channel_open(downstream) == L2_ERROR)
+ return L2_ERROR;
return L2_OK;
}
-static int hook_write(l2_context_t *ctx, l2_channel_t *below,
- const char *buf, size_t buf_size)
+static int hook_write(l2_context_t *ctx, l2_channel_t *downstream,
+ const char *buf, size_t bufsize)
{
+ l2_ch_buffer_t *cfg;
+
+ if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
+ return L2_ERROR;
+
+ if (bufsize > (cfg->bufsize - cfg->bufpos)) {
+ if (downstream != NULL)
+ if (l2_channel_write(downstream, cfg->buf, cfg->bufpos) == L2_ERROR)
+ return L2_ERROR;
+ cfg->bufpos = 0;
+ }
+
+ memcpy(cfg->buf+cfg->bufpos, buf, bufsize);
+ cfg->bufpos += bufsize;
+
return L2_OK;
}
-static int hook_flush(l2_context_t *ctx, l2_channel_t *below)
+static int hook_flush(l2_context_t *ctx, l2_channel_t *downstream)
{
+ l2_ch_buffer_t *cfg;
+
+ if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
+ return L2_ERROR;
+
+ if (cfg->bufpos > 0) {
+ if (downstream != NULL)
+ if (l2_channel_write(downstream, cfg->buf, cfg->bufpos) == L2_ERROR)
+ return L2_ERROR;
+ cfg->bufpos = 0;
+ }
+
+ if (downstream != NULL)
+ if (l2_channel_flush(downstream) == L2_ERROR)
+ return L2_ERROR;
return L2_OK;
}
-static int hook_close(l2_context_t *ctx, l2_channel_t *below)
+static int hook_close(l2_context_t *ctx, l2_channel_t *downstream)
{
+ l2_ch_buffer_t *cfg;
+
+ if (downstream != NULL)
+ if (l2_channel_close(downstream) == L2_ERROR)
+ return L2_ERROR;
+ if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
+ return L2_ERROR;
+ if (cfg->buf != NULL) {
+ free(cfg->buf);
+ cfg->buf = NULL;
+ }
return L2_OK;
}
static int hook_destroy(l2_context_t *ctx)
{
+ l2_ch_buffer_t *cfg;
+
+ if ((cfg = (l2_ch_buffer_t *)ctx->vp) == NULL)
+ return L2_ERROR;
+ if (cfg->buf != NULL)
+ free(cfg->buf);
+ free(cfg);
return L2_OK;
}
|