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 JSONToCString16(json_string_literal): | |
not at google - send to devlin
2012/11/12 18:38:13
Private functions prefix with _.
beaudoin
2012/11/13 18:44:26
I've also elected to make some other function priv
| |
9 """Converts a JSON string literal to a C++ UTF-16 string literal. This is | |
10 done by converting \\u#### to \\x####. | |
not at google - send to devlin
2012/11/12 18:38:13
Can you use a regex?
beaudoin
2012/11/13 18:44:26
I thought about it and could not find a way that w
| |
11 """ | |
12 c_string_literal = json_string_literal | |
13 i = 0 | |
14 while 1: | |
15 i = json_string_literal.find('\\', i) | |
16 if i == -1: break | |
17 if json_string_literal[i + 1] == 'u': | |
18 c_string_literal = (json_string_literal[0:i + 1] + 'x' + | |
19 json_string_literal[i + 2:]) | |
20 i += 2 | |
21 return c_string_literal | |
22 | |
23 def GenerateInt(field_info, content, lines): | |
not at google - send to devlin
2012/11/12 18:38:13
likewise, consider generating and returning Code o
beaudoin
2012/11/13 18:44:26
Thought about this too, but didn't want to reorgan
not at google - send to devlin
2012/11/13 20:28:07
Why do you need to reorganize the tools directory?
| |
24 """Generates an int to be included in a static structure initializer. If | |
25 content is not specified, generates the default int for this field. | |
26 """ | |
27 if content is None: | |
28 content = field_info['default'] | |
29 lines.append(' %s,' % content) | |
30 | |
31 def GenerateString(field_info, content, lines): | |
32 """Generates an UTF-8 string to be included in a static structure initializer. | |
33 If content is not specified, uses this field's default string or NULL if it | |
34 doesn't have any default. | |
35 """ | |
36 try: | |
37 if content is None: | |
38 content = field_info['default'] | |
39 # json.dumps quotes the string and escape characters as required. | |
40 lines.append(' %s,' % json.dumps(content)) | |
41 except KeyError: | |
42 lines.append(' NULL,') | |
43 | |
44 def GenerateString16(field_info, content, lines): | |
45 """Generates an UTF-16 string to be included in a static structure | |
46 initializer. If content is not specified, uses this field's default string or | |
47 NULL if it doesn't have any default. | |
48 """ | |
49 try: | |
50 if content is None: | |
51 content = field_info['default'] | |
52 # json.dumps quotes the string and escape characters as required. | |
53 lines.append(' L%s,' % JSONToCString16(json.dumps(content))) | |
54 except KeyError: | |
55 lines.append(' NULL,') | |
56 | |
57 def GenerateEnum(field_info, content, lines): | |
58 """Generates an enum to be included in a static structure initializer. If | |
59 content is not specified, generates the default enum for this field. | |
60 """ | |
61 if content is None: | |
62 content = field_info['default'] | |
63 lines.append(' %s,' % content) | |
64 | |
65 # A counter for the temporary variables containing generated static arrays. | |
66 var_counter = 0 | |
not at google - send to devlin
2012/11/12 18:38:13
If this is necessary, prefer to make the element g
beaudoin
2012/11/13 18:44:26
No longer necessary.
| |
67 | |
68 def GenerateArray(field_info, content, lines): | |
69 """Generates an array to be included in a static structure initializer. If | |
70 content is not specified, uses NULL. The array is assigned to a temporary | |
71 variable which is initilized before the structure. | |
72 """ | |
73 global var_counter | |
74 if content is None: | |
75 lines.append(' NULL,') | |
76 else: | |
77 # Create a new array variable and use it in the structure initializer. | |
78 var = 'temp_%s' % var_counter | |
not at google - send to devlin
2012/11/12 18:38:13
Can you use the name of the fields as a variable n
beaudoin
2012/11/13 18:44:26
Excellent idea. It forced me to disallow nested ar
| |
79 lines.append(' %s,' % var) | |
80 var_counter += 1 | |
81 # Generate the array content. | |
82 array_lines = [] | |
83 field_info['contents']['field'] = var; | |
84 array_lines.append(struct_generator.GenerateField( | |
85 field_info['contents']) + '[] = {') | |
86 for subcontent in content: | |
87 GenerateFieldContent(field_info['contents'], subcontent, array_lines) | |
88 array_lines.append('};') | |
89 # Prepend the generated array so it is initialized before the structure. | |
90 lines.reverse() | |
91 array_lines.reverse() | |
92 lines.extend(array_lines) | |
93 lines.reverse() | |
94 | |
95 # Map of supported types to generator functions. | |
96 content_generators = { | |
97 "int": GenerateInt, | |
98 "string": GenerateString, | |
99 "string16": GenerateString16, | |
100 "enum": GenerateEnum, | |
101 "array": GenerateArray, | |
102 } | |
103 | |
104 def GenerateFieldContent(field_info, content, lines): | |
105 """Generate the content of a field to be included in the static structure | |
106 initializer. | |
107 """ | |
108 return content_generators[field_info['type']](field_info, content, lines) | |
109 | |
110 def GenerateElement(type_name, schema, element_name, element): | |
111 """Generate the static structure initializer for one element. | |
112 """ | |
113 lines = []; | |
114 lines.append('const %s %s = {' % (type_name, element_name)); | |
115 for field_info in schema: | |
116 try: | |
117 content = element[field_info['field']] | |
not at google - send to devlin
2012/11/12 18:38:13
content = elemtent.get(field_info['field'], None)
beaudoin
2012/11/13 18:44:26
Done.
| |
118 except KeyError: | |
119 content = None | |
120 GenerateFieldContent(field_info, content, lines) | |
121 lines.append('};') | |
122 return '\n'.join(lines) | |
123 | |
124 def GenerateElements(type_name, schema, description): | |
125 """Generate the static structure initializer for all the elements in the | |
126 description['elements'] dictionary, as well as for any variables in | |
127 description['int_variables']. | |
128 """ | |
129 result = []; | |
130 try: | |
131 for var_name, value in description['int_variables'].items(): | |
132 result.append('const int %s = %s;' % (var_name, value)) | |
133 result.append('') | |
134 except KeyError: | |
135 pass | |
136 for element_name, element in description['elements'].items(): | |
137 result.append(GenerateElement(type_name, schema, element_name, element)) | |
138 result.append('') | |
139 return '\n'.join(result) | |
OLD | NEW |