-
Notifications
You must be signed in to change notification settings - Fork 2
/
dot_builder.c
74 lines (57 loc) · 1.34 KB
/
dot_builder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "dot_builder.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PRETTY_PRINT
#define _(str) dbuffer_pushStr(&graph->dbuffer, (str))
#ifdef PRETTY_PRINT
#define _p(str) _(str)
#else
#define _p(str)
#endif
void graph_init(struct Graph *graph, char *name, int isDirected) {
graph->isDirected = isDirected;
dbuffer_init(&graph->dbuffer);
if (isDirected)
_("digraph ");
else
_("graph ");
_(name);
_(" {");
_p("\n");
}
void graph_addEdge(struct Graph *graph, char *aName, char *bName) {
graph_addEdgeVProp(graph, aName, bName, NULL);
}
void graph_addEdgeVProp(struct Graph *graph, char *aName, char *bName,
char *edgeProps) {
_p("\t");
_(aName);
_(" -> ");
_(bName);
if (edgeProps != NULL) {
_("[");
_(edgeProps);
_("]");
}
_(";");
_p("\n");
}
void graph_setNodeProps(struct Graph *graph, char *nodeName, char *props) {
_(nodeName);
_("[");
_(props);
_("]");
_p("\n");
}
char *graph_finalize(struct Graph *graph) {
_("}");
dbuffer_pushChar(&graph->dbuffer, 0); // null terminate.
return (char *)graph->dbuffer.buffer;
}
void getNodeId(void *node, char *result) {
snprintf(result, MAX_NODE_ID, "node_%x", (unsigned long long)node);
}
#undef _
#undef _p