PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/python/jstring.i

#
Swig | 72 lines | 66 code | 6 blank | 0 comment | 0 complexity | 1ce9a906c1d1a8302b42e46cdc4b65ae MD5 | raw file
 1%include <typemaps/valtypes.swg>
 2
 3%fragment(SWIG_AsVal_frag(jstring),"header") {
 4SWIGINTERN int
 5SWIG_AsVal(jstring)(PyObject *obj, jstring *val)
 6{
 7  if (obj == Py_None) {
 8    if (val) *val = 0;
 9    return SWIG_OK;
10  }
11  
12  PyObject *tmp = 0;
13  int isunicode = PyUnicode_Check(obj);
14  if (!isunicode && PyString_Check(obj)) {
15    if (val) {
16      obj = tmp = PyUnicode_FromObject(obj);
17    }
18    isunicode = 1;
19  }
20  if (isunicode) {
21    if (val) {
22      if (sizeof(Py_UNICODE) == sizeof(jchar)) {
23	*val = JvNewString((const jchar *) PyUnicode_AS_UNICODE(obj),PyUnicode_GET_SIZE(obj));
24	return SWIG_NEWOBJ;
25      } else {
26	int len = PyUnicode_GET_SIZE(obj);
27	Py_UNICODE *pchars = PyUnicode_AS_UNICODE(obj);
28	*val = JvAllocString (len);
29	jchar *jchars = JvGetStringChars (*val);	
30	for (int i = 0; i < len; ++i) {
31	  jchars[i] = pchars[i];
32	}
33	return SWIG_NEWOBJ;
34      }
35    }
36    Py_XDECREF(tmp);
37    return SWIG_OK;
38  }
39  return SWIG_TypeError;
40}
41}
42
43%fragment(SWIG_From_frag(jstring),"header") {
44SWIGINTERNINLINE PyObject *
45SWIG_From(jstring)(jstring val)
46{
47  if (!val) {
48    return SWIG_Py_Void();
49  } 
50  if (sizeof(Py_UNICODE) == sizeof(jchar)) {    
51    return PyUnicode_FromUnicode((const Py_UNICODE *) JvGetStringChars(val),
52				 JvGetStringUTFLength(val));
53  } else {
54    int len = JvGetStringUTFLength(val);
55    Py_UNICODE pchars[len];
56    jchar *jchars = JvGetStringChars(val);
57    
58    for (int i = 0; i < len; i++) {      
59      pchars[i] = jchars[i];
60    }
61    return PyUnicode_FromUnicode((const Py_UNICODE *) pchars, len);
62  }
63}
64}
65
66%typemaps_asvalfrom(%checkcode(STRING),
67		    %arg(SWIG_AsVal(jstring)), 
68		    %arg(SWIG_From(jstring)), 
69		    %arg(SWIG_AsVal_frag(jstring)), 
70		    %arg(SWIG_From_frag(jstring)), 
71		    java::lang::String *);
72