Commit 080346be authored by medranocalvo's avatar medranocalvo

tcldot: simplify option matching with a macro

parent 60482089
......@@ -42,22 +42,20 @@ int edgecmd(ClientData clientData, Tcl_Interp * interp,
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
if (MATCHES_OPTION("delete", argv[1], c, length)) {
deleteEdge(gctx, g, e);
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listattributes", length) == 0)) {
} else if (MATCHES_OPTION("listattributes", argv[1], c, length)) {
listEdgeAttrs (interp, g);
return TCL_OK;
} else if ((c == 'l') && (strncmp(argv[1], "listnodes", length) == 0)) {
} else if (MATCHES_OPTION("listnodes", argv[1], c, length)) {
Tcl_AppendElement(interp, obj2cmd(agtail(e)));
Tcl_AppendElement(interp, obj2cmd(aghead(e)));
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "queryattributes", length) == 0)) {
} else if (MATCHES_OPTION("queryattributes", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -75,7 +73,7 @@ int edgecmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'q') && (strncmp(argv[1], "queryattributevalues", length) == 0)) {
} else if (MATCHES_OPTION("queryattributevalues", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -94,7 +92,7 @@ int edgecmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 's') && (strncmp(argv[1], "setattributes", length) == 0)) {
} else if (MATCHES_OPTION("setattributes", argv[1], c, length)) {
if (argc == 3) {
if (Tcl_SplitList
(interp, argv[2], &argc2,
......@@ -120,7 +118,7 @@ int edgecmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 's') && (strncmp(argv[1], "showname", length) == 0)) {
} else if (MATCHES_OPTION("showname", argv[1], c, length)) {
if (agisdirected(g))
s = "->";
else
......
......@@ -29,7 +29,8 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
ictx_t *ictx = gctx->ictx;
Agsym_t *a;
char c, buf[256], **argv2;
int i, j, length, argc2, rc;
int i, j, argc2, rc;
size_t length;
GVC_t *gvc = ictx->gvc;
GVJ_t *job = gvc->job;
......@@ -46,7 +47,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'a') && (strncmp(argv[1], "addedge", length) == 0)) {
if (MATCHES_OPTION("addedge", argv[1], c, length)) {
if ((argc < 4) || (argc % 2)) {
Tcl_AppendResult(interp, "Wrong # args: should be \"", argv[0],
" addedge tail head ?attributename attributevalue? ?...?\"",
......@@ -80,7 +81,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
setedgeattributes(agroot(g), e, &argv[4], argc - 4);
return TCL_OK;
} else if ((c == 'a') && (strncmp(argv[1], "addnode", length) == 0)) {
} else if (MATCHES_OPTION("addnode", argv[1], c, length)) {
if (argc % 2) {
/* if odd number of args then argv[2] is name */
n = agnode(g, argv[2], 1);
......@@ -93,8 +94,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
setnodeattributes(agroot(g), n, &argv[i], argc - i);
return TCL_OK;
} else if ((c == 'a')
&& (strncmp(argv[1], "addsubgraph", length) == 0)) {
} else if (MATCHES_OPTION("addsubgraph", argv[1], c, length)) {
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
"\" addsubgraph ?name? ?attributename attributevalue? ?...?",
......@@ -112,21 +112,21 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
setgraphattributes(sg, &argv[i], argc - i);
return TCL_OK;
} else if ((c == 'c') && (strncmp(argv[1], "countnodes", length) == 0)) {
} else if (MATCHES_OPTION("countnodes", argv[1], c, length)) {
sprintf(buf, "%d", agnnodes(g));
Tcl_AppendResult(interp, buf, NULL);
return TCL_OK;
} else if ((c == 'c') && (strncmp(argv[1], "countedges", length) == 0)) {
} else if (MATCHES_OPTION("countedges", argv[1], c, length)) {
sprintf(buf, "%d", agnedges(g));
Tcl_AppendResult(interp, buf, NULL);
return TCL_OK;
} else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
} else if (MATCHES_OPTION("delete", argv[1], c, length)) {
deleteGraph(gctx, g);
return TCL_OK;
} else if ((c == 'f') && (strncmp(argv[1], "findedge", length) == 0)) {
} else if (MATCHES_OPTION("findedge", argv[1], c, length)) {
if (argc < 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " findedge tailnodename headnodename\"", NULL);
......@@ -147,7 +147,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
Tcl_AppendElement(interp, obj2cmd(e));
return TCL_OK;
} else if ((c == 'f') && (strncmp(argv[1], "findnode", length) == 0)) {
} else if (MATCHES_OPTION("findnode", argv[1], c, length)) {
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " findnode nodename\"", NULL);
return TCL_ERROR;
......@@ -159,36 +159,31 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
Tcl_AppendResult(interp, obj2cmd(n), NULL);
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "layoutedges", length) == 0)) {
} else if (MATCHES_OPTION("layoutedges", argv[1], c, length)) {
g = agroot(g);
if (!aggetrec (g, "Agraphinfo_t",0))
tcldot_layout(gvc, g, (argc > 2) ? argv[2] : NULL);
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "layoutnodes", length) == 0)) {
} else if (MATCHES_OPTION("layoutnodes", argv[1], c, length)) {
g = agroot(g);
if (!aggetrec (g, "Agraphinfo_t",0))
tcldot_layout(gvc, g, (argc > 2) ? argv[2] : NULL);
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listattributes", length) == 0)) {
} else if (MATCHES_OPTION("listattributes", argv[1], c, length)) {
listGraphAttrs(interp, g);
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listedgeattributes", length) == 0)) {
} else if (MATCHES_OPTION("listedgeattributes", argv[1], c, length)) {
listEdgeAttrs (interp, g);
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listnodeattributes", length) == 0)) {
} else if (MATCHES_OPTION("listnodeattributes", argv[1], c, length)) {
listNodeAttrs (interp, g);
return TCL_OK;
} else if ((c == 'l') && (strncmp(argv[1], "listedges", length) == 0)) {
} else if (MATCHES_OPTION("listedges", argv[1], c, length)) {
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
Tcl_AppendElement(interp, obj2cmd(e));
......@@ -196,29 +191,26 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'l') && (strncmp(argv[1], "listnodes", length) == 0)) {
} else if (MATCHES_OPTION("listnodes", argv[1], c, length)) {
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
Tcl_AppendElement(interp, obj2cmd(n));
}
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listnodesrev", length) == 0)) {
} else if (MATCHES_OPTION("listnodesrev", argv[1], c, length)) {
for (n = aglstnode(g); n; n = agprvnode(g, n)) {
Tcl_AppendElement(interp, obj2cmd(n));
}
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listsubgraphs", length) == 0)) {
} else if (MATCHES_OPTION("listsubgraphs", argv[1], c, length)) {
for (sg = agfstsubg(g); sg; sg = agnxtsubg(sg)) {
Tcl_AppendElement(interp, obj2cmd(sg));
}
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "queryattributes", length) == 0)) {
} else if (MATCHES_OPTION("queryattributes", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -236,9 +228,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "queryattributevalues", length) ==
0)) {
} else if (MATCHES_OPTION("queryattributevalues", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -257,8 +247,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "queryedgeattributes", length) == 0)) {
} else if (MATCHES_OPTION("queryedgeattributes", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -276,8 +265,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "queryedgeattributevalues", length) == 0)) {
} else if (MATCHES_OPTION("queryedgeattributevalues", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -297,8 +285,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "querynodeattributes", length) == 0)) {
} else if (MATCHES_OPTION("querynodeattributes", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -317,9 +304,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "querynodeattributevalues", length) ==
0)) {
} else if (MATCHES_OPTION("querynodeattributevalues", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -338,7 +323,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'r') && (strncmp(argv[1], "render", length) == 0)) {
} else if (MATCHES_OPTION("render", argv[1], c, length)) {
char *canvas;
if (argc < 3) {
......@@ -387,7 +372,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
#if 0
#if HAVE_LIBGD
} else if ((c == 'r') && (strncmp(argv[1], "rendergd", length) == 0)) {
} else if (MATCHES_OPTION("rendergd", argv[1], c, length)) {
#if 0
void **hdl;
#endif
......@@ -431,8 +416,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
#endif
#endif
} else if ((c == 's')
&& (strncmp(argv[1], "setattributes", length) == 0)) {
} else if (MATCHES_OPTION("setattributes", argv[1], c, length)) {
if (argc == 3) {
if (Tcl_SplitList
(interp, argv[2], &argc2,
......@@ -462,8 +446,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 's')
&& (strncmp(argv[1], "setedgeattributes", length) == 0)) {
} else if (MATCHES_OPTION("setedgeattributes", argv[1], c, length)) {
if (argc == 3) {
if (Tcl_SplitList
(interp, argv[2], &argc2,
......@@ -488,8 +471,7 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 's')
&& (strncmp(argv[1], "setnodeattributes", length) == 0)) {
} else if (MATCHES_OPTION("setnodeattributes", argv[1], c, length)) {
if (argc == 3) {
if (Tcl_SplitList
(interp, argv[2], &argc2,
......@@ -514,11 +496,10 @@ int graphcmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 's') && (strncmp(argv[1], "showname", length) == 0)) {
} else if (MATCHES_OPTION("showname", argv[1], c, length)) {
Tcl_SetResult(interp, agnameof(g), TCL_STATIC);
return TCL_OK;
} else if ((c == 'w') && (strncmp(argv[1], "write", length) == 0)) {
} else if (MATCHES_OPTION("write", argv[1], c, length)) {
g = agroot(g);
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
......
......@@ -22,7 +22,8 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
)
{
char c, **argv2;
int i, j, length, argc2;
int i, j, argc2;
size_t length;
Agraph_t *g;
Agnode_t *n, *head;
Agedge_t *e;
......@@ -44,7 +45,7 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
length = strlen(argv[1]);
if ((c == 'a') && (strncmp(argv[1], "addedge", length) == 0)) {
if (MATCHES_OPTION("addedge", argv[1], c, length)) {
if ((argc < 3) || (!(argc % 2))) {
Tcl_AppendResult(interp, "Wrong # args: should be \"", argv[0], " addedge head ?attributename attributevalue? ?...?\"", NULL);
return TCL_ERROR;
......@@ -65,11 +66,11 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
setedgeattributes(agroot(g), e, &argv[3], argc - 3);
return TCL_OK;
} else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
} else if (MATCHES_OPTION("delete", argv[1], c, length)) {
deleteNode(gctx, g, n);
return TCL_OK;
} else if ((c == 'f') && (strncmp(argv[1], "findedge", length) == 0)) {
} else if (MATCHES_OPTION("findedge", argv[1], c, length)) {
if (argc < 3) {
Tcl_AppendResult(interp, "Wrong # args: should be \"", argv[0], " findedge headnodename\"", NULL);
return TCL_ERROR;
......@@ -85,33 +86,29 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
Tcl_AppendElement(interp, obj2cmd(head));
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listattributes", length) == 0)) {
} else if (MATCHES_OPTION("listattributes", argv[1], c, length)) {
listNodeAttrs (interp, g);
return TCL_OK;
} else if ((c == 'l') && (strncmp(argv[1], "listedges", length) == 0)) {
} else if (MATCHES_OPTION("listedges", argv[1], c, length)) {
for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
Tcl_AppendElement(interp, obj2cmd(e));
}
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listinedges", length) == 0)) {
} else if (MATCHES_OPTION("listinedges", argv[1], c, length)) {
for (e = agfstin(g, n); e; e = agnxtin(g, e)) {
Tcl_AppendElement(interp, obj2cmd(e));
}
return TCL_OK;
} else if ((c == 'l')
&& (strncmp(argv[1], "listoutedges", length) == 0)) {
} else if (MATCHES_OPTION("listoutedges", argv[1], c, length)) {
for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
Tcl_AppendElement(interp, obj2cmd(e));
}
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "queryattributes", length) == 0)) {
} else if (MATCHES_OPTION("queryattributes", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -129,9 +126,7 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 'q')
&& (strncmp(argv[1], "queryattributevalues", length) ==
0)) {
} else if (MATCHES_OPTION("queryattributevalues", argv[1], c, length)) {
for (i = 2; i < argc; i++) {
if (Tcl_SplitList
(interp, argv[i], &argc2,
......@@ -150,8 +145,7 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 's')
&& (strncmp(argv[1], "setattributes", length) == 0)) {
} else if (MATCHES_OPTION("setattributes", argv[1], c, length)) {
g = agroot(g);
if (argc == 3) {
if (Tcl_SplitList
......@@ -178,7 +172,7 @@ int nodecmd(ClientData clientData, Tcl_Interp * interp,
}
return TCL_OK;
} else if ((c == 's') && (strncmp(argv[1], "showname", length) == 0)) {
} else if (MATCHES_OPTION("showname", argv[1], c, length)) {
Tcl_SetResult(interp, agnameof(n), TCL_STATIC);
return TCL_OK;
......
......@@ -25,7 +25,8 @@ static int dotnew(ClientData clientData, Tcl_Interp * interp,
ictx_t *ictx = (ictx_t *)clientData;
Agraph_t *g;
char c;
int i, length;
int i;
size_t length;
Agdesc_t kind;
if ((argc < 2)) {
......@@ -36,15 +37,13 @@ static int dotnew(ClientData clientData, Tcl_Interp * interp,
}
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'd') && (strncmp(argv[1], "digraph", length) == 0)) {
if (MATCHES_OPTION("digraph", argv[1], c, length)) {
kind = Agdirected;
} else if ((c == 'd')
&& (strncmp(argv[1], "digraphstrict", length) == 0)) {
} else if (MATCHES_OPTION("digraphstrict", argv[1], c, length)) {
kind = Agstrictdirected;
} else if ((c == 'g') && (strncmp(argv[1], "graph", length) == 0)) {
} else if (MATCHES_OPTION("graph", argv[1], c, length)) {
kind = Agundirected;
} else if ((c == 'g')
&& (strncmp(argv[1], "graphstrict", length) == 0)) {
} else if (MATCHES_OPTION("graphstrict", argv[1], c, length)) {
kind = Agstrictundirected;
} else {
Tcl_AppendResult(interp, "bad graphtype \"", argv[1], "\": must be one of:",
......
......@@ -117,3 +117,11 @@ extern size_t Tcldot_string_writer(GVJ_t *job, const char *s, size_t len);
extern size_t Tcldot_channel_writer(GVJ_t *job, const char *s, size_t len);
extern void tcldot_layout(GVC_t *gvc, Agraph_t * g, char *engine);
/* Check whether string str corresponds to option opname.
* opname: literal string.
* str: the string to check.
* str0: first character of str.
* strn: length of str. */
#define MATCHES_OPTION(opname, str, str0, strn) \
(opname[0] == str0) && (sizeof(opname)-1 == strn) && (strncmp(opname, str, sizeof(opname)) == 0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment