aboutsummaryrefslogtreecommitdiff
path: root/src/graphs.c
blob: 51a26117d0a3f08d126e767250096c28bf32bee7 (plain)
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
140
141
142
143
144
#include "graphs.h"

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "err.h"

// #define TYPE strbuf
// #include "linked_list.h"
// #include "linked_list.inc.h"
// #undef TYPE

int create_graph_trie (vcomponent* ev, char* filename) {
	FILE* f = fopen(filename, "w");

	fputs("digraph {\n	rankdir=LR;", f);
	trie_to_dot(&ev->clines, f);
	fputs("}", f);

	fclose(f);

	INFO_F("Wrote '%s' to '%s'", vcomponent_get_val(ev, "X-HNH-FILENAME"), filename);

	return 0;
}

int helper_vcomponent (vcomponent* root, FILE* f) {
	fprintf(f, "subgraph \"cluster_root\" { label=File; \"%p\" [label=%s] }\n", root, root->type);

	TRIE(content_line)* trie = &root->clines;
	TRIE_NODE(content_line)* n = trie->root->child;

	if (! EMPTY(TRIE(content_line))(trie)) {
		fprintf(f, "subgraph \"cluster_%p\" {\n", root);
		fprintf(f, "\"%p\" [label=trie fontcolor=gray, color=gray];", trie);
		fprintf(f, "\"%p\" -> \"%p\" [color=red]\n", root, trie);
		while (n != NULL) {
			fprintf(f, "\"%p\" -> \"%p\" [color=gray]\n",
					(void*) trie,
					(void*) n);
			fprintf(f, "subgraph \"cluster_%c_%p\" {\ncolor=red; \n",
					n->c, root);
			trie_to_dot_helper( n, f );


			fputs("}", f);
			n = n->next;
		}
		fputs("}", f);
	}

	FOR(LLIST, vcomponent, child, &root->components) {
		fprintf(f, "\"%p\" -> \"%p\"\n", root, child);
		helper_vcomponent(child, f);
	}
	return 0;
}

int create_graph_vcomponent (vcomponent* root, char* outfile) {
	FILE* f = fopen(outfile, "w");
	if (f == NULL) {
		ERR_F("Error opening file %s, errno = %i", outfile, errno);
		return 1;
	}
	vcomponent* c = root;
	fputs("digraph {", f);
	helper_vcomponent(c, f);
	fputs("}", f);
	fclose(f);
	return 0;
}

#define T content_line

int trie_to_dot ( TRIE(T)* trie, FILE* f ) {
	TRIE_NODE(T)* n = trie->root->child;
	fprintf(f, "\"%p\" [label=root fontcolor=gray, color=gray];", trie);
	while (n != NULL) {
		fprintf(f, "\"%p\" -> \"%p\" [color=gray]\n",
				(void*) trie,
				(void*) n);
		fprintf(f, "subgraph \"cluster_%c\" {\n",
				n->c);
		trie_to_dot_helper( n, f );
		fputs("}", f);
		n = n->next;
	}
	return 0;
}

int trie_to_dot_helper ( TRIE_NODE(T)* root, FILE* f  ) {
	if (L(root) == NULL) {
		fprintf(f, "\"%p\"[label = \"%c\" style=filled fillcolor=white];\n",
				(void*) root, root->c);
	} else {
		fprintf(f, "\"%p\"[label = \"%c [%i]\" style=filled fillcolor=green];\n",
				(void*) root, root->c,
				SIZE(LLIST(content_set))(L(root))
				);
	}
	TRIE_NODE(T)* child = root->child;

	// ----------------------------------------
#if 1 /* Toggle values */
	if (L(root) != NULL) {

		FOR(LLIST, content_set, v, L(root)) {
			char buf[0x100];
			FMT(strbuf)(&v->key, buf);
			fprintf(f, "\"%p\" [label=\"%s\" shape=rectangle color=darkgreen];\n",
					v, buf);
			/* Edge between TRIE char node and data node */
			fprintf(f, "\"%p\" -> \"%p\";\n", root, v);

			/* Parameters */
			LLIST(strbuf)* keys = KEYS(TRIE(param_set))(&v->val);
			FOR(LLIST, strbuf, key, keys) {
				param_set* p = GET(TRIE(param_set))(&v->val, key->mem);

				fprintf(f, "\"%p\" [label=\"%s\" color=blue];\n",
						key, key->mem);
				/* Edge between data node and param key node */
				fprintf(f, "\"%p\" -> \"%p\";", v, key);

				FOR(LLIST, strbuf, str, p) {
					fprintf(f, "\"%p\" [label=\"%s\" color=orange];",
							str, str->mem);
					/* Edge between param key node and param value node */
					fprintf(f, "\"%p\" -> \"%p\";", key, str);
				}
			}
		}
	}
#endif
	// ----------------------------------------

	while (child != NULL) {
		fprintf(f, "\"%p\" -> \"%p\";\n",
				(void*) root, (void*) child);
		trie_to_dot_helper(child, f);
		child = child->next;
	}
	return 0;
}