cdata.i 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* -----------------------------------------------------------------------------
  2. * cdata.i
  3. *
  4. * SWIG library file containing macros for manipulating raw C data as strings.
  5. * ----------------------------------------------------------------------------- */
  6. %{
  7. typedef struct SWIGCDATA {
  8. char *data;
  9. int len;
  10. } SWIGCDATA;
  11. %}
  12. /* -----------------------------------------------------------------------------
  13. * Typemaps for returning binary data
  14. * ----------------------------------------------------------------------------- */
  15. #if SWIGGUILE
  16. %typemap(out) SWIGCDATA {
  17. $result = scm_from_locale_stringn($1.data,$1.len);
  18. }
  19. %typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH);
  20. #elif SWIGCHICKEN
  21. %typemap(out) SWIGCDATA {
  22. C_word *string_space = C_alloc(C_SIZEOF_STRING($1.len));
  23. $result = C_string(&string_space, $1.len, $1.data);
  24. }
  25. %typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH);
  26. #elif SWIGPHP
  27. %typemap(out) SWIGCDATA {
  28. ZVAL_STRINGL($result, $1.data, $1.len, 1);
  29. }
  30. %typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH);
  31. #elif SWIGJAVA
  32. %apply (char *STRING, int LENGTH) { (const void *indata, int inlen) }
  33. %typemap(jni) SWIGCDATA "jbyteArray"
  34. %typemap(jtype) SWIGCDATA "byte[]"
  35. %typemap(jstype) SWIGCDATA "byte[]"
  36. %fragment("SWIG_JavaArrayOutCDATA", "header") {
  37. static jbyteArray SWIG_JavaArrayOutCDATA(JNIEnv *jenv, char *result, jsize sz) {
  38. jbyte *arr;
  39. int i;
  40. jbyteArray jresult = JCALL1(NewByteArray, jenv, sz);
  41. if (!jresult)
  42. return NULL;
  43. arr = JCALL2(GetByteArrayElements, jenv, jresult, 0);
  44. if (!arr)
  45. return NULL;
  46. for (i=0; i<sz; i++)
  47. arr[i] = (jbyte)result[i];
  48. JCALL3(ReleaseByteArrayElements, jenv, jresult, arr, 0);
  49. return jresult;
  50. }
  51. }
  52. %typemap(out, fragment="SWIG_JavaArrayOutCDATA") SWIGCDATA
  53. %{$result = SWIG_JavaArrayOutCDATA(jenv, (char *)$1.data, $1.len); %}
  54. %typemap(javaout) SWIGCDATA {
  55. return $jnicall;
  56. }
  57. #else
  58. %echo "cdata.i module not supported."
  59. #endif
  60. /* -----------------------------------------------------------------------------
  61. * %cdata(TYPE [, NAME])
  62. *
  63. * Convert raw C data to a binary string.
  64. * ----------------------------------------------------------------------------- */
  65. %define %cdata(TYPE,NAME...)
  66. %insert("header") {
  67. #if #NAME == ""
  68. static SWIGCDATA cdata_##TYPE(TYPE *ptr, int nelements) {
  69. #else
  70. static SWIGCDATA cdata_##NAME(TYPE *ptr, int nelements) {
  71. #endif
  72. SWIGCDATA d;
  73. d.data = (char *) ptr;
  74. #if #TYPE != "void"
  75. d.len = nelements*sizeof(TYPE);
  76. #else
  77. d.len = nelements;
  78. #endif
  79. return d;
  80. }
  81. }
  82. %typemap(default) int nelements "$1 = 1;"
  83. #if #NAME == ""
  84. SWIGCDATA cdata_##TYPE(TYPE *ptr, int nelements);
  85. #else
  86. SWIGCDATA cdata_##NAME(TYPE *ptr, int nelements);
  87. #endif
  88. %enddef
  89. %typemap(default) int nelements;
  90. %rename(cdata) ::cdata_void(void *ptr, int nelements);
  91. %cdata(void);
  92. /* Memory move function. Due to multi-argument typemaps this appears to be wrapped as
  93. void memmove(void *data, const char *s); */
  94. void memmove(void *data, const void *indata, int inlen);