]> granicus.if.org Git - graphviz/commitdiff
consolidating file output into gvdevice framework
authorellson <devnull@localhost>
Wed, 3 Oct 2007 16:59:11 +0000 (16:59 +0000)
committerellson <devnull@localhost>
Wed, 3 Oct 2007 16:59:11 +0000 (16:59 +0000)
lib/common/emit.c
lib/gvc/gvdevice.c

index 3f03275976013c2a7beab8b421378080afc88a92..500b2aa8e20ea121599ddc6bf91642ae83fd7d8c 100644 (file)
 #include "config.h"
 #endif
 
-#ifdef WIN32
-#include <fcntl.h>
-#include <io.h>
-#endif
-#include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #include "render.h"
@@ -2220,7 +2215,6 @@ void emit_graph(GVJ_t * job, graph_t * g)
            setColorScheme (agget (g, "colorscheme"));
            setup_page(job, g);
            gvrender_begin_page(job);
-           gvdevice_prepare(job);
            gvrender_set_pencolor(job, DEFAULT_COLOR);
            gvrender_set_fillcolor(job, DEFAULT_FILL);
            gvrender_set_font(job, gvc->defaultfontname, gvc->defaultfontsize);
@@ -2247,8 +2241,8 @@ void emit_graph(GVJ_t * job, graph_t * g)
                gvrender_end_anchor(job);
 //         if (boxf_overlap(job->clip, job->pageBox))
                emit_view(job,g,flags);
-           gvdevice_format(job);
            gvrender_end_page(job);
+           gvdevice_format(job);
        } 
 
        if (job->numLayers > 1)
@@ -2691,44 +2685,6 @@ static void init_bb(graph_t *g)
                        init_bb_node(g, n);
 }
 
-static void auto_output_filename(GVJ_t *job)
-{
-    static char *buf;
-    static int bufsz;
-    char gidx[100];  /* large enough for '.' plus any integer */
-    char *fn, *p;
-    int len;
-
-    if (job->graph_index)
-       sprintf(gidx, ".%d", job->graph_index + 1);
-    else
-       gidx[0] = '\0';
-    if (!(fn = job->input_filename))
-       fn = "noname.dot";
-    len = strlen(fn)                   /* typically "something.dot" */
-       + strlen(gidx)                  /* "", ".2", ".3", ".4", ... */
-       + 1                             /* "." */
-       + strlen(job->output_langname)  /* e.g. "png" */
-       + 1;                            /* null terminaor */
-    if (bufsz < len) {
-           bufsz = len + 10;
-           buf = realloc(buf, bufsz * sizeof(char));
-    }
-    strcpy(buf, fn);
-    strcat(buf, gidx);
-    strcat(buf, ".");
-    if ((p = strchr(job->output_langname, ':'))) {
-        strcat(buf, p+1);
-       strcat(buf, ".");
-       strncat(buf, job->output_langname, (p - job->output_langname));
-    }
-    else {
-        strcat(buf, job->output_langname);
-    }
-
-    job->output_filename = buf;
-}
-
 extern gvevent_key_binding_t gvevent_key_binding[];
 extern int gvevent_key_binding_size;
 extern gvdevice_callbacks_t gvdevice_callbacks;
@@ -2793,31 +2749,7 @@ int gvRenderJobs (GVC_t * gvc, graph_t * g)
        }
        else {
            gvc->active_jobs = job;   /* first job of new list */
-           gvdevice_initialize(job);    /* FIXME? - semantics are unclear */
-
-            if (!job->output_file) {        /* if not yet opened */
-               if (gvc->common.auto_outfile_names)
-                   auto_output_filename(job);
-                if (job->output_filename) {
-                   job->output_file = fopen(job->output_filename, "w");
-                   if (job->output_file == NULL) {
-                       perror(job->output_filename);
-                       exit(1);
-                   }
-               }
-                else
-                    job->output_file = stdout;
-#ifdef WITH_CODEGENS
-               Output_file = job->output_file;
-#endif
-
-#ifdef HAVE_SETMODE
-#ifdef O_BINARY
-               if (job->flags & GVDEVICE_BINARY_FORMAT)
-                   setmode(fileno(job->output_file), O_BINARY);
-#endif
-#endif
-            }
+           gvdevice_initialize(job); /* open device or file output */
            gvrender_begin_job(job);
        }
        job->next_active = NULL;      /* terminate active list */
@@ -2838,10 +2770,6 @@ int gvRenderJobs (GVC_t * gvc, graph_t * g)
            job->common->show_boxes = Show_boxes; 
 #endif
            emit_graph(job, g); /* FIXME? - should this be a special case of finalize() ? */
