summaryrefslogtreecommitdiffstats
path: root/TOOLS/file2string.py
diff options
context:
space:
mode:
Diffstat (limited to 'TOOLS/file2string.py')
-rwxr-xr-xTOOLS/file2string.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/TOOLS/file2string.py b/TOOLS/file2string.py
index 6cdd1a72ae..90c9e03368 100755
--- a/TOOLS/file2string.py
+++ b/TOOLS/file2string.py
@@ -5,23 +5,27 @@
# of every string, so code using the string may need to remove that to get
# the exact contents of the original file.
+from __future__ import unicode_literals
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
-def main(infile):
+def file2string(infilename, infile, outfile):
+ outfile.write("// Generated from %s\n\n" % infilename)
+
conv = ['\\' + ("%03o" % c) for c in range(256)]
safe_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" \
"0123456789!#%&'()*+,-./:;<=>?[]^_{|}~ "
+
for c in safe_chars:
conv[ord(c)] = c
for c, esc in ("\nn", "\tt", r"\\", '""'):
conv[ord(c)] = '\\' + esc
for line in infile:
- sys.stdout.write('"' + ''.join(conv[pord(c)] for c in line) + '"\n')
+ outfile.write('"' + ''.join(conv[pord(c)] for c in line) + '"\n')
-with open(sys.argv[1], 'rb') as infile:
- sys.stdout.write("// Generated from %s\n\n" % sys.argv[1])
- main(infile)
+if __name__ == "__main__":
+ with open(sys.argv[1], 'rb') as infile:
+ file2string(sys.argv[1], infile, sys.stdout)