]> granicus.if.org Git - graphviz/commitdiff
improve disciplines
authorellson <devnull@localhost>
Fri, 26 Sep 2008 11:16:38 +0000 (11:16 +0000)
committerellson <devnull@localhost>
Fri, 26 Sep 2008 11:16:38 +0000 (11:16 +0000)
lib/inkpot/inkpot.h
lib/inkpot/inkpot_scheme.c
lib/inkpot/inkpot_structs.h

index 5f4e4097b75d1094b6a7ad00f8853f496637a4fc..636dd9699531e9a6e78b24172565cbe26f7709ea 100644 (file)
@@ -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, ... );
index b6cb4c816d00414cd8cee41d0d7fc72356fb0456..66e80cc602533549e1368857f596b6cac1ea05cd 100644 (file)
 
 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));
 };
index ce46d0db2f8c5645fb00effa90bcbf7f44009482..29b6aaaf7cfa5d6ec9c83886e0bcc8d415524c7e 100644 (file)
@@ -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 */