/tags/Root-branch-php-utl/SWIG/Examples/perl5/multimap/example.i
Swig | 93 lines | 68 code | 20 blank | 5 comment | 0 complexity | e1b80056c7cfd73ff730b74ce9e9ab6e MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
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(arginit) (int argc, char *argv[]) "$2 = 0;";
19
20%typemap(in) (int argc, char *argv[]) {
21 AV *tempav;
22 SV **tv;
23 I32 len;
24 int i;
25 if (!SvROK($input)) {
26 SWIG_exception(SWIG_ValueError,"$input is not an array.");
27 }
28 if (SvTYPE(SvRV($input)) != SVt_PVAV) {
29 SWIG_exception(SWIG_ValueError,"$input is not an array.");
30 }
31 tempav = (AV*)SvRV($input);
32 len = av_len(tempav);
33 $1 = (int) len+1;
34 $2 = (char **) malloc(($1+1)*sizeof(char *));
35 for (i = 0; i < $1; i++) {
36 tv = av_fetch(tempav, i, 0);
37 $2[i] = (char *) SvPV(*tv,PL_na);
38 }
39 $2[i] = 0;
40}
41
42%typemap(freearg) (int argc, char *argv[]) {
43 free($2);
44}
45
46extern int gcdmain(int argc, char *argv[]);
47
48%typemap(in) (char *bytes, int len) {
49 STRLEN temp;
50 $1 = (char *) SvPV($input, temp);
51 $2 = (int) temp;
52}
53
54extern int count(char *bytes, int len, char c);
55
56
57/* This example shows how to wrap a function that mutates a string */
58
59%typemap(in) (char *str, int len) {
60 STRLEN templen;
61 char *temp;
62 temp = (char *) SvPV($input,templen);
63 $2 = (int) templen;
64 $1 = (char *) malloc($2+1);
65 memmove($1,temp,$2);
66}
67
68/* Return the mutated string as a new object. */
69
70%typemap(argout) (char *str, int len) {
71 if (argvi >= items) {
72 EXTEND(sp,1);
73 }
74 $result = sv_newmortal();
75 sv_setpvn((SV*)ST(argvi++),$1,$2);
76 free($1);
77}
78
79extern void capitalize(char *str, int len);
80
81/* A multi-valued constraint. Force two arguments to lie
82 inside the unit circle */
83
84%typemap(check) (double cx, double cy) {
85 double a = $1*$1 + $2*$2;
86 if (a > 1.0) {
87 SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
88 }
89}
90
91extern void circle(double cx, double cy);
92
93