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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <TCollection_AsciiString.hxx>
23 %MappedType TCollection_AsciiString
26 #include <TCollection_AsciiString.hxx>
30 #include <qtextcodec.h>
35 #if PY_MAJOR_VERSION < 3
36 return (sipPy == Py_None || PyString_Check(sipPy) || PyUnicode_Check(sipPy));
38 return (sipPy == Py_None || PyUnicode_Check(sipPy));
43 // None is the only way to create a null (as opposed to empty) string.
44 *sipCppPtr = new TCollection_AsciiString();
46 return sipGetState(sipTransferObj);
49 #if PY_MAJOR_VERSION < 3
50 if ( PyString_Check( sipPy ) )
52 *sipCppPtr = new TCollection_AsciiString( PyString_AS_STRING( sipPy ) );
54 return sipGetState(sipTransferObj);
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));
66 // Note that this code doesn't handle code points greater than 0xffff very
69 Py_UNICODE *ucode = PyUnicode_AS_UNICODE(sipPy);
70 SIP_SSIZE_T len = PyUnicode_GET_SIZE(sipPy);
72 for (SIP_SSIZE_T i = 0; i < len; ++i)
73 aQStr.append((uint)ucode[i]);
76 aQStr = QString::fromUtf16((const ushort *)PyUnicode_AS_UNICODE(sipPy),
77 PyUnicode_GET_SIZE(sipPy));
80 TCollection_AsciiString aResStr;
83 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
86 QByteArray str = codec->fromUnicode( aQStr );
87 aResStr = TCollection_AsciiString( (Standard_CString)str.constData() );
90 aResStr = TCollection_AsciiString( aQStr.toLatin1().data() );
93 *sipCppPtr = new TCollection_AsciiString( aResStr );
95 return sipGetState(sipTransferObj);
101 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
102 if ( !sipCpp->IsEmpty() )
104 aQStr = codec ? codec->toUnicode( (char*)sipCpp->ToCString(), sipCpp->Length() ) :
105 QString( (char*)sipCpp->ToCString() );
110 #if defined(Py_UNICODE_WIDE)
111 #if QT_VERSION >= 0x040200
112 QVector<uint> ucs4 = aQStr.toUcs4();
114 if ((obj = PyUnicode_FromUnicode(NULL, ucs4.size())) == NULL)
117 memcpy(PyUnicode_AS_UNICODE(obj), ucs4.constData(),
118 ucs4.size() * sizeof (Py_UNICODE));
120 // Note that this code doesn't handle code points greater than 0xffff very
123 if ((obj = PyUnicode_FromUnicode(NULL, aQStr.length())) == NULL)
126 Py_UNICODE *pyu = PyUnicode_AS_UNICODE(obj);
128 for (int i = 0; i < aQStr.length(); ++i)
129 *pyu++ = (aQStr.at(i)).unicode();
132 if ((obj = PyUnicode_FromUnicode(NULL, aQStr.length())) == NULL)
135 memcpy(PyUnicode_AS_UNICODE(obj), aQStr.utf16(),
136 aQStr.length() * sizeof (Py_UNICODE));