Graphviz 12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
gc.c
Go to the documentation of this file.
1
6/*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16
17/*
18 * Written by Emden Gansner
19 */
20
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <cgraph/cgraph.h>
27#include <cgraph/cghdr.h>
28#include <cgraph/exit.h>
29#include <cgraph/ingraphs.h>
30#include <cgraph/stack.h>
31#include <common/render.h>
32#include <common/utils.h>
33
34typedef struct {
35 Agrec_t h;
38
39#define ND_dfs_mark(n) (((nodeinfo_t*)(n->base.data))->dfs_mark)
40
41#include <getopt.h>
42
43#define NODES 1
44#define EDGES 2
45#define CC 4
46#define CL 8
47
48#define DIRECTED 1
49#define UNDIRECTED 2
50
51static int tot_edges;
52static int tot_nodes;
53static int tot_cc;
54static int tot_cl;
55static int n_graphs;
56static int n_indent;
57static int recurse;
58static int silent;
59static int verbose;
60static int gtype;
61static int flags;
62static char *fname;
63static char **Inputs;
64static FILE *outfile;
65
66static char *useString = "Usage: gc [-necCaDUrsv?] <files>\n\
67 -n - print number of nodes\n\
68 -e - print number of edges\n\
69 -c - print number of connected components\n\
70 -C - print number of clusters\n\
71 -a - print all counts\n\
72 -D - only directed graphs\n\
73 -U - only undirected graphs\n\
74 -r - recursively analyze subgraphs\n\
75 -s - silent\n\
76 -v - verbose\n\
77 -? - print usage\n\
78By default, gc prints nodes and edges\n\
79If no files are specified, stdin is used\n";
80
81static void usage(int v)
82{
83 printf("%s",useString);
85}
86
87static void init(int argc, char *argv[])
88{
89 int c;
90
91 opterr = 0;
92 while ((c = getopt(argc, argv, "necCaDUrsv?")) != -1) {
93 switch (c) {
94 case 'e':
95 flags |= EDGES;
96 break;
97 case 'n':
98 flags |= NODES;
99 break;
100 case 'c':
101 flags |= CC;
102 break;
103 case 'C':
104 flags |= CL;
105 tot_cl = 0;
106 break;
107 case 'a':
108 flags = NODES | EDGES | CC | CL;
109 break;
110 case 'r':
111 recurse = 1;
112 break;
113 case 's':
114 silent = 1;
115 break;
116 case 'v':
117 verbose = 1;
118 break;
119 case 'D':
120 gtype = DIRECTED;
121 break;
122 case 'U':
124 break;
125 case '?':
126 if (optopt == '\0' || optopt == '?')
127 usage(0);
128 else {
129 fprintf(stderr, "gc: option -%c unrecognized\n",
130 optopt);
131 usage(1);
132 }
133 break;
134 default:
135 fprintf(stderr, "gc: unexpected error\n");
136 graphviz_exit(EXIT_FAILURE);
137 }
138 }
139 argv += optind;
140 argc -= optind;
141
142 if (argc)
143 Inputs = argv;
144 if (flags == 0)
145 flags = NODES | EDGES;
146 if (gtype == 0)
148 outfile = stdout;
149}
150
151static gv_stack_t Stk;
152
153static void push(Agnode_t * np)
154{
155 ND_dfs_mark(np) = -1;
156 stack_push(&Stk, np);
157}
158
159static Agnode_t *pop(void)
160{
161 if (stack_is_empty(&Stk)) {
162 return 0;
163 }
164 return stack_pop(&Stk);
165}
166
167static void cc_dfs(Agraph_t * g, Agnode_t * n)
168{
169 Agedge_t *e;
170 Agnode_t *nxt;
171
172 push(n);
173 while ((n = pop())) {
174 ND_dfs_mark(n) = 1;
175 for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
176 if (n == agtail(e))
177 nxt = aghead(e);
178 else
179 nxt = agtail(e);
180 if (ND_dfs_mark(nxt) == 0)
181 push(nxt);
182 }
183 }
184}
185
186static void cntCluster(Agraph_t * g, Agobj_t * sg, void *arg)
187{
188 (void)g;
189
190 if (AGTYPE(sg) == AGRAPH && is_a_cluster((Agraph_t *)sg))
191 *(int *) (arg) += 1;
192}
193
194static int cc_decompose(Agraph_t * g)
195{
196 int c_cnt = 0;
197 Agnode_t *n;
198
199 c_cnt = 0;
200 for (n = agfstnode(g); n; n = agnxtnode(g, n))
201 ND_dfs_mark(n) = 0;
202 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
203 if (ND_dfs_mark(n))
204 continue;
205 c_cnt++;
206 cc_dfs(g, n);
207 }
208
209 return c_cnt;
210}
211
212static void ipr(long num)
213{
214 printf(" %7ld", num);
215}
216
217static void
218wcp(int nnodes, int nedges, int ncc, int ncl, char *gname, char *filename)
219{
220 int i;
221
222 if (silent)
223 return;
224 for (i = 0; i < n_indent; i++)
225 fputs(" ", outfile);
226 if (flags & NODES)
227 ipr(nnodes);
228 if (flags & EDGES)
229 ipr(nedges);
230 if (flags & CC)
231 ipr(ncc);
232 if (flags & CL)
233 ipr(ncl);
234 if (fname)
235 printf(" %s (%s)\n", gname, filename);
236 else
237 printf(" %s\n", gname);
238}
239
240static void emit(Agraph_t * g, int root, int cl_count)
241{
242 int n_edges = agnedges(g);
243 int n_nodes = agnnodes(g);
244 int n_cc = 0;
245 int n_cl = 0;
246 char *file = 0;
247
248 if (flags & CC)
249 n_cc = cc_decompose(g);
250
251 if (flags & CL)
252 n_cl = cl_count;
253
254 if (root)
255 file = fname;
256 wcp(n_nodes, n_edges, n_cc, n_cl, agnameof(g), file);
257
258 if (root) {
259 n_graphs++;
260 tot_edges += n_edges;
261 tot_nodes += n_nodes;
262 tot_cc += n_cc;
263 tot_cl += n_cl;
264 }
265}
266
267#define GTYPE(g) (agisdirected(g)?DIRECTED:UNDIRECTED)
268
269static int eval(Agraph_t * g, int root)
270{
271 Agraph_t *subg;
272 int cl_count = 0;
273
274 if (root && !(GTYPE(g) & gtype))
275 return 1;
276
277 if (root) {
278 aginit(g, AGNODE, "nodeinfo", sizeof(nodeinfo_t), true);
279 }
280
281 if ((flags & CL) && root)
282 agapply(g, (Agobj_t *) g, cntCluster, &cl_count, false);
283
284 emit(g, root, cl_count);
285
286 if (recurse) {
287 n_indent++;
288 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg))
289 eval(subg, 0);
290 n_indent--;
291 }
292 return 0;
293}
294
295int main(int argc, char *argv[])
296{
297 Agraph_t *g;
298 Agraph_t *prev = NULL;
299 ingraph_state ig;
300 int rv = 0;
301
302 init(argc, argv);
303 newIngraph(&ig, Inputs);
304
305 while ((g = nextGraph(&ig)) != 0) {
306 if (prev)
307 agclose(prev);
308 prev = g;
309 fname = fileName(&ig);
310 if (verbose)
311 fprintf(stderr, "Process graph %s in file %s\n", agnameof(g),
312 fname);
313 rv |= eval(g, 1);
314 }
315
316 if (n_graphs > 1)
317 wcp(tot_nodes, tot_edges, tot_cc, tot_cl, "total", 0);
318
320
321 graphviz_exit(rv);
322}
int agapply(Agraph_t *g, Agobj_t *obj, agobjfn_t fn, void *arg, int preorder)
Definition apply.c:60
cgraph.h additions
abstract graph C library, Cgraph API
bool is_a_cluster(Agraph_t *g)
Definition utils.c:691
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
static void init(int argc, char *argv[])
Definition gc.c:87
static char ** Inputs
Definition gc.c:63
static int verbose
Definition gc.c:59
static void ipr(long num)
Definition gc.c:212
static int tot_nodes
Definition gc.c:52
static int tot_cc
Definition gc.c:53
static int recurse
Definition gc.c:57
static int tot_cl
Definition gc.c:54
static int cc_decompose(Agraph_t *g)
Definition gc.c:194
#define EDGES
Definition gc.c:44
static void wcp(int nnodes, int nedges, int ncc, int ncl, char *gname, char *filename)
Definition gc.c:218
static int n_indent
Definition gc.c:56
static int silent
Definition gc.c:58
static Agnode_t * pop(void)
Definition gc.c:159
#define UNDIRECTED
Definition gc.c:49
#define CC
Definition gc.c:45
static void cntCluster(Agraph_t *g, Agobj_t *sg, void *arg)
Definition gc.c:186
#define DIRECTED
Definition gc.c:48
#define CL
Definition gc.c:46
static int gtype
Definition gc.c:60
static gv_stack_t Stk
Definition gc.c:151
static int eval(Agraph_t *g, int root)
Definition gc.c:269
#define ND_dfs_mark(n)
Definition gc.c:39
static void emit(Agraph_t *g, int root, int cl_count)
Definition gc.c:240
static int n_graphs
Definition gc.c:55
static int tot_edges
Definition gc.c:51
#define NODES
Definition gc.c:43
#define GTYPE(g)
Definition gc.c:267
static char * useString
Definition gc.c:66
static int flags
Definition gc.c:61
static void cc_dfs(Agraph_t *g, Agnode_t *n)
Definition gc.c:167
static FILE * outfile
Definition gc.c:64
static char * fname
Definition gc.c:62
static void push(Agnode_t *np)
Definition gc.c:153
static char * gname
Definition gml2gv.c:23
node NULL
Definition grammar.y:149
int agnedges(Agraph_t *g)
Definition graph.c:164
int agnnodes(Agraph_t *g)
Definition graph.c:158
#define agtail(e)
Definition cgraph.h:889
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:93
#define aghead(e)
Definition cgraph.h:890
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:84
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:96
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
#define AGTYPE(obj)
returns AGRAPH, AGNODE, or AGEDGE depending on the type of the object
Definition cgraph.h:216
@ AGNODE
Definition cgraph.h:207
@ AGRAPH
Definition cgraph.h:207
void aginit(Agraph_t *g, int kind, const char *rec_name, int rec_size, int move_to_front)
attach new records to objects of specified kind
Definition rec.c:169
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:77
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:82
static const char * usage
Definition gvpr.c:53
$2 u p prev
Definition htmlparse.y:495
char * fileName(ingraph_state *sp)
Return name of current file being processed.
Definition ingraphs.c:156
Agraph_t * nextGraph(ingraph_state *sp)
Definition ingraphs.c:61
ingraph_state * newIngraph(ingraph_state *sp, char **files)
Definition ingraphs.c:140
supports user-supplied data
static int nedges
total no. of edges used in routing
Definition routespl.c:31
Implementation of a dynamically expanding stack data structure.
static void stack_push(gv_stack_t *stack, void *item)
Definition stack.h:21
static void * stack_pop(gv_stack_t *stack)
Definition stack.h:33
static void stack_reset(gv_stack_t *stack)
Definition stack.h:35
static bool stack_is_empty(const gv_stack_t *stack)
Definition stack.h:17
a generic header of Agraph_s, Agnode_s and Agedge_s
Definition cgraph.h:210
graph or subgraph
Definition cgraph.h:425
implementation of Agrec_t
Definition cgraph.h:172
int dfs_mark
Definition gc.c:36
int main()