]> granicus.if.org Git - graphviz/commitdiff
Alter the ingraphs library to accept in-core graphs.
authorerg <devnull@localhost>
Tue, 23 Jun 2009 15:28:14 +0000 (15:28 +0000)
committererg <devnull@localhost>
Tue, 23 Jun 2009 15:28:14 +0000 (15:28 +0000)
lib/ingraphs/ingraphs.c
lib/ingraphs/ingraphs.h

index 75504b06ce1d44748d13424ab06b0728873e140c..d70ce014891d96b8f6bd188af4d63bdf2cbb5648 100644 (file)
@@ -41,19 +41,19 @@ static void nextFile(ingraph_state * sp)
     void *rv = NULL;
     char *fname;
 
-    if (sp->Files == NULL) {
+    if (sp->u.Files == NULL) {
        if (sp->ctr++ == 0) {
            rv = sp->fns->dflt;
        }
     } else {
-       while ((fname = sp->Files[sp->ctr++])) {
+       while ((fname = sp->u.Files[sp->ctr++])) {
            if (*fname == '-') {
                rv = sp->fns->dflt;
                break;
            } else if ((rv = sp->fns->openf(fname)) != 0)
                break;
            else {
-               fprintf(stderr, "Can't open %s\n", sp->Files[sp->ctr - 1]);
+               fprintf(stderr, "Can't open %s\n", sp->u.Files[sp->ctr - 1]);
                sp->errors++;
            }
        }
@@ -71,6 +71,11 @@ Agraph_t *nextGraph(ingraph_state * sp)
 {
     Agraph_t *g;
 
+    if (sp->ingraphs) {
+       g = (Agraph_t*)(sp->u.Graphs[sp->ctr]);
+       if (g) sp->ctr++;
+       return g;
+    }
     if (sp->fp == NULL)
        nextFile(sp);
     g = NULL;
@@ -78,18 +83,19 @@ Agraph_t *nextGraph(ingraph_state * sp)
     while (sp->fp != NULL) {
        if ((g = sp->fns->readf(sp->fp)) != 0)
            break;
-       if (sp->Files)          /* Only close if not using stdin */
+       if (sp->u.Files)        /* Only close if not using stdin */
            sp->fns->closef(sp->fp);
        nextFile(sp);
     }
     return g;
 }
 
-/* newIng:
+/* new_ing:
  * Create new ingraph state. If sp is non-NULL, we
  * assume user is supplying memory.
  */
-ingraph_state *newIng(ingraph_state * sp, char **files, ingdisc * disc)
+static ingraph_state*
+new_ing(ingraph_state * sp, char **files, Agraph_t** graphs, ingdisc * disc)
 {
     if (!sp) {
        sp = (ingraph_state *) malloc(sizeof(ingraph_state));
@@ -100,7 +106,14 @@ ingraph_state *newIng(ingraph_state * sp, char **files, ingdisc * disc)
        sp->heap = 1;
     } else
        sp->heap = 0;
-    sp->Files = files;
+    if (graphs) {
+       sp->ingraphs = 1;
+       sp->u.Graphs = graphs;
+    }
+    else {
+       sp->ingraphs = 0;
+       sp->u.Graphs = files;
+    }
     sp->ctr = 0;
     sp->errors = 0;
     sp->fp = NULL;
@@ -122,6 +135,22 @@ ingraph_state *newIng(ingraph_state * sp, char **files, ingdisc * disc)
     return sp;
 }
 
+ingraph_state*
+newIng(ingraph_state * sp, char **files, ingdisc * disc)
+{
+    return new_ing(sp, files, 0, disc);
+}
+
+/* newIngGraphs:
+ * Create new ingraph state using supplied graphs. If sp is non-NULL, we
+ * assume user is supplying memory.
+ */
+ingraph_state*
+newIngGraphs(ingraph_state * sp , Agraph_t** graphs, ingdisc *disc)
+{
+    return new_ing(sp, 0, graphs, disc);
+}
+
 static void *dflt_open(char *f)
 {
     return fopen(f, "r");
@@ -159,7 +188,7 @@ ingraph_state *newIngraph(ingraph_state * sp, char **files, opengfn opf)
  */
 void closeIngraph(ingraph_state * sp)
 {
-    if (sp->Files && sp->fp)
+    if (!sp->ingraphs && sp->u.Files && sp->fp)
        sp->fns->closef(sp->fp);
     free(sp->fns);
     if (sp->heap)
@@ -173,9 +202,12 @@ char *fileName(ingraph_state * sp)
 {
     char *fname;
 
-    if (sp->Files) {
+    if (sp->ingraphs) {
+       return "<>";
+    }
+    else if (sp->u.Files) {
        if (sp->ctr) {
-           fname = sp->Files[sp->ctr - 1];
+           fname = sp->u.Files[sp->ctr - 1];
            if (*fname == '-')
                return "<stdin>";
            else
index 749ae580ce513cd4e35059854ab9f7045a24712b..bbb4d4ba8082d62916c877c4ce205f85312dff60 100644 (file)
@@ -39,8 +39,12 @@ extern "C" {
     } ingdisc;
 
     typedef struct {
-       char **Files;
+       union {
+           char**     Files;
+           Agraph_t** Graphs;
+       } u;
        int ctr;
+       int ingraphs;
        void *fp;
        ingdisc *fns;
        char heap;
@@ -49,6 +53,7 @@ extern "C" {
 
     extern ingraph_state *newIngraph(ingraph_state *, char **, opengfn);
     extern ingraph_state *newIng(ingraph_state *, char **, ingdisc *);
+    extern ingraph_state *newIngGraphs(ingraph_state *, Agraph_t**, ingdisc *);
     extern void closeIngraph(ingraph_state * sp);
     extern Agraph_t *nextGraph(ingraph_state *);
     extern char *fileName(ingraph_state *);