Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.0 KiB

  1. /* -----------------------------------------------------------------------------
  2. * See the LICENSE file for information on copyright, usage and redistribution
  3. * of SWIG, and the README file for authors - http://www.swig.org/release.html.
  4. *
  5. * tclwstrings.wg
  6. *
  7. * Utility methods for wchar strings
  8. * ----------------------------------------------------------------------------- */
  9. %{
  10. #include <wchar.h>
  11. %}
  12. %fragment("SWIG_AsWCharPtrAndSize","header") {
  13. SWIGINTERN int
  14. SWIG_AsWCharPtrAndSize(Tcl_Obj *obj, wchar_t** cptr, size_t* psize, int *alloc)
  15. {
  16. int len = 0;
  17. Tcl_UniChar *ustr = Tcl_GetUnicodeFromObj(obj, &len);
  18. if (ustr) {
  19. if (cptr) {
  20. Tcl_Encoding encoding = NULL;
  21. char *src = (char *) ustr;
  22. int srcLen = (len)*sizeof(Tcl_UniChar);
  23. int dstLen = sizeof(wchar_t)*(len + 1);
  24. char *dst = %new_array(dstLen, char);
  25. int flags = 0;
  26. Tcl_EncodingState *statePtr = 0;
  27. int srcRead = 0;
  28. int dstWrote = 0;
  29. int dstChars = 0;
  30. Tcl_UtfToExternal(0, encoding, src, srcLen, flags, statePtr, dst,
  31. dstLen, &srcRead, &dstWrote, &dstChars);
  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. %fragment("SWIG_FromWCharPtrAndSize","header") {
  41. SWIGINTERNINLINE Tcl_Obj *
  42. SWIG_FromWCharPtrAndSize(const wchar_t* carray, size_t size)
  43. {
  44. Tcl_Obj *res = NULL;
  45. if (size < INT_MAX) {
  46. Tcl_Encoding encoding = NULL;
  47. char *src = (char *) carray;
  48. int srcLen = (int)(size*sizeof(wchar_t));
  49. int dstLen = (int)(size*sizeof(Tcl_UniChar));
  50. char *dst = %new_array(dstLen, char);
  51. int flags = 0;
  52. Tcl_EncodingState *statePtr = 0;
  53. int srcRead = 0;
  54. int dstWrote = 0;
  55. int dstChars = 0;
  56. Tcl_ExternalToUtf(0, encoding, src, srcLen, flags, statePtr, dst,
  57. dstLen, &srcRead, &dstWrote, &dstChars);
  58. res = Tcl_NewUnicodeObj((Tcl_UniChar*)dst, (int)size);
  59. %delete_array(dst);
  60. }
  61. return res;
  62. }
  63. }