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

/trunk/Examples/mzscheme/multimap/example.i

#
Swig | 91 lines | 68 code | 18 blank | 5 comment | 0 complexity | e0e694f1608d2a44d3c405801ecaee94 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  Scheme_Object **elms;
21  if (!SCHEME_VECTORP($input)) {
22    scheme_wrong_type("$name","vector",$argnum,argc,argv);
23  }
24  $1 = SCHEME_VEC_SIZE($input);
25  elms = SCHEME_VEC_ELS($input);
26  if ($1 == 0) {
27    scheme_wrong_type("$name","vector",$argnum,argc,argv);
28  }
29  $2 = (char **) malloc(($1+1)*sizeof(char *));
30  for (i = 0; i < $1; i++) {
31    if (!SCHEME_STRINGP(elms[i])) {
32      free($2);
33      scheme_wrong_type("$name","vector",$argnum,argc,argv);      
34    }
35    $2[i] = SCHEME_STR_VAL(elms[i]);
36  }
37  $2[i] = 0;
38}
39
40%typemap(freearg) (int argc, char *argv[]) {
41  free($2);
42}
43extern int gcdmain(int argc, char *argv[]);
44
45%typemap(in) (char *bytes, int len) {
46  if (!SCHEME_STRINGP($input)) {
47     scheme_wrong_type("$name","string",1,argc,argv);
48  }	
49  $1 = SCHEME_STR_VAL($input);
50  $2 = SCHEME_STRLEN_VAL($input);
51}
52
53extern 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  if (!SCHEME_STRINGP($input)) {
60     scheme_wrong_type("$name","string",1,argc,argv);
61  }	
62  $2 = SCHEME_STRLEN_VAL($input);
63  $1 = (char *) malloc($2+1);
64  memmove($1,SCHEME_STR_VAL($input),$2);
65}
66
67/* Return the mutated string as a new object.  */
68
69%typemap(argout) (char *str, int len) {
70   Scheme_Object *s;
71   s = scheme_make_sized_string($1,$2,1);
72   SWIG_APPEND_VALUE(s);
73   free($1);
74}   
75
76extern void capitalize(char *str, int len);
77
78/* A multi-valued constraint.  Force two arguments to lie
79   inside the unit circle */
80
81%typemap(check) (double cx, double cy) {
82   double a = $1*$1 + $2*$2;
83   if (a > 1.0) {
84	SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
85        return NULL;
86   }
87}
88
89extern void circle(double cx, double cy);
90
91