]> granicus.if.org Git - graphviz/commitdiff
Make sure the graph discipline is set to sfio before calling agwrite, then reset it.
authorerg <devnull@localhost>
Thu, 16 Jul 2009 22:09:50 +0000 (22:09 +0000)
committererg <devnull@localhost>
Thu, 16 Jul 2009 22:09:50 +0000 (22:09 +0000)
lib/gvpr/actions.c
lib/gvpr/actions.h
lib/gvpr/compile.c
lib/gvpr/gprstate.h
lib/gvpr/gvpr.c

index 759d161b0e8f27b4904e8d9bdb82e5e0202467ce..51ea873d0815e8b60f9da536b5135972e93b5228 100644 (file)
@@ -542,11 +542,32 @@ int deleteObj(Agraph_t * g, Agobj_t * obj)
        return -1;
 }
 
+/* sfioWrite:
+ * If the graph is passed in from a library, its output discipline
+ * might not use sfio. In this case, we push an sfio discipline on
+ * the graph, write it, and then pop it off.
+ */
+int sfioWrite(Agraph_t * g, Sfio_t* fp, Agiodisc_t* dfltDisc)
+{
+    Agiodisc_t* saveio;
+    int rv;
+
+    if (g->clos->disc.io != dfltDisc) {
+       saveio = g->clos->disc.io;
+       g->clos->disc.io = dfltDisc;
+    }
+    rv = agwrite (g, fp);
+    if (g->clos->disc.io != dfltDisc) {
+       g->clos->disc.io = saveio;
+    }
+    return rv;
+}
+
 /* writeFile:
  * Write graph into file f.
  * Return 0 on success
  */
-int writeFile(Agraph_t * g, char *f)
+int writeFile(Agraph_t * g, char *f, Agiodisc_t* io)
 {
     int rv;
     Sfio_t *fp;
@@ -560,7 +581,7 @@ int writeFile(Agraph_t * g, char *f)
        exerror("Could not open %s for writing in writeG", f);
        return 1;
     }
-    rv = agwrite(g, fp);
+    rv = sfioWrite(g, fp, io);
     sfclose(fp);
     return rv;
 }
@@ -589,7 +610,7 @@ Agraph_t *readFile(char *f)
     return gp;
 }
 
-int fwriteFile(Expr_t * ex, Agraph_t * g, int fd)
+int fwriteFile(Expr_t * ex, Agraph_t * g, int fd, Agiodisc_t* io)
 {
     Sfio_t *sp;
 
@@ -598,7 +619,7 @@ int fwriteFile(Expr_t * ex, Agraph_t * g, int fd)
        exerror("fwriteG: %d: invalid descriptor", fd);
        return 0;
     }
-    return agwrite(g, sp);
+    return sfioWrite(g, sp, io);
 }
 
 Agraph_t *freadFile(Expr_t * ex, int fd)
