Aleksander Morgado | 2a511da | 2012-05-30 12:40:46 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 3 | # |
| 4 | # This program is free software; you can redistribute it and/or modify it under |
| 5 | # the terms of the GNU Lesser General Public License as published by the Free |
| 6 | # Software Foundation; either version 2 of the License, or (at your option) any |
| 7 | # later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 12 | # details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU Lesser General Public License along |
| 15 | # with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 16 | # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | # |
| 18 | # Copyright (C) 2012 Lanedo GmbH |
| 19 | # |
| 20 | |
| 21 | import string |
| 22 | import utils |
| 23 | from Variable import Variable |
| 24 | |
| 25 | """ |
| 26 | Variable type for Strings ('string' format) |
| 27 | """ |
| 28 | class VariableString(Variable): |
| 29 | |
| 30 | """ |
| 31 | Constructor |
| 32 | """ |
| 33 | def __init__(self, dictionary): |
| 34 | |
| 35 | # Call the parent constructor |
| 36 | Variable.__init__(self, dictionary) |
| 37 | |
| 38 | self.private_format = 'gchar *' |
| 39 | self.public_format = self.private_format |
| 40 | |
| 41 | # Strings will be heap-allocated |
| 42 | self.needs_dispose = True |
| 43 | |
| 44 | # We want to get the strings be passed as 'const' |
| 45 | self.pass_constant = True |
| 46 | |
| 47 | # Strings which are given as the full value of a TLV will NOT have a |
| 48 | # length prefix |
| 49 | self.length_prefix = False if 'type' in dictionary and dictionary['type'] == 'TLV' else True |
| 50 | |
| 51 | |
| 52 | """ |
| 53 | Read a string from the raw byte buffer. |
| 54 | """ |
| 55 | def emit_buffer_read(self, f, line_prefix, variable_name, buffer_name, buffer_len): |
| 56 | translations = { 'lp' : line_prefix, |
| 57 | 'variable_name' : variable_name, |
| 58 | 'buffer_name' : buffer_name, |
| 59 | 'buffer_len' : buffer_len, |
| 60 | 'length_prefix' : 'TRUE' if self.length_prefix else 'FALSE' } |
| 61 | |
| 62 | template = ( |
| 63 | '${lp}/* Read the string variable from the buffer */\n' |
| 64 | '${lp}qmi_utils_read_string_from_buffer (\n' |
| 65 | '${lp} &${buffer_name},\n' |
| 66 | '${lp} &${buffer_len},\n' |
| 67 | '${lp} ${length_prefix},\n' |
| 68 | '${lp} &(${variable_name}));\n') |
| 69 | f.write(string.Template(template).substitute(translations)) |
| 70 | |
| 71 | |
| 72 | """ |
| 73 | Write a string to the raw byte buffer. |
| 74 | """ |
| 75 | def emit_buffer_write(self, f, line_prefix, variable_name, buffer_name, buffer_len): |
| 76 | translations = { 'lp' : line_prefix, |
| 77 | 'variable_name' : variable_name, |
| 78 | 'buffer_name' : buffer_name, |
| 79 | 'buffer_len' : buffer_len, |
| 80 | 'length_prefix' : 'TRUE' if self.length_prefix else 'FALSE' } |
| 81 | |
| 82 | template = ( |
| 83 | '${lp}/* Write the string variable to the buffer */\n' |
| 84 | '${lp}qmi_utils_write_string_to_buffer (\n' |
| 85 | '${lp} &${buffer_name},\n' |
| 86 | '${lp} &${buffer_len},\n' |
| 87 | '${lp} ${length_prefix},\n' |
| 88 | '${lp} &(${variable_name}));\n') |
| 89 | f.write(string.Template(template).substitute(translations)) |
| 90 | |
| 91 | |
| 92 | """ |
| 93 | Get the string as printable |
| 94 | """ |
| 95 | def emit_get_printable(self, f, line_prefix, printable, buffer_name, buffer_len): |
| 96 | translations = { 'lp' : line_prefix, |
| 97 | 'printable' : printable, |
| 98 | 'buffer_name' : buffer_name, |
| 99 | 'buffer_len' : buffer_len, |
| 100 | 'length_prefix' : 'TRUE' if self.length_prefix else 'FALSE' } |
| 101 | |
| 102 | template = ( |
| 103 | '\n' |
| 104 | '${lp}{\n' |
| 105 | '${lp} gchar *tmp;\n' |
| 106 | '\n' |
| 107 | '${lp} /* Read the string variable from the buffer */\n' |
| 108 | '${lp} qmi_utils_read_string_from_buffer (\n' |
| 109 | '${lp} &${buffer_name},\n' |
| 110 | '${lp} &${buffer_len},\n' |
| 111 | '${lp} ${length_prefix},\n' |
| 112 | '${lp} &tmp);\n' |
| 113 | '\n' |
| 114 | '${lp} g_string_append_printf (${printable}, "%s", tmp);\n' |
| 115 | '${lp} g_free (tmp);\n' |
| 116 | '${lp}}\n') |
| 117 | f.write(string.Template(template).substitute(translations)) |
| 118 | |
| 119 | |
| 120 | """ |
| 121 | Copy the string |
| 122 | """ |
| 123 | def emit_copy(self, f, line_prefix, variable_name_from, variable_name_to): |
| 124 | translations = { 'lp' : line_prefix, |
| 125 | 'from' : variable_name_from, |
| 126 | 'to' : variable_name_to } |
| 127 | |
| 128 | template = ( |
| 129 | '${lp}${to} = g_strdup (${from});\n') |
| 130 | f.write(string.Template(template).substitute(translations)) |
| 131 | |
| 132 | |
| 133 | """ |
| 134 | Dispose the string |
| 135 | """ |
| 136 | def emit_dispose(self, f, line_prefix, variable_name): |
| 137 | translations = { 'lp' : line_prefix, |
| 138 | 'variable_name' : variable_name } |
| 139 | |
| 140 | template = ( |
| 141 | '${lp}g_free (${variable_name});\n') |
| 142 | f.write(string.Template(template).substitute(translations)) |