-
Notifications
You must be signed in to change notification settings - Fork 0
/
geckoDraw.cpp
90 lines (75 loc) · 2.65 KB
/
geckoDraw.cpp
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
//
// Created by millad on 11/28/18.
//
#include "geckoDraw.h"
#include "geckoHierarchicalTree.h"
#include <stdlib.h>
#include <cstring>
#include <cstdio>
#include <unordered_set>
__attribute__((always_inline))
inline
void __geckoDrawPerNode(FILE *f, GeckoLocation *p) {
if(p == NULL)
return;
char line[4096];
string parentName = p->getLocationName();
sprintf(&line[0], "\"%s\" [shape=box];\n", parentName.c_str());
fwrite(&line[0], sizeof(char), strlen(&line[0]), f);
const vector<GeckoLocation *> &children = p->getChildren();
const int size = children.size();
for(int i=0;i<size;i++) {
const string &childName = children[i]->getLocationName();
sprintf(&line[0], "\"%s\" -> \"%s\";\n", parentName.c_str(), childName.c_str());
fwrite(&line[0], sizeof(char), strlen(&line[0]), f);
__geckoDrawPerNode(f, children[i]);
}
}
__attribute__((always_inline))
inline
void __geckoDrawWithAllNodes(FILE *f, unordered_map<string, GeckoLocation*> &locMap) {
char line[4096];
unordered_set<string> alreadyBoxDrawn;
for(auto iter=locMap.begin(); iter != locMap.end(); iter++) {
string currentLocName = iter->first;
if(iter->second == NULL) {
// fprintf(stderr, "==============ERROR IN SECOND: %s\n", currentLocName.c_str());
continue;
}
if(iter->second->getParent() == NULL) {
// fprintf(stderr, "==============ERROR IN PARENT: %s\n", currentLocName.c_str());
continue;
}
string parentName = iter->second->getParent()->getLocationName();
if(alreadyBoxDrawn.find(currentLocName) == alreadyBoxDrawn.end()) {
sprintf(&line[0], "\"%s\" [shape=box];\n", currentLocName.c_str());
fwrite(&line[0], sizeof(char), strlen(&line[0]), f);
}
if(alreadyBoxDrawn.find(parentName) == alreadyBoxDrawn.end()) {
sprintf(&line[0], "\"%s\" [shape=box];\n", parentName.c_str());
fwrite(&line[0], sizeof(char), strlen(&line[0]), f);
}
sprintf(&line[0], "\"%s\" -> \"%s\";\n", parentName.c_str(), currentLocName.c_str());
fwrite(&line[0], sizeof(char), strlen(&line[0]), f);
}
}
void geckoDrawHierarchyTree(char *rootNode, char *filename) {
FILE *f;
char line[128];
if(filename == NULL || strlen(filename) == 0) {
#ifdef WARNING
fprintf(stderr, "===GECKO: Incorrect output filename for 'draw' clause.\n");
#endif
return;
}
f = fopen(filename, "w");
sprintf(&line[0], "digraph Gecko {\n");
fwrite(&line[0], sizeof(char), strlen(&line[0]), f);
// GeckoLocation *root = GeckoLocation::find(string(rootNode));
// __geckoDrawPerNode(f, root);
unordered_map<string, GeckoLocation*> locMap = GeckoLocation::getAllLocations();
__geckoDrawWithAllNodes(f, locMap);
sprintf(&line[0], "}\n");
fwrite(&line[0], sizeof(char), strlen(&line[0]), f);
fclose(f);
}