From: John Ellson Date: Tue, 14 Aug 2012 20:58:00 +0000 (-0400) Subject: consolidate handle generation into obj2cmd() X-Git-Tag: LAST_LIBGRAPH~32^2~355^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a312e26c72957b360f5e1daf17b55a4fb7254375;p=graphviz consolidate handle generation into obj2cmd() --- diff --git a/tclpkg/tcldot/tcldot-edgecmd.c b/tclpkg/tcldot/tcldot-edgecmd.c index 340004402..3cb06d12d 100644 --- a/tclpkg/tcldot/tcldot-edgecmd.c +++ b/tclpkg/tcldot/tcldot-edgecmd.c @@ -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') diff --git a/tclpkg/tcldot/tcldot-graphcmd.c b/tclpkg/tcldot/tcldot-graphcmd.c index 73b1cf421..a6dec6d10 100644 --- a/tclpkg/tcldot/tcldot-graphcmd.c +++ b/tclpkg/tcldot/tcldot-graphcmd.c @@ -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) { diff --git a/tclpkg/tcldot/tcldot-id.c b/tclpkg/tcldot/tcldot-id.c index d430ea966..978cc4649 100644 --- a/tclpkg/tcldot/tcldot-id.c +++ b/tclpkg/tcldot/tcldot-id.c @@ -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 = { diff --git a/tclpkg/tcldot/tcldot-nodecmd.c b/tclpkg/tcldot/tcldot-nodecmd.c index c92a7873e..0199d95f6 100644 --- a/tclpkg/tcldot/tcldot-nodecmd.c +++ b/tclpkg/tcldot/tcldot-nodecmd.c @@ -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; diff --git a/tclpkg/tcldot/tcldot-util.c b/tclpkg/tcldot/tcldot-util.c index 0bff8985f..4c149cf9d 100644 --- a/tclpkg/tcldot/tcldot-util.c +++ b/tclpkg/tcldot/tcldot-util.c @@ -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) diff --git a/tclpkg/tcldot/tcldot.c b/tclpkg/tcldot/tcldot.c index bc54eac5c..ff3339a40 100644 --- a/tclpkg/tcldot/tcldot.c +++ b/tclpkg/tcldot/tcldot.c @@ -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 } diff --git a/tclpkg/tcldot/tcldot.h b/tclpkg/tcldot/tcldot.h index a6fcefda4..d9ec94e62 100644 --- a/tclpkg/tcldot/tcldot.h +++ b/tclpkg/tcldot/tcldot.h @@ -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);