index 59190573f10047219635fc9f4a6dcd3b7cfb72c7..5fc43fd9ae5131a4ef2ffb4dee9ec4f7ccab33db 100644 (file)
@@ -38,8 +38,9 @@ extern "C" {
     extern Agedge_t *addEdge(Agraph_t * g, Agedge_t * e, int doAdd);
     extern Agraph_t *sameG(void *p1, void *p2, char *fn, char *msg);
     extern int compare(Agobj_t *, Agobj_t *);
-    extern int writeFile(Agraph_t *, char *);
-    extern int fwriteFile(Expr_t *, Agraph_t *, int);
+    extern int sfioWrite(Agraph_t *, Sfio_t*, Agiodisc_t*);
+    extern int writeFile(Agraph_t *, char *, Agiodisc_t*);
+    extern int fwriteFile(Expr_t *, Agraph_t *, int, Agiodisc_t*);
     extern Agraph_t *readFile(char *);
     extern Agraph_t *freadFile(Expr_t *, int);
     extern int openFile(Expr_t *, char *, char *);
index 2e9c13ebd8ef4ac4c966a6ca287b277f76d138fd..6134808629ab06d488cbcb78037258637639af9e 100644 (file)
 #define PTR2INT(v) ((Sflong_t)(v))
 #endif
 
+static int iofread(void *chan, char *buf, int bufsize)
+{
+    return read(sffileno((Sfio_t *) chan), buf, bufsize);
+}
+
+static int ioputstr(void *chan, char *str)
+{
+    return sfputr((Sfio_t *) chan, str, -1);
+}
+
+static int ioflush(void *chan)
+{
+    return sfsync((Sfio_t *) chan);
+}
+
+static Agiodisc_t gprIoDisc = { iofread, ioputstr, ioflush };
+
+#ifdef GVDLL
+static Agdisc_t gprDisc = { 0, 0, &gprIoDisc };
+#else
+static Agdisc_t gprDisc = { &AgMemDisc, &AgIdDisc, &gprIoDisc };
+#endif
+
 /* nameOf:
  * Return name of object. 
  * Assumes obj !=  NULL
@@ -1090,7 +1113,7 @@ getval(Expr_t * pgm, Exnode_t * node, Exid_t * sym, Exref_t * ref,
                error(ERROR_WARNING, "NULL graph passed to write()");
                v.integer = 1;
            } else
-               v.integer = agwrite(gp, state->outFile);
+               v.integer = sfioWrite (gp, state->outFile, state->dfltIO);
            break;
        case F_writeg:
            gp = INT2PTR(Agraph_t *, args[0].integer);
@@ -1098,7 +1121,7 @@ getval(Expr_t * pgm, Exnode_t * node, Exid_t * sym, Exref_t * ref,
                error(ERROR_WARNING, "NULL graph passed to writeG()");
                v.integer = 1;
            } else
-               v.integer = writeFile(gp, args[1].string);
+               v.integer = writeFile(gp, args[1].string, state->dfltIO);
            break;
        case F_readg:
            gp = readFile(args[0].string);
@@ -1110,7 +1133,7 @@ getval(Expr_t * pgm, Exnode_t * node, Exid_t * sym, Exref_t * ref,
                error(ERROR_WARNING, "NULL graph passed to fwriteG()");
                v.integer = 1;
            } else
-               v.integer = fwriteFile(pgm, gp, args[1].integer);
+               v.integer = fwriteFile(pgm, gp, args[1].integer, state->dfltIO);
            break;
        case F_freadg:
            gp = freadFile(pgm, args[0].integer);
@@ -2299,6 +2322,9 @@ comp_prog *compileProg(parse_prog * inp, Gpr_t * state, int flags)
     char *endg_sfx = 0;
     int i, useflags = 0;
 
+    /* Initialize default io */
+    state->dfltIO = &gprIoDisc;
+
     /* Make sure we have enough bits for types */
     assert(BITS_PER_BYTE * sizeof(tctype) >= (1 << TBITS));
 
@@ -2423,29 +2449,6 @@ void ptchk(void)
        printf("%d: %d %d\n", i, tchk[i][0], tchk[i][1]);
 }
 
-static int iofread(void *chan, char *buf, int bufsize)
-{
-    return read(sffileno((Sfio_t *) chan), buf, bufsize);
-}
-
-static int ioputstr(void *chan, char *str)
-{
-    return sfputr((Sfio_t *) chan, str, -1);
-}
-
-static int ioflush(void *chan)
-{
-    return sfsync((Sfio_t *) chan);
-}
-
-static Agiodisc_t gprIoDisc = { iofread, ioputstr, ioflush };
-
-#ifdef GVDLL
-static Agdisc_t gprDisc = { 0, 0, &gprIoDisc };
-#else
-static Agdisc_t gprDisc = { &AgMemDisc, &AgIdDisc, &gprIoDisc };
-#endif
-
 /* readG:
  * Read graph from file and initialize
  * dynamic data.
index aa57fe3f3686ace53d3d4cbc97ff32d8a6571f5d..4a8d7adadf9c500eb6198084bc924eab0dc7fe4c 100644 (file)
@@ -46,6 +46,7 @@ extern "C" {
        char *tgtname;
        char *infname;
        Sfio_t *outFile;
+       Agiodisc_t* dfltIO;
        trav_type tvt;
        Agnode_t *tvroot;
        Agedge_t *tvedge;
index 01ed4be5534eedd3c80cddf02af7a63131c10f7c..47636f63c1aeb4fda2e0e50823b5a8f22b84b2cc 100644 (file)
@@ -960,7 +960,7 @@ int gvpr (int argc, char *argv[], gvpropts * uopts)
                if (uopts && (uopts->flags & GV_USE_OUTGRAPH))
                    addOutputGraph (state, uopts);
                else
-                   agwrite(state->outgraph, opts->outFile);
+                   sfioWrite (state->outgraph, opts->outFile, state->dfltIO);
            }
 
            if (!incoreGraphs)