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 | |
Aleksander Morgado | 2a511da | 2012-05-30 12:40:46 +0200 | [diff] [blame] | 44 | # Strings which are given as the full value of a TLV will NOT have a |
| 45 | # length prefix |
| 46 | self.length_prefix = False if 'type' in dictionary and dictionary['type'] == 'TLV' else True |
| 47 | |
| 48 | |
| 49 | """ |
| 50 | Read a string from the raw byte buffer. |
| 51 | """ |
| 52 | def emit_buffer_read(self, f, line_prefix, variable_name, buffer_name, buffer_len): |
| 53 | translations = { 'lp' : line_prefix, |
| 54 | 'variable_name' : variable_name, |
| 55 | 'buffer_name' : buffer_name, |
| 56 | 'buffer_len' : buffer_len, |
| 57 | 'length_prefix' : 'TRUE' if self.length_prefix else 'FALSE' } |
| 58 | |
| 59 | template = ( |
| 60 | '${lp}/* Read the string variable from the buffer */\n' |
| 61 | '${lp}qmi_utils_read_string_from_buffer (\n' |
| 62 | '${lp} &${buffer_name},\n' |
| 63 | '${lp} &${buffer_len},\n' |
| 64 | '${lp} ${length_prefix},\n' |
| 65 | '${lp} &(${variable_name}));\n') |
| 66 | f.write(string.Template(template).substitute(translations)) |
| 67 | |
| 68 | |
| 69 | """ |
| 70 | Write a string to the raw byte buffer. |
| 71 | """ |
| 72 | def emit_buffer_write(self, f, line_prefix, variable_name, buffer_name, buffer_len): |
| 73 | translations = { 'lp' : line_prefix, |
| 74 | 'variable_name' : variable_name, |
| 75 | 'buffer_name' : buffer_name, |
| 76 | 'buffer_len' : buffer_len, |
| 77 | 'length_prefix' : 'TRUE' if self.length_prefix else 'FALSE' } |
| 78 | |
| 79 | template = ( |
| 80 | '${lp}/* Write the string variable to the buffer */\n' |
| 81 | '${lp}qmi_utils_write_string_to_buffer (\n' |
| 82 | '${lp} &${buffer_name},\n' |
| 83 | '${lp} &${buffer_len},\n' |
| 84 | '${lp} ${length_prefix},\n' |
| 85 | '${lp} &(${variable_name}));\n') |
| 86 | f.write(string.Template(template).substitute(translations)) |
| 87 | |
| 88 | |
| 89 | """ |
| 90 | Get the string as printable |
| 91 | """ |
| 92 | def emit_get_printable(self, f, line_prefix, printable, buffer_name, buffer_len): |
| 93 | translations = { 'lp' : line_prefix, |
| 94 | 'printable' : printable, |
| 95 | 'buffer_name' : buffer_name, |
| 96 | 'buffer_len' : buffer_len, |
| 97 | 'length_prefix' : 'TRUE' if self.length_prefix else 'FALSE' } |
| 98 | |
| 99 | template = ( |
| 100 | '\n' |
| 101 | '${lp}{\n' |
| 102 | '${lp} gchar *tmp;\n' |
| 103 | '\n' |
| 104 | '${lp} /* Read the string variable from the buffer */\n' |
| 105 | '${lp} qmi_utils_read_string_from_buffer (\n' |
| 106 | '${lp} &${buffer_name},\n' |
| 107 | '${lp} &${buffer_len},\n' |
| 108 | '${lp} ${length_prefix},\n' |
| 109 | '${lp} &tmp);\n' |
| 110 | '\n' |
| 111 | '${lp} g_string_append_printf (${printable}, "%s", tmp);\n' |
| 112 | '${lp} g_free (tmp);\n' |
| 113 | '${lp}}\n') |
| 114 | f.write(string.Template(template).substitute(translations)) |
| 115 | |
| 116 | |
| 117 | """ |
Aleksander Morgado | 4130a72 | 2012-07-03 13:58:52 +0200 | [diff] [blame] | 118 | Variable declaration |
Aleksander Morgado | 2a511da | 2012-05-30 12:40:46 +0200 | [diff] [blame] | 119 | """ |
Aleksander Morgado | 4130a72 | 2012-07-03 13:58:52 +0200 | [diff] [blame] | 120 | def build_variable_declaration(self, line_prefix, variable_name): |
| 121 | translations = { 'lp' : line_prefix, |
| 122 | 'name' : variable_name } |
| 123 | |
| 124 | template = ( |
| 125 | '${lp}gchar *${name};\n') |
| 126 | return string.Template(template).substitute(translations) |
| 127 | |
| 128 | |
| 129 | """ |
| 130 | Getter for the string type |
| 131 | """ |
| 132 | def build_getter_declaration(self, line_prefix, variable_name): |
| 133 | translations = { 'lp' : line_prefix, |
| 134 | 'name' : variable_name } |
| 135 | |
| 136 | template = ( |
| 137 | '${lp}const gchar **${name},\n') |
| 138 | return string.Template(template).substitute(translations) |
| 139 | |
| 140 | |
| 141 | """ |
| 142 | Documentation for the getter |
| 143 | """ |
| 144 | def build_getter_documentation(self, line_prefix, variable_name): |
| 145 | translations = { 'lp' : line_prefix, |
| 146 | 'name' : variable_name } |
| 147 | |
| 148 | template = ( |
| 149 | '${lp}@${name}: a placeholder for the output constant string, or #NULL if not required.\n') |
| 150 | return string.Template(template).substitute(translations) |
| 151 | |
| 152 | |
| 153 | """ |
| 154 | Builds the String getter implementation |
| 155 | """ |
| 156 | def build_getter_implementation(self, line_prefix, variable_name_from, variable_name_to, to_is_reference): |
| 157 | translations = { 'lp' : line_prefix, |
| 158 | 'from' : variable_name_from, |
| 159 | 'to' : variable_name_to } |
| 160 | |
| 161 | if to_is_reference: |
| 162 | template = ( |
| 163 | '${lp}if (${to})\n' |
| 164 | '${lp} *${to} = ${from};\n') |
| 165 | return string.Template(template).substitute(translations) |
| 166 | else: |
| 167 | template = ( |
| 168 | '${lp}${to} = ${from};\n') |
| 169 | return string.Template(template).substitute(translations) |
| 170 | |
| 171 | |
| 172 | """ |
| 173 | Setter for the string type |
| 174 | """ |
| 175 | def build_setter_declaration(self, line_prefix, variable_name): |
| 176 | translations = { 'lp' : line_prefix, |
| 177 | 'name' : variable_name } |
| 178 | |
| 179 | template = ( |
| 180 | '${lp}const gchar *${name},\n') |
| 181 | return string.Template(template).substitute(translations) |
| 182 | |
| 183 | |
| 184 | """ |
| 185 | Documentation for the setter |
| 186 | """ |
| 187 | def build_setter_documentation(self, line_prefix, variable_name): |
| 188 | translations = { 'lp' : line_prefix, |
| 189 | 'name' : variable_name } |
| 190 | |
| 191 | template = ( |
| 192 | '${lp}@${name}: a placeholder for the output constant string, or #NULL if not required.\n') |
| 193 | return string.Template(template).substitute(translations) |
| 194 | |
| 195 | |
| 196 | """ |
| 197 | Builds the String setter implementation |
| 198 | """ |
| 199 | def build_setter_implementation(self, line_prefix, variable_name_from, variable_name_to): |
Aleksander Morgado | 2a511da | 2012-05-30 12:40:46 +0200 | [diff] [blame] | 200 | translations = { 'lp' : line_prefix, |
| 201 | 'from' : variable_name_from, |
| 202 | 'to' : variable_name_to } |
| 203 | |
| 204 | template = ( |
Aleksander Morgado | 4130a72 | 2012-07-03 13:58:52 +0200 | [diff] [blame] | 205 | '${lp}g_free (${to});\n' |
Aleksander Morgado | 2a511da | 2012-05-30 12:40:46 +0200 | [diff] [blame] | 206 | '${lp}${to} = g_strdup (${from});\n') |
Aleksander Morgado | 4130a72 | 2012-07-03 13:58:52 +0200 | [diff] [blame] | 207 | return string.Template(template).substitute(translations) |
Aleksander Morgado | 2a511da | 2012-05-30 12:40:46 +0200 | [diff] [blame] | 208 | |
| 209 | |
| 210 | """ |
| 211 | Dispose the string |
| 212 | """ |
Aleksander Morgado | 4130a72 | 2012-07-03 13:58:52 +0200 | [diff] [blame] | 213 | def build_dispose(self, line_prefix, variable_name): |
Aleksander Morgado | 2a511da | 2012-05-30 12:40:46 +0200 | [diff] [blame] | 214 | translations = { 'lp' : line_prefix, |
| 215 | 'variable_name' : variable_name } |
| 216 | |
| 217 | template = ( |
| 218 | '${lp}g_free (${variable_name});\n') |
Aleksander Morgado | 4130a72 | 2012-07-03 13:58:52 +0200 | [diff] [blame] | 219 | return string.Template(template).substitute(translations) |