forked from simonlindholm/decomp-permuter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strip_other_fns.py
74 lines (58 loc) · 1.92 KB
/
strip_other_fns.py
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
import re
import argparse
from typing import Optional
from pathlib import Path
def _find_bracket_end(input: str, start_index: int) -> int:
level = 1
assert input[start_index] == "{"
i = start_index + 1
while i < len(input):
if input[i] == "{":
level += 1
elif input[i] == "}":
level -= 1
if level == 0:
break
i += 1
assert level == 0, "unbalanced {}"
return i
def strip_other_fns(source: str, keep_fn_name: str) -> str:
result = ""
remain = source
while True:
fn_regex = re.compile(r"^.*\s+\**(\w+)\(.*\)\s*?{", re.M)
fn = re.search(fn_regex, remain)
if fn is None:
result += remain
remain = ""
break
fn_name = fn.group(1)
bracket_end = _find_bracket_end(remain, fn.end() - 1)
if fn_name.startswith("PERM"):
result += remain[: bracket_end + 1]
elif fn_name == keep_fn_name:
result += "\n\n" + remain[: bracket_end + 1] + "\n\n"
else:
result += remain[: fn.end() - 1].rstrip() + ";"
remain = remain[bracket_end + 1 :]
return result
def strip_other_fns_and_write(
source: str, fn_name: str, out_filename: Optional[str] = None
) -> None:
stripped = strip_other_fns(source, fn_name)
if out_filename is None:
print(stripped)
else:
with open(out_filename, "w", encoding="utf-8") as f:
f.write(stripped)
def main() -> None:
parser = argparse.ArgumentParser(
description="Remove all but a single function definition from a file."
)
parser.add_argument("c_file", help="File containing the function.")
parser.add_argument("fn_name", help="Function name.")
args = parser.parse_args()
source = Path(args.c_file).read_text()
strip_other_fns_and_write(source, args.fn_name, args.c_file)
if __name__ == "__main__":
main()