This example shows how one can filter a graph changing nodes shape. Nodes with default shape (ellipse) will have shape changed to hexagon. Nodes with hexagon shape will be changed to default shape. Other nodes will remain unchanged. For faster access to node attributes this example uses attribute descriptor.
The input file is assumed to be in GraphViz DOT language.
Below are the figures of graph before and after change:
Before
After 
1 #pragma package ".." 2 #include "chagraph.h" 3 #include <stdlib.h> 4 5 int main(int argc, char *argv[]) 6 { 7 Agraph_t *g; 8 Agnode_t *n; 9 10 if (argc < 2) { 11 printf("Usage: %s file\n" 12 " file -- file in GraphViz dot format\n", 13 argv[0]); 14 exit(EXIT_FAILURE); 15 } 16 FILE *f = fopen(argv[1], "r"); 17 FILE *outf = fopen(argv[2], "w"); 18 g = agread(f, 0); 19 fclose(f); 20 21 Agsym_t *sym; 22 sym = agattr(g, AGNODE, "shape", ""); 23 char *str; 24 for (n = agfstnode(g); n; n = agnxtnode(n)) { 25 str = agxget(n, sym); 26 if (strcmp(str, "") == 0) { 27 agxset(n, sym, "hexagon"); 28 } else if (strcmp(str, "hexagon") == 0) { 29 agxset(n, sym, ""); 30 } 31 } 32 33 agwrite(g, outf); 34 agclose(g); 35 return 0; 36 }
Code description
- 21-31
- Iterate over all nodes of graph. If node shape is "hexagon" change it to default node shape (ellipse). If node has default shape change it to "hexagon".
- 21
- Use attribute descriptor for faster access to node attributes
- 25
- Get value of node attribute
- 34
- Close graph and free memeory allocated for it