From f8d02a06a93cd1f231870fc1f386a3e472649a99 Mon Sep 17 00:00:00 2001 From: ellson Date: Fri, 26 Sep 2008 11:16:38 +0000 Subject: [PATCH] improve disciplines --- lib/inkpot/inkpot.h | 16 ++++++++-------- lib/inkpot/inkpot_scheme.c | 29 ++++++++++++----------------- lib/inkpot/inkpot_structs.h | 3 +-- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/lib/inkpot/inkpot.h b/lib/inkpot/inkpot.h index 5f4e4097b..636dd9699 100644 --- a/lib/inkpot/inkpot.h +++ b/lib/inkpot/inkpot.h @@ -33,17 +33,17 @@ typedef enum { /* opaque inkpot struct */ typedef struct inkpot_s inkpot_t; -/* template for writer functions */ -size_t (*writer) (void *closure, const char *data, size_t length); +/* writer discipline */ +typedef struct inkpot_disc_s { + size_t (*out_writer) (void *out_closure, const char *data, size_t length); + size_t (*err_writer) (void *err_closure, const char *data, size_t length); +} inkpot_disc_t; -/* malloc an inkpot and perform some initialization */ +/* malloc an inkpot. */ extern inkpot_t *inkpot_init ( void ); -/* Provide alternate out and err writer functions */ -/* Defaults to internal functions using fwrite() to stdout and stderr */ -/* No need to call these funtions if the defaults suffice. */ -extern inkpot_status_t inkpot_out_writer ( inkpot_t *inkpot, void *writer, void *closure); -extern inkpot_status_t inkpot_err_writer ( inkpot_t *inkpot, void *writer, void *closure); +/* Discplines default to stdout, stderr. Use this to provide other writers */ +extern inkpot_status_t inkpot_disciplines ( inkpot_t *inkpot, inkpot_disc_t disc, void *out_closure, void *err_closure ); /* The list of schemes for color input interpretation, NULL scheme terminates */ extern inkpot_status_t inkpot_schemes ( inkpot_t *inkpot, const char *scheme, ... ); diff --git a/lib/inkpot/inkpot_scheme.c b/lib/inkpot/inkpot_scheme.c index b6cb4c816..66e80cc60 100644 --- a/lib/inkpot/inkpot_scheme.c +++ b/lib/inkpot/inkpot_scheme.c @@ -25,9 +25,11 @@ static size_t inkpot_writer (void *closure, const char *data, size_t length) { - return fwrite(data, sizeof(char), length, (FILE *)closure); + return fwrite(data, sizeof(char), length, (FILE *)closure); } +static inkpot_disc_t inkpot_default_disc = { inkpot_writer, inkpot_writer }; + static inkpot_status_t inkpot_clear ( inkpot_t *inkpot ) { inkpot->scheme_bits = 0; /* clear schemes */ @@ -45,28 +47,21 @@ inkpot_t *inkpot_init ( void ) inkpot = malloc(sizeof(inkpot_t)); if (inkpot) { - inkpot->out_writer = inkpot_writer; + inkpot->disc = inkpot_default_disc; inkpot->out_closure = stdout; - inkpot->err_writer = inkpot_writer; inkpot->err_closure = stderr; - rc = inkpot_clear ( inkpot ); assert ( rc == INKPOT_SUCCESS ); } return inkpot; } -inkpot_status_t inkpot_out_writer ( inkpot_t *inkpot, void *writer, void *closure ) +inkpot_status_t inkpot_disciplines ( inkpot_t *inkpot, inkpot_disc_t disc, void *out_closure, void *err_closure ) { - inkpot->out_writer = writer; - inkpot->out_closure = closure; - return ((inkpot->status = INKPOT_SUCCESS)); -} + inkpot->disc = disc; + inkpot->out_closure = out_closure; + inkpot->out_closure = out_closure; -inkpot_status_t inkpot_err_writer ( inkpot_t *inkpot, void *writer, void *closure ) -{ - inkpot->err_writer = writer; - inkpot->err_closure = closure; return ((inkpot->status = INKPOT_SUCCESS)); } @@ -517,7 +512,7 @@ inkpot_status_t inkpot_get_index ( inkpot_t *inkpot, unsigned int *index ) static void errputs(inkpot_t *inkpot, const char *s) { - inkpot->err_writer(inkpot->out_closure, s, strlen(s)); + inkpot->disc.err_writer(inkpot->err_closure, s, strlen(s)); } inkpot_status_t inkpot_debug_schemes( inkpot_t *inkpot ) @@ -709,7 +704,7 @@ inkpot_status_t inkpot_write ( inkpot_t *inkpot ) rc = inkpot_get(inkpot, &color); if (rc == INKPOT_SUCCESS) - inkpot->out_writer(inkpot->out_closure, color, strlen(color)); + inkpot->disc.out_writer(inkpot->out_closure, color, strlen(color)); if (rc == INKPOT_COLOR_NONAME) { value_idx = inkpot->value_idx; if (value_idx < SZT_VALUES) @@ -724,7 +719,7 @@ inkpot_status_t inkpot_write ( inkpot_t *inkpot ) for (m = 0; m < 4; m++) *p++ = *q++; sprintf(buf, "#%02x%02x%02x%02x", rgba[0], rgba[1], rgba[2], rgba[3]); - inkpot->out_writer(inkpot->out_closure, buf, sizeof(buf)); + inkpot->disc.out_writer(inkpot->out_closure, buf, sizeof(buf)); } return rc; } @@ -748,7 +743,7 @@ inkpot_status_t inkpot_error ( inkpot_t *inkpot ) case INKPOT_FAIL: m = "INKPOT_FAIL\n"; break; } - inkpot->err_writer(inkpot->err_closure, m, strlen(m)); + inkpot->disc.err_writer(inkpot->err_closure, m, strlen(m)); return ((inkpot->status = INKPOT_SUCCESS)); }; diff --git a/lib/inkpot/inkpot_structs.h b/lib/inkpot/inkpot_structs.h index ce46d0db2..29b6aaaf7 100644 --- a/lib/inkpot/inkpot_structs.h +++ b/lib/inkpot/inkpot_structs.h @@ -132,8 +132,7 @@ struct inkpot_s { /* The Ink Pot */ *name, /* The current input name, or NULL. */ *out_name; /* The current output name, or NULL. */ - size_t (*out_writer) (void *closure, const char *s, size_t len); - size_t (*err_writer) (void *closure, const char *s, size_t len); + inkpot_disc_t disc; /* writers and closures for out and err */ void *out_closure, *err_closure; inkpot_status_t status; /* The status after the last operation */ -- 2.40.0