diff options
author | 2014-12-15 15:19:55 +0100 | |
---|---|---|
committer | 2014-12-15 15:19:55 +0100 | |
commit | 09a636c2ffdc4086b1316e4efb8d8c9c05d2b291 (patch) | |
tree | a84b5e73e05d4e78d1544a6b75bd417d5afe56ca /src/build/file2str.py | |
parent | 00eeff4aded4aad7162906f08c95576799c0b61f (diff) |
Converted file2str to python (nw)
Diffstat (limited to 'src/build/file2str.py')
-rw-r--r-- | src/build/file2str.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/build/file2str.py b/src/build/file2str.py new file mode 100644 index 00000000000..42bdef843a5 --- /dev/null +++ b/src/build/file2str.py @@ -0,0 +1,56 @@ +#!/usr/bin/python + +import string +import sys +import os + +if (len(sys.argv) < 4) : + print('Usage:') + print(' file2str <source.lay> <output.h> <varname> [<type>]') + print('') + print('The default <type> is char, with an assumed NULL terminator') + sys.exit(0) + +terminate = 1 +srcfile = sys.argv[1] +dstfile = sys.argv[2] +varname = sys.argv[3] + +if (len(sys.argv) >= 5) : + type = sys.argv[4] + terminate = 0 +else: + type = 'char' + +try: + myfile = open(srcfile, 'rb') +except IOError: + print("Unable to open source file '%s'" % srcfile) + sys.exit(-1) + +bytes = os.path.getsize(srcfile) +try: + dst = open(dstfile,'w') + dst.write('extern const %s %s[];\n' % ( type, varname )); + dst.write('const %s %s[] =\n{\n\t' % ( type, varname)); + offs = 0 + with open(srcfile, "rb") as src: + while True: + chunk = src.read(16) + if chunk: + for b in chunk: + dst.write('0x%02x' % ord(b)) + offs = offs + 1 + if offs != bytes: + dst.write(',') + else: + break + if offs != bytes: + dst.write('\n\t') + if terminate == 1: + dst.write(',0x00') + dst.write('\n};\n') + dst.close() +except IOError: + print("Unable to open output file '%s'" % dstfile) + sys.exit(-1)
\ No newline at end of file |