]> granicus.if.org Git - graphviz/commitdiff
consolidate handle generation into obj2cmd()
authorJohn Ellson <ellson@research.att.com>
Tue, 14 Aug 2012 20:58:00 +0000 (16:58 -0400)
committerJohn Ellson <ellson@research.att.com>
Tue, 14 Aug 2012 20:58:00 +0000 (16:58 -0400)
tclpkg/tcldot/tcldot-edgecmd.c
tclpkg/tcldot/tcldot-graphcmd.c
tclpkg/tcldot/tcldot-id.c
tclpkg/tcldot/tcldot-nodecmd.c
tclpkg/tcldot/tcldot-util.c
tclpkg/tcldot/tcldot.c
tclpkg/tcldot/tcldot.h

index 340004402d4697797b6c5d0213015076acfc9ab6..3cb06d12d7aa5261396a06884a8e7ed95809693d 100644 (file)
@@ -21,12 +21,13 @@ int edgecmd(ClientData clientData, Tcl_Interp * interp,
 #endif                         /* TCLOBJ */
     )
 {
-    char c, buf[32], *s, **argv2;
+    char c, *s, **argv2;
     int i, j, length, argc2;
     Agraph_t *g;
     Agedge_t *e;
     Agsym_t *a;
 #ifndef WITH_CGRAPH
+    buf[32];
     Agedge_t **ep;
     ictx_t *ictx = (ictx_t *)clientData;
 #else
@@ -46,7 +47,7 @@ int edgecmd(ClientData clientData, Tcl_Interp * interp,
     }
     e = *ep;
 #else
-    e = cmd2e(gctx,argv[0]);
+    e = cmd2e(argv[0]);
     if (!e) {
         Tcl_AppendResult(interp, "Edge \"", argv[0], "\" not found", NULL);
         return TCL_ERROR;
@@ -74,16 +75,16 @@ int edgecmd(ClientData clientData, Tcl_Interp * interp,
     } else if ((c == 'l') && (strncmp(argv[1], "listnodes", length) == 0)) {
 #ifndef WITH_CGRAPH
        tclhandleString(ictx->nodeTblPtr, buf, AGID(agtail(e)));
+       Tcl_AppendElement(interp, buf);
 #else
-       sprintf(buf,"node%p",agtail(e));
+       Tcl_AppendElement(interp, obj2cmd(agtail(e)));
 #endif
-       Tcl_AppendElement(interp, buf);
 #ifndef WITH_CGRAPH
        tclhandleString(ictx->nodeTblPtr, buf, AGID(aghead(e)));
+       Tcl_AppendElement(interp, buf);
 #else
-       sprintf(buf,"node%p",aghead(e));
+       Tcl_AppendElement(interp, obj2cmd(aghead(e)));
 #endif
-       Tcl_AppendElement(interp, buf);
        return TCL_OK;
 
     } else if ((c == 'q')
index 73b1cf421d0497c0ef3560aa626e05af98df596e..a6dec6d10c0da8e8bc754208dca5cc94e44cb325 100644 (file)
@@ -52,7 +52,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
     }
     g = *gp;
 #else
-    g = cmd2g(gctx,argv[0]);
+    g = cmd2g(argv[0]);
     if (!g) {
        Tcl_AppendResult(interp, "Graph \"", argv[0], "\" not found", NULL);
        return TCL_ERROR;
@@ -80,7 +80,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
            tail = *np;
        }
 #else
-        tail = cmd2n(gctx,argv[2]);
+        tail = cmd2n(argv[2]);
         if (!tail) {
            if (!(tail = agfindnode(g, argv[2]))) {
                Tcl_AppendResult(interp, "Tail node \"", argv[2], "\" not found.", NULL);
@@ -103,7 +103,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
            head = *np;
        }
 #else
-        head = cmd2n(gctx,argv[2]);
+        head = cmd2n(argv[2]);
         if (!head) {
            if (!(head = agfindnode(g, argv[3]))) {
                Tcl_AppendResult(interp, "Head node \"", argv[3], "\" not found.", NULL);
@@ -115,12 +115,11 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
            Tcl_AppendResult(interp, "Head node ", argv[3], " is not in the graph.", NULL);
            return TCL_ERROR;
        }
-#ifdef WITH_CGRAPH
-       e = agedge(g, tail, head, NULL, 1);
-       sprintf(buf,"edge%p",e);
-       Tcl_AppendResult(interp, buf, NULL);
-#else
+#ifndef WITH_CGRAPH
        e = agedge(g, tail, head);
+#else
+       e = agedge(g, tail, head, NULL, 1);
+       Tcl_AppendResult(interp, obj2cmd(e), NULL);
 #endif
 #ifndef WITH_CGRAPH
        if (!(ep = (Agedge_t **) tclhandleXlateIndex(ictx->edgeTblPtr, AGID(e))) || *ep != e) {
@@ -188,8 +187,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
        np = (Agnode_t **)tclhandleXlateIndex(ictx->nodeTblPtr, AGID(n));
        *np = n;
 #else
-       sprintf(buf,"node%p",n);
-       Tcl_AppendResult(interp, buf, NULL);
+       Tcl_AppendResult(interp, obj2cmd(n), NULL);
 #endif
        setnodeattributes(agroot(g), n, &argv[i], argc - i);
        reset_layout(gvc, g);
@@ -206,8 +204,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
            /* if odd number of args then argv[2] is name */
 #ifdef WITH_CGRAPH
            sg = agsubg(g, argv[2], 1);
-           sprintf(buf,"graph%p",sg);
-           Tcl_AppendResult(interp, buf, NULL);
+           Tcl_AppendResult(interp, obj2cmd(sg), NULL);
 #else
            sg = agsubg(g, argv[2]);
            if (!  (sgp = (Agraph_t **) tclhandleXlateIndex(ictx->graphTblPtr, AGID(sg))) || *sgp != sg) {
@@ -292,10 +289,10 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
        }
 #ifndef WITH_CGRAPH
        tclhandleString(ictx->edgeTblPtr, buf, AGID(e));
+       Tcl_AppendElement(interp, buf);
 #else
-       sprintf(buf,"edge%p",e);
+       Tcl_AppendElement(interp, obj2cmd(e));
 #endif
-       Tcl_AppendElement(interp, buf);
        return TCL_OK;
 
     } else if ((c == 'f') && (strncmp(argv[1], "findnode", length) == 0)) {
@@ -309,10 +306,10 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
        }
 #ifndef WITH_CGRAPH
        tclhandleString(ictx->nodeTblPtr, buf, AGID(n));
+       Tcl_AppendResult(interp, buf, NULL);
 #else
-       sprintf(buf,"node%p",n);
+       Tcl_AppendResult(interp, obj2cmd(n), NULL);
 #endif
-       Tcl_AppendResult(interp, buf, NULL);
        return TCL_OK;
 
     } else if ((c == 'l')
@@ -349,10 +346,10 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
            for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
 #ifndef WITH_CGRAPH
                tclhandleString(ictx->edgeTblPtr, buf, AGID(e));
+               Tcl_AppendElement(interp, buf);
 #else
-               sprintf(buf,"edge%p",e);
+               Tcl_AppendElement(interp, obj2cmd(e));
 #endif
-               Tcl_AppendElement(interp, buf);
            }
        }
        return TCL_OK;
@@ -361,10 +358,10 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
        for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
 #ifndef WITH_CGRAPH
            tclhandleString(ictx->nodeTblPtr, buf, AGID(n));
+           Tcl_AppendElement(interp, buf);
 #else
-           sprintf(buf,"node%p",n);
+           Tcl_AppendElement(interp, obj2cmd(n));
 #endif
-           Tcl_AppendElement(interp, buf);
            
        }
        return TCL_OK;
@@ -374,10 +371,10 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
        for (n = aglstnode(g); n; n = agprvnode(g, n)) {
 #ifndef WITH_CGRAPH
            tclhandleString(ictx->nodeTblPtr, buf, AGID(n));
+           Tcl_AppendElement(interp, buf);
 #else
-           sprintf(buf,"node%p",n);
+           Tcl_AppendElement(interp, obj2cmd(n));
 #endif
-           Tcl_AppendElement(interp, buf);
        }
        return TCL_OK;
 
@@ -385,8 +382,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
               && (strncmp(argv[1], "listsubgraphs", length) == 0)) {
 #ifdef WITH_CGRAPH
        for (sg = agfstsubg(g); sg; sg = agnxtsubg(sg)) {
-           sprintf(buf,"graph%p",sg);
-           Tcl_AppendElement(interp, buf);
+           Tcl_AppendElement(interp, obj2cmd(g));
        }
 #else
        if (g->meta_node) {
index d430ea966a4db7a613aaa829e5f9495e1ba6bd4f..978cc4649209bf11878ce227dde3de1627cb57dd 100644 (file)
@@ -85,18 +85,17 @@ static void myiddisc_idregister(void *state, int objtype, void *obj) {
     ictx_t *ictx = gctx->ictx;
     Tcl_Interp *interp = ictx->interp;
     Tcl_CmdProc *proc = NULL;
-    char buf[32] = "";
 
     switch (objtype) {
-        case AGRAPH: sprintf(buf,"graph%p",obj); proc=graphcmd; break;
-        case AGNODE: sprintf(buf,"node%p",obj); proc=nodecmd; break;
+        case AGRAPH: proc=graphcmd; break;
+        case AGNODE: proc=nodecmd; break;
         case AGINEDGE:
-        case AGOUTEDGE: sprintf(buf,"edge%p",obj); proc=edgecmd; break;
+        case AGOUTEDGE: proc=edgecmd; break;
     }
 #ifndef TCLOBJ
-    Tcl_CreateCommand(interp, buf, proc, (ClientData) gctx, (Tcl_CmdDeleteProc *) NULL);
+    Tcl_CreateCommand(interp, obj2cmd(obj), proc, (ClientData) gctx, (Tcl_CmdDeleteProc *) NULL);
 #else
-    Tcl_CreateObjCommand(interp, buf, proc, (ClientData) gctx, (Tcl_CmdDeleteProc *) NULL);
+    Tcl_CreateObjCommand(interp, obj2cmd(obj), proc, (ClientData) gctx, (Tcl_CmdDeleteProc *) NULL);
 #endif           
 }
 Agiddisc_t myiddisc = {
index c92a7873ea7fec5eb8879b9918957fb7a0bde361..0199d95f61a28eed2a3ed27a5e8df3589637c37c 100644 (file)
@@ -21,13 +21,14 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
 #endif                         /* TCLOBJ */
     )
 {
-    char c, buf[32], **argv2;
+    char c, **argv2;
     int i, j, length, argc2;
     Agraph_t *g;
     Agnode_t *n, *head;
     Agedge_t *e;
     Agsym_t *a;
 #ifndef WITH_CGRAPH
+    char buf[32];
     unsigned long id;
     Agnode_t **np;
     Agedge_t **ep;
@@ -49,7 +50,7 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
     }
     n = *np;
 #else
-    n = cmd2n(gctx,argv[0]);
+    n = cmd2n(argv[0]);
     if (!n) {
        Tcl_AppendResult(interp, "Node \"", argv[0], "\" not found", NULL);
        return TCL_ERROR;
@@ -76,7 +77,7 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
            head = *np;
        }
 #else
-       head = cmd2n(gctx,argv[2]);
+       head = cmd2n(argv[2]);
        if (!head) {
            if (!(head = agfindnode(g, argv[2]))) {
                Tcl_AppendResult(interp, "Head node \"", argv[2], "\" not found.", NULL);
@@ -90,8 +91,7 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
        }
 #ifdef WITH_CGRAPH
        e = agedge(g, n, head, NULL, 1);
-        sprintf(buf,"edge%p",e);
-        Tcl_AppendResult(interp, buf, NULL);
+        Tcl_AppendResult(interp, obj2cmd(e), NULL);
 #else
        e = agedge(g, n, head);
        if (!(ep = (Agedge_t **) tclhandleXlateIndex(ictx->edgeTblPtr, AGID(e))) || *ep != e) {
@@ -136,18 +136,18 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
        if (!(e = agfindedge(g, n, head))) {
 #ifndef WITH_CGRAPH
            tclhandleString(ictx->nodeTblPtr, buf, AGID(head));
+           Tcl_AppendResult(interp, "Edge \"", argv[0], " - ", buf, "\" not found.", NULL);
 #else
-           sprintf(buf,"node%p",head);
+           Tcl_AppendResult(interp, "Edge \"", argv[0], " - ", obj2cmd(head), "\" not found.", NULL);
 #endif
-           Tcl_AppendResult(interp, "Edge \"", argv[0], " - ", buf, "\" not found.", NULL);
            return TCL_ERROR;
        }
 #ifndef WITH_CGRAPH
        tclhandleString(ictx->edgeTblPtr, buf, AGID(e));
+       Tcl_AppendElement(interp, buf);
 #else
-       sprintf(buf,"edge%p",head);
+       Tcl_AppendElement(interp, obj2cmd(head));
 #endif
-       Tcl_AppendElement(interp, buf);
        return TCL_OK;
 
     } else if ((c == 'l')
@@ -159,10 +159,10 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
        for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
 #ifndef WITH_CGRAPH
            tclhandleString(ictx->edgeTblPtr, buf, AGID(e));
+           Tcl_AppendElement(interp, buf);
 #else
-           sprintf(buf,"edge%p",e);
+           Tcl_AppendElement(interp, obj2cmd(e));
 #endif
-           Tcl_AppendElement(interp, buf);
        }
        return TCL_OK;
 
@@ -171,10 +171,10 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
        for (e = agfstin(g, n); e; e = agnxtin(g, e)) {
 #ifndef WITH_CGRAPH
            tclhandleString(ictx->edgeTblPtr, buf, AGID(e));
+           Tcl_AppendElement(interp, buf);
 #else
-           sprintf(buf,"edge%p",e);
+           Tcl_AppendElement(interp, obj2cmd(e));
 #endif
-           Tcl_AppendElement(interp, buf);
        }
        return TCL_OK;
 
@@ -183,10 +183,10 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
        for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
 #ifndef WITH_CGRAPH
            tclhandleString(ictx->edgeTblPtr, buf, AGID(e));
+           Tcl_AppendElement(interp, buf);
 #else
-           sprintf(buf,"edge%p",e);
+           Tcl_AppendElement(interp, obj2cmd(e));
 #endif
-           Tcl_AppendElement(interp, buf);
        }
        return TCL_OK;
 
index 0bff8985fdf46cc2c066cc8fc5f119d129c91dc3..4c149cf9d1d9264c72b300e9b64fcec173c7c7a3 100644 (file)
@@ -38,37 +38,53 @@ void reset_layout(GVC_t *gvc, Agraph_t * sg)
 #ifdef WITH_CGRAPH
 /* handles (tcl commands) to obj* */
 
-Agraph_t *cmd2g(gctx_t *gctx, char *cmd) {
+Agraph_t *cmd2g(char *cmd) {
     Agraph_t *g = NULL;
 
     if (sscanf(cmd, "graph%p", &g) != 1 || !g)
         return NULL;
     return g;
 }
-Agnode_t *cmd2n(gctx_t *gctx, char *cmd) {
+Agnode_t *cmd2n(char *cmd) {
     Agnode_t *n = NULL;
 
     if (sscanf(cmd, "node%p", &n) != 1 || !n)
         return NULL;
     return n;
 }
-Agedge_t *cmd2e(gctx_t *gctx, char *cmd) {
+Agedge_t *cmd2e(char *cmd) {
     Agedge_t *e = NULL;
 
     if (sscanf(cmd, "edge%p", &e) != 1 || !e)
         return NULL;
     return e;
 }
+
+
+/* obj* to handles (tcl commands) */
+
+char *obj2cmd (void *obj) {
+    static char buf[32];
+
+    switch (AGTYPE(obj)) {
+        case AGRAPH: sprintf(buf,"graph%p",obj); break;
+        case AGNODE: sprintf(buf,"node%p",obj); break;
+        case AGINEDGE: 
+        case AGOUTEDGE: sprintf(buf,"edge%p",obj); break;
+    }
+    return buf;
+}
+
 #endif // WITH_CGRAPH
 
 #ifdef WITH_CGRAPH
 void deleteEdge(gctx_t *gctx, Agraph_t * g, Agedge_t *e)
 {
-    char buf[32];
+    char *hndl;
 
-    sprintf(buf,"edge%p",e);
+    hndl = obj2cmd(e);
     agdelete(gctx->g, e);  /* delete edge from root graph */
-    Tcl_DeleteCommand(gctx->ictx->interp, buf);
+    Tcl_DeleteCommand(gctx->ictx->interp, hndl);
 }
 static void deleteNodeEdges(gctx_t *gctx, Agraph_t *g, Agnode_t *n)
 {
@@ -83,13 +99,13 @@ static void deleteNodeEdges(gctx_t *gctx, Agraph_t *g, Agnode_t *n)
 }
 void deleteNode(gctx_t * gctx, Agraph_t *g, Agnode_t *n)
 {
-    char buf[32];
+    char *hndl;
 
     deleteNodeEdges(gctx, gctx->g, n); /* delete all edges to/from node in root graph */
 
-    sprintf(buf,"node%p",n);
+    hndl = obj2cmd(n);
     agdelete(gctx->g, n); /* delete node from root graph */
-    Tcl_DeleteCommand(gctx->ictx->interp, buf);
+    Tcl_DeleteCommand(gctx->ictx->interp, hndl);
 }
 static void deleteGraphNodes(gctx_t * gctx, Agraph_t *g)
 {
@@ -105,20 +121,20 @@ static void deleteGraphNodes(gctx_t * gctx, Agraph_t *g)
 void deleteGraph(gctx_t * gctx, Agraph_t *g)
 {
     Agraph_t *sg;
-    char buf[32];
+    char *hndl;
 
     for (sg = agfstsubg (g); sg; sg = agnxtsubg (sg)) {
        deleteGraph(gctx, sg);
     }
     deleteGraphNodes(gctx, g);
 
-    sprintf(buf,"graph%p",g);
+    hndl = obj2cmd(g);
     if (g == agroot(g)) {
        agclose(g);
     } else {
        agdelsubg(agroot(g), g);
     }
-    Tcl_DeleteCommand(gctx->ictx->interp, buf);
+    Tcl_DeleteCommand(gctx->ictx->interp, hndl);
 }
 #else
 void deleteEdge(ictx_t * ictx, Agraph_t * g, Agedge_t *e)
index bc54eac5c8be7993b6ef54f0ec3676d4e2f144a6..ff3339a400be42ecc3ed7c497146b8b9149b5144 100644 (file)
@@ -31,7 +31,6 @@ static int dotnew(ClientData clientData, Tcl_Interp * interp,
     int kind;
     unsigned long id;
 #else
-    char buf[32];
     Agdesc_t kind;
 #endif
 
@@ -118,8 +117,7 @@ static int dotnew(ClientData clientData, Tcl_Interp * interp,
     GD_drawing(g) = NULL;
 
 #ifdef WITH_CGRAPH
-    sprintf(buf, "graph%p", g);
-    Tcl_AppendResult(interp, buf, NULL);
+    Tcl_AppendResult(interp, obj2cmd(g), NULL);
 #endif
 
     return TCL_OK;
@@ -210,7 +208,6 @@ static int dotread(ClientData clientData, Tcl_Interp * interp,
     Tcl_Channel channel;
     int mode;
 #ifdef WITH_CGRAPH
-    char buf[32];
     ictx_t *ictx = (ictx_t *)clientData;
 
     ictx->myioDisc.afread = myiodisc_afread;  /* replace afread to use Tcl Channels */
@@ -254,8 +251,7 @@ static int dotread(ClientData clientData, Tcl_Interp * interp,
 #ifndef WITH_CGRAPH
     return (tcldot_fixup((ictx_t *)clientData, g));
 #else
-    sprintf(buf, "graph%p", g);
-    Tcl_AppendResult(interp, buf, NULL);
+    Tcl_AppendResult(interp, obj2cmd(g), NULL);
     return TCL_OK;
 #endif
 }
@@ -270,7 +266,6 @@ static int dotstring(ClientData clientData, Tcl_Interp * interp,
 {
     Agraph_t *g;
 #ifdef WITH_CGRAPH
-    char buf[32];
     ictx_t *ictx = (ictx_t *)clientData;
     rdr_t rdr;
 
@@ -308,8 +303,7 @@ static int dotstring(ClientData clientData, Tcl_Interp * interp,
 #ifndef WITH_CGRAPH
     return (tcldot_fixup((ictx_t *)clientData, g));
 #else
-    sprintf(buf, "graph%p", g);
-    Tcl_AppendResult(interp, buf, NULL);
+    Tcl_AppendResult(interp, obj2cmd(g), NULL);
     return TCL_OK;
 #endif
 }
index a6fcefda468791e81bc0b8c1aaed7b55deeaca56..d9ec94e625ef2dedacf1c86311de8bb48a567e5c 100644 (file)
@@ -110,9 +110,10 @@ extern int myiodisc_afread(void* channel, char *ubuf, int n);
 extern int myiodisc_memiofread(void *chan, char *buf, int bufsize);
 extern Agiddisc_t myiddisc;
 extern Agraph_t *agread_usergets (ictx_t *ictx, FILE * fp, int (*usergets)(void *chan, char *buf, int bufsize));
-extern Agraph_t *cmd2g(gctx_t *gctx, char *cmd);
-extern Agnode_t *cmd2n(gctx_t *gctx, char *cmd);
-extern Agedge_t *cmd2e(gctx_t *gctx, char *cmd);
+extern Agraph_t *cmd2g(char *cmd);
+extern Agnode_t *cmd2n(char *cmd);
+extern Agedge_t *cmd2e(char *cmd);
+extern char *obj2cmd(void *obj);
 extern void deleteEdge(gctx_t *gctx, Agraph_t * g, Agedge_t * e);
 extern void deleteNode(gctx_t *gctx, Agraph_t * g, Agnode_t * n);
 extern void deleteGraph(gctx_t *gctx, Agraph_t * g);