diff options
| author | 3gg <3gg@shellblade.net> | 2026-03-06 13:26:57 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2026-03-06 13:26:57 -0800 |
| commit | f5c89b3bd5d74849757fd5b4d1a300068522a3ca (patch) | |
| tree | d6f6e4c81745b393d7594b334710f30c0b2df3bd /SDL-3.2.8/build-scripts/rename_types.py | |
Diffstat (limited to 'SDL-3.2.8/build-scripts/rename_types.py')
| -rwxr-xr-x | SDL-3.2.8/build-scripts/rename_types.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/SDL-3.2.8/build-scripts/rename_types.py b/SDL-3.2.8/build-scripts/rename_types.py new file mode 100755 index 0000000..137b409 --- /dev/null +++ b/SDL-3.2.8/build-scripts/rename_types.py | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | # | ||
| 3 | # This script renames symbols in the specified paths | ||
| 4 | |||
| 5 | import argparse | ||
| 6 | import os | ||
| 7 | import pathlib | ||
| 8 | import re | ||
| 9 | import sys | ||
| 10 | |||
| 11 | |||
| 12 | def main(): | ||
| 13 | if len(args.args) < 1: | ||
| 14 | print("Usage: %s files_or_directories ..." % sys.argv[0]) | ||
| 15 | exit(1) | ||
| 16 | |||
| 17 | replacements = { | ||
| 18 | "SDL_bool": "bool", | ||
| 19 | "SDL_TRUE": "true", | ||
| 20 | "SDL_FALSE": "false", | ||
| 21 | } | ||
| 22 | entries = args.args[0:] | ||
| 23 | |||
| 24 | regex = create_regex_from_replacements(replacements) | ||
| 25 | |||
| 26 | for entry in entries: | ||
| 27 | path = pathlib.Path(entry) | ||
| 28 | if not path.exists(): | ||
| 29 | print("%s doesn't exist, skipping" % entry) | ||
| 30 | continue | ||
| 31 | |||
| 32 | replace_symbols_in_path(path, regex, replacements) | ||
| 33 | |||
| 34 | def create_regex_from_replacements(replacements): | ||
| 35 | return re.compile(r"\b(%s)\b" % "|".join(map(re.escape, replacements.keys()))) | ||
| 36 | |||
| 37 | def replace_symbols_in_file(file, regex, replacements): | ||
| 38 | try: | ||
| 39 | with file.open("r", encoding="UTF-8", newline="") as rfp: | ||
| 40 | original = rfp.read() | ||
| 41 | contents = regex.sub(lambda mo: replacements[mo.string[mo.start():mo.end()]], original) | ||
| 42 | if contents != original: | ||
| 43 | with file.open("w", encoding="UTF-8", newline="") as wfp: | ||
| 44 | wfp.write(contents) | ||
| 45 | except UnicodeDecodeError: | ||
| 46 | print("%s is not text, skipping" % file) | ||
| 47 | except Exception as err: | ||
| 48 | print("%s" % err) | ||
| 49 | |||
| 50 | |||
| 51 | def replace_symbols_in_dir(path, regex, replacements): | ||
| 52 | for entry in path.glob("*"): | ||
| 53 | if entry.is_dir(): | ||
| 54 | replace_symbols_in_dir(entry, regex, replacements) | ||
| 55 | else: | ||
| 56 | print("Processing %s" % entry) | ||
| 57 | replace_symbols_in_file(entry, regex, replacements) | ||
| 58 | |||
| 59 | |||
| 60 | def replace_symbols_in_path(path, regex, replacements): | ||
| 61 | if path.is_dir(): | ||
| 62 | replace_symbols_in_dir(path, regex, replacements) | ||
| 63 | else: | ||
| 64 | replace_symbols_in_file(path, regex, replacements) | ||
| 65 | |||
| 66 | |||
| 67 | if __name__ == "__main__": | ||
| 68 | |||
| 69 | parser = argparse.ArgumentParser(fromfile_prefix_chars='@') | ||
| 70 | parser.add_argument("args", nargs="*") | ||
| 71 | args = parser.parse_args() | ||
| 72 | |||
| 73 | try: | ||
| 74 | main() | ||
| 75 | except Exception as e: | ||
| 76 | print(e) | ||
| 77 | exit(-1) | ||
| 78 | |||
| 79 | exit(0) | ||
| 80 | |||
