[olympic] Improve error handling in file-to-cpp

This commit is contained in:
Gary Talent 2025-04-16 20:11:28 -05:00
parent 3fe62464c3
commit eef51a6d2b

View File

@ -15,6 +15,9 @@ import pathlib
import sys import sys
tool_name = 'file-to-cpp'
def write_txt_file(path: str, txt: str): def write_txt_file(path: str, txt: str):
with open(path, "w") as f: with open(path, "w") as f:
f.write(txt) f.write(txt)
@ -22,18 +25,22 @@ def write_txt_file(path: str, txt: str):
def file_to_hex(path: str, line_prefix: str) -> tuple[str, int]: def file_to_hex(path: str, line_prefix: str) -> tuple[str, int]:
with open(path, 'rb') as f: try:
out = line_prefix with open(path, 'rb') as f:
data = f.read() out = line_prefix
i = 1 data = f.read()
for b in data: i = 1
out += f"{b:#0{4}x}," for b in data:
if i % 10 == 0: out += f"{b:#0{4}x},"
out += '\n\t' if i % 10 == 0:
else: out += '\n\t'
out += ' ' else:
i += 1 out += ' '
return out[:-1], len(data) i += 1
return out[:-1], len(data)
except FileNotFoundError:
print(f'{tool_name}: included file not found: {path}', file=sys.stderr)
sys.exit(1)
def file_to_cpp(path: str, cpp_name: str) -> tuple[str, str]: def file_to_cpp(path: str, cpp_name: str) -> tuple[str, str]:
@ -76,11 +83,11 @@ def proc_rsrc_file(rsrc_path: str):# Open and read the JSON file
all_files_func += f'\nox::Vector<ox::SpanView<uint8_t>> {all_files}() noexcept {{\n\treturn {{\n' all_files_func += f'\nox::Vector<ox::SpanView<uint8_t>> {all_files}() noexcept {{\n\treturn {{\n'
for f in data['files']: for f in data['files']:
if 'path' not in f: if 'path' not in f:
print('src file path missing', file=sys.stderr) print(f'{tool_name}: src file path missing', file=sys.stderr)
sys.exit(1) sys.exit(1)
path = f['path'] path = f['path']
if 'cpp_name' not in f: if 'cpp_name' not in f:
print('var name missing', file=sys.stderr) print(f'{tool_name}: var name missing', file=sys.stderr)
sys.exit(1) sys.exit(1)
cpp_name = f['cpp_name'] cpp_name = f['cpp_name']
c, h = file_to_cpp(os.path.join(base_path, path), cpp_name) c, h = file_to_cpp(os.path.join(base_path, path), cpp_name)