2016-12-17 12:24:05 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Convert the contents of a file into a C string constant.
|
|
|
|
# Note that the compiler will implicitly add an extra 0 byte at the end
|
|
|
|
# of every string, so code using the string may need to remove that to get
|
|
|
|
# the exact contents of the original file.
|
|
|
|
|
2016-12-17 16:12:56 +00:00
|
|
|
from __future__ import unicode_literals
|
2016-12-17 12:24:05 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
# Indexing a byte string yields int on Python 3.x, and a str on Python 2.x
|
|
|
|
def pord(c):
|
|
|
|
return ord(c) if type(c) == str else c
|
|
|
|
|
2016-12-17 16:12:56 +00:00
|
|
|
def file2string(infilename, infile, outfile):
|
|
|
|
outfile.write("// Generated from %s\n\n" % infilename)
|
|
|
|
|
2016-12-17 12:24:05 +00:00
|
|
|
conv = ['\\' + ("%03o" % c) for c in range(256)]
|
|
|
|
safe_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" \
|
|
|
|
"0123456789!#%&'()*+,-./:;<=>?[]^_{|}~ "
|
2016-12-17 16:12:56 +00:00
|
|
|
|
2016-12-17 12:24:05 +00:00
|
|
|
for c in safe_chars:
|
|
|
|
conv[ord(c)] = c
|
|
|
|
for c, esc in ("\nn", "\tt", r"\\", '""'):
|
|
|
|
conv[ord(c)] = '\\' + esc
|
|
|
|
for line in infile:
|
2016-12-17 16:12:56 +00:00
|
|
|
outfile.write('"' + ''.join(conv[pord(c)] for c in line) + '"\n')
|
2016-12-17 12:24:05 +00:00
|
|
|
|
2016-12-17 16:12:56 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
with open(sys.argv[1], 'rb') as infile:
|
2017-01-05 15:45:26 +00:00
|
|
|
file2string(sys.argv[1], infile, sys.stdout)
|