PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/tcl/tclwstrings.swg

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