File: data-to-c.py

package info (click to toggle)
glib2.0 2.78.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,024 kB
  • sloc: ansic: 495,608; xml: 17,404; python: 9,572; perl: 1,144; sh: 1,094; cpp: 487; makefile: 225
file content (17 lines) | stat: -rwxr-xr-x 411 bytes parent folder | download | duplicates (8)
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)