diff options
Diffstat (limited to 'src/contrib/SDL-3.2.20/cmake/xxd.py')
-rwxr-xr-x | src/contrib/SDL-3.2.20/cmake/xxd.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/cmake/xxd.py b/src/contrib/SDL-3.2.20/cmake/xxd.py new file mode 100755 index 0000000..678946a --- /dev/null +++ b/src/contrib/SDL-3.2.20/cmake/xxd.py | |||
@@ -0,0 +1,37 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | import argparse | ||
4 | import os | ||
5 | import pathlib | ||
6 | import re | ||
7 | |||
8 | def main(): | ||
9 | parser = argparse.ArgumentParser(allow_abbrev=False, description="Convert file into includable C header") | ||
10 | parser.add_argument("--in", "-i", type=pathlib.Path, metavar="INPUT", dest="input", required=True, help="Input file") | ||
11 | parser.add_argument("--out", "-o", type=pathlib.Path, metavar="OUTPUT", dest="output", required=True, help="Output header") | ||
12 | parser.add_argument("--columns", type=int, default=12, help="Column count") | ||
13 | args = parser.parse_args() | ||
14 | |||
15 | t = pathlib.Path() | ||
16 | varname, _ = re.subn("[^a-zA-Z0-9]", "_", str(args.input.name)) | ||
17 | |||
18 | binary_data = args.input.open("rb").read() | ||
19 | |||
20 | with args.output.open("w", newline="\n") as fout: | ||
21 | fout.write("unsigned char {}[] = {{\n".format(varname)) | ||
22 | bytes_written = 0 | ||
23 | while bytes_written < len(binary_data): | ||
24 | col = bytes_written % args.columns | ||
25 | if col == 0: | ||
26 | fout.write(" ") | ||
27 | column_data = binary_data[bytes_written:bytes_written+args.columns] | ||
28 | fout.write(", ".join("0x{:02x}".format(d) for d in column_data)) | ||
29 | bytes_written += len(column_data) | ||
30 | if bytes_written < len(binary_data): | ||
31 | fout.write(",\n") | ||
32 | else: | ||
33 | fout.write("\n") | ||
34 | fout.write("}};\nunsigned int {}_len = {:d};\n".format(varname, len(binary_data))) | ||
35 | |||
36 | if __name__ == "__main__": | ||
37 | raise SystemExit(main()) | ||