PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/python/multimap/example.i

#
Swig | 121 lines | 91 code | 21 blank | 9 comment | 0 complexity | 474fc528d7ee6d68608aa9d3b543b212 MD5 | raw file
  1/* File : example.i */
  2%module example
  3
  4%{
  5extern int gcd(int x, int y);
  6extern int gcdmain(int argc, char *argv[]);
  7extern int count(char *bytes, int len, char c);
  8extern void capitalize (char *str, int len);
  9extern void circle (double cx, double cy);
 10extern int squareCubed (int n, int *OUTPUT);
 11%}
 12
 13%include exception.i
 14%include typemaps.i
 15
 16extern int    gcd(int x, int y);
 17
 18%typemap(in,fragment="t_output_helper") (int argc, char *argv[]) {
 19  int i;
 20  if (!PyList_Check($input)) {
 21    SWIG_exception(SWIG_ValueError, "Expecting a list");
 22  }
 23  $1 = PyList_Size($input);
 24  if ($1 == 0) {
 25    SWIG_exception(SWIG_ValueError, "List must contain at least 1 element");
 26  }
 27  $2 = (char **) malloc(($1+1)*sizeof(char *));
 28  for (i = 0; i < $1; i++) {
 29    PyObject *s = PyList_GetItem($input,i);
 30%#if PY_VERSION_HEX >= 0x03000000
 31    if (!PyUnicode_Check(s)) 
 32%#else
 33    if (!PyString_Check(s)) 
 34%#endif
 35    {
 36      free($2);
 37      SWIG_exception(SWIG_ValueError, "List items must be strings");
 38    }
 39%#if PY_VERSION_HEX >= 0x03000000
 40    {
 41    int l;
 42    $2[i] = PyUnicode_AsStringAndSize(s, &l);
 43    }
 44%#else
 45    $2[i] = PyString_AsString(s);
 46%#endif
 47
 48  }
 49  $2[i] = 0;
 50}
 51
 52extern int gcdmain(int argc, char *argv[]);
 53
 54%typemap(in) (char *bytes, int len) {
 55
 56%#if PY_VERSION_HEX >= 0x03000000
 57  if (!PyUnicode_Check($input)) {
 58    PyErr_SetString(PyExc_ValueError,"Expected a string");
 59    return NULL;
 60  }
 61  $1 = PyUnicode_AsStringAndSize($input, &$2);
 62%#else
 63  if (!PyString_Check($input)) {
 64    PyErr_SetString(PyExc_ValueError,"Expected a string");
 65    return NULL;
 66  }
 67  $1 = PyString_AsString($input);
 68  $2 = PyString_Size($input);
 69%#endif
 70}
 71
 72extern int count(char *bytes, int len, char c);
 73
 74
 75/* This example shows how to wrap a function that mutates a string */
 76
 77/* Since str is modified, we make a copy of the Python object
 78   so that we don't violate it's mutability */
 79
 80%typemap(in) (char *str, int len) {
 81%#if PY_VERSION_HEX >= 0x03000000
 82   $2 = PyUnicode_GetSize($input);
 83   $1 = (char *) malloc($2+1);
 84   memmove($1,PyUnicode_AsString($input),$2);
 85%#else
 86   $2 = PyString_Size($input);
 87   $1 = (char *) malloc($2+1);
 88   memmove($1,PyString_AsString($input),$2);
 89%#endif
 90}
 91
 92/* Return the mutated string as a new object.  The t_output_helper
 93   function takes an object and appends it to the output object
 94   to create a tuple */
 95
 96%typemap(argout) (char *str, int len) {
 97   PyObject *o;
 98%#if PY_VERSION_HEX >= 0x03000000
 99   o = PyUnicode_FromStringAndSize($1,$2);
100%#else
101   o = PyString_FromStringAndSize($1,$2);
102%#endif
103   $result = t_output_helper($result,o);
104   free($1);
105}   
106
107extern void capitalize(char *str, int len);
108
109/* A multi-valued constraint.  Force two arguments to lie
110   inside the unit circle */
111
112%typemap(check) (double cx, double cy) {
113   double a = $1*$1 + $2*$2;
114   if (a > 1.0) {
115	SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
116   }
117}
118
119extern void circle(double cx, double cy);
120
121