1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 #include <TCollection_AsciiString.hxx>
27 %MappedType TCollection_AsciiString
30 #include <NCollection_Sequence.hxx>
34 #include <qtextcodec.h>
39 #if PY_MAJOR_VERSION < 3
40 return (sipPy == Py_None || PyString_Check(sipPy) || PyUnicode_Check(sipPy));
42 return (sipPy == Py_None || PyUnicode_Check(sipPy));
47 // None is the only way to create a null (as opposed to empty) string.
48 *sipCppPtr = new TCollection_AsciiString();
50 return sipGetState(sipTransferObj);
53 #if PY_MAJOR_VERSION < 3
54 if ( PyString_Check( sipPy ) )
56 *sipCppPtr = new TCollection_AsciiString( PyString_AS_STRING( sipPy ) );
58 return sipGetState(sipTransferObj);
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));
70 // Note that this code doesn't handle code points greater than 0xffff very
73 Py_UNICODE *ucode = PyUnicode_AS_UNICODE(sipPy);
74 SIP_SSIZE_T len = PyUnicode_GET_SIZE(sipPy);
76 for (SIP_SSIZE_T i = 0; i < len; ++i)
77 aQStr.append((uint)ucode[i]);
80 aQStr = QString::fromUtf16((const ushort *)PyUnicode_AS_UNICODE(sipPy),
81 PyUnicode_GET_SIZE(sipPy));
84 TCollection_AsciiString aResStr;
87 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
90 QByteArray str = codec->fromUnicode( aQStr );
91 aResStr = TCollection_AsciiString( (Standard_CString)str.constData() );
94 aResStr = TCollection_AsciiString( aQStr.toLatin1().data() );
97 *sipCppPtr = new TCollection_AsciiString( aResStr );
99 return sipGetState(sipTransferObj);
105 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
106 if ( !sipCpp->IsEmpty() )
108 aQStr = codec ? codec->toUnicode( (char*)sipCpp->ToCString(), sipCpp->Length() ) :
109 QString( (char*)sipCpp->ToCString() );
114 #if defined(Py_UNICODE_WIDE)
115 #if QT_VERSION >= 0x040200
116 QVector<uint> ucs4 = aQStr.toUcs4();
118 if ((obj = PyUnicode_FromUnicode(NULL, ucs4.size())) == NULL)
121 memcpy(PyUnicode_AS_UNICODE(obj), ucs4.constData(),
122 ucs4.size() * sizeof (Py_UNICODE));
124 // Note that this code doesn't handle code points greater than 0xffff very
127 if ((obj = PyUnicode_FromUnicode(NULL, aQStr.length())) == NULL)
130 Py_UNICODE *pyu = PyUnicode_AS_UNICODE(obj);
132 for (int i = 0; i < aQStr.length(); ++i)
133 *pyu++ = (aQStr.at(i)).unicode();
136 if ((obj = PyUnicode_FromUnicode(NULL, aQStr.length())) == NULL)
139 memcpy(PyUnicode_AS_UNICODE(obj), aQStr.utf16(),
140 aQStr.length() * sizeof (Py_UNICODE));