import source from 1.3.40
[external/swig.git] / Examples / ruby / multimap / example.i
1 %module example
2
3 %{
4 extern int gcd(int x, int y);
5 extern int gcdmain(int argc, char *argv[]);
6 extern int count(char *bytes, int len, char c);
7 extern void capitalize (char *str, int len);
8 extern void circle (double cx, double cy);
9 extern int squareCubed (int n, int *OUTPUT);
10 %}
11
12 %include exception.i
13 %include typemaps.i
14
15 extern int    gcd(int x, int y);
16
17 %typemap(in) (int argc, char *argv[]) {
18   int i;
19   
20   if (TYPE($input) != T_ARRAY) {
21     SWIG_exception(SWIG_ValueError, "Expected an array");
22   }
23   $1 = RARRAY($input)->len;
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     VALUE   s = rb_ary_entry($input,i);
30     if (TYPE(s) != T_STRING) {
31       free($2);
32       SWIG_exception(SWIG_ValueError, "List items must be strings");
33     }
34     $2[i] = STR2CSTR(s);
35   }
36   $2[i] = 0;
37 }
38
39 %typemap(freearg) (int argc, char *argv[]) {
40   free($2);
41 }
42
43 extern int gcdmain(int argc, char *argv[]);
44
45 %typemap(in) (char *bytes, int len) {
46   if (TYPE($input) != T_STRING) {
47     SWIG_exception(SWIG_ValueError, "Expected a string");
48   }
49   $1 = STR2CSTR($input);
50   $2 = RSTRING($input)->len;
51 }
52
53 extern int count(char *bytes, int len, char c);
54
55
56 /* This example shows how to wrap a function that mutates a string */
57
58 %typemap(in) (char *str, int len) {
59   char *temp;
60   if (TYPE($input) != T_STRING) {
61     SWIG_exception(SWIG_ValueError,"Expected a string");
62   }
63   temp = STR2CSTR($input);
64   $2 = RSTRING($input)->len;
65   $1 = (char *) malloc($2+1);
66   memmove($1,temp,$2);
67 }
68
69 /* Return the mutated string as a new object.  */
70
71 %typemap(argout, fragment="output_helper") (char *str, int len) {
72    VALUE o;
73    o = rb_str_new($1,$2);
74    $result = output_helper($result,o);
75    free($1);
76 }   
77
78 extern void capitalize(char *str, int len);
79
80 /* A multi-valued constraint.  Force two arguments to lie
81    inside the unit circle */
82
83 %typemap(check) (double cx, double cy) {
84    double a = $1*$1 + $2*$2;
85    if (a > 1.0) {
86         SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
87    }
88 }
89
90 extern void circle(double cx, double cy);
91
92