Debian Codegraph @

lib/cgraph/cgraph.3

raw
1 .de P0
2 .nf
3 \f5
4 ..
5 .de P1
6 \fP
7 .fi
8 ..
9 .de Ss
10 .fl
11 .ne 2
12 .SS "\\$1"
13 ..
14 .TH LIBCGRAPH 3 "28 FEBRUARY 2013"
15 .SH "NAME"
16 \fBlibcgraph\fR \- abstract graph library
17 .SH "SYNOPSIS"
18 .\"ta .75i 1.5i 2.25i 3i 3.75i 4.5i 5.25i 6i
19 .PP
20 .nf
21 .P0
22 #include <graphviz/cgraph.h>
23 .P1
24 .SS "TYPES"
25 .P0
26 Agraph_t;
27 Agnode_t;
28 Agedge_t;
29 Agdesc_t;
30 Agdisc_t;
31 Agsym_t;
32 Agrec_t;
33 Agcbdisc_t;
34 .P1
35 .SS "GLOBALS"
36 .P0
37 Agiddisc_t AgIdDisc;
38 Agiodisc_t AgIoDisc;
39 Agdisc_t AgDefaultDisc;
40 .P1
41 .SS "GRAPHS"
42 .P0
43 Agraph_t *agopen(char *name, Agdesc_t kind, Agdisc_t *disc);
44 int agclose(Agraph_t *g);
45 Agraph_t *agread(void *channel, Agdisc_t *);
46 Agraph_t *agmemread(char *);
47 Agraph_t *agconcat(Agraph_t *g, const char *filename, void *channel, Agdisc_t *disc);
48 int agwrite(Agraph_t *g, void *channel);
49 int agnnodes(Agraph_t *g),agnedges(Agraph_t *g), agnsubg(Agraph_t * g);
50 int agisdirected(Agraph_t * g),agisundirected(Agraph_t * g),agisstrict(Agraph_t * g), agissimple(Agraph_t * g);
51 bool graphviz_acyclic(Agraph_t *g, const graphviz_acyclic_options_t *opts, size_t *num_rev);
52 void graphviz_tred(Agraph_t *g, const graphviz_tred_options_t *opts);
53 void graphviz_unflatten(Agraph_t *g, const graphviz_unflatten_options_t *opts);
54 .SS "SUBGRAPHS"
55 .P0
56 Agraph_t *agsubg(Agraph_t *g, char *name, int createflag);
57 Agraph_t *agidsubg(Agraph_t * g, unsigned long id, int cflag);
58 Agraph_t *agfstsubg(Agraph_t *g), agnxtsubg(Agraph_t *);
59 Agraph_t *agparent(Agraph_t *g);
60 int agdelsubg(Agraph_t * g, Agraph_t * sub); /* same as agclose() */
61 .P1
62 .SS "NODES"
63 .P0
64 Agnode_t *agnode(Agraph_t *g, char *name, int createflag);
65 Agnode_t *agidnode(Agraph_t *g, ulong id, int createflag);
66 Agnode_t *agsubnode(Agraph_t *g, Agnode_t *n, int createflag);
67 Agnode_t *agfstnode(Agraph_t *g);
68 Agnode_t *agnxtnode(Agraph_t *g, Agnode_t *n);
69 Agnode_t *agprvnode(Agraph_t *g, Agnode_t *n);
70 Agnode_t *aglstnode(Agraph_t *g);
71 int agdelnode(Agraph_t *g, Agnode_t *n);
72 int agdegree(Agraph_t *g, Agnode_t *n, int use_inedges, int use_outedges);
73 int agcountuniqedges(Agraph_t * g, Agnode_t * n, int in, int out);
74 .P1
75 .SS "EDGES"
76 .P0
77 Agedge_t *agedge(Agraph_t* g, Agnode_t *t, Agnode_t *h, char *name, int createflag);
78 Agedge_t *agidedge(Agraph_t * g, Agnode_t * t, Agnode_t * h, unsigned long id, int createflag);
79 Agedge_t *agsubedge(Agraph_t *g, Agedge_t *e, int createflag);
80 Agnode_t *aghead(Agedge_t *e), *agtail(Agedge_t *e);
81 Agedge_t *agfstedge(Agraph_t* g, Agnode_t *n);
82 Agedge_t *agnxtedge(Agraph_t* g, Agedge_t *e, Agnode_t *n);
83 Agedge_t *agfstin(Agraph_t* g, Agnode_t *n);
84 Agedge_t *agnxtin(Agraph_t* g, Agedge_t *e);
85 Agedge_t *agfstout(Agraph_t* g, Agnode_t *n);
86 Agedge_t *agnxtout(Agraph_t* g, Agedge_t *e);
87 int agdeledge(Agraph_t *g, Agedge_t *e);
88 Agedge_t *agopp(Agedge_t *e);
89 int ageqedge(Agedge_t *e0, Agedge_t *e1);
90 .SS "STRING ATTRIBUTES"
91 .P0
92 Agsym_t *agattr_text(Agraph_t *g, int kind, char *name, const char *value);
93 Agsym_t *agattrsym(void *obj, char *name);
94 Agsym_t *agnxtattr(Agraph_t *g, int kind, Agsym_t *attr);
95 char *agget(void *obj, char *name);
96 char *agxget(void *obj, Agsym_t *sym);
97 int agset(void *obj, char *name, char *value);
98 int agxset(void *obj, Agsym_t *sym, char *value);
99 int agsafeset(void *obj, char *name, char *value, char *def);
100 int agcopyattr(void *, void *);
101 .P1
102 .SS "RECORDS"
103 .P0
104 void *agbindrec(void *obj, char *name, unsigned int size, move_to_front);
105 Agrec_t *aggetrec(void *obj, char *name, int move_to_front);
106 int agdelrec(Agraph_t *g, void *obj, char *name);
107 void aginit(Agraph_t * g, int kind, char *rec_name, int rec_size, int move_to_front);
108 void agclean(Agraph_t * g, int kind, char *rec_name);
109 .P1
110 .SS "CALLBACKS"
111 .P0
112 int *agpopdisc(Agraph_t *g);
113 void agpushdisc(Agraph_t *g, Agcbdisc_t *disc);
114 int agcallbacks(Agraph_t * g, int flag);
115 .P1
116 .SS "STRINGS"
117 .P0
118 char *agstrdup(Agraph_t *, char *);
119 char *agstrdup_html(Agraph_t *, char *);
120 int aghtmlstr(char *);
121 char *agstrbind(Agraph_t * g, char *);
122 int strfree(Agraph_t *, char *);
123 char *agstrcanon(char *, char *);
124 .P1
125 .SS "GENERIC OBJECTS"
126 .P0
127 Agraph_t *agraphof(void*);
128 Agraph_t *agroot(void*);
129 int agcontains(Agraph_t*, void*);
130 char *agnameof(void*);
131 void agdelete(Agraph_t *g, void *obj);
132 int agobjkind(void *obj);
133 Agrec_t *AGDATA(void *obj);
134 ulong AGID(void *obj);
135 int AGTYPE(void *obj);
136 .P1
137 .SS "ERROR REPORTING"
138 .P0
139 typedef enum { AGWARN, AGERR, AGMAX, AGPREV } agerrlevel_t;
140 typedef int (*agusererrf) (char*);
141 agerrlevel_t agerrno;
142 agerrlevel_t agseterr(agerrlevel_t);
143 char *aglasterr(void);
144 int agerr(agerrlevel_t level, char *fmt, ...);
145 void agerrorf(char *fmt, ...);
146 void agwarningf(char *fmt, ...);
147 int agerrors(void);
148 agusererrf agseterrf(agusererrf);
149 .P1
150 .SH "DESCRIPTION"
151 Libcgraph supports graph programming by maintaining graphs in memory
152 and reading and writing graph files.
153 Graphs are composed of nodes, edges, and nested subgraphs.
154 These graph objects may be attributed with string name-value pairs
155 and programmer-defined records (see Attributes).
156 .PP
157 All of Libcgraph's global symbols have the prefix \fBag\fR (case varying).
158 In the following, if a function has a parameter \fBint createflag\fP and the
159 object does not exist, the function
160 will create the specified object if \fBcreateflag\fP is non-zero; otherwise,
161 it will return NULL.
162 .SH "GRAPH AND SUBGRAPHS"
163 .PP
164 A ``main'' or ``root'' graph defines a namespace for a collection of
165 graph objects (subgraphs, nodes, edges) and their attributes.
166 Objects may be named by unique strings or by integer IDs.
167 .PP
168 \fBagopen\fP creates a new graph with the given name and kind.
169 (Graph kinds are \fBAgdirected\fP, \fBAgundirected\fP,
170 \fBAgstrictdirected\fP, and \fBAgstrictundirected\fP.
171 A strict graph cannot have multi-edges or self-arcs.)
172 The final argument points to a discpline structure which can be used
173 to tailor I/O and ID allocation. Typically, a NULL
174 value will be used to indicate the default discipline \fBAgDefaultDisc\fP.
175 \fBagclose\fP deletes a graph, freeing its associated storage.
176 \fBagread\fP, \fBagwrite\fP, and \fBagconcat\fP perform file I/O
177 using the graph file language described below. \fBagread\fP
178 constructs a new graph while \fBagconcat\fP merges the file
179 contents with a pre-existing graph. Though I/O methods may
180 be overridden, the default is that the channel argument is
181 a stdio FILE pointer. In that case, if any of the streams are
182 wide-oriented, the behavior is undefined.
183 \fBagmemread\fP attempts to read a graph from the input string.
184 .PP
185 The functions \fBagisdirected\fP, \fBagisundirected\fP, \fBagisstrict\fP, and \fBagissimple\fP
186 can be used to query if a graph is directed, undirected, strict (at most one edge with a given tail
187 and head), or simple (strict with no loops), respectively,
188 .PP
189 \fBagsubg\fP finds or creates
190 a subgraph by name.
191 \fBagidsubg\fP allows a programmer to specify the subgraph
192 by a unique integer ID.
193 A new subgraph is initially empty and
194 is of the same kind as its parent. Nested subgraph trees may be created.
195 A subgraph's name is only interpreted relative to its parent.
196 A program can scan subgraphs under a given graph
197 using \fBagfstsubg\fP and \fBagnxtsubg\fP. A subgraph is
198 deleted with \fBagdelsubg\fP (or \fBagclose\fP).
199 The \fBagparent\fP function returns the immediate parent graph of a subgraph, or itself if the
200 graph is already a root graph.
201 .PP
202 By default, nodes are stored in ordered sets for efficient random
203 access to insert, find, and delete nodes.
204 The edges of a node are also stored in ordered sets.
205 The sets are maintained internally as splay tree dictionaries
206 using Phong Vo's cdt library.
207 .PP
208 \fBagnnodes\fP, \fBagnedges\fP, and \fBagnsubg\fP return the
209 sizes of node, edge and subgraph sets of a graph.
210 The function \fBagdegree\fP returns
211 the size of the edge set of a nodes, and takes flags
212 to select in-edges, out-edges, or both.
213 The function \fBagcountuniqedges\fP returns
214 the size of the edge set of a nodes, and takes flags
215 to select in-edges, out-edges, or both. Unlike \fBagdegree\fP, each loop is only
216 counted once.
217 .SH "NODES"
218 A node is created by giving a unique string name or
219 programmer defined integer ID, and is represented by a
220 unique internal object. (Node equality can checked
221 by pointer comparison.)
222 .PP
223 \fBagnode\fP searches in a graph or subgraph for a node
224 with the given name, and returns it if found.
225 \fBagidnode\fP allows a programmer to specify the node
226 by a unique integer ID.
227 \fBagsubnode\fP performs a similar operation on
228 an existing node and a subgraph.
229 .PP
230 \fBagfstnode\fP and \fBagnxtnode\fP scan node lists.
231 \fBagprvnode\fP and \fPaglstnode\fP are symmetric but scan backward.
232 The default sequence is order of creation (object timestamp.)
233 \fBagdelnode\fP removes a node from a graph or subgraph.
234 .SH "EDGES"
235 .PP
236 An abstract edge has two endpoint nodes called tail and head
237 where all outedges of the same node have it as the tail
238 value and similarly all inedges have it as the head.
239 In an undirected graph, head and tail are interchangeable.
240 If a graph has multi-edges between the same pair of nodes,
241 the edge's string name behaves as a secondary key.
242 .PP
243 \fBagedge\fP searches in a graph or subgraph for an
244 edge between the given endpoints (with an optional
245 multi-edge selector name) and returns it if found or created.
246 Note that, in undirected graphs, a search tries both orderings of
247 the tail and head nodes.
248 If the \fBname\fP
249 is NULL, then an anonymous internal
250 value is generated. \fBagidedge\fP allows a programmer
251 to create an edge by giving its unique integer ID.
252 \fBagsubedge\fP performs a similar operation on
253 an existing edge and a subgraph.
254 \fBagfstin\fP, \fBagnxtin\fP, \fBagfstout\fP, and
255 \fBagnxtout\fP visit directed in- and out- edge lists,
256 and ordinarily apply only in directed graphs.
257 \fBagfstedge\fP and \fBagnxtedge\fP visit all edges
258 incident to a node. \fBagtail\fP and \fBaghead\fP
259 get the endpoint of an edge.
260 \fBagdeledge\fP removes an edge from a graph or subgraph.
261 .PP
262 Note that an abstract edge has two distinct concrete representations:
263 as an in-edge and as an out-edge. In particular, the pointer as an out-edge
264 is different from the pointer as an in-edge. The function \fBageqedge\fP
265 canonicalizes the pointers before doing a comparison and so can be used to
266 test edge equality. The sense of an edge can be flipped using \fBagopp\fP.
267 .SH "INTERNAL ATTRIBUTES"
268 Programmer-defined values may be dynamically
269 attached to graphs, subgraphs, nodes, and edges.
270 Such values are either character string data (for I/O)
271 or uninterpreted binary records
272 (for implementing algorithms efficiently).
273 .SH "STRING ATTRIBUTES"
274 String attributes are handled automatically in reading
275 and writing graph files.
276 A string attribute is identified by name and by
277 an internal symbol table entry (\fBAgsym_t\fP) created by Libcgraph.
278 Attributes of nodes, edges, and graphs (with their subgraphs)
279 have separate namespaces. The contents of an \fBAgsym_t\fP
280 have a \fBchar* name\fP for the attribute's name, a \fBchar* defval\fP
281 field for the attribute's default value, and an \fBint id\fP field containing
282 the index of the attribute's specific value for an object in the object's array
283 of attribute values.
284 .PP
285 \fBagattr\fP creates or looks up attributes.
286 \fBkind\fP may be \fBAGRAPH\fP, \fBAGNODE\fP, or \fBAGEDGE\fP.
287 If \fBvalue\fP is \fB(char*)0)\fP, the request is to search
288 for an existing attribute of the given kind and name.
289 Otherwise, if the attribute already exists, its default
290 for creating new objects is set to the given value;
291 if it does not exist, a new attribute is created with the
292 given default, and the default is applied to all pre-existing
293 objects of the given kind. If \fBg\fP is NULL, the default is
294 set for all graphs created subsequently.
295 \fBagattrsym\fP is a helper function
296 that looks up an attribute for a graph object given as an argument.
297 \fBagnxtattr\fP permits traversing the list of attributes of
298 a given type. If \fBNULL\fP is passed as an argument it gets
299 the first attribute; otherwise it returns the next one in
300 succession or returns \fBNULL\fP at the end of the list.
301 \fBagget\fP and \fPagset\fP allow fetching and updating a
302 string attribute for an object taking the attribute name as
303 an argument.
304 \fBagxget\fP and \fBagxset\fP do this but with
305 an attribute symbol table entry as an argument (to avoid
306 the cost of the string lookup).
307 Note that \fPagset\fP will fail unless the attribute is
308 first defined using \fBagattr\fP.
309 \fBagsafeset\fP is a
310 convenience function that ensures the given attribute is
311 declared before setting it locally on an object.
312 .PP
313 It is sometimes convenient to copy all of the attributes from one
314 object to another. This can be done using \fBagcopyattr\fP. This
315 fails and returns non-zero of argument objects are different kinds,
316 or if all of the attributes of the source object have not been declared
317 for the target object.
318 .SH "STRINGS"
319 Libcgraph performs its own storage management of strings as
320 reference-counted strings.
321 The caller does not need to dynamically allocate storage.
322 .PP
323 \fBagstrdup\fP returns a pointer to a reference-counted copy of
324 the argument string, creating one if necessary. \fBagstrbind\fP
325 returns a pointer to a reference-counted string if it exists, or NULL if not.
326 All uses of cgraph strings need to be freed using \fBagstrfree\fP
327 in order to correctly maintain the reference count.
328 .PP
329 The cgraph parser handles HTML-like strings. These should be
330 indistinguishable from other strings for most purposes. To create
331 an HTML-like string, use \fBagstrdup_html\fP. The \fBaghtmlstr\fP
332 function can be used to query if a string is an ordinary string or
333 an HTML-like string.
334 .PP
335 \fBagstrcanon\fP returns a pointer to a version of the input string
336 canonicalized for output for later re-parsing. This includes quoting
337 special characters and keywords. The application passes in a buffer as the
338 second argument. Note that the buffer may not be used; if the input string is in
339 canonical form, the function will just return a pointer to it.
340 The input string must have been created using
341 \fBagstrdup\fP or \fBagstrdup_html\fP.
342 .SH "RECORDS"
343 Uninterpreted records may be attached to graphs, subgraphs, nodes,
344 and edges for efficient operations on values such as marks, weights,
345 counts, and pointers needed by algorithms. Application programmers
346 define the fields of these records, but they must be declared with
347 a common header as shown below.
348 .PP
349 .P0
350 typedef struct {
351 Agrec_t header;
352 /* programmer-defined fields follow */
353 } user_data_t;
354 .P1
355 .PP
356 Records are created and managed by Libcgraph. A programmer must
357 explicitly attach them to the objects in a graph, either to
358 individual objects one at a time via \fBagbindrec\fP, or to
359 all the objects of the same class in a graph via \fBaginit\fP.
360 (Note that for graphs, aginit is applied recursively to the
361 graph and its subgraphs if rec_size is negative (of the
362 actual rec_size.))
363 The \fBname\fP argument of a record distinguishes various types of records,
364 and is programmer defined (Libcgraph reserves the prefix \fB_ag\fR).
365 If size is 0, the call to \fBagbindrec\fP is simply a lookup.
366 The function \fBaggetrec\fP can also be used for lookup.
367 \fBagdelrec\fP deletes a named record from one object.
368 \fBagclean\fP does the same for all objects of the same
369 class in an entire graph.
370
371 Internally, records are maintained in circular linked lists
372 attached to graph objects.
373 To allow referencing application-dependent data without function
374 calls or search, Libcgraph allows setting and locking the list
375 pointer of a graph, node, or edge on a particular record.
376 This pointer can be obtained with the macro \fBAGDATA(obj)\fP.
377 A cast, generally within a macro or inline function,
378 is usually applied to convert the list pointer to
379 an appropriate programmer-defined type.
380
381 To control the setting of this pointer,
382 the \fBmove_to_front\fP flag may be \fBTRUE\fP
383 or \fBFALSE\fP.
384 If \fBmove_to_front\fP is \fBTRUE\fP, the record will be locked at the
385 head of the list, so it can be accessed directly by \fBAGDATA(obj)\fP.
386 The lock can be subsequently released or reset by a call to \fBaggetrec\fP.
387
388 .SH "DISCIPLINES"
389 (This section is not intended for casual users.)
390 Programmer-defined disciplines customize certain resources-
391 ID namespace and I/O - needed by Libcgraph.
392 A discipline struct (or NULL) is passed at graph creation time.
393 .PP
394 .P0
395 struct Agdisc_s { /* user's discipline */
396 Agiddisc_t *id;
397 Agiodisc_t *io;
398 } ;
399 .P1
400 .PP
401 A default discipline is supplied when NULL is given for
402 any of these fields.
403
404 .SH "ID DISCIPLINE"
405 An ID allocator discipline allows a client to control assignment
406 of IDs (uninterpreted integer values) to objects, and possibly how
407 they are mapped to and from strings.
408
409 .P0
410 struct Agiddisc_s { /* object ID allocator */
411 void *(*open) (Agraph_t * g, Agdisc_t*); /* associated with a graph */
412 long (*map) (void *state, int objtype, char *str, unsigned long *id, int createflag);
413 long (*alloc) (void *state, int objtype, unsigned long id);
414 void (*free) (void *state, int objtype, unsigned long id);
415 char *(*print) (void *state, int objtype, unsigned long id);
416 void (*close) (void *state);
417 };
418 .P1
419 .PP
420 \fIopen\fP permits the ID discipline to initialize any data
421 structures that it maintains per individual graph.
422 Its return value is then passed as the first argument (void *state) to
423 all subsequent ID manager calls.
424 .PP
425 \fIalloc\fP informs the ID manager that Libcgraph is attempting
426 to create an object with a specific ID that was given by a client.
427 The ID manager should return TRUE (nonzero) if the ID can be
428 allocated, or FALSE (which aborts the operation).
429 .PP
430 \fIfree\fP is called to inform the ID manager that the
431 object labeled with the given ID is about to go out of existence.
432 .PP
433 \fImap\fP is called to create or look-up IDs by string name
434 (if supported by the ID manager). Returning TRUE (nonzero)
435 in all cases means that the request succeeded (with a valid
436 ID stored through \fIresult\fP. There are four cases:
437 .RS
438 .IP \[bu] 2
439 \fIname != NULL\fP and \fIcreateflag == 1\fP:
440 This requests mapping a string (e.g. a name in a graph file) into a new ID.
441 If the ID manager can comply, then it stores the result and returns TRUE.
442 It is then also responsible for being able to \fIprint\fP the ID again
443 as a string. Otherwise the ID manager may return FALSE but it must
444 implement the following (at least for graph file reading and writing to work):
445 .IP \[bu]
446 \fIname == NULL\fP and \fIcreateflag == 1\fP:
447 The ID manager creates a unique new ID of its own choosing.
448 Although it may return FALSE if it does not support anonymous objects,
449 but this is strongly discouraged (to support "local names" in graph files.)
450 .IP \[bu]
451 \fIname != NULL\fP and \fIcreateflag == 0\fP:
452 This is a namespace probe. If the name was previously mapped into
453 an allocated ID by the ID manager, then the manager must return this ID.
454 Otherwise, the ID manager may either return FALSE, or may store
455 any unallocated ID into result. (This is convenient, for example,
456 if names are known to be digit strings that are directly converted into integer values.)
457 .IP \[bu]
458 \fIname == NULL\fP and \fIcreateflag == 0\fP: forbidden.
459 .RE
460 .PP
461 \fIprint\fP is allowed to return a pointer to a static buffer;
462 a caller must copy its value if needed past subsequent calls.
463 \fINULL\fP should be returned by ID managers that do not map names.
464 .PP
465 The \fImap\fP and \fIalloc\fP calls do not pass a pointer to the
466 newly allocated object. If a client needs to install object
467 pointers in a handle table, it can obtain them via
468 new object callbacks.
469 .SH "IO DISCIPLINE"
470 .PP
471 The I/O discipline provides an abstraction for the reading and writing of graphs.
472 .P0
473 struct Agiodisc_s {
474 int (*fread)(void *chan, char *buf, int bufsize);
475 int (*putstr)(void *chan, char *str);
476 int (*flush)(void *chan); /* sync */
477 } ;
478 .P1
479 .PP
480 Normally, the \fBFILE\fP structure and its related functions are used for I/O. At times, though,
481 an application may need to use a totally different type of character source. The associated
482 state or stream information is provided by the \fIchan\fP argument to \fBagread\fP or \fBagwrite\fP.
483 The discipline function \fIfread\fP and \fIputstr\fP provide the corresponding functions for
484 read and writing.
485
486 .SH "CALLBACKS"
487 .PP
488 An \fBAgcbdisc_t\fP defines callbacks to be invoked by Libcgraph when
489 initializing, modifying, or finalizing graph objects.
490 Disciplines are kept on a stack. Libcgraph automatically
491 calls the methods on the stack, top-down. Callbacks are installed
492 with \fBagpushdisc\fP, uninstalled with \fBagpopdisc\fP, and
493 can be held pending or released via \fBagcallbacks\fP.
494 .SH "GENERIC OBJECTS"
495 \fBagroot\fP takes any graph object (graph, subgraph, node, edge) and returns
496 the root graph in which it lives. \fBagraphof\fP does the same, except it
497 is the identity function on graphs and subgraphs. Note that there is no
498 function to return the least subgraph containing an object, in part because
499 this is not well-defined as nodes and edges may be in incomparable subgraphs.
500 .PP
501 \fBagcontains\fP(\fIg\fP,\fIobj\fP) returns non-zero if \fIobj\fP is a member
502 of (sub)graph \fIg\fP. \fBagdelete\fP(\fIg\fP,\fIobj\fP) is equivalent
503 to \fBagclose\fP, \fBagdelnode\fP, and \fBagdeledge\fP for \fIobj\fP being a
504 graph, node or edge, respectively. It returns -1 if \fIobj\fP does not
505 belong to \fIg\fP.
506 .PP
507 \fBAGDATA\fP, \fBAGID\fP, and \fBAGTYPE\fP are macros returning the specified
508 fields of the argument object. The first is described in the \fBRECORDS\fP
509 section above. The second returns the unique integer ID associated with
510 the object. The last returns \fBAGRAPH\fP, \fBAGNODE\fP, and \fBAGEDGE\fP
511 depending on the type of the object.
512 .PP
513 \fBagnameof\fP returns a string descriptor for the object. It returns the name
514 of the node or graph, and the key of an edge.
515 \fBagobjkind\fP is a synonym for \fBAGTYPE\fP.
516
517 .SH "ERROR REPORTING"
518 The library provides a variety of mechanisms to control the reporting
519 of errors and warnings. At present, there are basically two types of
520 messages: warnings and errors. A message is only written if its
521 type has higher priority than a programmer-controlled minimum, which is
522 \fBAGWARN\fP by default. The programmer can set this value using
523 \fBagseterr\fP, which returns the previous value. Calling
524 \fBagseterr(AGMAX)\fP turns off the writing of messages.
525 .PP
526 The function \fBagerr\fP is the main entry point for reporting an
527 anomaly. The first argument indicates the type of message. Usually,
528 the first argument is \fBAGWARN\fP or \fBAGERR\fP to indicate warnings
529 and errors, respectively. Sometimes additional context information is
530 only available in functions calling the function where the error is
531 actually caught. In this case, the calling function can indicate that
532 it is continuing the current error by using \fBAGPREV\fP as the first
533 argument. The remaining arguments to \fBagerr\fP are the same as
534 the arguments to \fBprintf\fP.
535 .PP
536 The functions \fBagwarningf\fP and \fBagerrorf\fP are shorthand for
537 \fBagerr(AGWARN,...)\fP and \fBagerr(AGERR,...)\fP, respectively.
538 .PP
539 Some applications desire to directly control the writing of messages.
540 Such an application can use the function \fBagseterrf\fP to register
541 the function that the library should call to actually write the message.
542 The previous error function is returned. By default, the message is
543 written to \fBstderr\fP.
544 .PP
545 Errors not written are stored in a log file. The last recorded error
546 can be retrieved by calling \fBaglasterr\fP.
547 Unless the printing of error messages has been completely disabled by a call
548 to \fBagseterr(AGMAX)\fP, standard error must not be wide-oriented, even if
549 a user-provided error printing function is provided.
550 .PP
551 The function \fBagerrors\fP returns non-zero if errors have been reported.
552 .SH "EXAMPLE PROGRAM"
553 .P0
554 #include <cgraph.h>
555 #include <stdbool.h>
556 #include <stddef.h>
557 #include <stdio.h>
558
559 typedef struct {
560 Agrec_t hdr;
561 int x;
562 int y;
563 int z;
564 } mydata;
565
566 int main(int argc, char **argv) {
567 Agraph_t *g;
568 mydata *p;
569
570 if ((g = agread(stdin, NULL))) {
571 int cnt = 0;
572 Agsym_t *attr = NULL;
573 while ((attr = agnxtattr(g, AGNODE, attr))) {
574 cnt++;
575 }
576 printf("The graph %s has %d attributes\\n", agnameof(g), cnt);
577
578 // make the graph have a node color attribute, default is blue
579 attr = agattr_text(g, AGNODE, "color", "blue");
580
581 // create a new graph of the same kind as g
582 Agraph_t *h = agopen("tmp", g->desc, NULL);
583
584 // this is a way of counting all the edges of the graph
585 cnt = 0;
586 for (Agnode_t *v = agfstnode(g); v != NULL; v = agnxtnode(g, v)) {
587 for (Agedge_t *e = agfstout(g, v); e != NULL; e = agnxtout(g, e)) {
588 cnt++;
589 }
590 }
591
592 // attach records to edges
593 for (Agnode_t *v = agfstnode(g); v != NULL; v = agnxtnode(g, v)) {
594 for (Agedge_t *e = agfstout(g, v); e != NULL; e = agnxtout(g, e)) {
595 p = (mydata *)agbindrec(e, "mydata", sizeof(mydata), true);
596 p->x = 27; // meaningless data access example
597 ((mydata *)(AGDATA(e)))->y = 999; // another example
598 }
599 }
600 }
601 return 0;
602 }
603
604 .P1
605 .SH "EXAMPLE GRAPH FILES"
606 .P0
607 digraph G {
608 a -> b;
609 c [shape=box];
610 a -> c [weight=29,label="some text"];
611 subgraph anything {
612 /* the following affects only x,y,z */
613 node [shape=circle];
614 a; x; y -> z; y -> z; /* multiple edges */
615 }
616 }
617
618 strict graph H {
619 n0 -- n1 -- n2 -- n0; /* a cycle */
620 n0 -- {a b c d}; /* a star */
621 n0 -- n3;
622 n0 -- n3 [weight=1]; /* same edge because graph is strict */
623 }
624 .P1
625 .SH "SEE ALSO"
626 .BR cdt (3)
627 .br
628
629 .SH "BUGS"
630 It is difficult to change endpoints of edges, delete string attributes or
631 modify edge keys. The work-around is to create a new object and copy the
632 contents of an old one (but new object obviously has a different ID,
633 internal address, and object creation timestamp).
634
635 The API lacks convenient functions to substitute programmer-defined ordering of
636 nodes and edges but in principle this can be supported.
637
638 The library is not thread safe.
639 .SH "AUTHOR"
640 Stephen North, [email protected], AT&T Research.