From aa7692e1f391ed4cb0a37e967163af50c6d80cd4 Mon Sep 17 00:00:00 2001 From: ellson Date: Fri, 26 Sep 2008 02:50:12 +0000 Subject: [PATCH] schemes as var_args list --- lib/inkpot/inkpot.h | 40 ++++++--- lib/inkpot/inkpot_scheme.c | 163 ++++++++++++++++++++++-------------- lib/inkpot/inkpot_structs.h | 3 +- lib/inkpot/test.c | 17 ++-- 4 files changed, 140 insertions(+), 83 deletions(-) diff --git a/lib/inkpot/inkpot.h b/lib/inkpot/inkpot.h index 4cbbecb5e..5f4e4097b 100644 --- a/lib/inkpot/inkpot.h +++ b/lib/inkpot/inkpot.h @@ -30,24 +30,38 @@ typedef enum { INKPOT_FAIL } inkpot_status_t; +/* opaque inkpot struct */ typedef struct inkpot_s inkpot_t; +/* template for writer functions */ size_t (*writer) (void *closure, const char *data, size_t length); +/* malloc an inkpot and perform some initialization */ extern inkpot_t *inkpot_init ( void ); -extern inkpot_status_t inkpot_writer_fn ( inkpot_t *inkpot, void *writer, void *out_closure, void *err_closure ); -extern inkpot_status_t inkpot_clear ( inkpot_t *inkpot ); -extern inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme ); -extern inkpot_status_t inkpot_translate ( inkpot_t *inkpot, const char *scheme ); +/* 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); +/* The list of schemes for color input interpretation, NULL scheme terminates */ +extern inkpot_status_t inkpot_schemes ( inkpot_t *inkpot, const char *scheme, ... ); +/* The scheme for color output representation, */ +extern inkpot_status_t inkpot_translate ( inkpot_t *inkpot, const char *scheme ); + +/* set inkpot color by name as interpeted by the current schemes */ extern inkpot_status_t inkpot_set ( inkpot_t *inkpot, const char *color ); +/* set inkpot color to the default (from the first scheme specified) */ extern inkpot_status_t inkpot_set_default ( inkpot_t *inkpot ); +/* set inkpot color by value, which may or may not have a name in the current or any schemes */ extern inkpot_status_t inkpot_set_rgba ( inkpot_t *inkpot, unsigned char rgba[4] ); -extern inkpot_status_t inkpot_write ( inkpot_t *inkpot ); - +/* get inkpot color name in the translation scheme, or for colors without a name in the translated scheme, + * set NULL and return INKPOT_COLOR_NONAME */ extern inkpot_status_t inkpot_get ( inkpot_t *inkpot, const char **color ); + +/* get inkpot color value in various formats */ extern inkpot_status_t inkpot_get_rgba ( inkpot_t *inkpot, unsigned char *rgba ); extern inkpot_status_t inkpot_get_hsva ( inkpot_t *inkpot, unsigned char *hsva ); extern inkpot_status_t inkpot_get_cmyk ( inkpot_t *inkpot, unsigned char *cmyk ); @@ -55,11 +69,17 @@ extern inkpot_status_t inkpot_get_RGBA ( inkpot_t *inkpot, double *RGBA ); extern inkpot_status_t inkpot_get_HSVA ( inkpot_t *inkpot, double *HSVA ); extern inkpot_status_t inkpot_get_index ( inkpot_t *inkpot, unsigned int *index ); -extern inkpot_status_t inkpot_print_schemes ( inkpot_t *inkpot ); -extern inkpot_status_t inkpot_print_names ( inkpot_t *inkpot ); -extern inkpot_status_t inkpot_print_names_out ( inkpot_t *inkpot ); -extern inkpot_status_t inkpot_print_values ( inkpot_t *inkpot ); +/* output the current color to out_writer (default stdout) */ +/* returns INKPOT_COLOR_NONAME if it converted the color to a hex numeric string value */ +extern inkpot_status_t inkpot_write ( inkpot_t *inkpot ); + +/* debugging and error functions that oput to the err_writer (default stderr) */ +extern inkpot_status_t inkpot_debug_schemes ( inkpot_t *inkpot ); +extern inkpot_status_t inkpot_debug_names ( inkpot_t *inkpot ); +extern inkpot_status_t inkpot_debug_names_out ( inkpot_t *inkpot ); +extern inkpot_status_t inkpot_debug_values ( inkpot_t *inkpot ); +/* write the most recent inkpot status to the err writer */ extern inkpot_status_t inkpot_error ( inkpot_t *inkpot ); #ifdef __cplusplus diff --git a/lib/inkpot/inkpot_scheme.c b/lib/inkpot/inkpot_scheme.c index 8f90e8d28..bd935666f 100644 --- a/lib/inkpot/inkpot_scheme.c +++ b/lib/inkpot/inkpot_scheme.c @@ -15,6 +15,7 @@ **********************************************************/ #include +#include #include #include #include @@ -28,10 +29,10 @@ static size_t inkpot_writer (void *closure, const char *data, size_t length) return fwrite(data, sizeof(char), length, (FILE *)closure); } -inkpot_status_t inkpot_clear ( inkpot_t *inkpot ) +static inkpot_status_t inkpot_clear ( inkpot_t *inkpot ) { inkpot->scheme_bits = 0; /* clear schemes */ - inkpot->name = NULL; /* clear cached value */ + inkpot->name = NULL; /* clear cached names */ inkpot->scheme_index = NULL; inkpot->out_name = NULL; inkpot->out_scheme_index = NULL; @@ -45,8 +46,9 @@ inkpot_t *inkpot_init ( void ) inkpot = malloc(sizeof(inkpot_t)); if (inkpot) { - inkpot->writer = inkpot_writer; + inkpot->out_writer = inkpot_writer; inkpot->out_closure = stdout; + inkpot->err_writer = inkpot_writer; inkpot->err_closure = stderr; rc = inkpot_clear ( inkpot ); @@ -55,12 +57,17 @@ inkpot_t *inkpot_init ( void ) return inkpot; } -inkpot_status_t inkpot_writer_fn ( inkpot_t *inkpot, void *writer, void *out_closure, void *err_closure ) +inkpot_status_t inkpot_out_writer ( inkpot_t *inkpot, void *writer, void *closure ) { - inkpot->writer = writer; - inkpot->out_closure = out_closure; - inkpot->err_closure = err_closure; + inkpot->out_writer = writer; + inkpot->out_closure = closure; + return ((inkpot->status = INKPOT_SUCCESS)); +} +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)); } @@ -111,7 +118,7 @@ static inkpot_scheme_index_t *inkpot_find_scheme_index ( const char *scheme ) inkpot_scheme_index_cmpf); } -inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme ) +static inkpot_status_t inkpot_scheme ( inkpot_t *inkpot, const char *scheme ) { inkpot_scheme_name_t *inkpot_scheme_name; inkpot_scheme_index_t *inkpot_scheme_index; @@ -119,6 +126,7 @@ inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme ) if (scheme == NULL) return ((inkpot->status = INKPOT_SCHEME_UNKNOWN)); + inkpot_scheme_name = inkpot_find_scheme_name(scheme); if (inkpot_scheme_name) { idx = inkpot_scheme_name - TAB_SCHEMES_NAME; @@ -148,6 +156,29 @@ inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme ) return ((inkpot->status = INKPOT_SUCCESS)); } +inkpot_status_t inkpot_schemes ( inkpot_t *inkpot, const char *scheme, ... ) +{ + inkpot_status_t rc; + va_list argp; + const char *s; + + if (scheme == NULL) + return ((inkpot->status = INKPOT_SCHEME_UNKNOWN)); + + rc = inkpot_clear(inkpot); + if (rc != INKPOT_SUCCESS) + return rc; + + va_start(argp, scheme); + for (s = scheme; s; s = va_arg(argp, const char*)) { + rc = inkpot_scheme(inkpot, s); + if (rc != INKPOT_SUCCESS) + break; + } + va_end(argp); + return rc; +} + inkpot_status_t inkpot_translate ( inkpot_t *inkpot, const char *scheme ) { inkpot_scheme_name_t *inkpot_scheme_name; @@ -156,6 +187,7 @@ inkpot_status_t inkpot_translate ( inkpot_t *inkpot, const char *scheme ) if (scheme == NULL) return ((inkpot->status = INKPOT_SCHEME_UNKNOWN)); + inkpot_scheme_name = inkpot_find_scheme_name(scheme); if (inkpot_scheme_name) { idx = inkpot_scheme_name - TAB_SCHEMES_NAME; @@ -166,6 +198,7 @@ inkpot_status_t inkpot_translate ( inkpot_t *inkpot, const char *scheme ) } return ((inkpot->status = INKPOT_SUCCESS)); } + inkpot_scheme_index = inkpot_find_scheme_index(scheme); if (! inkpot_scheme_index) return ((inkpot->status = INKPOT_SCHEME_UNKNOWN)); @@ -475,83 +508,83 @@ inkpot_status_t inkpot_get_index ( inkpot_t *inkpot, unsigned int *index ) return ((inkpot->status = INKPOT_FAIL)); } -static void iputs(inkpot_t *inkpot, const char *s) +static void errputs(inkpot_t *inkpot, const char *s) { - inkpot->writer(inkpot->out_closure, s, strlen(s)); + inkpot->err_writer(inkpot->out_closure, s, strlen(s)); } -inkpot_status_t inkpot_print_schemes( inkpot_t *inkpot ) +inkpot_status_t inkpot_debug_schemes( inkpot_t *inkpot ) { IDX_SCHEMES_NAME i; int found; - iputs(inkpot, "schemes:\n"); + errputs(inkpot, "schemes:\n"); for (i = 0; i < SZT_SCHEMES_NAME; i++) { found = 0; if ((1<scheme_bits) { - iputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]); - iputs(inkpot, " (in)"); + errputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]); + errputs(inkpot, " (in)"); if (i == inkpot->default_scheme_name_idx) - iputs(inkpot, " (default)"); + errputs(inkpot, " (default)"); found++; } if ((1<out_scheme_bit) { if (! found) - iputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]); - iputs(inkpot, " (out)"); + errputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]); + errputs(inkpot, " (out)"); found++; } if (found) - iputs(inkpot, "\n"); + errputs(inkpot, "\n"); } found = 0; if (inkpot->scheme_index) { - iputs(inkpot, &TAB_STRINGS[inkpot->scheme_index->string_idx]); - iputs(inkpot, " (indexed) (in)"); + errputs(inkpot, &TAB_STRINGS[inkpot->scheme_index->string_idx]); + errputs(inkpot, " (indexed) (in)"); found++; } if (inkpot->out_scheme_index) { if (! found) { - iputs(inkpot, &TAB_STRINGS[inkpot->out_scheme_index->string_idx]); - iputs(inkpot, " (indexed)"); + errputs(inkpot, &TAB_STRINGS[inkpot->out_scheme_index->string_idx]); + errputs(inkpot, " (indexed)"); } - iputs(inkpot, " (out)"); + errputs(inkpot, " (out)"); found++; } if (found) - iputs(inkpot, "\n"); - iputs(inkpot, "\n"); + errputs(inkpot, "\n"); + errputs(inkpot, "\n"); return ((inkpot->status = INKPOT_SUCCESS)); } -static inkpot_status_t inkpot_print_scheme_names( inkpot_t *inkpot, int scheme_bits ) +static inkpot_status_t inkpot_debug_scheme_names( inkpot_t *inkpot, int scheme_bits ) { IDX_SCHEMES_NAME i; int found = 0; - iputs(inkpot, "("); + errputs(inkpot, "("); for (i = 0; i < SZT_SCHEMES_NAME; i++) { if ((1 << i) & scheme_bits) { if (found++) - iputs(inkpot, " "); - iputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]); + errputs(inkpot, " "); + errputs(inkpot, &TAB_STRINGS[TAB_SCHEMES_NAME[i].string_idx]); } } - iputs(inkpot, ")"); + errputs(inkpot, ")"); return INKPOT_SUCCESS; } -static void inkpot_print_rgba( inkpot_t *inkpot, unsigned char *rgba ) +static void inkpot_debug_rgba( inkpot_t *inkpot, unsigned char *rgba ) { char buf[20]; sprintf(buf, "%d,%d,%d,%d", rgba[0], rgba[1], rgba[2], rgba[3]); - iputs(inkpot, buf); + errputs(inkpot, buf); } -static inkpot_status_t inkpot_print_names_schemes( inkpot_t *inkpot, BIT_SCHEMES_NAME scheme_bits, inkpot_scheme_index_t *scheme_index ) +static inkpot_status_t inkpot_debug_names_schemes( inkpot_t *inkpot, BIT_SCHEMES_NAME scheme_bits, inkpot_scheme_index_t *scheme_index ) { inkpot_name_t *name; IDX_NAMES i; @@ -564,13 +597,13 @@ static inkpot_status_t inkpot_print_names_schemes( inkpot_t *inkpot, BIT_SCHEMES for (i = 0; i < SZT_NAMES; i++) { name = &TAB_NAMES[i]; if (scheme_bits & name->scheme_bits) { - iputs(inkpot, &TAB_STRINGS[TAB_NAMES[i].string_idx]); - inkpot_print_scheme_names(inkpot, scheme_bits); - iputs(inkpot, " "); - inkpot_print_rgba(inkpot, TAB_VALUES[name->value_idx].rgba); + errputs(inkpot, &TAB_STRINGS[TAB_NAMES[i].string_idx]); + inkpot_debug_scheme_names(inkpot, scheme_bits); + errputs(inkpot, " "); + inkpot_debug_rgba(inkpot, TAB_VALUES[name->value_idx].rgba); if (name->value_idx == inkpot->default_value_idx) - iputs(inkpot, " (default)"); - iputs(inkpot, "\n"); + errputs(inkpot, " (default)"); + errputs(inkpot, "\n"); } } } @@ -585,44 +618,44 @@ static inkpot_status_t inkpot_print_names_schemes( inkpot_t *inkpot, BIT_SCHEMES for (k = first; k < last; k++) { v = TAB_IXVALUES[k]; sprintf(buf, "%d(", k - first); - iputs(inkpot, buf); - iputs(inkpot, &TAB_STRINGS[scheme_index->string_idx]); - iputs(inkpot, ") "); + errputs(inkpot, buf); + errputs(inkpot, &TAB_STRINGS[scheme_index->string_idx]); + errputs(inkpot, ") "); if (v < SZT_VALUES) - inkpot_print_rgba(inkpot, TAB_VALUES[v].rgba); + inkpot_debug_rgba(inkpot, TAB_VALUES[v].rgba); else - inkpot_print_rgba(inkpot, TAB_NONAME_VALUES[v - SZT_VALUES].rgba); - iputs(inkpot, "\n"); + inkpot_debug_rgba(inkpot, TAB_NONAME_VALUES[v - SZT_VALUES].rgba); + errputs(inkpot, "\n"); } } - iputs(inkpot, "\n"); + errputs(inkpot, "\n"); return ((inkpot->status = INKPOT_SUCCESS)); } -inkpot_status_t inkpot_print_names( inkpot_t *inkpot ) +inkpot_status_t inkpot_debug_names( inkpot_t *inkpot ) { BIT_SCHEMES_NAME scheme_bits = inkpot->scheme_bits; inkpot_scheme_index_t *scheme_index = inkpot->scheme_index; - iputs(inkpot, "names (in):\n"); - return inkpot_print_names_schemes(inkpot, scheme_bits, scheme_index); + errputs(inkpot, "names (in):\n"); + return inkpot_debug_names_schemes(inkpot, scheme_bits, scheme_index); } -inkpot_status_t inkpot_print_names_out( inkpot_t *inkpot ) +inkpot_status_t inkpot_debug_names_out( inkpot_t *inkpot ) { BIT_SCHEMES_NAME scheme_bits = inkpot->out_scheme_bit; inkpot_scheme_index_t *scheme_index = inkpot->out_scheme_index; - iputs(inkpot, "names (out):\n"); - return inkpot_print_names_schemes(inkpot, scheme_bits, scheme_index); + errputs(inkpot, "names (out):\n"); + return inkpot_debug_names_schemes(inkpot, scheme_bits, scheme_index); } -/* Print all values that are members of the currently activated +/* Print all values that are members of the currently listed * name schemes, with the names in those schemes. * Does not print the indexes in index schemes that a value may * be a member of. */ -inkpot_status_t inkpot_print_values( inkpot_t *inkpot ) +inkpot_status_t inkpot_debug_values( inkpot_t *inkpot ) { inkpot_value_t *value; inkpot_name_t *name; @@ -631,7 +664,7 @@ inkpot_status_t inkpot_print_values( inkpot_t *inkpot ) BIT_SCHEMES_NAME scheme_bits; int found; - iputs(inkpot, "values:\n"); + errputs(inkpot, "values:\n"); for (i = 0; i < SZT_VALUES; i++) { value = &TAB_VALUES[i]; found = 0; @@ -642,18 +675,18 @@ inkpot_status_t inkpot_print_values( inkpot_t *inkpot ) scheme_bits = name->scheme_bits & inkpot->scheme_bits; if (scheme_bits) { if (found++) - iputs(inkpot, " "); + errputs(inkpot, " "); else - inkpot_print_rgba(inkpot, TAB_VALUES[i].rgba); - iputs(inkpot, " "); - iputs(inkpot, &TAB_STRINGS[name->string_idx]); - inkpot_print_scheme_names(inkpot, scheme_bits); + inkpot_debug_rgba(inkpot, TAB_VALUES[i].rgba); + errputs(inkpot, " "); + errputs(inkpot, &TAB_STRINGS[name->string_idx]); + inkpot_debug_scheme_names(inkpot, scheme_bits); } } if (found) - iputs(inkpot, "\n"); + errputs(inkpot, "\n"); } - iputs(inkpot, "\n"); + errputs(inkpot, "\n"); return ((inkpot->status = INKPOT_SUCCESS)); } @@ -669,7 +702,7 @@ inkpot_status_t inkpot_write ( inkpot_t *inkpot ) rc = inkpot_get(inkpot, &color); if (rc == INKPOT_SUCCESS) - inkpot->writer(inkpot->out_closure, color, strlen(color)); + inkpot->out_writer(inkpot->out_closure, color, strlen(color)); if (rc == INKPOT_COLOR_NONAME) { value_idx = inkpot->value_idx; if (value_idx < SZT_VALUES) @@ -684,7 +717,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->writer(inkpot->out_closure, buf, sizeof(buf)); + inkpot->out_writer(inkpot->out_closure, buf, sizeof(buf)); } return rc; } @@ -708,7 +741,7 @@ inkpot_status_t inkpot_error ( inkpot_t *inkpot ) case INKPOT_FAIL: m = "INKPOT_FAIL\n"; break; } - inkpot->writer(inkpot->err_closure, m, strlen(m)); + inkpot->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 0916bce90..a3db276dd 100644 --- a/lib/inkpot/inkpot_structs.h +++ b/lib/inkpot/inkpot_structs.h @@ -128,7 +128,8 @@ struct inkpot_s { /* The Ink Pot */ *name, /* The current input name, or NULL. */ *out_name; /* The current output name, or NULL. */ - size_t (*writer) (void *closure, const char *s, size_t len); + size_t (*out_writer) (void *closure, const char *s, size_t len); + size_t (*err_writer) (void *closure, const char *s, size_t len); void *out_closure, *err_closure; inkpot_status_t status; /* The status after the last operation */ diff --git a/lib/inkpot/test.c b/lib/inkpot/test.c index a2bf0724c..f5ad8c8c1 100644 --- a/lib/inkpot/test.c +++ b/lib/inkpot/test.c @@ -35,12 +35,12 @@ int main (int argc, char *argv[]) } if (argc < 4) { - rc = inkpot_activate(inkpot, "x11"); + rc = inkpot_schemes(inkpot, "x11", NULL); assert(rc == INKPOT_SUCCESS); } else { for (i = 3; i < argc; i++) { - rc = inkpot_activate(inkpot, argv[i]); + rc = inkpot_schemes(inkpot, argv[i]); if (rc == INKPOT_SCHEME_UNKNOWN) inkpot_error(inkpot); else @@ -48,16 +48,19 @@ int main (int argc, char *argv[]) } } - inkpot_print_schemes(inkpot); +/* ------------- */ + + inkpot_debug_schemes(inkpot); - inkpot_print_names(inkpot); + inkpot_debug_names(inkpot); - inkpot_print_names_out(inkpot); + inkpot_debug_names_out(inkpot); - inkpot_print_values(inkpot); + inkpot_debug_values(inkpot); - fprintf(stdout, "%s ", color); +/* ------------- */ + fprintf(stdout, "%s ", color); rc = inkpot_set(inkpot, color); if (rc == INKPOT_COLOR_UNKNOWN) { fprintf(stdout, "(unknown) "); -- 2.40.0