From: erg Date: Thu, 16 Jul 2009 22:09:50 +0000 (+0000) Subject: Make sure the graph discipline is set to sfio before calling agwrite, then reset it. X-Git-Tag: LAST_LIBGRAPH~32^2~1813 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=83c8824149c8b3071075beb76eacf0c2b91acd68;p=graphviz Make sure the graph discipline is set to sfio before calling agwrite, then reset it. --- diff --git a/lib/gvpr/actions.c b/lib/gvpr/actions.c index 759d161b0..51ea873d0 100644 --- a/lib/gvpr/actions.c +++ b/lib/gvpr/actions.c @@ -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) diff --git a/lib/gvpr/actions.h b/lib/gvpr/actions.h index 59190573f..5fc43fd9a 100644 --- a/lib/gvpr/actions.h +++ b/lib/gvpr/actions.h @@ -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 *); diff --git a/lib/gvpr/compile.c b/lib/gvpr/compile.c index 2e9c13ebd..613480862 100644 --- a/lib/gvpr/compile.c +++ b/lib/gvpr/compile.c @@ -60,6 +60,29 @@ #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. diff --git a/lib/gvpr/gprstate.h b/lib/gvpr/gprstate.h index aa57fe3f3..4a8d7adad 100644 --- a/lib/gvpr/gprstate.h +++ b/lib/gvpr/gprstate.h @@ -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; diff --git a/lib/gvpr/gvpr.c b/lib/gvpr/gvpr.c index 01ed4be55..47636f63c 100644 --- a/lib/gvpr/gvpr.c +++ b/lib/gvpr/gvpr.c @@ -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)