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