-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnum_of_func_extract.c
51 lines (41 loc) · 1.2 KB
/
num_of_func_extract.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
#include <stdio.h>
#include <jansson.h> // jansson.h 라이브러리 이용
#include <string.h>
//'FuncDef' 노드를 카운트하는 함수
int count_functions(json_t *root) {
int count = 0;
json_t *stack[1000];
int top = 0;
stack[top++] = root;
//반복
while (top > 0) {
json_t *node = stack[--top];
if (json_is_object(node)) {
// check
if (strcmp(json_string_value(json_object_get(node, "_nodetype")), "FuncDef") == 0) {
count++;
}
const char *key;
json_t *value;
json_object_foreach(node, key, value) {
stack[top++] = value;
}
}
else if (json_is_array(node)) {
size_t size = json_array_size(node);
for (size_t i = 0; i < size; i++) {
stack[top++] = json_array_get(node, i);
}
}
}
return count;
}
/*
int main() {
json_t *root = json_load_file("ast.json", 0, NULL);
printf("Number of functions: %d\n", count_functions(root));
json_decref(root); // 메모리를 해제
return 0;
}
*/
// node & stack