Salome HOME
Merge branch 'BR_HYDRO_IMPS_WIN' of ssh://gitolite3@git.salome-platform.org/modules...
[modules/hydro.git] / src / HYDROPy / CAS / TCollection_AsciiString.sip
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 %ExportedHeaderCode
20 #include <TCollection_AsciiString.hxx>
21 %End
22
23 %MappedType TCollection_AsciiString
24 {
25 %TypeHeaderCode
26 #include <TCollection_AsciiString.hxx>
27 %End
28
29 %TypeCode
30 #include <qtextcodec.h>
31 %End
32
33 %ConvertToTypeCode
34     if (sipIsErr == NULL)
35 #if PY_MAJOR_VERSION < 3
36       return (sipPy == Py_None || PyString_Check(sipPy) || PyUnicode_Check(sipPy));
37 #else
38       return (sipPy == Py_None || PyUnicode_Check(sipPy));
39 #endif
40
41     if (sipPy == Py_None)
42     {
43       // None is the only way to create a null (as opposed to empty) string.
44       *sipCppPtr = new TCollection_AsciiString();
45
46       return sipGetState(sipTransferObj);
47     }
48
49 #if PY_MAJOR_VERSION < 3
50     if ( PyString_Check( sipPy ) )
51     {
52       *sipCppPtr = new TCollection_AsciiString( PyString_AS_STRING( sipPy ) );
53
54       return sipGetState(sipTransferObj);
55     }
56 #endif
57
58     QString aQStr;
59
60 // Function qpycore_PyObject_AsQString()
61 #if defined(Py_UNICODE_WIDE)
62 #if QT_VERSION >= 0x040200
63     aQStr = QString::fromUcs4((const uint *)PyUnicode_AS_UNICODE(sipPy),
64             PyUnicode_GET_SIZE(sipPy));
65 #else
66     // Note that this code doesn't handle code points greater than 0xffff very
67     // well.
68
69     Py_UNICODE *ucode = PyUnicode_AS_UNICODE(sipPy);
70     SIP_SSIZE_T len = PyUnicode_GET_SIZE(sipPy);
71
72     for (SIP_SSIZE_T i = 0; i < len; ++i)
73         aQStr.append((uint)ucode[i]);
74 #endif
75 #else
76     aQStr = QString::fromUtf16((const ushort *)PyUnicode_AS_UNICODE(sipPy),
77             PyUnicode_GET_SIZE(sipPy));
78 #endif
79
80     TCollection_AsciiString aResStr;
81     if( !aQStr.isNull() )
82     {
83       QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
84       if( codec )
85       {
86         QByteArray str = codec->fromUnicode( aQStr );
87         aResStr = TCollection_AsciiString( (Standard_CString)str.constData() );
88       }
89       else
90         aResStr = TCollection_AsciiString( aQStr.toLatin1().data() );
91     }
92
93     *sipCppPtr = new TCollection_AsciiString( aResStr );
94
95     return sipGetState(sipTransferObj);
96 %End
97
98 %ConvertFromTypeCode
99     QString aQStr;
100     
101     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
102     if ( !sipCpp->IsEmpty() )
103     {
104       aQStr = codec ? codec->toUnicode( (char*)sipCpp->ToCString(), sipCpp->Length() ) :
105                       QString( (char*)sipCpp->ToCString() );
106     }
107     
108     PyObject *obj;
109
110 #if defined(Py_UNICODE_WIDE)
111 #if QT_VERSION >= 0x040200
112     QVector<uint> ucs4 = aQStr.toUcs4();
113
114     if ((obj = PyUnicode_FromUnicode(NULL, ucs4.size())) == NULL)
115         return NULL;
116
117     memcpy(PyUnicode_AS_UNICODE(obj), ucs4.constData(),
118             ucs4.size() * sizeof (Py_UNICODE));
119 #else
120     // Note that this code doesn't handle code points greater than 0xffff very
121     // well.
122
123     if ((obj = PyUnicode_FromUnicode(NULL, aQStr.length())) == NULL)
124         return NULL;
125
126     Py_UNICODE *pyu = PyUnicode_AS_UNICODE(obj);
127
128     for (int i = 0; i < aQStr.length(); ++i)
129         *pyu++ = (aQStr.at(i)).unicode();
130 #endif
131 #else
132     if ((obj = PyUnicode_FromUnicode(NULL, aQStr.length())) == NULL)
133         return NULL;
134
135     memcpy(PyUnicode_AS_UNICODE(obj), aQStr.utf16(),
136             aQStr.length() * sizeof (Py_UNICODE));
137 #endif
138
139     return obj;
140 %End
141   
142 };
143