rodata2cpp.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import argparse
  2. def main():
  3. parser = argparse.ArgumentParser(description='Convert rodata into C++ source with embedded file content')
  4. parser.add_argument('symbol', help='symbol name exported from generated file')
  5. parser.add_argument('rodata', type=argparse.FileType('rb'), help='input .rodata file path')
  6. parser.add_argument('cpp', type=argparse.FileType('w', encoding='UTF-8'), help='destination .cpp file path')
  7. args = parser.parse_args()
  8. args.cpp.write('static_assert(sizeof(unsigned int) == 4, "ups, something gone wrong");\n\n')
  9. args.cpp.write('extern "C" {\n')
  10. args.cpp.write(' extern const unsigned char ' + args.symbol + '[] = {\n')
  11. cnt = 0
  12. for ch in args.rodata.read():
  13. args.cpp.write('0x%02x, ' % ch)
  14. cnt += 1
  15. if cnt % 50 == 1:
  16. args.cpp.write('\n')
  17. args.cpp.write(' };\n')
  18. args.cpp.write(' extern const unsigned int ' + args.symbol + 'Size = sizeof(' + args.symbol + ');\n')
  19. args.cpp.write('}\n')
  20. args.rodata.close()
  21. args.cpp.close()
  22. if __name__ == '__main__':
  23. main()