-
-           /* Flush is necessary because we may be writing to a pipe. */
-           if (job->output_file && ! job->external_context && job->output_lang != TK)
-               fflush(job->output_file);
        }
 
         /* the last job, after all input graphs are processed,
index 8dd11e9fe8f84ac261a1ebaab799dd7c681bf93b..22ffcd8a2bf0d74758f29ff71cc0368e5cb13c1d 100644 (file)
 #include "config.h"
 #endif
 
+#ifdef WIN32
+#include <fcntl.h>
+#include <io.h>
+#endif
+#include <stdlib.h>
 #include <string.h>
 
 #include "const.h"
@@ -77,20 +82,83 @@ int gvdevice_features(GVC_t * gvc)
 
 #endif
 
-void gvdevice_initialize(GVJ_t * firstjob)
+static void auto_output_filename(GVJ_t *job)
 {
-    gvdevice_engine_t *gvde = firstjob->device.engine;
+    static char *buf;
+    static int bufsz;
+    char gidx[100];  /* large enough for '.' plus any integer */
+    char *fn, *p;
+    int len;
+
+    if (job->graph_index)
+        sprintf(gidx, ".%d", job->graph_index + 1);
+    else
+        gidx[0] = '\0';
+    if (!(fn = job->input_filename))
+        fn = "noname.dot";
+    len = strlen(fn)                    /* typically "something.dot" */
+        + strlen(gidx)                  /* "", ".2", ".3", ".4", ... */
+        + 1                             /* "." */
+        + strlen(job->output_langname)  /* e.g. "png" */
+        + 1;                            /* null terminaor */
+    if (bufsz < len) {
+            bufsz = len + 10;
+            buf = realloc(buf, bufsz * sizeof(char));
+    }
+    strcpy(buf, fn);
+    strcat(buf, gidx);
+    strcat(buf, ".");
+    if ((p = strchr(job->output_langname, ':'))) {
+        strcat(buf, p+1);
+        strcat(buf, ".");
+        strncat(buf, job->output_langname, (p - job->output_langname));
+    }
+    else {
+        strcat(buf, job->output_langname);
+    }
 
-    if (gvde && gvde->initialize)
-       gvde->initialize(firstjob);
+    job->output_filename = buf;
 }
 
-void gvdevice_prepare(GVJ_t * job)
+#ifdef WITH_CODEGENS
+extern FILE* Output_file;
+#endif
+
+void gvdevice_initialize(GVJ_t * job)
 {
     gvdevice_engine_t *gvde = job->device.engine;
+    GVC_t *gvc = job->gvc;
 
-    if (gvde && gvde->prepare)
-       gvde->prepare(job);
+    if (gvde && gvde->initialize) {
+       gvde->initialize(job);
+    }
+    else {
+        /* if the device has now initialization then it uses file output */
+        if (!job->output_file) {        /* if not yet opened */
+            if (gvc->common.auto_outfile_names)
+                 auto_output_filename(job);
+            if (job->output_filename) {
+                 job->output_file = fopen(job->output_filename, "w");
+                 if (job->output_file == NULL) {
+                     perror(job->output_filename);
+                     exit(1);
+                 }
+            }
+            else
+                 job->output_file = stdout;
+
+#ifdef WITH_CODEGENS
+            Output_file = job->output_file;
+#endif
+
+#ifdef HAVE_SETMODE
+#ifdef O_BINARY
+            if (job->flags & GVDEVICE_BINARY_FORMAT)
+                setmode(fileno(job->output_file), O_BINARY);
+#endif
+#endif
+        }
+    }
 }
 
 void gvdevice_format(GVJ_t * job)
@@ -99,16 +167,20 @@ void gvdevice_format(GVJ_t * job)
 
     if (gvde && gvde->format)
        gvde->format(job);
+    if (job->output_file && ! job->external_context && job->output_lang != TK)
+       fflush(job->output_file);
 }
 
 void gvdevice_finalize(GVJ_t * firstjob)
 {
     gvdevice_engine_t *gvde = firstjob->device.engine;
     GVJ_t *job;
+    boolean finalized_p = FALSE;
 
     if (gvde) {
        if (gvde->finalize) {
            gvde->finalize(firstjob);
+           finalized_p = TRUE;
        }
     }
 #ifdef WITH_CODEGENS
@@ -120,16 +192,18 @@ void gvdevice_finalize(GVJ_t * firstjob)
     }
 #endif
 
-    /* FIXME - file output should be its own device */
-    for (job = firstjob; job; job = job->next_active) {
-       if (job->output_filename
-         && job->output_file != stdout 
-         && ! job->external_context) {
-           if (job->output_file) {
-               fclose(job->output_file);
-               job->output_file = NULL;
+    if (! finalized_p) {
+        /* if the device has no finalization then it uses file output */
+        for (job = firstjob; job; job = job->next_active) {
+           if (job->output_filename
+             && job->output_file != stdout 
+             && ! job->external_context) {
+               if (job->output_file) {
+                   fclose(job->output_file);
+                   job->output_file = NULL;
+               }
+                job->output_filename = NULL;
            }
-            job->output_filename = NULL;
-       }
+        }
     }
 }