-
Notifications
You must be signed in to change notification settings - Fork 2
/
DocGraphWriter.m
139 lines (113 loc) · 3.1 KB
/
DocGraphWriter.m
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <unistd.h>
#include "DocGraphWriter.h"
/*
* For writing code that supports both libgraph and libcgraph, see
* http://www.graphviz.org/dot.demo/example.c
*/
@implementation DocGraphWriter
- (id) init
{
self = [super init];
mNodes = [NSMutableDictionary new];
mEdges = [NSMutableArray new];
// NOTE: No need to call aginit() just for libgraph, since we use a context
// to support the graph layout, see gvLayout() in -layout.
mGraphContext = gvContext();
#ifdef WITH_CGRAPH
mGraph = agopen("g", Agdirected, NULL);
#else
mGraph = agopen("g", AGDIGRAPH);
#endif
ETAssert(mGraphContext != NULL);
ETAssert(mGraph != NULL);
[self setGraphAttribute: @"rankdir" with: @"BT"];
[self setGraphAttribute: @"size" with: @"2."];
[self setGraphAttribute: @"dpi" with: @"72"];
return self;
}
- (void) dealloc
{
[self cleanupGraph];
}
- (void) cleanupGraph
{
gvFreeLayout(mGraphContext, mGraph);
agclose(mGraph);
gvFreeContext(mGraphContext);
}
- (void) layout
{
gvLayout(mGraphContext, mGraph, "dot");
}
- (void) generateFile: (NSString*) path withFormat: (NSString*) format
{
gvRenderFilename(mGraphContext, mGraph,
(char*) [format UTF8String], (char*) [path UTF8String]);
}
- (NSString*) generateWithFormat: (NSString*) format
{
NSFileHandle* handle = [[NSFileManager defaultManager] tempFile];
FILE* file = fdopen([handle fileDescriptor], "w+");
gvRender(mGraphContext, mGraph,
(char*) [format UTF8String], file);
[handle seekToFileOffset: 0];
NSData* data = [handle readDataToEndOfFile];
NSString* str = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
return str;
}
- (NSValue*) addNode: (NSString*) node
{
NILARG_EXCEPTION_TEST(node);
ETAssert(mGraph != NULL);
NSValue* pointer = [mNodes objectForKey: node];
if (pointer)
return pointer;
//NSLog(@"Add node %@", node);
#ifdef WITH_CGRAPH
Agnode_t *n = agnode(mGraph, (char*)[node UTF8String], 1);
#else
Agnode_t *n = agnode(mGraph, (char*)[node UTF8String]);
#endif
NSValue* value = [NSValue valueWithPointer: n];
[mNodes setObject: value forKey: node];
return value;
}
- (void) addEdge: (NSString*) nodeA to: (NSString*) nodeB
{
NILARG_EXCEPTION_TEST(nodeA);
NILARG_EXCEPTION_TEST(nodeB);
ETAssert(mGraph != NULL);
//NSLog(@"Add edge from %@ to %@", nodeA, nodeB);
NSValue* A = [self addNode: nodeA];
NSValue* B = [self addNode: nodeB];
#ifdef WITH_CGRAPH
agedge(mGraph, [A pointerValue], [B pointerValue], "", 1);
#else
agedge(mGraph, [A pointerValue], [B pointerValue]);
#endif
}
- (void) setAttribute: (NSString*) attribute
with: (NSString*) value
on: (NSString*) node
{
NILARG_EXCEPTION_TEST(attribute);
NILARG_EXCEPTION_TEST(value);
NILARG_EXCEPTION_TEST(node);
NSValue* n = [self addNode: node];
agsafeset([n pointerValue],
(char*) [attribute UTF8String],
(char*) [value UTF8String], "");
}
- (void) setGraphAttribute: (NSString*) attribute
with: (NSString*) value
{
NILARG_EXCEPTION_TEST(attribute);
NILARG_EXCEPTION_TEST(value);
ETAssert(mGraph != NULL);
agsafeset(mGraph,
(char*) [attribute UTF8String],
(char*) [value UTF8String], "");
}
@end