]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """Buffers are character arrays that may contain null bytes.\r |
2 | \r | |
3 | There are a number of variants depending on:\r | |
4 | - how the buffer is allocated (for output buffers), and\r | |
5 | - whether and how the size is passed into and/or out of the called function.\r | |
6 | """\r | |
7 | \r | |
8 | \r | |
9 | from bgenType import Type, InputOnlyMixIn, OutputOnlyMixIn, InputOnlyType, OutputOnlyType\r | |
10 | from bgenOutput import *\r | |
11 | \r | |
12 | \r | |
13 | # Map common types to their format characters\r | |
14 | type2format = {\r | |
15 | 'long': 'l',\r | |
16 | 'int': 'i',\r | |
17 | 'short': 'h',\r | |
18 | 'char': 'b',\r | |
19 | 'unsigned long': 'l',\r | |
20 | 'unsigned int': 'i',\r | |
21 | 'unsigned short': 'h',\r | |
22 | 'unsigned char': 'b',\r | |
23 | }\r | |
24 | \r | |
25 | \r | |
26 | # ----- PART 1: Fixed character buffers -----\r | |
27 | \r | |
28 | \r | |
29 | class FixedInputOutputBufferType(InputOnlyType):\r | |
30 | \r | |
31 | """Fixed buffer -- passed as (inbuffer, outbuffer)."""\r | |
32 | \r | |
33 | def __init__(self, size, datatype = 'char', sizetype = 'int', sizeformat = None):\r | |
34 | self.typeName = "Buffer"\r | |
35 | self.size = str(size)\r | |
36 | self.datatype = datatype\r | |
37 | self.sizetype = sizetype\r | |
38 | self.sizeformat = sizeformat or type2format[sizetype]\r | |
39 | self.label_needed = 0\r | |
40 | \r | |
41 | def getArgDeclarations(self, name, reference=False, constmode=False, outmode=False):\r | |
42 | if reference:\r | |
43 | raise RuntimeError, "Cannot pass buffer types by reference"\r | |
44 | return (self.getBufferDeclarations(name, constmode, outmode) +\r | |
45 | self.getSizeDeclarations(name, outmode))\r | |
46 | \r | |
47 | def getBufferDeclarations(self, name, constmode=False, outmode=False):\r | |
48 | return self.getInputBufferDeclarations(name, constmode) + \\r | |
49 | self.getOutputBufferDeclarations(name, constmode, outmode)\r | |
50 | \r | |
51 | def getInputBufferDeclarations(self, name, constmode=False):\r | |
52 | if constmode:\r | |
53 | const = "const "\r | |
54 | else:\r | |
55 | const = ""\r | |
56 | return ["%s%s *%s__in__" % (const, self.datatype, name)]\r | |
57 | \r | |
58 | def getOutputBufferDeclarations(self, name, constmode=False, outmode=False):\r | |
59 | if constmode:\r | |
60 | raise RuntimeError, "Cannot use const output buffer"\r | |
61 | if outmode:\r | |
62 | out = "*"\r | |
63 | else:\r | |
64 | out = ""\r | |
65 | return ["%s%s %s__out__[%s]" % (self.datatype, out, name, self.size)]\r | |
66 | \r | |
67 | def getSizeDeclarations(self, name, outmode=False):\r | |
68 | if outmode:\r | |
69 | out = "*"\r | |
70 | else:\r | |
71 | out = ""\r | |
72 | return ["%s%s %s__len__" %(self.sizetype, out, name)]\r | |
73 | \r | |
74 | def getAuxDeclarations(self, name):\r | |
75 | return ["int %s__in_len__" %(name)]\r | |
76 | \r | |
77 | def getargsFormat(self):\r | |
78 | return "s#"\r | |
79 | \r | |
80 | def getargsArgs(self, name):\r | |
81 | return "&%s__in__, &%s__in_len__" % (name, name)\r | |
82 | \r | |
83 | def getargsCheck(self, name):\r | |
84 | Output("if (%s__in_len__ != %s)", name, self.size)\r | |
85 | OutLbrace()\r | |
86 | Output('PyErr_SetString(PyExc_TypeError, "buffer length should be %s");',\r | |
87 | self.size)\r | |
88 | Output("goto %s__error__;", name)\r | |
89 | self.label_needed = 1\r | |
90 | OutRbrace()\r | |
91 | self.transferSize(name)\r | |
92 | \r | |
93 | def transferSize(self, name):\r | |
94 | Output("%s__len__ = %s__in_len__;", name, name)\r | |
95 | \r | |
96 | def passOutput(self, name):\r | |
97 | return "%s__in__, %s__out__" % (name, name)\r | |
98 | \r | |
99 | def mkvalueFormat(self):\r | |
100 | return "s#"\r | |
101 | \r | |
102 | def mkvalueArgs(self, name):\r | |
103 | return "%s__out__, (int)%s" % (name, self.size)\r | |
104 | \r | |
105 | def cleanup(self, name):\r | |
106 | if self.label_needed:\r | |
107 | DedentLevel()\r | |
108 | Output(" %s__error__: ;", name)\r | |
109 | IndentLevel()\r | |
110 | \r | |
111 | \r | |
112 | class FixedCombinedInputOutputBufferType(FixedInputOutputBufferType):\r | |
113 | \r | |
114 | """Like fixed buffer -- but same parameter is input and output."""\r | |
115 | \r | |
116 | def passOutput(self, name):\r | |
117 | return "(%s *)memcpy(%s__out__, %s__in__, %s)" % \\r | |
118 | (self.datatype, name, name, self.size)\r | |
119 | \r | |
120 | \r | |
121 | class InputOnlyBufferMixIn(InputOnlyMixIn):\r | |
122 | \r | |
123 | def getOutputBufferDeclarations(self, name, constmode=False, outmode=False):\r | |
124 | return []\r | |
125 | \r | |
126 | \r | |
127 | class OutputOnlyBufferMixIn(OutputOnlyMixIn):\r | |
128 | \r | |
129 | def getInputBufferDeclarations(self, name, constmode=False):\r | |
130 | return []\r | |
131 | \r | |
132 | class OptionalInputBufferMixIn:\r | |
133 | \r | |
134 | """Add to input buffers if the buffer may be omitted: pass None in Python\r | |
135 | and the C code will get a NULL pointer and zero size"""\r | |
136 | \r | |
137 | def getargsFormat(self):\r | |
138 | return "z#"\r | |
139 | \r | |
140 | \r | |
141 | class FixedInputBufferType(InputOnlyBufferMixIn, FixedInputOutputBufferType):\r | |
142 | \r | |
143 | """Fixed size input buffer -- passed without size information.\r | |
144 | \r | |
145 | Instantiate with the size as parameter.\r | |
146 | """\r | |
147 | \r | |
148 | def passInput(self, name):\r | |
149 | return "%s__in__" % name\r | |
150 | \r | |
151 | class OptionalFixedInputBufferType(OptionalInputBufferMixIn, FixedInputBufferType):\r | |
152 | pass\r | |
153 | \r | |
154 | class FixedOutputBufferType(OutputOnlyBufferMixIn, FixedInputOutputBufferType):\r | |
155 | \r | |
156 | """Fixed size output buffer -- passed without size information.\r | |
157 | \r | |
158 | Instantiate with the size as parameter.\r | |
159 | """\r | |
160 | \r | |
161 | def passOutput(self, name):\r | |
162 | return "%s__out__" % name\r | |
163 | \r | |
164 | \r | |
165 | class VarInputBufferType(FixedInputBufferType):\r | |
166 | \r | |
167 | """Variable size input buffer -- passed as (buffer, size).\r | |
168 | \r | |
169 | Instantiate without size parameter.\r | |
170 | """\r | |
171 | \r | |
172 | def __init__(self, datatype = 'char', sizetype = 'int', sizeformat = None):\r | |
173 | FixedInputBufferType.__init__(self, "0", datatype, sizetype, sizeformat)\r | |
174 | \r | |
175 | def getargsCheck(self, name):\r | |
176 | Output("%s__len__ = %s__in_len__;", name, name)\r | |
177 | \r | |
178 | def passInput(self, name):\r | |
179 | return "%s__in__, %s__len__" % (name, name)\r | |
180 | \r | |
181 | class ReverseInputBufferMixin:\r | |
182 | """ Mixin for input buffers that are passed as (size, buffer) """\r | |
183 | \r | |
184 | def passInput(self, name):\r | |
185 | return "%s__len__, %s__in__" % (name, name)\r | |
186 | \r | |
187 | class OptionalVarInputBufferType(OptionalInputBufferMixIn, VarInputBufferType):\r | |
188 | pass\r | |
189 | \r | |
190 | # ----- PART 2: Structure buffers -----\r | |
191 | \r | |
192 | \r | |
193 | class StructInputOutputBufferType(FixedInputOutputBufferType):\r | |
194 | \r | |
195 | """Structure buffer -- passed as a structure pointer.\r | |
196 | \r | |
197 | Instantiate with the struct type as parameter.\r | |
198 | """\r | |
199 | \r | |
200 | def __init__(self, type):\r | |
201 | FixedInputOutputBufferType.__init__(self, "sizeof(%s)" % type)\r | |
202 | self.typeName = self.type = type\r | |
203 | \r | |
204 | def getInputBufferDeclarations(self, name, constmode=False):\r | |
205 | if constmode:\r | |
206 | const = "const "\r | |
207 | else:\r | |
208 | const = ""\r | |
209 | return ["%s%s *%s__in__" % (const, self.type, name)]\r | |
210 | \r | |
211 | def getSizeDeclarations(self, name, outmode=False):\r | |
212 | return []\r | |
213 | \r | |
214 | def getAuxDeclarations(self, name):\r | |
215 | return ["int %s__in_len__" % (name)]\r | |
216 | \r | |
217 | def getOutputBufferDeclarations(self, name, constmode=False, outmode=False):\r | |
218 | if constmode:\r | |
219 | raise RuntimeError, "Cannot use const output buffer"\r | |
220 | if outmode:\r | |
221 | out = "*"\r | |
222 | else:\r | |
223 | out = ""\r | |
224 | return ["%s%s %s__out__" % (self.type, out, name)]\r | |
225 | \r | |
226 | def getargsArgs(self, name):\r | |
227 | return "(char **)&%s__in__, &%s__in_len__" % (name, name)\r | |
228 | \r | |
229 | def transferSize(self, name):\r | |
230 | pass\r | |
231 | \r | |
232 | def passInput(self, name):\r | |
233 | return "%s__in__" % name\r | |
234 | \r | |
235 | def passOutput(self, name):\r | |
236 | return "%s__in__, &%s__out__" % (name, name)\r | |
237 | \r | |
238 | def mkvalueArgs(self, name):\r | |
239 | return "(char *)&%s__out__, (int)%s" % (name, self.size)\r | |
240 | \r | |
241 | \r | |
242 | class StructCombinedInputOutputBufferType(StructInputOutputBufferType):\r | |
243 | \r | |
244 | """Like structure buffer -- but same parameter is input and output."""\r | |
245 | \r | |
246 | def passOutput(self, name):\r | |
247 | return "(%s *)memcpy((char *)%s__out__, (char *)%s__in__, %s)" % \\r | |
248 | (self.type, name, name, self.size)\r | |
249 | \r | |
250 | \r | |
251 | class StructInputBufferType(InputOnlyBufferMixIn, StructInputOutputBufferType):\r | |
252 | \r | |
253 | """Fixed size input buffer -- passed as a pointer to a structure.\r | |
254 | \r | |
255 | Instantiate with the struct type as parameter.\r | |
256 | """\r | |
257 | \r | |
258 | \r | |
259 | class StructByValueBufferType(StructInputBufferType):\r | |
260 | \r | |
261 | """Fixed size input buffer -- passed as a structure BY VALUE.\r | |
262 | \r | |
263 | Instantiate with the struct type as parameter.\r | |
264 | """\r | |
265 | \r | |
266 | def passInput(self, name):\r | |
267 | return "*%s__in__" % name\r | |
268 | \r | |
269 | \r | |
270 | class StructOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType):\r | |
271 | \r | |
272 | """Fixed size output buffer -- passed as a pointer to a structure.\r | |
273 | \r | |
274 | Instantiate with the struct type as parameter.\r | |
275 | """\r | |
276 | \r | |
277 | def getSizeDeclarations(self, name, outmode=False):\r | |
278 | return []\r | |
279 | \r | |
280 | def getAuxDeclarations(self, name):\r | |
281 | return []\r | |
282 | \r | |
283 | def passOutput(self, name):\r | |
284 | return "&%s__out__" % name\r | |
285 | \r | |
286 | \r | |
287 | class ArrayOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType):\r | |
288 | \r | |
289 | """Fixed size output buffer -- declared as a typedef, passed as an array.\r | |
290 | \r | |
291 | Instantiate with the struct type as parameter.\r | |
292 | """\r | |
293 | \r | |
294 | def getSizeDeclarations(self, name, outmode=False):\r | |
295 | return []\r | |
296 | \r | |
297 | def getAuxDeclarations(self, name):\r | |
298 | return []\r | |
299 | \r | |
300 | def passOutput(self, name):\r | |
301 | return "%s__out__" % name\r |