-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_structure.c
143 lines (132 loc) · 4.18 KB
/
print_structure.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
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
#include "print_structure.h"
#include "frame_init.h"
#include "command.h"
/**
* generate program praph
*
* @param structure_graph pointer to program graph
* @param config_json pointer to program configuration json
*/
void
generate_call_structure(graph *structure_graph, cJSON *config_json){
/* init structure_graph */
structure_graph->node = (graph_node**) malloc(sizeof(graph_node*) * MAX_NODE_NUM);
structure_graph->node_num = 0;
structure_graph->root_node = NULL;
init_nodes(structure_graph, config_json);//add nodes
init_edges(structure_graph, config_json); //add edges
}
void
__parse_call_structure(graph_node *node, char **outbuf_ptr) {
if (node->child_num == 0) {
return;
}
memcpy(outbuf_ptr[0], node->name, strlen(node->name) * sizeof(char));
outbuf_ptr[0] += strlen(node->name);
outbuf_ptr[0][0] = '(';
outbuf_ptr[0] ++;
outbuf_ptr[0][0] = ')';
outbuf_ptr[0] ++;
outbuf_ptr[0][0] = ' ';
outbuf_ptr[0] ++;
outbuf_ptr[0][0] = '{';
outbuf_ptr[0] += 1;
outbuf_ptr[0][0] = '\n';
outbuf_ptr[0] += 1;
for (int i = 0; i < node->child_num; i ++) {
/* Number of function the loop contain */
int count = 0;
if (node->loop_nums[i] > 1) {
int j = i;
while (j < node->child_num) {
if(node->loop_nums[j] > 1){
count ++;
}
j ++;
}
}
if (count > 0) {
/* add loop to outbuf */
char str[50] = {};
sprintf(str," loop(%d) {\n",node->loop_nums[i]);
int len = strlen(str);
memcpy(outbuf_ptr[0], str, len);
outbuf_ptr[0] += len;
for (int j = 0; j < count; j++){
for (int k = 0; k < 8; k ++){
outbuf_ptr[0][0] = ' ';
outbuf_ptr[0] ++;
}
len = strlen(node->childs[i + j]->name);
memcpy(outbuf_ptr[0], node->childs[i + j]->name, len);
outbuf_ptr[0] += len;
outbuf_ptr[0][0] = ';';
outbuf_ptr[0] += 1;
outbuf_ptr[0][0] = '\n';
outbuf_ptr[0] += 1;
}
for (int k = 0; k < 4; k ++){
outbuf_ptr[0][0] = ' ';
outbuf_ptr[0] ++;
}
outbuf_ptr[0][0] = '}';
outbuf_ptr[0] ++;
outbuf_ptr[0][0] = '\n';
outbuf_ptr[0] ++;
i += count;
}else{
for (int k = 0; k < 4; k ++){
outbuf_ptr[0][0] = ' ';
outbuf_ptr[0] ++;
}
int len = strlen(node->childs[i]->name);
memcpy(outbuf_ptr[0], node->childs[i]->name, len);
outbuf_ptr[0] += len;
outbuf_ptr[0][0] = ';';
outbuf_ptr[0] += 1;
outbuf_ptr[0][0] = '\n';
outbuf_ptr[0] += 1;
}
}
outbuf_ptr[0][0] = '}';
outbuf_ptr[0] += 1;
outbuf_ptr[0][0] = '\n';
outbuf_ptr[0] += 1;
outbuf_ptr[0][0] = '\n';
outbuf_ptr[0] += 1;
for (int i = 0; i < node->child_num; i ++){
__parse_call_structure(node->childs[i], outbuf_ptr);
}
}
/**
* Parse function call structure.
*
* @param structure_graph pointer to program graph
* @param outbuf_ptr pointer to output buffer
*/
void
parse_call_structure(graph *structure_graph, char *outbuf_ptr){
__parse_call_structure(structure_graph->root_node, &outbuf_ptr);
}
/**
* Parse config file and output to file.
* @param config_buf pointer to configuration
*/
void
print_structure(cJSON *config_json) {
graph *structure_graph = (graph*) malloc(sizeof(graph));
char *outbuf = (char*) malloc(sizeof(char) * MAX_FILE_SIZE);
memset(outbuf, 0, sizeof(char) * MAX_FILE_SIZE);
generate_call_structure(structure_graph, config_json);
parse_call_structure(structure_graph, outbuf);
FILE *file;
char filename[MAX_FILE_NAME];
sprintf(filename, "%s/%s", arguments.outfile, "structure.log");
file = fopen(filename, "w");
fprintf(file, "%s", outbuf);
fflush(file);
/* clean */
fclose(file);
free(outbuf);
clear_graph(structure_graph);
}