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