-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_functions.py
More file actions
224 lines (206 loc) · 4.57 KB
/
get_functions.py
File metadata and controls
224 lines (206 loc) · 4.57 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os
import checker
g_index = 0
def get_start(lst, j, i):
global g_index
flag = 0
while j >= 0:
i = len(lst[j]) - 1
while i >= 0:
if lst[j][i] == '(':
flag = 1
break
i -= 1
if flag == 1:
break
j -= 1
flag = 0
if i == 0 or (lst[j][i - 1] == ' ' or lst[j][i - 1] == '\t'):
while j >= 0:
i = len(lst[j]) - 1
while i >= 0:
if lst[j][i] != '\t' and lst[j][i] != ' ':
flag = 1
break
i -= 1
if flag == 1:
break
j -= 1
i -= 1
while i >= 0 and lst[j][i] != '\t' and lst[j][i] != ' ':
i -= 1
g_index = j
return i + 1
def get_end(lst, start):
global g_index
i = start
while i < len(lst[g_index]) and lst[g_index][i] != '(':
i += 1
return i - 1
def creat_string(string, start, end):
while string[start] == '*':
start += 1
size = (end - start )
new_str = [' '] * (size + 1)
i = 0
while start <= end:
new_str[i] = string[start]
start+=1
i += 1
return new_str
def in_quote(string, i):
i -= 1
while i >= 0:
if string[i] == 39 or string[i] == 34:
return 1
i -= 1
return 0
def is_called(list, j, i):
i += 1
while (j < len(list)):
while (i < len(list[j])):
if (list[j][i] == '{' and not in_quote(list[j], i)):
return 0
if (list[j][i] == ';' and not in_quote(list[j], i)):
return 1
i += 1
i = 0
j += 1
return 0
def is_alpha_numirique(c):
return ((c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') or (c >= '0' and c <= '9') or c == '_')
def is_empty(list, i):
while i >= 0:
if (list[i] != '\n' and list[i] != ' ' and list[i] != '\t'):
return (0)
i -= 1
return (1)
def get_end_2(list, j, i):
global g_index
i -= 1
if i < 0:
i = len(list[j - 1])
j -= 1
while is_empty(list[j], i):
j -= 1
i = len(list[j]) - 1
g_index = j
while (i >= 0):
if list[j][i] != '\n' and list[j][i] != ' ' and list[j][i] != '\t':
return i
i -= 1
g_index = j
return i
def get_start_2(list, i):
i -= 1
while list[i] == ' ' or list[i] == '\t':
i -= 1
while i >= 0:
if not is_alpha_numirique(list[i]):
return i + 1
i -= 1
return i + 1
def is_function(list, j, i):
i -= 1
flag = 0
while j >= 0:
while i >= 0:
if list[j][i] != '\n' and list[j][i] != '\t' and list[j][i] != ' ':
flag = 1
break
i -= 1
if flag == 1:
break
j -= 1
i = len(list[j]) - 1
index = 0
while i >= 0 and is_alpha_numirique(list[j][i]):
i -= 1
if i < 0 or (list[j][i] != '\n' and list[j][i] != '\t' and list[j][i] != ' '):
return (0)
return (1)
def get_files(DirectoryPath):
Files = []
if not os.path.exists(DirectoryPath):
print("no such file or directory: " + DirectoryPath)
exit(1)
for root, dirs, files in os.walk(DirectoryPath):
for file in files:
Files.append(os.path.join(root, file))
if not Files:
print("there are no .c files in " + DirectoryPath)
exit(1)
tab = [-1] * len(Files)
index = 0
for j in range(len(Files)):
if ".c" not in Files[j]:
tab[index] = j
index += 1
i = 0
while tab[i] != -1:
Files[tab[i]] = "non"
i += 1
return (Files)
def file_optimizer(file):
i = 0
j = 0
flag = 0
while j < len(file):
i = 0
while i < len(file[j]):
if file[j][i] == '{' and not in_quote(file[j], i):
if flag == 0:
flag += 1
else:
file[j][i] = '<'
flag += 1
if file[j][i] == '}' and not in_quote(file[j], i):
flag -= 1
i += 1
j += 1
def get_used_function(FilePath, Functions):
global g_index
data = open(FilePath, 'r')
chars = [list(line) for line in data]
j = 0
end = 0
start = 0
while j < len(chars):
i = 0
while i < len(chars[j]):
if chars[j][i] == '(' and is_called(chars, j, i) and is_function(chars, j, i):
end = get_end_2(chars, j, i)
start = get_start_2(chars[g_index], end)
result = creat_string(chars[g_index], start, end)
if not checker.is_operator(result) and not checker.already_there(Functions, result):
Functions.append(result)
i += 1
j += 1
def get_created_function(FilePath, Functions):
global g_index
data = open(FilePath, 'r')
chars = [list(line) for line in data]
j = 0
end = 0
start = 0
file_optimizer(chars)
while j < len(chars):
i = 0
while i < len(chars[j]):
if chars[j][i] == '{':
start = get_start(chars, j, i)
end = get_end(chars, start)
result = creat_string(chars[g_index], start, end)
if not checker.already_there(Functions, result):
Functions.append(result)
i += 1
j += 1
def get_all_fts(dir, CreatedFunctions, UsedFunctions):
Files = get_files(dir)
j = 0
while j < len(Files):
if (Files[j] != "non"):
get_created_function(Files[j], CreatedFunctions)
get_used_function(Files[j], UsedFunctions)
j += 1
return UsedFunctions