Debian Codegraph @

build-aux/qmi-codegen/VariableString.py

raw
1 # -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 #
3 # This program is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17 # Copyright (C) 2012 Lanedo GmbH
18 # Copyright (C) 2012-2022 Aleksander Morgado <[email protected]>
19 # Copyright (c) 2022 Qualcomm Innovation Center, Inc.
20 #
21
22 import string
23 import utils
24 from Variable import Variable
25
26 """
27 Variable type for Strings ('string' format)
28 """
29 class VariableString(Variable):
30
31 def __init__(self, service, dictionary):
32
33 # Call the parent constructor
34 Variable.__init__(self, service, dictionary)
35
36 self.private_format = 'gchar *'
37 self.public_format = self.private_format
38 self.element_type = 'utf8'
39
40 # Public format for the GIR array compat methods in 1.32
41 self.private_format_gir = self.private_format
42 self.public_format_gir = self.public_format
43 self.element_type_gir = self.element_type
44
45 if 'fixed-size' in dictionary:
46 self.is_fixed_size = True
47 # Fixed-size strings
48 self.needs_dispose = False
49 self.length_prefix_size = 0
50 self.n_size_prefix_bytes = 0
51 self.fixed_size = dictionary['fixed-size']
52 self.max_size = ''
53 else:
54 self.is_fixed_size = False
55 self.fixed_size = '-1'
56 # Variable-length strings in heap
57 self.needs_dispose = True
58 self.clear_method = 'qmi_helpers_clear_string'
59 self.free_method_gir = 'g_free'
60 if 'size-prefix-format' in dictionary:
61 if dictionary['size-prefix-format'] == 'guint8':
62 self.length_prefix_size = 8
63 self.n_size_prefix_bytes = 1
64 elif dictionary['size-prefix-format'] == 'guint16':
65 self.length_prefix_size = 16
66 self.n_size_prefix_bytes = 2
67 else:
68 raise ValueError('Invalid size prefix format (%s): not guint8 or guint16' % dictionary['size-prefix-format'])
69 # Strings which are given as the full value of a TLV and which don't have
70 # a explicit 'size-prefix-format' will NOT have a length prefix
71 elif 'type' in dictionary and dictionary['type'] == 'TLV':
72 self.length_prefix_size = 0
73 self.n_size_prefix_bytes = 0
74 else:
75 # Default to UINT8
76 self.length_prefix_size = 8
77 self.n_size_prefix_bytes = 1
78 self.max_size = dictionary['max-size'] if 'max-size' in dictionary else ''
79
80
81 def emit_buffer_read(self, f, line_prefix, tlv_out, error, variable_name):
82 translations = { 'lp' : line_prefix,
83 'tlv_out' : tlv_out,
84 'variable_name' : variable_name,
85 'error' : error }
86
87 if self.is_fixed_size:
88 translations['fixed_size'] = self.fixed_size
89
90 # Fixed sized strings exposed in public fields will need to be
91 # explicitly allocated in heap
92 if self.public:
93 translations['fixed_size_plus_one'] = int(self.fixed_size) + 1
94 template = (
95 '${lp}${variable_name} = g_malloc (${fixed_size_plus_one});\n'
96 '${lp}if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &${variable_name}[0], ${error})) {\n'
97 '${lp} g_free (${variable_name});\n'
98 '${lp} ${variable_name} = NULL;\n'
99 '${lp} goto ${tlv_out};\n'
100 '${lp}}\n'
101 '${lp}${variable_name}[${fixed_size}] = \'\\0\';\n')
102 else:
103 template = (
104 '${lp}if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &${variable_name}[0], ${error}))\n'
105 '${lp} goto ${tlv_out};\n'
106 '${lp}${variable_name}[${fixed_size}] = \'\\0\';\n')
107 else:
108 translations['n_size_prefix_bytes'] = self.n_size_prefix_bytes
109 translations['max_size'] = self.max_size if self.max_size != '' else '0'
110 template = (
111 '${lp}if (!qmi_message_tlv_read_string (message, init_offset, &offset, ${n_size_prefix_bytes}, ${max_size}, &(${variable_name}), ${error}))\n'
112 '${lp} goto ${tlv_out};\n')
113 f.write(string.Template(template).substitute(translations))
114
115
116 def emit_buffer_write(self, f, line_prefix, tlv_name, variable_name):
117 translations = { 'lp' : line_prefix,
118 'tlv_name' : tlv_name,
119 'variable_name' : variable_name,
120 'fixed_size' : self.fixed_size,
121 'n_size_prefix_bytes' : self.n_size_prefix_bytes }
122
123 template = (
124 '${lp}if (!qmi_message_tlv_write_string (self, ${n_size_prefix_bytes}, ${variable_name}, ${fixed_size}, error)) {\n'
125 '${lp} g_prefix_error (error, "Cannot write string in TLV \'${tlv_name}\': ");\n'
126 '${lp} return NULL;\n'
127 '${lp}}\n')
128
129 f.write(string.Template(template).substitute(translations))
130
131
132 def emit_get_printable(self, f, line_prefix, is_personal):
133 translations = { 'lp' : line_prefix }
134
135 if self.is_fixed_size:
136 translations['fixed_size'] = self.fixed_size
137 translations['fixed_size_plus_one'] = int(self.fixed_size) + 1
138 template = (
139 '\n'
140 '${lp}{\n'
141 '${lp} gchar tmp[${fixed_size_plus_one}] = { \'\\0\' };\n'
142 '\n'
143 '${lp} if (!qmi_message_tlv_read_fixed_size_string (message, init_offset, &offset, ${fixed_size}, &tmp[0], &error))\n'
144 '${lp} goto out;\n')
145 else:
146 translations['n_size_prefix_bytes'] = self.n_size_prefix_bytes
147 translations['max_size'] = self.max_size if self.max_size != '' else '0'
148 template = (
149 '\n'
150 '${lp}{\n'
151 '${lp} g_autofree gchar *tmp = NULL;\n'
152 '\n'
153 '${lp} if (!qmi_message_tlv_read_string (message, init_offset, &offset, ${n_size_prefix_bytes}, ${max_size}, &tmp, &error))\n'
154 '${lp} goto out;\n')
155
156 if self.personal_info or is_personal:
157 translations['if_show_field'] = 'if (qmi_utils_get_show_personal_info ()) '
158 else:
159 translations['if_show_field'] = ''
160
161 template += (
162 '${lp} ${if_show_field}{\n'
163 '${lp} g_string_append (printable, tmp);\n')
164
165 if self.personal_info or is_personal:
166 template += (
167 '${lp} } else {\n'
168 '${lp} g_string_append_printf (printable, "\'###\'");\n')
169
170 template += (
171 '${lp} }\n'
172 '${lp}}\n')
173
174 f.write(string.Template(template).substitute(translations))
175
176
177 def build_variable_declaration(self, line_prefix, variable_name):
178 translations = { 'lp' : line_prefix,
179 'name' : variable_name }
180
181 if self.is_fixed_size:
182 translations['fixed_size_plus_one'] = int(self.fixed_size) + 1
183 template = (
184 '${lp}gchar ${name}[${fixed_size_plus_one}];\n')
185 else:
186 template = (
187 '${lp}gchar *${name};\n')
188 return string.Template(template).substitute(translations)
189
190
191 def build_struct_field_declaration(self, line_prefix, variable_name):
192 translations = { 'lp' : line_prefix,
193 'name' : variable_name }
194
195 # Fixed sized strings given in public structs are given as pointers,
196 # instead of as fixed-sized arrays directly in the struct.
197 template = (
198 '${lp}gchar *${name};\n')
199 return string.Template(template).substitute(translations)
200
201
202 def build_getter_declaration(self, line_prefix, variable_name):
203 if not self.visible:
204 return ""
205
206 translations = { 'lp' : line_prefix,
207 'name' : variable_name }
208
209 template = (
210 '${lp}const gchar **${name},\n')
211 return string.Template(template).substitute(translations)
212
213
214 def build_getter_documentation(self, line_prefix, variable_name):
215 if not self.visible:
216 return ""
217
218 translations = { 'lp' : line_prefix,
219 'name' : variable_name }
220
221 template = (
222 '${lp}@${name}: (out)(optional)(transfer none): a placeholder for the output constant string, or %NULL if not required.\n')
223 return string.Template(template).substitute(translations)
224
225
226 def build_getter_implementation(self, line_prefix, variable_name_from, variable_name_to):
227 if not self.visible:
228 return ""
229
230 translations = { 'lp' : line_prefix,
231 'from' : variable_name_from,
232 'to' : variable_name_to }
233
234 template = (
235 '${lp}if (${to})\n'
236 '${lp} *${to} = ${from};\n')
237 return string.Template(template).substitute(translations)
238
239
240 def build_setter_declaration(self, line_prefix, variable_name):
241 if not self.visible:
242 return ""
243
244 translations = { 'lp' : line_prefix,
245 'name' : variable_name }
246
247 template = (
248 '${lp}const gchar *${name},\n')
249 return string.Template(template).substitute(translations)
250
251
252 def build_setter_documentation(self, line_prefix, variable_name):
253 if not self.visible:
254 return ""
255
256 translations = { 'lp' : line_prefix,
257 'name' : variable_name }
258
259 if self.is_fixed_size:
260 translations['fixed_size'] = self.fixed_size
261 template = (
262 '${lp}@${name}: a constant string of exactly ${fixed_size} characters.\n')
263 elif self.max_size != '':
264 translations['max_size'] = self.max_size
265 template = (
266 '${lp}@${name}: a constant string with a maximum length of ${max_size} characters.\n')
267 else:
268 template = (
269 '${lp}@${name}: a constant string.\n')
270 return string.Template(template).substitute(translations)
271
272
273 def build_setter_implementation(self, line_prefix, variable_name_from, variable_name_to):
274 if not self.visible:
275 return ""
276
277 translations = { 'lp' : line_prefix,
278 'from' : variable_name_from,
279 'to' : variable_name_to }
280
281 if self.is_fixed_size:
282 translations['fixed_size'] = self.fixed_size
283 template = (
284 '${lp}if (!${from} || strlen (${from}) != ${fixed_size}) {\n'
285 '${lp} g_set_error (error,\n'
286 '${lp} QMI_CORE_ERROR,\n'
287 '${lp} QMI_CORE_ERROR_INVALID_ARGS,\n'
288 '${lp} "Input variable \'${from}\' must be ${fixed_size} characters long");\n'
289 '${lp} return FALSE;\n'
290 '${lp}}\n'
291 '${lp}memcpy (${to}, ${from}, ${fixed_size});\n'
292 '${lp}${to}[${fixed_size}] = \'\\0\';\n')
293 else:
294 template = ''
295 if self.max_size != '':
296 translations['max_size'] = self.max_size
297 template += (
298 '${lp}if (${from} && strlen (${from}) > ${max_size}) {\n'
299 '${lp} g_set_error (error,\n'
300 '${lp} QMI_CORE_ERROR,\n'
301 '${lp} QMI_CORE_ERROR_INVALID_ARGS,\n'
302 '${lp} "Input variable \'${from}\' must be less than ${max_size} characters long");\n'
303 '${lp} return FALSE;\n'
304 '${lp}}\n')
305 template += (
306 '${lp}g_free (${to});\n'
307 '${lp}${to} = g_strdup (${from} ? ${from} : "");\n')
308
309 return string.Template(template).substitute(translations)
310
311
312 def build_struct_field_documentation(self, line_prefix, variable_name):
313 translations = { 'lp' : line_prefix,
314 'name' : variable_name }
315
316 if self.is_fixed_size:
317 translations['fixed_size'] = self.fixed_size
318 template = (
319 '${lp}@${name}: a string of exactly ${fixed_size} characters.\n')
320 elif self.max_size != '':
321 translations['max_size'] = self.max_size
322 template = (
323 '${lp}@${name}: a string with a maximum length of ${max_size} characters.\n')
324 else:
325 template = (
326 '${lp}@${name}: a string.\n')
327 return string.Template(template).substitute(translations)
328
329
330 def build_dispose(self, line_prefix, variable_name):
331 # Fixed-size strings don't need dispose
332 if self.is_fixed_size and not self.public:
333 return ''
334
335 translations = { 'lp' : line_prefix,
336 'variable_name' : variable_name }
337
338 template = (
339 '${lp}g_clear_pointer (&${variable_name}, (GDestroyNotify)g_free);\n')
340 return string.Template(template).substitute(translations)
341
342
343 def build_copy_gir(self, line_prefix, variable_name_from, variable_name_to):
344 translations = { 'lp' : line_prefix,
345 'variable_name_from' : variable_name_from,
346 'variable_name_to' : variable_name_to }
347
348 template = (
349 '${lp}${variable_name_to} = g_strdup (${variable_name_from});\n')
350 return string.Template(template).substitute(translations)
351
352
353 def build_copy_to_gir(self, line_prefix, variable_name_from, variable_name_to):
354 return self.build_copy_gir (line_prefix, variable_name_from, variable_name_to)
355
356
357 def build_copy_from_gir(self, line_prefix, variable_name_from, variable_name_to):
358 return self.build_copy_gir (line_prefix, variable_name_from, variable_name_to)
359
360
361 def flag_public(self):
362 # Call the parent method
363 Variable.flag_public(self)
364 # Fixed-sized strings will need dispose if they are in the public header
365 if self.is_fixed_size:
366 self.needs_dispose = True