--- /dev/null
+// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+%ExportedHeaderCode
+#include <TCollection_AsciiString.hxx>
+%End
+
+%MappedType TCollection_AsciiString
+{
+%TypeHeaderCode
+#include <NCollection_Sequence.hxx>
+%End
+
+%TypeCode
+#include <qtextcodec.h>
+%End
+
+%ConvertToTypeCode
+ if (sipIsErr == NULL)
+#if PY_MAJOR_VERSION < 3
+ return (sipPy == Py_None || PyString_Check(sipPy) || PyUnicode_Check(sipPy));
+#else
+ return (sipPy == Py_None || PyUnicode_Check(sipPy));
+#endif
+
+ if (sipPy == Py_None)
+ {
+ // None is the only way to create a null (as opposed to empty) string.
+ *sipCppPtr = new TCollection_AsciiString();
+
+ return sipGetState(sipTransferObj);
+ }
+
+#if PY_MAJOR_VERSION < 3
+ if ( PyString_Check( sipPy ) )
+ {
+ *sipCppPtr = new TCollection_AsciiString( PyString_AS_STRING( sipPy ) );
+
+ return sipGetState(sipTransferObj);
+ }
+#endif
+
+ QString aQStr;
+
+// Function qpycore_PyObject_AsQString()
+#if defined(Py_UNICODE_WIDE)
+#if QT_VERSION >= 0x040200
+ aQStr = QString::fromUcs4((const uint *)PyUnicode_AS_UNICODE(sipPy),
+ PyUnicode_GET_SIZE(sipPy));
+#else
+ // Note that this code doesn't handle code points greater than 0xffff very
+ // well.
+
+ Py_UNICODE *ucode = PyUnicode_AS_UNICODE(sipPy);
+ SIP_SSIZE_T len = PyUnicode_GET_SIZE(sipPy);
+
+ for (SIP_SSIZE_T i = 0; i < len; ++i)
+ aQStr.append((uint)ucode[i]);
+#endif
+#else
+ aQStr = QString::fromUtf16((const ushort *)PyUnicode_AS_UNICODE(sipPy),
+ PyUnicode_GET_SIZE(sipPy));
+#endif
+
+ TCollection_AsciiString aResStr;
+ if( !aQStr.isNull() )
+ {
+ QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
+ if( codec )
+ {
+ QByteArray str = codec->fromUnicode( aQStr );
+ aResStr = TCollection_AsciiString( (Standard_CString)str.constData() );
+ }
+ else
+ aResStr = TCollection_AsciiString( aQStr.toLatin1().data() );
+ }
+
+ *sipCppPtr = new TCollection_AsciiString( aResStr );
+
+ return sipGetState(sipTransferObj);
+%End
+
+%ConvertFromTypeCode
+ QString aQStr;
+
+ QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
+ if ( !sipCpp->IsEmpty() )
+ {
+ aQStr = codec ? codec->toUnicode( (char*)sipCpp->ToCString(), sipCpp->Length() ) :
+ QString( (char*)sipCpp->ToCString() );
+ }
+
+ PyObject *obj;
+
+#if defined(Py_UNICODE_WIDE)
+#if QT_VERSION >= 0x040200
+ QVector<uint> ucs4 = aQStr.toUcs4();
+
+ if ((obj = PyUnicode_FromUnicode(NULL, ucs4.size())) == NULL)
+ return NULL;
+
+ memcpy(PyUnicode_AS_UNICODE(obj), ucs4.constData(),
+ ucs4.size() * sizeof (Py_UNICODE));
+#else
+ // Note that this code doesn't handle code points greater than 0xffff very
+ // well.
+
+ if ((obj = PyUnicode_FromUnicode(NULL, aQStr.length())) == NULL)
+ return NULL;
+
+ Py_UNICODE *pyu = PyUnicode_AS_UNICODE(obj);
+
+ for (int i = 0; i < aQStr.length(); ++i)
+ *pyu++ = (aQStr.at(i)).unicode();
+#endif
+#else
+ if ((obj = PyUnicode_FromUnicode(NULL, aQStr.length())) == NULL)
+ return NULL;
+
+ memcpy(PyUnicode_AS_UNICODE(obj), aQStr.utf16(),
+ aQStr.length() * sizeof (Py_UNICODE));
+#endif
+
+ return obj;
+%End
+
+};
+