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.
5 def _GenerateArrayField(field_info
):
6 """Generate a string defining an array field in a C structure.
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'];
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.
19 field
= field_info
['field']
20 type = field_info
['type']
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
28 return 'const %s %s' % (field_info
['ctype'], field
)
30 return _GenerateArrayField(field_info
)
32 raise RuntimeError('Unknown field type "%s"' % type)
34 def GenerateStruct(type_name
, schema
):
35 """Generate a string defining a structure containing the fields specified in
39 lines
.append('struct %s {' % type_name
)
40 for field_info
in schema
:
41 lines
.append(' ' + GenerateField(field_info
) + ';')
43 return '\n'.join(lines
) + '\n';