OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import json |
| 6 import struct_generator |
| 7 |
| 8 def JSONStringLiteralToC(json_string_literal): |
| 9 c_string_literal = json_string_literal |
| 10 i = 0 |
| 11 while 1: |
| 12 i = json_string_literal.find('\\', i) |
| 13 if i == -1: break |
| 14 if json_string_literal[i + 1] == 'u': |
| 15 c_string_literal = json_string_literal[0:i + 1] + 'x' + \ |
| 16 json_string_literal[i + 2:] |
| 17 i += 2 |
| 18 return c_string_literal |
| 19 |
| 20 def GenerateInt(field_info, content, lines): |
| 21 if content is None: |
| 22 content = field_info['default'] |
| 23 lines.append(' %s,' % content) |
| 24 |
| 25 def GenerateString(field_info, content, lines): |
| 26 try: |
| 27 if content is None: |
| 28 content = field_info['default'] |
| 29 # json.dumps quotes the string and escape characters as required. |
| 30 lines.append(' %s,' % json.dumps(content)) |
| 31 except KeyError: |
| 32 lines.append(' NULL,') |
| 33 |
| 34 def GenerateString16(field_info, content, lines): |
| 35 try: |
| 36 if content is None: |
| 37 content = field_info['default'] |
| 38 # json.dumps quotes the string and escape characters as required. |
| 39 lines.append(' L%s,' % JSONStringLiteralToC(json.dumps(content))) |
| 40 except KeyError: |
| 41 lines.append(' NULL,') |
| 42 |
| 43 def GenerateEnum(field_info, content, lines): |
| 44 if content is None: |
| 45 content = field_info['default'] |
| 46 lines.append(' %s,' % content) |
| 47 |
| 48 var_counter = 0 |
| 49 |
| 50 def GenerateArray(field_info, content, lines): |
| 51 global var_counter |
| 52 if content is None: |
| 53 lines.append(' NULL,') |
| 54 else: |
| 55 # Prepend an array |
| 56 var = 'temp_%s' % var_counter |
| 57 lines.append(' %s,' % var) |
| 58 var_counter += 1 |
| 59 array_lines = [] |
| 60 array_lines.append(struct_generator.GenerateField( |
| 61 var, field_info['contents']) + '[] = {') |
| 62 for subcontent in content: |
| 63 GenerateFieldContent(field_info['contents'], subcontent, array_lines) |
| 64 array_lines.append('};') |
| 65 lines.reverse() |
| 66 array_lines.reverse() |
| 67 lines.extend(array_lines) |
| 68 lines.reverse() |
| 69 |
| 70 content_generators = { |
| 71 "int": GenerateInt, |
| 72 "string": GenerateString, |
| 73 "string16": GenerateString16, |
| 74 "enum": GenerateEnum, |
| 75 "array": GenerateArray, |
| 76 } |
| 77 |
| 78 def GenerateFieldContent(field_info, content, lines): |
| 79 return content_generators[field_info['type']](field_info, content, lines) |
| 80 |
| 81 def GenerateElement(type_name, schema, element_name, element): |
| 82 lines = []; |
| 83 lines.append('const %s %s = {' % (type_name, element_name)); |
| 84 for field_info in schema: |
| 85 try: |
| 86 content = element[field_info['field']] |
| 87 except KeyError: |
| 88 content = None |
| 89 GenerateFieldContent(field_info, content, lines) |
| 90 lines.append('};') |
| 91 return '\n'.join(lines) |
| 92 |
| 93 def GenerateElements(type_name, schema, content): |
| 94 result = []; |
| 95 result.append('const int kCurrentDataVersion = %s;' % content['version']) |
| 96 result.append('') |
| 97 for element_name, element in content['engines'].items(): |
| 98 result.append(GenerateElement(type_name, schema, element_name, element)) |
| 99 result.append('') |
| 100 return '\n'.join(result) |
OLD | NEW |