PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/chicken/multimap/example.i

#
Swig | 96 lines | 71 code | 18 blank | 7 comment | 0 complexity | 3ec51da38706cea5647a704977273ee9 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. if (!C_swig_is_vector ($input)) {
  17. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a vector");
  18. }
  19. $1 = C_header_size ($input);
  20. $2 = (char **) malloc(($1+1)*sizeof(char *));
  21. for (i = 0; i < $1; i++) {
  22. C_word o = C_block_item ($input, i);
  23. if (!C_swig_is_string (o)) {
  24. char err[50];
  25. free($2);
  26. sprintf (err, "$input[%d] is not a string", i);
  27. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, err);
  28. }
  29. $2[i] = C_c_string (o);
  30. }
  31. $2[i] = 0;
  32. }
  33. %typemap(freearg) (int argc, char *argv[]) {
  34. free($2);
  35. }
  36. extern int gcdmain(int argc, char *argv[]);
  37. %typemap(in) (char *bytes, int len) {
  38. if (!C_swig_is_string ($input)) {
  39. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a string");
  40. }
  41. $1 = C_c_string ($input);
  42. $2 = C_header_size ($input);
  43. }
  44. extern int count(char *bytes, int len, char c);
  45. /* This example shows how to wrap a function that mutates a string */
  46. %typemap(in) (char *str, int len)
  47. %{ if (!C_swig_is_string ($input)) {
  48. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a string");
  49. }
  50. $2 = C_header_size ($input);
  51. $1 = (char *) malloc ($2+1);
  52. memmove ($1, C_c_string ($input), $2);
  53. %}
  54. /* Return the mutated string as a new object. Notice the if MANY construct ... they must be at column 0. */
  55. %typemap(argout) (char *str, int len) (C_word *scmstr)
  56. %{ scmstr = C_alloc (C_SIZEOF_STRING ($2));
  57. SWIG_APPEND_VALUE(C_string (&scmstr, $2, $1));
  58. free ($1);
  59. %}
  60. extern void capitalize (char *str, int len);
  61. /* A multi-valued constraint. Force two arguments to lie
  62. inside the unit circle */
  63. %typemap(check) (double cx, double cy) {
  64. double a = $1*$1 + $2*$2;
  65. if (a > 1.0) {
  66. SWIG_exception (SWIG_ValueError, "cx and cy must be in unit circle");
  67. }
  68. }
  69. extern void circle (double cx, double cy);
  70. /* Test out multiple return values */
  71. extern int squareCubed (int n, int *OUTPUT);
  72. %{
  73. /* Returns n^3 and set n2 to n^2 */
  74. int squareCubed (int n, int *n2) {
  75. *n2 = n * n;
  76. return (*n2) * n;
  77. };
  78. %}