1/* Typemap definitions, to allow SWIG to properly handle 'char**' data types. */
2
3%typemap(in) char ** {
4  /* Check if is a list  */
5  if (PyList_Check($input)) {
6    int size = PyList_Size($input);
7    int i = 0;
8    $1 = (char **) malloc((size+1) * sizeof(char*));
9    for (i = 0; i < size; i++) {
10      PyObject *o = PyList_GetItem($input,i);
11      if (PyString_Check(o))
12        $1[i] = PyString_AsString(o);
13      else {
14        PyErr_SetString(PyExc_TypeError,"list must contain strings");
15        free($1);
16        return NULL;
17      }
18    }
19    $1[i] = 0;
20  } else if ($input == Py_None) {
21    $1 =  NULL;
22  } else {
23    PyErr_SetString(PyExc_TypeError,"not a list");
24    return NULL;
25  }
26}
27
28%typemap(freearg) char** {
29  free((char *) $1);
30}
31
32%typemap(out) char** {
33  int len;
34  int i;
35  len = 0;
36  while ($1[len]) len++;
37  $result = PyList_New(len);
38  for (i = 0; i < len; i++) {
39    PyList_SetItem($result, i, PyString_FromString($1[i]));
40  }
41}
42
43/* Typemap definitions to allow SWIG to properly handle char buffer. */
44
45// typemap for a char buffer
46// See also SBThread::GetStopDescription.
47%typemap(in) (char *dst, size_t dst_len) {
48   if (!PyInt_Check($input)) {
49       PyErr_SetString(PyExc_ValueError, "Expecting an integer");
50       return NULL;
51   }
52   $2 = PyInt_AsLong($input);
53   if ($2 <= 0) {
54       PyErr_SetString(PyExc_ValueError, "Positive integer expected");
55       return NULL;
56   }
57   $1 = (char *) malloc($2);
58}
59
60// Return the char buffer.  Discarding any previous return result
61// See also SBThread::GetStopDescription.
62%typemap(argout) (char *dst, size_t dst_len) {
63   Py_XDECREF($result);   /* Blow away any previous result */
64   $result = PyString_FromStringAndSize(($1),result);
65   free($1);
66}
67
68
69// typemap for an outgoing buffer
70// See also SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len).
71%typemap(in) (const char *cstr, uint32_t cstr_len) {
72   if (PyString_Check($input)) {
73      $1 = (char *) PyString_AsString($input);
74      $2 = PyString_Size($input);
75   }
76   else if(PyByteArray_Check($input)) {
77      $1 = (char *) PyByteArray_AsString($input);
78      $2 = PyByteArray_Size($input);
79   }
80   else {
81      PyErr_SetString(PyExc_ValueError, "Expecting a string");
82      return NULL;
83   }
84}
85// Ditto for SBProcess::PutSTDIN(const char *src, size_t src_len).
86%typemap(in) (const char *src, size_t src_len) {
87   if (PyString_Check($input)) {
88      $1 = (char *) PyString_AsString($input);
89      $2 = PyString_Size($input);
90   }
91   else if(PyByteArray_Check($input)) {
92      $1 = (char *) PyByteArray_AsString($input);
93      $2 = PyByteArray_Size($input);
94   }
95   else {
96      PyErr_SetString(PyExc_ValueError, "Expecting a string");
97      return NULL;
98   }
99}
100// And SBProcess::WriteMemory.
101%typemap(in) (const void *buf, size_t size) {
102   if (PyString_Check($input)) {
103      $1 = (void *) PyString_AsString($input);
104      $2 = PyString_Size($input);
105   }
106   else if(PyByteArray_Check($input)) {
107      $1 = (void *) PyByteArray_AsString($input);
108      $2 = PyByteArray_Size($input);
109   }
110   else {
111      PyErr_SetString(PyExc_ValueError, "Expecting a string");
112      return NULL;
113   }
114}
115
116// For SBDebugger::DispatchInput
117%typemap(in) (const void *data, size_t data_len) {
118   if (PyString_Check($input)) {
119      $1 = static_cast<void *>(PyString_AsString($input));
120      $2 = PyString_Size($input);
121   }
122   else if(PyByteArray_Check($input)) {
123      $1 = static_cast<void *>(PyByteArray_AsString($input));
124      $2 = PyByteArray_Size($input);
125   }
126   else {
127      PyErr_SetString(PyExc_ValueError, "Expecting a string or byte array");
128      return NULL;
129   }
130}
131
132// typemap for an incoming buffer
133// See also SBProcess::ReadMemory.
134%typemap(in) (void *buf, size_t size) {
135   if (PyInt_Check($input)) {
136      $2 = PyInt_AsLong($input);
137   } else if (PyLong_Check($input)) {
138      $2 = PyLong_AsLong($input);
139   } else {
140      PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object");
141      return NULL;
142   }
143   if ($2 <= 0) {
144       PyErr_SetString(PyExc_ValueError, "Positive integer expected");
145       return NULL;
146   }
147   $1 = (void *) malloc($2);
148}
149
150// Return the buffer.  Discarding any previous return result
151// See also SBProcess::ReadMemory.
152%typemap(argout) (void *buf, size_t size) {
153   Py_XDECREF($result);   /* Blow away any previous result */
154   $result = PyString_FromStringAndSize(static_cast<const char*>($1),result);
155   free($1);
156}
157
158// these typemaps allow Python users to pass list objects
159// and have them turn into C++ arrays (this is useful, for instance
160// when creating SBData objects from lists of numbers)
161%typemap(in) (uint64_t* array, size_t array_len) {
162  /* Check if is a list  */
163  if (PyList_Check($input)) {
164    int size = PyList_Size($input);
165    int i = 0;
166    $2 = size;
167    $1 = (uint64_t*) malloc(size * sizeof(uint64_t));
168    for (i = 0; i < size; i++) {
169      PyObject *o = PyList_GetItem($input,i);
170      if (PyInt_Check(o)) {
171        $1[i] = PyInt_AsLong(o);
172      }
173      else {
174        PyErr_SetString(PyExc_TypeError,"list must contain numbers");
175        free($1);
176        return NULL;
177      }
178    }
179  } else if ($input == Py_None) {
180    $1 =  NULL;
181    $2 = 0;
182  } else {
183    PyErr_SetString(PyExc_TypeError,"not a list");
184    return NULL;
185  }
186}
187
188%typemap(freearg) (uint64_t* array, size_t array_len) {
189  free($1);
190}
191
192%typemap(in) (uint32_t* array, size_t array_len) {
193  /* Check if is a list  */
194  if (PyList_Check($input)) {
195    int size = PyList_Size($input);
196    int i = 0;
197    $2 = size;
198    $1 = (uint32_t*) malloc(size * sizeof(uint32_t));
199    for (i = 0; i < size; i++) {
200      PyObject *o = PyList_GetItem($input,i);
201      if (PyInt_Check(o)) {
202        $1[i] = PyInt_AsLong(o);
203      }
204      else {
205        PyErr_SetString(PyExc_TypeError,"list must contain numbers");
206        free($1);
207        return NULL;
208      }
209    }
210  } else if ($input == Py_None) {
211    $1 =  NULL;
212    $2 = 0;
213  } else {
214    PyErr_SetString(PyExc_TypeError,"not a list");
215    return NULL;
216  }
217}
218
219%typemap(freearg) (uint32_t* array, size_t array_len) {
220  free($1);
221}
222
223%typemap(in) (int64_t* array, size_t array_len) {
224  /* Check if is a list  */
225  if (PyList_Check($input)) {
226    int size = PyList_Size($input);
227    int i = 0;
228    $2 = size;
229    $1 = (int64_t*) malloc(size * sizeof(int64_t));
230    for (i = 0; i < size; i++) {
231      PyObject *o = PyList_GetItem($input,i);
232      if (PyInt_Check(o)) {
233        $1[i] = PyInt_AsLong(o);
234      }
235      else {
236        PyErr_SetString(PyExc_TypeError,"list must contain numbers");
237        free($1);
238        return NULL;
239      }
240    }
241  } else if ($input == Py_None) {
242    $1 =  NULL;
243    $2 = 0;
244  } else {
245    PyErr_SetString(PyExc_TypeError,"not a list");
246    return NULL;
247  }
248}
249
250%typemap(freearg) (int64_t* array, size_t array_len) {
251  free($1);
252}
253
254%typemap(in) (int32_t* array, size_t array_len) {
255  /* Check if is a list  */
256  if (PyList_Check($input)) {
257    int size = PyList_Size($input);
258    int i = 0;
259    $2 = size;
260    $1 = (int32_t*) malloc(size * sizeof(int32_t));
261    for (i = 0; i < size; i++) {
262      PyObject *o = PyList_GetItem($input,i);
263      if (PyInt_Check(o)) {
264        $1[i] = PyInt_AsLong(o);
265      }
266      else {
267        PyErr_SetString(PyExc_TypeError,"list must contain numbers");
268        free($1);
269        return NULL;
270      }
271    }
272  } else if ($input == Py_None) {
273    $1 =  NULL;
274    $2 = 0;
275  } else {
276    PyErr_SetString(PyExc_TypeError,"not a list");
277    return NULL;
278  }
279}
280
281%typemap(freearg) (int32_t* array, size_t array_len) {
282  free($1);
283}
284
285%typemap(in) (double* array, size_t array_len) {
286  /* Check if is a list  */
287  if (PyList_Check($input)) {
288    int size = PyList_Size($input);
289    int i = 0;
290    $2 = size;
291    $1 = (double*) malloc(size * sizeof(double));
292    for (i = 0; i < size; i++) {
293      PyObject *o = PyList_GetItem($input,i);
294      if (PyFloat_Check(o)) {
295        $1[i] = PyFloat_AsDouble(o);
296      }
297      else {
298        PyErr_SetString(PyExc_TypeError,"list must contain floating-point numbers");
299        free($1);
300        return NULL;
301      }
302    }
303  } else if ($input == Py_None) {
304    $1 =  NULL;
305    $2 = 0;
306  } else {
307    PyErr_SetString(PyExc_TypeError,"not a list");
308    return NULL;
309  }
310}
311
312%typemap(freearg) (double* array, size_t array_len) {
313  free($1);
314}
315
316// these typemaps wrap SBModule::GetVersion() from requiring a memory buffer
317// to the more Pythonic style where a list is returned and no previous allocation
318// is necessary - this will break if more than 50 versions are ever returned
319%typemap(typecheck) (uint32_t *versions, uint32_t num_versions) {
320    $1 = ($input == Py_None ? 1 : 0);
321}
322
323%typemap(in, numinputs=0) (uint32_t *versions) {
324    $1 = (uint32_t*)malloc(sizeof(uint32_t) * 50);
325}
326
327%typemap(in, numinputs=0) (uint32_t num_versions) {
328    $1 = 50;
329}
330
331%typemap(argout) (uint32_t *versions, uint32_t num_versions) {
332    uint32_t count = result;
333    if (count >= $2)
334        count = $2;
335    PyObject* list = PyList_New(count);
336    for (uint32_t j = 0; j < count; j++)
337    {
338        if ($1[j] < UINT32_MAX)
339        {
340            PyObject* item = PyInt_FromLong($1[j]);
341            int ok = PyList_SetItem(list,j,item);
342            if (ok != 0)
343            {
344                $result = Py_None;
345                break;
346            }
347        }
348        else
349            break;
350    }
351    $result = list;
352}
353
354%typemap(freearg) (uint32_t *versions) {
355    free($1);
356}
357
358// For lldb::SBInputReader::Callback
359%typemap(in) (lldb::SBInputReader::Callback callback, void *callback_baton) {
360  if (!($input == Py_None || PyCallable_Check(reinterpret_cast<PyObject*>($input)))) {
361    PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
362    return NULL;
363  }
364
365  // FIXME (filcab): We can't currently check if our callback is already
366  // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous
367  // baton) nor can we just remove all traces of a callback, if we want to
368  // revert to a file logging mechanism.
369
370  // Don't lose the callback reference
371  Py_INCREF($input);
372  $1 = LLDBSwigPythonCallSBInputReaderCallback;
373  $2 = $input;
374}
375
376%typemap(typecheck) (lldb::SBInputReader::Callback callback, void *baton) {
377  $1 = $input == Py_None;
378  $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject*>($input));
379}
380
381// For Log::LogOutputCallback
382%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
383  if (!($input == Py_None || PyCallable_Check(reinterpret_cast<PyObject*>($input)))) {
384    PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
385    return NULL;
386  }
387
388  // FIXME (filcab): We can't currently check if our callback is already
389  // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous
390  // baton) nor can we just remove all traces of a callback, if we want to
391  // revert to a file logging mechanism.
392
393  // Don't lose the callback reference
394  Py_INCREF($input);
395  $1 = LLDBSwigPythonCallPythonLogOutputCallback;
396  $2 = $input;
397}
398
399%typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) {
400  $1 = $input == Py_None;
401  $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject*>($input));
402}
403
404%typemap(in) FILE * {
405   if ($input == Py_None)
406      $1 = NULL;
407   else if (!PyFile_Check($input)) {
408      int fd = PyObject_AsFileDescriptor($input);
409      PyObject *py_mode = PyObject_GetAttrString($input, "mode");
410      if (!py_mode) {
411         PyErr_SetString(PyExc_TypeError,"not a file-like object");
412         return NULL;
413      }
414      const char *mode = PyString_AsString(py_mode);
415      if (-1 != fd && mode) {
416         FILE *f;
417         if ((f = fdopen(fd, mode)))
418            $1 = f;
419         else
420            PyErr_SetString(PyExc_TypeError, strerror(errno));
421      } else {
422         PyErr_SetString(PyExc_TypeError,"not a file-like object");
423         return NULL;
424      }
425   }
426   else
427      $1 = PyFile_AsFile($input);
428}
429
430%typemap(out) FILE * {
431   char mode[4] = {0};
432#ifdef __MACOSX__
433   int i = 0;
434   short flags = $1->_flags;
435
436   if (flags & __SRD)
437      mode[i++] = 'r';
438   else if (flags & __SWR)
439      mode[i++] = 'w';
440   else // if (flags & __SRW)
441      mode[i++] = 'a';
442#endif
443   $result = PyFile_FromFile($1, const_cast<char*>(""), mode, fclose);
444}
445
446%typemap(in) (const char* string, int len) {
447    if ($input == Py_None)
448    {
449        $1 = NULL;
450        $2 = 0;
451    }
452    else if (PyUnicode_Check($input))
453    {
454        $1 = PyString_AsString(PyUnicode_AsUTF8String($input));
455        $2 = strlen($1);
456    }
457    else if (PyString_Check($input))
458    {
459        $1 = PyString_AsString($input);
460        $2 = PyString_Size($input);
461    }
462    else
463    {
464        PyErr_SetString(PyExc_TypeError,"not a string-like object");
465        return NULL;
466    }
467}
468