51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
|
#!/usr/bin/env python3
|
||
|
import argparse
|
||
|
from pathlib import Path
|
||
|
from PIL import Image
|
||
|
|
||
|
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):
|
||
|
string_list.append(f"if (strcmp(filename, \"{data["filename"]}\") == 0) {{ *data = &data{index}; *size = sizeof(data{index}); return 1; }}")
|
||
|
return f"""int get_file_pointer(const char* filename, void ** data, size_t * size) {{
|
||
|
{'\n'.join(string_list)}
|
||
|
return 0;
|
||
|
}}"""
|
||
|
|
||
|
code = f"""#include \"data.h\"
|
||
|
#include <string.h>
|
||
|
{generate_data()}
|
||
|
{generate_hashmap_init()}
|
||
|
"""
|
||
|
|
||
|
with open(output, 'w') as f:
|
||
|
f.write(code)
|