PageRenderTime 173ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Root-branch-php-utl/SWIG/Examples/perl5/multimap/example.i

#
Swig | 93 lines | 68 code | 20 blank | 5 comment | 0 complexity | e1b80056c7cfd73ff730b74ce9e9ab6e 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(arginit) (int argc, char *argv[]) "$2 = 0;";
  15. %typemap(in) (int argc, char *argv[]) {
  16. AV *tempav;
  17. SV **tv;
  18. I32 len;
  19. int i;
  20. if (!SvROK($input)) {
  21. SWIG_exception(SWIG_ValueError,"$input is not an array.");
  22. }
  23. if (SvTYPE(SvRV($input)) != SVt_PVAV) {
  24. SWIG_exception(SWIG_ValueError,"$input is not an array.");
  25. }
  26. tempav = (AV*)SvRV($input);
  27. len = av_len(tempav);
  28. $1 = (int) len+1;
  29. $2 = (char **) malloc(($1+1)*sizeof(char *));
  30. for (i = 0; i < $1; i++) {
  31. tv = av_fetch(tempav, i, 0);
  32. $2[i] = (char *) SvPV(*tv,PL_na);
  33. }
  34. $2[i] = 0;
  35. }
  36. %typemap(freearg) (int argc, char *argv[]) {
  37. free($2);
  38. }
  39. extern int gcdmain(int argc, char *argv[]);
  40. %typemap(in) (char *bytes, int len) {
  41. STRLEN temp;
  42. $1 = (char *) SvPV($input, temp);
  43. $2 = (int) temp;
  44. }
  45. extern int count(char *bytes, int len, char c);
  46. /* This example shows how to wrap a function that mutates a string */
  47. %typemap(in) (char *str, int len) {
  48. STRLEN templen;
  49. char *temp;
  50. temp = (char *) SvPV($input,templen);
  51. $2 = (int) templen;
  52. $1 = (char *) malloc($2+1);
  53. memmove($1,temp,$2);
  54. }
  55. /* Return the mutated string as a new object. */
  56. %typemap(argout) (char *str, int len) {
  57. if (argvi >= items) {
  58. EXTEND(sp,1);
  59. }
  60. $result = sv_newmortal();
  61. sv_setpvn((SV*)ST(argvi++),$1,$2);
  62. free($1);
  63. }
  64. extern void capitalize(char *str, int len);
  65. /* A multi-valued constraint. Force two arguments to lie
  66. inside the unit circle */
  67. %typemap(check) (double cx, double cy) {
  68. double a = $1*$1 + $2*$2;
  69. if (a > 1.0) {
  70. SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
  71. }
  72. }
  73. extern void circle(double cx, double cy);