File: example.i

package info (click to toggle)
swig1.3 1.3.40-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,512 kB
  • ctags: 14,780
  • sloc: cpp: 41,436; ansic: 31,416; python: 9,313; java: 7,332; makefile: 5,264; cs: 5,231; yacc: 4,995; sh: 4,646; ruby: 3,774; perl: 3,638; lisp: 1,993; php: 1,736; tcl: 1,266; ml: 708
file content (121 lines) | stat: -rw-r--r-- 2,941 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* File : example.i */
%module example

%{
extern int gcd(int x, int y);
extern int gcdmain(int argc, char *argv[]);
extern int count(char *bytes, int len, char c);
extern void capitalize (char *str, int len);
extern void circle (double cx, double cy);
extern int squareCubed (int n, int *OUTPUT);
%}

%include exception.i
%include typemaps.i

extern int    gcd(int x, int y);

%typemap(in,fragment="t_output_helper") (int argc, char *argv[]) {
  int i;
  if (!PyList_Check($input)) {
    SWIG_exception(SWIG_ValueError, "Expecting a list");
  }
  $1 = PyList_Size($input);
  if ($1 == 0) {
    SWIG_exception(SWIG_ValueError, "List must contain at least 1 element");
  }
  $2 = (char **) malloc(($1+1)*sizeof(char *));
  for (i = 0; i < $1; i++) {
    PyObject *s = PyList_GetItem($input,i);
%#if PY_VERSION_HEX >= 0x03000000
    if (!PyUnicode_Check(s)) 
%#else
    if (!PyString_Check(s)) 
%#endif
    {
      free($2);
      SWIG_exception(SWIG_ValueError, "List items must be strings");
    }
%#if PY_VERSION_HEX >= 0x03000000
    {
    int l;
    $2[i] = PyUnicode_AsStringAndSize(s, &l);
    }
%#else
    $2[i] = PyString_AsString(s);
%#endif

  }
  $2[i] = 0;
}

extern int gcdmain(int argc, char *argv[]);

%typemap(in) (char *bytes, int len) {

%#if PY_VERSION_HEX >= 0x03000000
  if (!PyUnicode_Check($input)) {
    PyErr_SetString(PyExc_ValueError,"Expected a string");
    return NULL;
  }
  $1 = PyUnicode_AsStringAndSize($input, &$2);
%#else
  if (!PyString_Check($input)) {
    PyErr_SetString(PyExc_ValueError,"Expected a string");
    return NULL;
  }
  $1 = PyString_AsString($input);
  $2 = PyString_Size($input);
%#endif
}

extern int count(char *bytes, int len, char c);


/* This example shows how to wrap a function that mutates a string */

/* Since str is modified, we make a copy of the Python object
   so that we don't violate it's mutability */

%typemap(in) (char *str, int len) {
%#if PY_VERSION_HEX >= 0x03000000
   $2 = PyUnicode_GetSize($input);
   $1 = (char *) malloc($2+1);
   memmove($1,PyUnicode_AsString($input),$2);
%#else
   $2 = PyString_Size($input);
   $1 = (char *) malloc($2+1);
   memmove($1,PyString_AsString($input),$2);
%#endif
}

/* Return the mutated string as a new object.  The t_output_helper
   function takes an object and appends it to the output object
   to create a tuple */

%typemap(argout) (char *str, int len) {
   PyObject *o;
%#if PY_VERSION_HEX >= 0x03000000
   o = PyUnicode_FromStringAndSize($1,$2);
%#else
   o = PyString_FromStringAndSize($1,$2);
%#endif
   $result = t_output_helper($result,o);
   free($1);
}   

extern void capitalize(char *str, int len);

/* A multi-valued constraint.  Force two arguments to lie
   inside the unit circle */

%typemap(check) (double cx, double cy) {
   double a = $1*$1 + $2*$2;
   if (a > 1.0) {
	SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
   }
}

extern void circle(double cx, double cy);