PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Swig | 91 lines | 68 code | 18 blank | 5 comment | 0 complexity | e0e694f1608d2a44d3c405801ecaee94 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* File : example.i */
  2. %module example
  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. %include exception.i
  12. %include typemaps.i
  13. extern int gcd(int x, int y);
  14. %typemap(in) (int argc, char *argv[]) {
  15. int i;
  16. Scheme_Object **elms;
  17. if (!SCHEME_VECTORP($input)) {
  18. scheme_wrong_type("$name","vector",$argnum,argc,argv);
  19. }
  20. $1 = SCHEME_VEC_SIZE($input);
  21. elms = SCHEME_VEC_ELS($input);
  22. if ($1 == 0) {
  23. scheme_wrong_type("$name","vector",$argnum,argc,argv);
  24. }
  25. $2 = (char **) malloc(($1+1)*sizeof(char *));
  26. for (i = 0; i < $1; i++) {
  27. if (!SCHEME_STRINGP(elms[i])) {
  28. free($2);
  29. scheme_wrong_type("$name","vector",$argnum,argc,argv);
  30. }
  31. $2[i] = SCHEME_STR_VAL(elms[i]);
  32. }
  33. $2[i] = 0;
  34. }
  35. %typemap(freearg) (int argc, char *argv[]) {
  36. free($2);
  37. }
  38. extern int gcdmain(int argc, char *argv[]);
  39. %typemap(in) (char *bytes, int len) {
  40. if (!SCHEME_STRINGP($input)) {
  41. scheme_wrong_type("$name","string",1,argc,argv);
  42. }
  43. $1 = SCHEME_STR_VAL($input);
  44. $2 = SCHEME_STRLEN_VAL($input);
  45. }
  46. extern int count(char *bytes, int len, char c);
  47. /* This example shows how to wrap a function that mutates a string */
  48. %typemap(in) (char *str, int len) {
  49. if (!SCHEME_STRINGP($input)) {
  50. scheme_wrong_type("$name","string",1,argc,argv);
  51. }
  52. $2 = SCHEME_STRLEN_VAL($input);
  53. $1 = (char *) malloc($2+1);
  54. memmove($1,SCHEME_STR_VAL($input),$2);
  55. }
  56. /* Return the mutated string as a new object. */
  57. %typemap(argout) (char *str, int len) {
  58. Scheme_Object *s;
  59. s = scheme_make_sized_string($1,$2,1);
  60. SWIG_APPEND_VALUE(s);
  61. free($1);
  62. }
  63. extern void capitalize(char *str, int len);
  64. /* A multi-valued constraint. Force two arguments to lie
  65. inside the unit circle */
  66. %typemap(check) (double cx, double cy) {
  67. double a = $1*$1 + $2*$2;
  68. if (a > 1.0) {
  69. SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
  70. return NULL;
  71. }
  72. }
  73. extern void circle(double cx, double cy);