-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdot-wasm-native.c
83 lines (63 loc) · 1.55 KB
/
dot-wasm-native.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
75
76
77
78
79
80
81
82
83
// Mostly taken from https://github.com/mdaines/viz.js
#include <graphviz/gvc.h>
extern gvplugin_library_t gvplugin_core_LTX_library;
extern gvplugin_library_t gvplugin_dot_layout_LTX_library;
char *errorMessage = NULL;
int vizErrorf(char *buf) {
errorMessage = buf;
return 0;
}
char* vizLastErrorMessage() {
return errorMessage;
}
char* vizRenderFromString(const char *src, const char *format, const char *engine) {
errorMessage = NULL;
GVC_t *context;
Agraph_t *graph;
char *result = NULL;
unsigned int length;
context = gvContext();
gvAddLibrary(context, &gvplugin_core_LTX_library);
gvAddLibrary(context, &gvplugin_dot_layout_LTX_library);
agseterr(AGERR);
agseterrf(vizErrorf);
agreadline(1);
while ((graph = agmemread(src))) {
if (result == NULL) {
gvLayout(context, graph, engine);
gvRenderData(context, graph, format, &result, &length);
gvFreeLayout(context, graph);
}
agclose(graph);
src = "";
}
return result;
}
void freeStringRenderString(char *data) {
gvFreeRenderData(data);
}
void test(char* string){
char *result = vizRenderFromString(string, "svg", "dot");
printf("%lu\n", result ? strlen(result) : 0);
freeStringRenderString(result);
char *lastError = vizLastErrorMessage();
if(lastError) printf("Error: %s\n\n", lastError);
}
int main(){
test("digraph graphname\
{\
a -> b -> c;\
b -> d;\
}");
test("digraph graphname\
{\
a -> b -> c;\
b -> d;\
");
test("digraph graphname\
{\
a -> b -> c;\
b -> d;\
}");
return 0;
}