Debian Codegraph @

doc/libguide/samples.tex

raw
1 \section{A sample program: {\tt simple.c}}
2 \label{sec:simple}
3 This following code illustrates an application which uses \gviz\ to
4 position a graph using the \dot\ layout and then writes the output
5 using the {\tt plain} format.
6 An application can replace the call to {\tt gvRender} with its own
7 function for rendering the graph, using the layout information
8 encoded in the graph structure (cf. Section~\ref{sec:layout_info}).
9 \begin{verbatim}
10 #include <gvc.h>
11
12 int main(int argc, char **argv)
13 {
14 GVC_t *gvc;
15 Agraph_t *g;
16 FILE *fp;
17
18 gvc = gvContext();
19
20 if (argc > 1)
21 fp = fopen(argv[1], "r");
22 else
23 fp = stdin;
24 g = agread(fp, 0);
25
26 gvLayout(gvc, g, "dot");
27
28 gvRender(gvc, g, "plain", stdout);
29
30 gvFreeLayout(gvc, g);
31
32 agclose(g);
33
34 return (gvFreeContext(gvc));
35 }
36 \end{verbatim}
37 \newpage
38
39 \section{A sample program: {\tt dot.c}}
40 \label{sec:dot}
41 This example shows how an application might read a stream of input graphs,
42 lay out each, and then use the \gviz\ renderers to write the drawings
43 to an output file. Indeed, this
44 is precisely how the \dot\ program is written, ignoring some signal
45 handling, its specific declaration of
46 the {\tt Info} data (cf. Section~\ref{sec:info}), and a few other
47 minor details.
48 \begin{verbatim}
49 #include <gvc.h>
50
51 int main(int argc, char **argv)
52 {
53 Agraph_t *g, *prev = NULL;
54 GVC_t *gvc;
55
56 gvc = gvContext();
57 gvParseArgs(gvc, argc, argv);
58
59 while ((g = gvNextInputGraph(gvc))) {
60 if (prev) {
61 gvFreeLayout(gvc, prev);
62 agclose(prev);
63 }
64 gvLayoutJobs(gvc, g);
65 gvRenderJobs(gvc, g);
66 prev = g;
67 }
68 return (gvFreeContext(gvc));
69 }
70
71 \end{verbatim}
72 \newpage
73
74 \section{A sample program: {\tt demo.c}}
75 \label{sec:demo}
76 This example provides a modification of the previous example. Again it
77 relies on the \gviz\ renderers, but now it creates the graph dynamically
78 rather than reading the graph from a file.
79
80 Note that either the graph or the {\tt argv[]} values have to specify which
81 layout algorithm is used, as explained in Section~\ref{sec:gvc}. Specifically,
82 the input graph must have the {\tt layout} attribute set, or the command line
83 arguments must contain a valid {\tt "-K"} flag.
84 If not, {\tt gvParseArgs} will look at the base name part of {\tt argv[0]} and
85 use that as the name of desired layout program. For this to work, the executable
86 program needs to be renamed as one of the \gviz\ layout programs (cf. Section~\ref{sec:intro}).
87 \begin{verbatim}
88 #include <gvc.h>
89
90 int main(int argc, char **argv)
91 {
92 Agraph_t *g;
93 Agnode_t *n, *m;
94 Agedge_t *e;
95 Agsym_t *a;
96 GVC_t *gvc;
97
98 /* set up a graphviz context */
99 gvc = gvContext();
100
101 /* parse command line args - minimally argv[0] sets layout engine */
102 gvParseArgs(gvc, argc, argv);
103
104 /* Create a simple digraph */
105 g = agopen("g", Agdirected);
106 n = agnode(g, "n", 1);
107 m = agnode(g, "m", 1);
108 e = agedge(g, n, m, 0, 1);
109
110 /* Set an attribute - in this case one that affects the visible rendering */
111 agsafeset(n, "color", "red", "");
112
113 /* Compute a layout using layout engine from command line args */
114 gvLayoutJobs(gvc, g);
115
116 /* Write the graph according to -T and -o options */
117 gvRenderJobs(gvc, g);
118
119 /* Free layout data */
120 gvFreeLayout(gvc, g);
121
122 /* Free graph structures */
123 agclose(g);
124
125 /* close output file, free context, and return number of errors */
126 return (gvFreeContext(gvc));
127 }
128 \end{verbatim}