From fb3483672193c2fc360d3c6e450dc051541556e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Fri, 10 Jan 2014 11:02:14 +0100 Subject: add eina_graph_dot_write() --- src/lib/eina_graph.c | 25 +++++++++++++++++++++++++ src/lib/eina_graph.h | 10 ++++++++++ src/tests/eina_graph_suite.c | 6 ++++++ 3 files changed, 41 insertions(+) diff --git a/src/lib/eina_graph.c b/src/lib/eina_graph.c index 4cea67f..e3bf209 100644 --- a/src/lib/eina_graph.c +++ b/src/lib/eina_graph.c @@ -159,3 +159,28 @@ eina_graph_edge_add(Eina_Graph *g, unsigned int v, unsigned int w) return EINA_TRUE; } + +EAPI void +eina_graph_dot_write(Eina_Graph *g, FILE *f) +{ + int r; + char buf[32]; + unsigned int i, j, n; + Eina_Array *ar; + _Eina_Graph * _g = (_Eina_Graph *) g; + + fwrite("digraph {\n", 10, 1, f); + + for (i = 0; i< _g->vertices; i++) + { + ar = _g->adjs[i]; + n = eina_array_count_get(ar); + for (j = 0; j < n; j++) + { + r = snprintf(buf, 32, "%u -> %u;\n", i, eina_array_uint_nth_get(ar, j)); + fwrite(buf, r, 1, f); + } + } + + fwrite("}\n", 2, 1, f); +} diff --git a/src/lib/eina_graph.h b/src/lib/eina_graph.h index c748ede..3297a22 100644 --- a/src/lib/eina_graph.h +++ b/src/lib/eina_graph.h @@ -29,6 +29,7 @@ #ifndef _EINA_GRAPH_H #define _EINA_GRAPH_H +#include #include /** @@ -130,4 +131,13 @@ eina_graph_self_loops(const Eina_Graph *g); EAPI Eina_Bool eina_graph_edge_add(Eina_Graph *g, unsigned int v, unsigned int w); +/** + * generate dot file from the graph. + * + * @param g the graph to add to edje to. + * @param f the output stream to write to. + */ +EAPI void +eina_graph_dot_write(Eina_Graph *g, FILE *f); + #endif /* _EINA_GRAPH_H */ diff --git a/src/tests/eina_graph_suite.c b/src/tests/eina_graph_suite.c index 138d852..bb4cdf1 100644 --- a/src/tests/eina_graph_suite.c +++ b/src/tests/eina_graph_suite.c @@ -120,6 +120,8 @@ END_TEST START_TEST (test_eina_graph_simple) { + FILE *outf; + ck_assert(eina_graph_init() == 1); ck_assert(eina_graph_init() == 2); @@ -153,6 +155,10 @@ START_TEST (test_eina_graph_simple) ck_assert(eina_graph_degree_avg(g) < 2.154); ck_assert(eina_graph_self_loops(g) == 1); + outf = fopen("/tmp/eina_graph.dot", "w"); + eina_graph_dot_write(g, outf); + fclose(outf); + eina_graph_free(g); ck_assert(eina_graph_shutdown() == 1); -- cgit v1.1-2-g2b99