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