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 raise TypeError('Integer fields must be specified.') |
| 23 lines.append(' %s,' % content) |
| 24 |
| 25 def GenerateString(field_info, content, lines): |
| 26 if content is None: |
| 27 lines.append(' NULL,') |
| 28 else: |
| 29 # json.dumps quotes the string and escape characters as required. |
| 30 lines.append(' %s,' % json.dumps(content)) |
| 31 |
| 32 def GenerateString16(field_info, content, lines): |
| 33 if content is None: |
| 34 lines.append(' NULL,') |
| 35 else: |
| 36 # json.dumps quotes the string and escape characters as required. |
| 37 lines.append(' L%s,' % JSONStringLiteralToC(json.dumps(content))) |
| 38 |
| 39 def GenerateEnum(field_info, content, lines): |
| 40 if content is None: |
| 41 content = field_info['default'] |
| 42 lines.append(' %s,' % content) |
| 43 |
| 44 var_counter = 0 |
| 45 |
| 46 def GenerateArray(field_info, content, lines): |
| 47 global var_counter |
| 48 if content is None: |
| 49 lines.append(' NULL,') |
| 50 else: |
| 51 # Prepend an array |
| 52 var = 'temp_%s' % var_counter |
| 53 lines.append(' %s,' % var) |
| 54 var_counter += 1 |
| 55 array_lines = [] |
| 56 array_lines.append(struct_generator.GenerateField( |
| 57 var, field_info['contents']) + '[] = {') |
| 58 for subcontent in content: |
| 59 GenerateFieldContent(field_info['contents'], subcontent, array_lines) |
| 60 array_lines.append('};') |
| 61 lines.reverse() |
| 62 array_lines.reverse() |
| 63 lines.extend(array_lines) |
| 64 lines.reverse() |
| 65 |
| 66 content_generators = { |
| 67 "int": GenerateInt, |
| 68 "string": GenerateString, |
| 69 "string16": GenerateString16, |
| 70 "enum": GenerateEnum, |
| 71 "array": GenerateArray, |
| 72 } |
| 73 |
| 74 def GenerateFieldContent(field_info, content, lines): |
| 75 return content_generators[field_info['type']](field_info, content, lines) |
| 76 |
| 77 def GenerateElement(type_name, schema, element_name, element): |
| 78 lines = []; |
| 79 lines.append('const %s %s = {' % (type_name, element_name)); |
| 80 for field_info in schema: |
| 81 try: |
| 82 content = element[field_info['field']] |
| 83 except KeyError: |
| 84 content = None |
| 85 GenerateFieldContent(field_info, content, lines) |
| 86 lines.append('};') |
| 87 return '\n'.join(lines) |
| 88 |
| 89 def GenerateElements(type_name, schema, elements): |
| 90 result = []; |
| 91 for element_name, element in elements.items(): |
| 92 result.append(GenerateElement(type_name, schema, element_name, element)) |
| 93 result.append('') |
| 94 return '\n'.join(result) |
OLD | NEW |