rubystrings.swg 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* ------------------------------------------------------------
  2. * utility methods for char strings
  3. * ------------------------------------------------------------ */
  4. %fragment("SWIG_AsCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") {
  5. SWIGINTERN int
  6. SWIG_AsCharPtrAndSize(VALUE obj, char** cptr, size_t* psize, int *alloc)
  7. {
  8. if (TYPE(obj) == T_STRING) {
  9. char *cstr = StringValuePtr(obj);
  10. size_t size = RSTRING_LEN(obj) + 1;
  11. if (cptr) {
  12. if (alloc) {
  13. if (*alloc == SWIG_NEWOBJ) {
  14. *cptr = %new_copy_array(cstr, size, char);
  15. } else {
  16. *cptr = cstr;
  17. *alloc = SWIG_OLDOBJ;
  18. }
  19. }
  20. }
  21. if (psize) *psize = size;
  22. return SWIG_OK;
  23. } else {
  24. swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
  25. if (pchar_descriptor) {
  26. void* vptr = 0;
  27. if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) {
  28. if (cptr) *cptr = (char *)vptr;
  29. if (psize) *psize = vptr ? (strlen((char*)vptr) + 1) : 0;
  30. if (alloc) *alloc = SWIG_OLDOBJ;
  31. return SWIG_OK;
  32. }
  33. }
  34. }
  35. return SWIG_TypeError;
  36. }
  37. }
  38. %fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") {
  39. SWIGINTERNINLINE VALUE
  40. SWIG_FromCharPtrAndSize(const char* carray, size_t size)
  41. {
  42. if (carray) {
  43. if (size > LONG_MAX) {
  44. swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
  45. return pchar_descriptor ?
  46. SWIG_NewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : Qnil;
  47. } else {
  48. return rb_str_new(carray, %numeric_cast(size,long));
  49. }
  50. } else {
  51. return Qnil;
  52. }
  53. }
  54. }