51 lines
1.3 KiB
Python
Executable file
51 lines
1.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("input")
|
|
parser.add_argument("output")
|
|
|
|
args = parser.parse_args()
|
|
|
|
input = Path(args.input)
|
|
output = Path(args.output)
|
|
|
|
if not input.exists():
|
|
print("Input " + str(input.absolute()) + " not found.")
|
|
raise SystemExit(1)
|
|
|
|
file_array = list()
|
|
|
|
for child in Path(input).iterdir():
|
|
if child.is_file():
|
|
file_array.append({"filename": child.name, "data": child.read_bytes()})
|
|
|
|
def generate_data():
|
|
string_list = list()
|
|
for index, data in enumerate(file_array):
|
|
hexList = list()
|
|
for byte in data["data"]:
|
|
hexList.append(f"0x{byte:02x}")
|
|
string_list.append(f"unsigned char data{index}[] = {{ {','.join(hexList)} }};")
|
|
return '\n'.join(string_list)
|
|
|
|
def generate_hashmap_init():
|
|
string_list = list()
|
|
for index, data in enumerate(file_array):
|
|
filename = '"' + data["filename"] + '"'
|
|
string_list.append(f"if (strcmp(filename, {filename}) == 0) {{ *data = &data{index}; *size = sizeof(data{index}); return 1; }}")
|
|
strings = '\n'.join(string_list)
|
|
return f"""int get_file_pointer(const char* filename, void ** data, size_t * size) {{
|
|
{strings}
|
|
return 0;
|
|
}}"""
|
|
|
|
code = f"""#include \"data.h\"
|
|
#include <string.h>
|
|
{generate_data()}
|
|
{generate_hashmap_init()}
|
|
"""
|
|
|
|
with open(output, 'w') as f:
|
|
f.write(code)
|