1 /* ----------------------------------------------------------------------------- 2 * tclwstrings.wg 3 * 4 * Utility methods for wchar strings 5 * ----------------------------------------------------------------------------- */ 6 7 %{ 8 #include <wchar.h> 9 %} 10 11 %fragment("SWIG_AsWCharPtrAndSize","header") { 12 SWIGINTERN int 13 SWIG_AsWCharPtrAndSize(Tcl_Obj *obj, wchar_t** cptr, size_t* psize, int *alloc) 14 { 15 int len = 0; 16 Tcl_UniChar *ustr = Tcl_GetUnicodeFromObj(obj, &len); 17 if (ustr) { 18 if (cptr) { 19 Tcl_Encoding encoding = NULL; 20 char *src = (char *) ustr; 21 int srcLen = (len)*sizeof(Tcl_UniChar); 22 int dstLen = sizeof(wchar_t)*(len + 1); 23 char *dst = %new_array(dstLen, char); 24 int flags = 0; 25 Tcl_EncodingState *statePtr = 0; 26 int srcRead = 0; 27 int dstWrote = 0; 28 int dstChars = 0; 29 Tcl_UtfToExternal(0, encoding, src, srcLen, flags, statePtr, dst, 30 dstLen, &srcRead, &dstWrote, &dstChars); 31 32 if (alloc) *alloc = SWIG_NEWOBJ; 33 } 34 if (psize) *psize = len + 1; 35 return SWIG_OK; 36 } 37 return SWIG_TypeError; 38 } 39 } 40 41 %fragment("SWIG_FromWCharPtrAndSize","header") { 42 SWIGINTERNINLINE Tcl_Obj * 43 SWIG_FromWCharPtrAndSize(const wchar_t* carray, size_t size) 44 { 45 Tcl_Obj *res = NULL; 46 if (size < INT_MAX) { 47 Tcl_Encoding encoding = NULL; 48 char *src = (char *) carray; 49 int srcLen = (int)(size*sizeof(wchar_t)); 50 int dstLen = (int)(size*sizeof(Tcl_UniChar)); 51 char *dst = %new_array(dstLen, char); 52 int flags = 0; 53 Tcl_EncodingState *statePtr = 0; 54 int srcRead = 0; 55 int dstWrote = 0; 56 int dstChars = 0; 57 58 Tcl_ExternalToUtf(0, encoding, src, srcLen, flags, statePtr, dst, 59 dstLen, &srcRead, &dstWrote, &dstChars); 60 61 res = Tcl_NewUnicodeObj((Tcl_UniChar*)dst, (int)size); 62 %delete_array(dst); 63 } 64 return res; 65 } 66 } 67 68