#! /usr/bin/env python3 # # Copyright 2016 - 2025 gary@drinkingtea.net # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. # import argparse import sys def write_txt_file(path: str, txt: str): with open(path, "w") as f: f.write(txt) print(f'Wrote {path}') def file_to_hex(path: str, line_prefix: str) -> tuple[str, int]: with open(path, 'rb') as f: out = line_prefix data = f.read() i = 1 for b in data: out += f"{b:#0{4}x}," if i % 10 == 0: out += '\n\t' else: out += ' ' i += 1 return out[:-1], len(data) def file_to_cpp(path: str, var_name: str, namespace: str, cpp_path: str, hpp_path: str): if len(namespace) > 0: push_ns = f'namespace {namespace} {{\n' pop_ns = '\n}\n' data, dataLen = file_to_hex(path, "\t") cpp = '\n#include \n\n' cpp = '\n#include \n\n' cpp += push_ns cpp += f'\nstatic constexpr ox::Array {var_name}Data {{\n{data}\n}};\n' cpp += f'\nox::SpanView {var_name}() noexcept {{ return {var_name}Data; }}\n' cpp += pop_ns write_txt_file(cpp_path, cpp) if hpp_path is not None: hpp = '\n#include \n\n' hpp += push_ns hpp += f'\n[[nodiscard]]\nox::SpanView {var_name}() noexcept;\n' hpp += pop_ns write_txt_file(hpp_path, hpp) def main() -> int: parser = argparse.ArgumentParser() parser.add_argument('--file', help='path to file', required=True) parser.add_argument('--cpp', help='path to output cpp file', required=True) parser.add_argument('--hpp', help='path to output hpp file') parser.add_argument('--namespace', help='path to output hpp file') parser.add_argument('--var-name', help='path to output hpp file', required=True) args = parser.parse_args() file_to_cpp(args.file, args.var_name, args.namespace, args.cpp, args.hpp) return 0 if __name__ == '__main__': try: err = main() sys.exit(err) except KeyboardInterrupt: sys.exit(1)