1 /* ------------------------------------------------------------ 2 * utility methods for wchar_t strings 3 * ------------------------------------------------------------ */ 4 5 %{ 6 #if PY_VERSION_HEX >= 0x03020000 7 # define SWIGPY_UNICODE_ARG(obj) ((PyObject*) (obj)) 8 #else 9 # define SWIGPY_UNICODE_ARG(obj) ((PyUnicodeObject*) (obj)) 10 #endif 11 %} 12 13 %fragment("SWIG_AsWCharPtrAndSize","header",fragment="<wchar.h>",fragment="SWIG_pwchar_descriptor") { 14 SWIGINTERN int 15 SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc) 16 { 17 PyObject *tmp = 0; 18 int isunicode = PyUnicode_Check(obj); 19 %#if PY_VERSION_HEX < 0x03000000 20 if (!isunicode && PyString_Check(obj)) { 21 obj = tmp = PyUnicode_FromObject(obj); 22 isunicode = 1; 23 } 24 %#endif 25 if (isunicode) { 26 Py_ssize_t len = PyUnicode_GetSize(obj); 27 if (cptr) { 28 *cptr = %new_array(len + 1, wchar_t); 29 PyUnicode_AsWideChar(SWIGPY_UNICODE_ARG(obj), *cptr, len); 30 (*cptr)[len] = 0; 31 } 32 if (psize) *psize = (size_t) len + 1; 33 if (alloc) *alloc = cptr ? SWIG_NEWOBJ : 0; 34 Py_XDECREF(tmp); 35 return SWIG_OK; 36 } else { 37 swig_type_info* pwchar_descriptor = SWIG_pwchar_descriptor(); 38 if (pwchar_descriptor) { 39 void * vptr = 0; 40 if (SWIG_ConvertPtr(obj, &vptr, pwchar_descriptor, 0) == SWIG_OK) { 41 if (cptr) *cptr = (wchar_t *)vptr; 42 if (psize) *psize = vptr ? (wcslen((wchar_t *)vptr) + 1) : 0; 43 return SWIG_OK; 44 } 45 } 46 } 47 return SWIG_TypeError; 48 } 49 } 50 51 %fragment("SWIG_FromWCharPtrAndSize","header",fragment="<wchar.h>",fragment="SWIG_pwchar_descriptor") { 52 SWIGINTERNINLINE PyObject * 53 SWIG_FromWCharPtrAndSize(const wchar_t * carray, size_t size) 54 { 55 if (carray) { 56 if (size > INT_MAX) { 57 swig_type_info* pwchar_descriptor = SWIG_pwchar_descriptor(); 58 return pwchar_descriptor ? 59 SWIG_InternalNewPointerObj(%const_cast(carray,wchar_t *), pwchar_descriptor, 0) : SWIG_Py_Void(); 60 } else { 61 return PyUnicode_FromWideChar(carray, %numeric_cast(size,int)); 62 } 63 } else { 64 return SWIG_Py_Void(); 65 } 66 } 67 } 68 69 70