aboutsummaryrefslogtreecommitdiff
path: root/Examples/tcl/multimap/example.i
blob: afe7ed7f9f957aaa2eb0ac046cebae29496c0b8b (plain)
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
/* File : example.i */
%module example
%include exception.i

extern int    gcd(int x, int y);

%typemap(tcl8,in) (int argc, char *argv[]) {
  Tcl_Obj **listobjv = 0;
  int i;
  if (Tcl_ListObjGetElements(interp,$input, &$1, &listobjv) == TCL_ERROR) {
    SWIG_exception(SWIG_ValueError,"Expected a list");
    return TCL_ERROR;
  }
  $2 = (char **) malloc(($1+1)*sizeof(char *));
  for (i = 0; i < $1; i++) {
    $2[i] = Tcl_GetStringFromObj(listobjv[i],0);
  }
  $2[i] = 0;
}

%typemap(tcl8,freearg) char *argv[] {
  if ($1) {
    free($1);
  }
}

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

%typemap(tcl8,in) (char *bytes, int len) {
  $1 = Tcl_GetStringFromObj($input,&$2);
}

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


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

%typemap(tcl8,in) (char *str, int len) {
  char *temp;
  temp = Tcl_GetStringFromObj($input,&$2);
  $1 = (char *) malloc($2+1);
  memmove($1,temp,$2);
}

/* Return the mutated string as a new object.   */

%typemap(tcl8,argout) (char *str, int len) {
 Tcl_Obj *o;
 o = Tcl_NewStringObj($1,$2);
 Tcl_ListObjAppendElement(interp,$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");
        return TCL_ERROR;
   }
}

extern void circle(double cx, double cy);