PageRenderTime 30ms CodeModel.GetById 23ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/tcl/multimap/example.i

#
Swig | 80 lines | 56 code | 19 blank | 5 comment | 0 complexity | 1079b63b377a5174cdb9777018c37315 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
15extern int    gcd(int x, int y);
16
17%typemap(arginit) (int argc, char *argv[]) "$2 = 0;";
18
19%typemap(in) (int argc, char *argv[]) {
20  Tcl_Obj **listobjv = 0;
21  int i;
22  if (Tcl_ListObjGetElements(interp,$input, &$1, &listobjv) == TCL_ERROR) {
23    SWIG_exception(SWIG_ValueError,"Expected a list");
24    return TCL_ERROR;
25  }
26  $2 = (char **) malloc(($1+1)*sizeof(char *));
27  for (i = 0; i < $1; i++) {
28    $2[i] = Tcl_GetStringFromObj(listobjv[i],0);
29  }
30  $2[i] = 0;
31}
32
33%typemap(freearg) char *argv[] {
34  if ($1) {
35    free($1);
36  }
37}
38
39extern int gcdmain(int argc, char *argv[]);
40
41%typemap(in) (char *bytes, int len) {
42  $1 = Tcl_GetStringFromObj($input,&$2);
43}
44
45extern int count(char *bytes, int len, char c);
46
47
48/* This example shows how to wrap a function that mutates a string */
49
50%typemap(in) (char *str, int len) {
51  char *temp;
52  temp = Tcl_GetStringFromObj($input,&$2);
53  $1 = (char *) malloc($2+1);
54  memmove($1,temp,$2);
55}
56
57/* Return the mutated string as a new object.   */
58
59%typemap(argout) (char *str, int len) {
60 Tcl_Obj *o;
61 o = Tcl_NewStringObj($1,$2);
62 Tcl_ListObjAppendElement(interp,$result,o);
63 free($1);
64}   
65
66extern void capitalize(char *str, int len);
67
68
69/* A multi-valued constraint.  Force two arguments to lie
70   inside the unit circle */
71
72%typemap(check) (double cx, double cy) {
73   double a = $1*$1 + $2*$2;
74   if (a > 1.0) {
75	SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
76        return TCL_ERROR;
77   }
78}
79
80extern void circle(double cx, double cy);