blob: 901b01473cb0f028f0a803e12caca5e430dc9587 [file] [log] [blame]
Austin Schuh24adb6b2015-09-06 17:37:40 -07001#!/usr/bin/env python
2
Austin Schuh9d823002019-04-14 12:53:17 -07003import os, os.path, sys, argparse
Austin Schuh24adb6b2015-09-06 17:37:40 -07004
Austin Schuh9d823002019-04-14 12:53:17 -07005SOURCE_TEMPLATE = """
Austin Schuh24adb6b2015-09-06 17:37:40 -07006#include "internal/Embedded.h"
7
8#include <string>
9#include <unordered_map>
10
11namespace {
Austin Schuh9d823002019-04-14 12:53:17 -070012%s
Austin Schuh24adb6b2015-09-06 17:37:40 -070013
Austin Schuh9d823002019-04-14 12:53:17 -070014 const std::unordered_map<std::string, EmbeddedContent> embedded = {
15%s
16 };
Austin Schuh24adb6b2015-09-06 17:37:40 -070017
18} // namespace
19
20const EmbeddedContent* findEmbeddedContent(const std::string& name) {
Austin Schuh9d823002019-04-14 12:53:17 -070021 const auto found = embedded.find(name);
22 if (found == embedded.end()) {
23 return nullptr;
24 }
25 return &found->second;
26}\n
Austin Schuh24adb6b2015-09-06 17:37:40 -070027"""
28
Austin Schuh9d823002019-04-14 12:53:17 -070029MAX_SLICE = 70
30
31
32def as_byte(data):
33 if sys.version_info < (3,):
34 return ord(data)
35 else:
36 return data
37
38
39def parse_arguments():
40 parser = argparse.ArgumentParser(description="Embedded content generator")
41 parser.add_argument('--output', '-o', action='store', dest='output_file', type=str, help='Output File', required=True)
42 parser.add_argument('--file', '-f', action='store', nargs='+', dest='input_file', type=str, help='Output File', required=True)
43 return parser.parse_args()
44
45
46def create_file_byte(name, file_bytes):
47 output = []
48 output.append(' const char %s[] = {' % name)
49
50 for start in range(0, len(file_bytes), MAX_SLICE):
51 output.append('' + "".join(["'\\x%02x'," % as_byte(x) for x in file_bytes[start:start+MAX_SLICE]]) + "\n")
52 output.append('0};\n')
53 return ''.join(output)
54
55
56def create_file_info(file_list):
57 output = []
58 for name, base, length in file_list:
59 output.append(' {"/%s", { %s, %d }},\n' % (base, name, length))
60 return ''.join(output)
61
62
63def main():
64 args = parse_arguments()
65
66 files = []
67 index = 1
68 file_byte_entries = []
69
70 for file_name in args.input_file:
71 with open(file_name, 'rb') as f:
72 file_bytes = f.read()
73 name = "fileData%d" % index
74 index += 1
75 files.append((name, os.path.basename(file_name), len(file_bytes)))
76 file_byte_entries.append(create_file_byte(name, file_bytes))
77
78 with open(args.output_file, 'w') as output_file:
79 output_file.write(SOURCE_TEMPLATE % (''.join(file_byte_entries), create_file_info(files)))
80
81
82if __name__ == '__main__':
83 main()