diff options
author | wm4 <wm4@mplayer2.org> | 2011-12-26 01:13:07 +0100 |
---|---|---|
committer | wm4 <wm4@mplayer2.org> | 2012-03-31 02:58:52 +0200 |
commit | 9564e0754854e723a79ebf12410853874fbf4cfd (patch) | |
tree | 929678484c2e964bc88abac7e3ed4073156e3688 /bin_to_header.py | |
parent | 67e7da2d70253edbe53a71e270247557b32efe27 (diff) | |
download | mpv-9564e0754854e723a79ebf12410853874fbf4cfd.tar.bz2 mpv-9564e0754854e723a79ebf12410853874fbf4cfd.tar.xz |
Add bin_to_header.py
Diffstat (limited to 'bin_to_header.py')
-rw-r--r-- | bin_to_header.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/bin_to_header.py b/bin_to_header.py new file mode 100644 index 0000000000..137a5b3728 --- /dev/null +++ b/bin_to_header.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Script to embed arbitrary binary files in C header files. + +CHARS_PER_LINE = 19 + +import sys +import os + +if len(sys.argv) != 3: + print("Embed binary files in C headers.") + print("Usage: ") + print(" bin_to_header.py infile outfile.h") + print("outfile.h will be overwritten with the new contents.") + sys.exit(1) + +infile_name = sys.argv[1] +outfile_name = sys.argv[2] + +varname = os.path.splitext(os.path.basename(outfile_name))[0] + +infile = open(infile_name, "rb") +outfile = open(outfile_name, "w") + +outfile.write("// Generated with " + " ".join(sys.argv) + "\n") +outfile.write("\nstatic const unsigned char " + varname + "[] = {\n") + +while True: + data = infile.read(CHARS_PER_LINE) + if len(data) == 0: + break + outfile.write(" ") + for c in data: + # make it work both in Python 2.x (c is str) and 3.x (c is int) + if type(c) != int: + c = ord(c) + outfile.write("{0:3},".format(c)) + outfile.write("\n") + +outfile.write("};\n") + +infile.close() +outfile.close() |