Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Writes C++ header/cc source files for embedding resources into C++.""" | 5 """Writes C++ header/cc source files for embedding resources into C++.""" |
| 6 | 6 |
| 7 import datetime | 7 import datetime |
| 8 import os | 8 import os |
| 9 | 9 |
| 10 # 80-character max line length, minus 4 spaces, and 2 quotations. | |
| 11 MAX_LINE_LEN = 74 | |
| 12 | |
| 10 | 13 |
| 11 def WriteSource(base_name, | 14 def WriteSource(base_name, |
| 12 dir_from_src, | 15 dir_from_src, |
| 13 output_dir, | 16 output_dir, |
| 14 global_string_map): | 17 global_string_map): |
| 15 """Writes C++ header/cc source files for the given map of string variables. | 18 """Writes C++ header/cc source files for the given map of string variables. |
| 16 | 19 |
| 17 Args: | 20 Args: |
| 18 base_name: The basename of the file, without the extension. | 21 base_name: The basename of the file, without the extension. |
| 19 dir_from_src: Path from src to the directory that will contain the file, | 22 dir_from_src: Path from src to the directory that will contain the file, |
| 20 using forward slashes. | 23 using forward slashes. |
| 21 output_dir: Directory to output the sources to. | 24 output_dir: Directory to output the sources to. |
| 22 global_string_map: Map of variable names to their string values. These | 25 global_string_map: Map of variable names to their string values. These |
| 23 variables will be available as globals. | 26 variables will be available as globals. |
| 24 """ | 27 """ |
| 25 copyright = '\n'.join([ | 28 copyright = '\n'.join([ |
| 26 '// Copyright %s The Chromium Authors. All rights reserved.', | 29 '// Copyright %s The Chromium Authors. All rights reserved.', |
| 27 '// Use of this source code is governed by a BSD-style license that ' | 30 '// Use of this source code is governed by a BSD-style license that ' |
| 28 'can be', | 31 'can be', |
| 29 '// found in the LICENSE file.']) % datetime.date.today().year | 32 '// found in the LICENSE file.']) % datetime.date.today().year |
|
samuong
2014/12/11 00:22:19
Now that we're kicking off the generation of (some
srawlins
2014/12/12 23:41:28
Done.
| |
| 30 | 33 |
| 31 # Write header file. | 34 # Write header file. |
| 32 externs = [] | 35 externs = [] |
| 33 for name in global_string_map.iterkeys(): | 36 for name in global_string_map.iterkeys(): |
| 34 externs += ['extern const char %s[];' % name] | 37 externs += ['extern const char %s[];' % name] |
| 35 | 38 |
| 36 temp = '_'.join(dir_from_src.split('/') + [base_name]) | 39 temp = '_'.join(dir_from_src.split('/') + [base_name]) |
| 37 define = temp.upper() + '_H_' | 40 define = temp.upper() + '_H_' |
| 38 header = '\n'.join([ | 41 header = '\n'.join([ |
| 39 copyright, | 42 copyright, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 50 f.write(header) | 53 f.write(header) |
| 51 | 54 |
| 52 # Write cc file. | 55 # Write cc file. |
| 53 def EscapeLine(line): | 56 def EscapeLine(line): |
| 54 return line.replace('\\', '\\\\').replace('"', '\\"') | 57 return line.replace('\\', '\\\\').replace('"', '\\"') |
| 55 | 58 |
| 56 definitions = [] | 59 definitions = [] |
| 57 for name, contents in global_string_map.iteritems(): | 60 for name, contents in global_string_map.iteritems(): |
| 58 lines = [] | 61 lines = [] |
| 59 if '\n' not in contents: | 62 if '\n' not in contents: |
| 60 lines = [' "%s"' % EscapeLine(contents)] | 63 |
| 64 contents = EscapeLine(contents) | |
| 65 while len(contents) > MAX_LINE_LEN: | |
| 66 if contents[MAX_LINE_LEN-1] == '\\': | |
| 67 lines.append(' "%s"' % contents[:MAX_LINE_LEN-1]) | |
| 68 contents = contents[MAX_LINE_LEN-1:] | |
| 69 else: | |
| 70 lines.append(' "%s"' % contents[:MAX_LINE_LEN]) | |
| 71 contents = contents[MAX_LINE_LEN:] | |
| 72 | |
| 73 lines.append(' "%s"' % contents) | |
|
samuong
2014/12/11 00:22:19
Maybe an easier way to do this is to just pump eve
srawlins
2014/12/12 23:41:29
Done.
| |
| 61 else: | 74 else: |
| 62 for line in contents.split('\n'): | 75 for line in contents.split('\n'): |
| 63 lines += [' "%s\\n"' % EscapeLine(line)] | 76 lines += [' "%s\\n"' % EscapeLine(line)] |
| 64 definitions += ['const char %s[] =\n%s;' % (name, '\n'.join(lines))] | 77 definitions += ['const char %s[] =\n%s;' % (name, '\n'.join(lines))] |
| 65 | 78 |
| 66 cc = '\n'.join([ | 79 cc = '\n'.join([ |
| 67 copyright, | 80 copyright, |
| 68 '', | 81 '', |
| 69 '#include "%s"' % (dir_from_src + '/' + base_name + '.h'), | 82 '#include "%s"' % (dir_from_src + '/' + base_name + '.h'), |
| 70 '', | 83 '', |
| 71 '\n'.join(definitions)]) | 84 '\n'.join(definitions)]) |
| 72 cc += '\n' | 85 cc += '\n' |
| 73 | 86 |
| 74 with open(os.path.join(output_dir, base_name + '.cc'), 'w') as f: | 87 with open(os.path.join(output_dir, base_name + '.cc'), 'w') as f: |
| 75 f.write(cc) | 88 f.write(cc) |
| OLD | NEW |