-
Notifications
You must be signed in to change notification settings - Fork 11
/
graphviz-js-wrapper.c
52 lines (41 loc) · 1.08 KB
/
graphviz-js-wrapper.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
#include <gvc.h>
#include <string.h>
extern gvplugin_library_t gvplugin_dot_layout_LTX_library;
extern gvplugin_library_t gvplugin_core_LTX_library;
// To work around a bug in emscripten
#include <dirent.h>
struct dirent _fake_dirent;
static GVC_t *gvc;
enum {ERRBUF_LEN = 1024};
static char errbuf[ERRBUF_LEN];
static int emusererrf(char *str)
{
strncpy(errbuf, str, ERRBUF_LEN-1);
errbuf[ERRBUF_LEN-1] = 0;
return 0;
}
char *emGvRecentError()
{
return errbuf;
}
char *graphvizjs(char *input, char *layoutType, char *format)
{
graph_t *g;
char *output = NULL;
unsigned int outputLength = 0;
int res = 0;
if (!gvc) {
gvc = gvContext();
// Define the static plugins
gvAddLibrary(gvc, &gvplugin_core_LTX_library);
gvAddLibrary(gvc, &gvplugin_dot_layout_LTX_library);
agseterrf(emusererrf);
}
g = agmemread(input);
gvLayout(gvc, g, layoutType);
res = gvRenderData(gvc, g, format, &output, &outputLength);
gvFreeLayout(gvc, g);
agclose(g);
if (res == 0) return output;
else return NULL;
}