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 def _GenerateArrayField(field_info): |
| 6 """Generate a string defining an array field in a C structure. |
| 7 """ |
| 8 contents = field_info['contents'] |
| 9 contents['field'] = '* ' + field_info['field'] |
| 10 if contents['type'] == 'array': |
| 11 raise RuntimeError('Nested arrays are not supported.') |
| 12 return (GenerateField(contents) + ';\n' + |
| 13 ' const size_t %s_size') % field_info['field']; |
| 14 |
| 15 def GenerateField(field_info): |
| 16 """Generate a string defining a field of the type specified by |
| 17 field_info['type'] in a C structure. |
| 18 """ |
| 19 field = field_info['field'] |
| 20 type = field_info['type'] |
| 21 if type == 'int': |
| 22 return 'const int %s' % field |
| 23 elif type == 'string': |
| 24 return 'const char* const %s' % field |
| 25 elif type == 'string16': |
| 26 return 'const wchar_t* const %s' % field |
| 27 elif type == 'enum': |
| 28 return 'const %s %s' % (field_info['ctype'], field) |
| 29 elif type == 'array': |
| 30 return _GenerateArrayField(field_info) |
| 31 else: |
| 32 raise RuntimeError('Unknown field type "%s"' % type) |
| 33 |
| 34 def GenerateStruct(type_name, schema): |
| 35 """Generate a string defining a structure containing the fields specified in |
| 36 the schema list. |
| 37 """ |
| 38 lines = []; |
| 39 lines.append('struct %s {' % type_name) |
| 40 for field_info in schema: |
| 41 lines.append(' ' + GenerateField(field_info) + ';') |
| 42 lines.append('};'); |
| 43 return '\n'.join(lines) + '\n'; |
OLD | NEW |