/trunk/Examples/chicken/multimap/example.i
Swig | 96 lines | 71 code | 18 blank | 7 comment | 0 complexity | 3ec51da38706cea5647a704977273ee9 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) (int argc, char *argv[]) { 19 int i; 20 if (!C_swig_is_vector ($input)) { 21 swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a vector"); 22 } 23 $1 = C_header_size ($input); 24 $2 = (char **) malloc(($1+1)*sizeof(char *)); 25 for (i = 0; i < $1; i++) { 26 C_word o = C_block_item ($input, i); 27 if (!C_swig_is_string (o)) { 28 char err[50]; 29 free($2); 30 sprintf (err, "$input[%d] is not a string", i); 31 swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, err); 32 } 33 $2[i] = C_c_string (o); 34 } 35 $2[i] = 0; 36} 37 38%typemap(freearg) (int argc, char *argv[]) { 39 free($2); 40} 41extern int gcdmain(int argc, char *argv[]); 42 43%typemap(in) (char *bytes, int len) { 44 if (!C_swig_is_string ($input)) { 45 swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a string"); 46 } 47 $1 = C_c_string ($input); 48 $2 = C_header_size ($input); 49} 50 51extern int count(char *bytes, int len, char c); 52 53 54/* This example shows how to wrap a function that mutates a string */ 55 56%typemap(in) (char *str, int len) 57%{ if (!C_swig_is_string ($input)) { 58 swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a string"); 59 } 60 $2 = C_header_size ($input); 61 $1 = (char *) malloc ($2+1); 62 memmove ($1, C_c_string ($input), $2); 63%} 64 65/* Return the mutated string as a new object. Notice the if MANY construct ... they must be at column 0. */ 66 67%typemap(argout) (char *str, int len) (C_word *scmstr) 68%{ scmstr = C_alloc (C_SIZEOF_STRING ($2)); 69 SWIG_APPEND_VALUE(C_string (&scmstr, $2, $1)); 70 free ($1); 71%} 72 73extern void capitalize (char *str, int len); 74 75/* A multi-valued constraint. Force two arguments to lie 76 inside the unit circle */ 77 78%typemap(check) (double cx, double cy) { 79 double a = $1*$1 + $2*$2; 80 if (a > 1.0) { 81 SWIG_exception (SWIG_ValueError, "cx and cy must be in unit circle"); 82 } 83} 84 85extern void circle (double cx, double cy); 86 87/* Test out multiple return values */ 88 89extern int squareCubed (int n, int *OUTPUT); 90%{ 91/* Returns n^3 and set n2 to n^2 */ 92int squareCubed (int n, int *n2) { 93 *n2 = n * n; 94 return (*n2) * n; 95}; 96%}