File: data-to-c.py

package info (click to toggle)
gobject-introspection 1.74.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 68,092 kB
  • sloc: ansic: 524,161; xml: 32,098; python: 21,234; yacc: 1,707; perl: 1,411; sh: 1,044; lex: 499; cpp: 171; makefile: 77; lisp: 1
file content (17 lines) | stat: -rwxr-xr-x 411 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python3

import sys

if len(sys.argv) < 4:
    print("Usage: {0} <filename> <variable> <output>")

with open(sys.argv[1], "r", encoding="utf-8", errors="backslashreplace") as f:
    in_data = f.read()

b = [r"\x{:02x}".format(ord(c)) for c in in_data]

out_data = 'const char {0}[] = "'.format(sys.argv[2])
out_data += "".join(b) + '";\n'

with open(sys.argv[3], "w") as f:
    f.write(out_data)