1# encoding=utf-8 2# Copyright © 2018 Intel Corporation 3# 4# Permission is hereby granted, free of charge, to any person obtaining a 5# copy of this software and associated documentation files (the "Software"), 6# to deal in the Software without restriction, including without limitation 7# the rights to use, copy, modify, merge, publish, distribute, sublicense, 8# and/or sell copies of the Software, and to permit persons to whom the 9# Software is furnished to do so, subject to the following conditions: 10# 11# The above copyright notice and this permission notice (including the next 12# paragraph) shall be included in all copies or substantial portions of the 13# Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21# IN THE SOFTWARE. 22 23# Converts a file to a C/C++ #include containing a string 24 25import argparse 26import io 27import os 28 29 30def get_args(): 31 parser = argparse.ArgumentParser() 32 parser.add_argument('input', help="Name of input file") 33 parser.add_argument('output', help="Name of output file") 34 parser.add_argument("-n", "--name", 35 help="Name of C variable") 36 parser.add_argument("-b", "--binary", dest='binary', action='store_const', 37 const=True, default=False) 38 args = parser.parse_args() 39 return args 40 41 42def filename_to_C_identifier(n): 43 if n[0] != '_' and not n[0].isalpha(): 44 n = "_" + n[1:] 45 46 return "".join([c if c.isalnum() or c == "_" else "_" for c in n]) 47 48 49def emit_byte(f, b): 50 f.write("0x{:02x}, ".format(ord(b)).encode('utf-8')) 51 52 53def process_file(args): 54 with io.open(args.input, "rb") as infile: 55 try: 56 with io.open(args.output, "wb") as outfile: 57 # If a name was not specified on the command line, pick one based on the 58 # name of the input file. If no input filename was specified, use 59 # from_stdin. 60 if args.name is not None: 61 name = args.name 62 else: 63 name = filename_to_C_identifier(args.input) 64 65 outfile.write("static const char {}[] = {{\n".format(name).encode('utf-8')) 66 67 linecount = 0 68 while True: 69 byte = infile.read(1) 70 if byte == b"": 71 break 72 73 if not args.binary: 74 assert(ord(byte) != 0) 75 76 emit_byte(outfile, byte) 77 linecount = linecount + 1 78 if linecount > 20: 79 outfile.write(b"\n ") 80 linecount = 0 81 if not args.binary: 82 outfile.write(b"\n0") 83 outfile.write(b"\n};\n\n") 84 except Exception: 85 # In the event that anything goes wrong, delete the output file, 86 # then re-raise the exception. Deleteing the output file should 87 # ensure that the build system doesn't try to use the stale, 88 # half-generated file. 89 os.unlink(args.output) 90 raise 91 92 93def main(): 94 args = get_args() 95 process_file(args) 96 97 98if __name__ == "__main__": 99 main() 100