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;
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;
}
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;
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)
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 *);
#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
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);
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);
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);
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));
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.