| Index: tools/json_to_struct/element_generator.py
|
| diff --git a/tools/json_to_struct/element_generator.py b/tools/json_to_struct/element_generator.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..1410a7d4f1cac2ed6045d1270abf637145ef6e31
|
| --- /dev/null
|
| +++ b/tools/json_to_struct/element_generator.py
|
| @@ -0,0 +1,94 @@
|
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +# Use of this source code is governed by a BSD-style license that can be
|
| +# found in the LICENSE file.
|
| +
|
| +import json
|
| +import struct_generator
|
| +
|
| +def JSONStringLiteralToC(json_string_literal):
|
| + c_string_literal = json_string_literal
|
| + i = 0
|
| + while 1:
|
| + i = json_string_literal.find('\\', i)
|
| + if i == -1: break
|
| + if json_string_literal[i + 1] == 'u':
|
| + c_string_literal = json_string_literal[0:i + 1] + 'x' + \
|
| + json_string_literal[i + 2:]
|
| + i += 2
|
| + return c_string_literal
|
| +
|
| +def GenerateInt(field_info, content, lines):
|
| + if content is None:
|
| + raise TypeError('Integer fields must be specified.')
|
| + lines.append(' %s,' % content)
|
| +
|
| +def GenerateString(field_info, content, lines):
|
| + if content is None:
|
| + lines.append(' NULL,')
|
| + else:
|
| + # json.dumps quotes the string and escape characters as required.
|
| + lines.append(' %s,' % json.dumps(content))
|
| +
|
| +def GenerateString16(field_info, content, lines):
|
| + if content is None:
|
| + lines.append(' NULL,')
|
| + else:
|
| + # json.dumps quotes the string and escape characters as required.
|
| + lines.append(' L%s,' % JSONStringLiteralToC(json.dumps(content)))
|
| +
|
| +def GenerateEnum(field_info, content, lines):
|
| + if content is None:
|
| + content = field_info['default']
|
| + lines.append(' %s,' % content)
|
| +
|
| +var_counter = 0
|
| +
|
| +def GenerateArray(field_info, content, lines):
|
| + global var_counter
|
| + if content is None:
|
| + lines.append(' NULL,')
|
| + else:
|
| + # Prepend an array
|
| + var = 'temp_%s' % var_counter
|
| + lines.append(' %s,' % var)
|
| + var_counter += 1
|
| + array_lines = []
|
| + array_lines.append(struct_generator.GenerateField(
|
| + var, field_info['contents']) + '[] = {')
|
| + for subcontent in content:
|
| + GenerateFieldContent(field_info['contents'], subcontent, array_lines)
|
| + array_lines.append('};')
|
| + lines.reverse()
|
| + array_lines.reverse()
|
| + lines.extend(array_lines)
|
| + lines.reverse()
|
| +
|
| +content_generators = {
|
| + "int": GenerateInt,
|
| + "string": GenerateString,
|
| + "string16": GenerateString16,
|
| + "enum": GenerateEnum,
|
| + "array": GenerateArray,
|
| +}
|
| +
|
| +def GenerateFieldContent(field_info, content, lines):
|
| + return content_generators[field_info['type']](field_info, content, lines)
|
| +
|
| +def GenerateElement(type_name, schema, element_name, element):
|
| + lines = [];
|
| + lines.append('const %s %s = {' % (type_name, element_name));
|
| + for field_info in schema:
|
| + try:
|
| + content = element[field_info['field']]
|
| + except KeyError:
|
| + content = None
|
| + GenerateFieldContent(field_info, content, lines)
|
| + lines.append('};')
|
| + return '\n'.join(lines)
|
| +
|
| +def GenerateElements(type_name, schema, elements):
|
| + result = [];
|
| + for element_name, element in elements.items():
|
| + result.append(GenerateElement(type_name, schema, element_name, element))
|
| + result.append('')
|
| + return '\n'.join(result)
|
|
|