/tags/rel-1-3-15/SWIG/Examples/mzscheme/multimap/example.i

# · Swig · 81 lines · 60 code · 16 blank · 5 comment · 0 complexity · ee30c6f12ed6b4f8a536769e845cfa55 MD5 · raw file

  1. /* File : example.i */
  2. %module example
  3. %include exception.i
  4. %include typemaps.i
  5. extern int gcd(int x, int y);
  6. %typemap(mzscheme,in) (int argc, char *argv[]) {
  7. int i;
  8. Scheme_Object **elms;
  9. if (!SCHEME_VECTORP($input)) {
  10. scheme_wrong_type("$name","vector",$argnum,argc,argv);
  11. }
  12. $1 = SCHEME_VEC_SIZE($input);
  13. elms = SCHEME_VEC_ELS($input);
  14. if ($1 == 0) {
  15. scheme_wrong_type("$name","vector",$argnum,argc,argv);
  16. }
  17. $2 = (char **) malloc(($1+1)*sizeof(char *));
  18. for (i = 0; i < $1; i++) {
  19. if (!SCHEME_STRINGP(elms[i])) {
  20. free($2);
  21. scheme_wrong_type("$name","vector",$argnum,argc,argv);
  22. }
  23. $2[i] = SCHEME_STR_VAL(elms[i]);
  24. }
  25. $2[i] = 0;
  26. }
  27. %typemap(mzscheme,freear) (int argc, char *argv[]) {
  28. free($2);
  29. }
  30. extern int gcdmain(int argc, char *argv[]);
  31. %typemap(mzscheme,in) (char *bytes, int len) {
  32. if (!SCHEME_STRINGP($input)) {
  33. scheme_wrong_type("$name","string",1,argc,argv);
  34. }
  35. $1 = SCHEME_STR_VAL($input);
  36. $2 = SCHEME_STRLEN_VAL($input);
  37. }
  38. extern int count(char *bytes, int len, char c);
  39. /* This example shows how to wrap a function that mutates a string */
  40. %typemap(mzscheme,in) (char *str, int len) {
  41. if (!SCHEME_STRINGP($input)) {
  42. scheme_wrong_type("$name","string",1,argc,argv);
  43. }
  44. $2 = SCHEME_STRLEN_VAL($input);
  45. $1 = (char *) malloc($2+1);
  46. memmove($1,SCHEME_STR_VAL($input),$2);
  47. }
  48. /* Return the mutated string as a new object. */
  49. %typemap(mzscheme,argout) (char *str, int len) {
  50. Scheme_Object *s;
  51. s = scheme_make_sized_string($1,$2,1);
  52. SWIG_APPEND_VALUE(s);
  53. free($1);
  54. }
  55. extern void capitalize(char *str, int len);
  56. /* A multi-valued constraint. Force two arguments to lie
  57. inside the unit circle */
  58. %typemap(check) (double cx, double cy) {
  59. double a = $1*$1 + $2*$2;
  60. if (a > 1.0) {
  61. SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
  62. return NULL;
  63. }
  64. }
  65. extern void circle(double cx, double cy);