1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 | """
|
4 | Generate C++ strings that can be used by pyutil._ResourceLoader
|
5 |
|
6 | Used for
|
7 |
|
8 | - _devbuild/help/
|
9 | - stdlib/
|
10 | """
|
11 |
|
12 | import sys
|
13 | from mycpp.mylib import log
|
14 |
|
15 |
|
16 | def main(argv):
|
17 | paths = argv[1:]
|
18 |
|
19 | out_f = sys.stdout
|
20 |
|
21 | # Invoked with _devbuild/help/* stdlib/*.ysh
|
22 |
|
23 | #log('paths %s', paths)
|
24 |
|
25 | out_f.write('''
|
26 | #include "cpp/embedded_file.h"
|
27 |
|
28 | namespace embedded_file {
|
29 | ''')
|
30 |
|
31 | # Write global strings
|
32 | for i, rel_path in enumerate(paths):
|
33 | with open(rel_path) as f:
|
34 | contents = f.read()
|
35 |
|
36 | # zZXx is a unique string that shouldn't appear in any file
|
37 | out_f.write('GLOBAL_STR(gStr%d, R"zZXx(%s)zZXx");\n\n' % (i, contents))
|
38 |
|
39 | out_f.write('''
|
40 |
|
41 | TextFile array[] = {
|
42 | ''')
|
43 |
|
44 | # Write global array entries
|
45 | for i, rel_path in enumerate(paths):
|
46 | out_f.write(' {.rel_path = "%s", .contents = gStr%d},\n' % (rel_path, i))
|
47 |
|
48 | out_f.write('''
|
49 | {.rel_path = nullptr, .contents = nullptr},
|
50 | };
|
51 |
|
52 | } // namespace embedded_file
|
53 |
|
54 | TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer
|
55 | ''')
|
56 |
|
57 |
|
58 | if __name__ == '__main__':
|
59 | try:
|
60 | main(sys.argv)
|
61 | except RuntimeError as e:
|
62 | print('FATAL: %s' % e, file=sys.stderr)
|
63 | sys.exit(1)
|
64 |
|
65 |
|
66 | # vim: sw=2
|