Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Helper script to inline files as const char[] into a C header file. | |
| 7 | |
| 8 Example: | |
|
Jamie
2016/06/04 00:24:37
Make it clear here that a.vert and b.frag are inpu
Yuwei
2016/06/04 01:31:42
Done.
| |
| 9 | |
| 10 a.vert: | |
| 11 1" | |
| 12 2 | |
| 13 3\ | |
| 14 | |
| 15 b.frag: | |
| 16 4 | |
| 17 5 | |
| 18 6 | |
| 19 | |
| 20 remoting_c_text_inliner.py "OUTPUT_H_" "output.h" "a.vert" "b.frag": | |
| 21 #ifndef OUTPUT_H | |
| 22 #define OUTPUT_H | |
| 23 | |
| 24 const char kAVert[] = "1\"\n" | |
| 25 + "2\n" | |
|
Jamie
2016/06/04 00:24:37
You can't append string like this in C. Just omitt
Yuwei
2016/06/04 01:31:42
Done.
| |
| 26 + "3\\"; | |
| 27 | |
| 28 const char kBFrag[] = "4\n" | |
| 29 + "5\n" | |
| 30 + "6"; | |
| 31 | |
| 32 #endif // OUTPUT_H | |
| 33 | |
| 34 """ | |
| 35 | |
| 36 import os.path | |
| 37 import sys | |
| 38 | |
| 39 def escape_line(line): | |
| 40 # encode('string-escape') doesn't escape double quote so you need to manually | |
| 41 # escape it. | |
| 42 return line.encode('string-escape').replace('"', '\\"') | |
| 43 | |
| 44 def main(): | |
| 45 if len(sys.argv) < 4: | |
| 46 print 'Usage: remoting_c_text_inliner.py <include-guard> ' + \ | |
| 47 '<output-file> <input-files...>' | |
| 48 return 1 | |
| 49 | |
| 50 include_guard = sys.argv[1] | |
| 51 | |
| 52 output_path = sys.argv[2] | |
|
Jamie
2016/06/04 00:24:37
Don't need blank lines between these.
Yuwei
2016/06/04 01:31:42
Removed blank line between output_path and include
| |
| 53 | |
| 54 output_file = open(output_path, 'w') | |
| 55 | |
| 56 output_file.write('#ifndef ' + include_guard + '\n' + | |
| 57 '#define ' + include_guard + '\n\n') | |
| 58 | |
| 59 argc = len(sys.argv) | |
| 60 | |
| 61 for i in xrange(3, argc): | |
| 62 input_path = sys.argv[i] | |
| 63 input_file = open(input_path, 'r') | |
|
Jamie
2016/06/04 00:24:37
You can use the "with" keyword to automatically cl
Yuwei
2016/06/04 01:31:42
Done.
| |
| 64 | |
| 65 # hello_world.vert -> kHelloWorldVert | |
| 66 const_name = 'k' + os.path.basename(input_path).title().replace('_', '') \ | |
| 67 .replace('.', '') | |
|
Jamie
2016/06/04 00:24:37
Since you're losing path information when converti
Yuwei
2016/06/04 01:31:42
Done. Added checks.
| |
| 68 text = input_file.read() | |
| 69 lines = text.splitlines() | |
| 70 | |
| 71 # a, b -> "a, "b -> "a\n" + "b -> "a\n" + "b" | |
|
Jamie
2016/06/04 00:24:37
I don't understand this comment.
Yuwei
2016/06/04 01:31:42
Just trying to explain how the strings are concate
| |
| 72 inlined = '\\n"\n + '.join(map(lambda x: '"' + escape_line(x), lines)) + '"' | |
| 73 output_file.write('const char ' + const_name + '[]' + ' = ' + inlined + | |
| 74 ';\n\n') | |
| 75 input_file.close() | |
| 76 | |
| 77 output_file.write('#endif // ' + include_guard + '\n') | |
| 78 output_file.close() | |
| 79 | |
| 80 return 0 | |
| 81 | |
| 82 | |
| 83 if __name__ == '__main__': | |
| 84 sys.exit(main()) | |
| OLD | NEW |