1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <YACSPrs_toString.h>
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
24 #include <TypeConversions.hxx>
26 #include <TypeCode.hxx>
28 using namespace YACS::ENGINE;
30 char * toString(CORBA::Any* theAny, QString& theRetStr)
32 if ( !theAny ) theRetStr += QString("< ? >");
35 CORBA::Any anAny = *theAny;
36 if ( !anAny.value() ) theRetStr += QString("< ? >");
39 std::ostringstream astr;
42 switch ( anAny.type()->kind() )
44 case CORBA::tk_string: {
46 theRetStr += QString(retstr);
49 case CORBA::tk_long: {
52 astr << l << std::ends;
53 theRetStr += QString(astr.str());
56 case CORBA::tk_double: {
59 astr << d << std::ends;
60 //astr << setw(25) << setprecision(18) << d << std::ends;
61 QString aRetStr = QString(astr.str());
63 while ( i < (int ) theRetStr.length() && theRetStr.at(i++) == ' ' ) {
66 theRetStr += aRetStr.mid(startstr,aRetStr.length());
69 case CORBA::tk_sequence: {
70 theRetStr += QString("[");
72 CORBA::Long aSeqLength = 0;
73 *(anAny.type()->parameter(1)) >>= aSeqLength;
75 if ( aSeqLength == 0 )
77 theRetStr += QString(" ]");
81 // TO DO : implement recursion for the sequence type
82 /*CORBA::TypeCode* aType;
83 *(anAny.type()->parameter(0)) >>= aType;
84 switch ( aType->kind() )
86 case CORBA::tk_string: {
87 printf("StringElem\n");
88 CORBA::StringSeq* aStringSeq;
90 for (int i=0; i < aSeqLength; i++)
93 anArg <<= aStringSeq[i];
94 toString( &anArg, theRetStr );
95 if ( i < aSeqLength-1 ) theRetStr += QString(",");
99 case CORBA::tk_double: {
100 printf("DoubleElem\n");
101 CORBA::DoubleSeq* aDoubleSeq;
102 anAny >>= aDoubleSeq;
103 for (int i=0; i < aSeqLength; i++)
106 anArg <<= aDoubleSeq[i];
107 toString( &anArg, theRetStr );
108 if ( i < aSeqLength-1 ) theRetStr += QString(",");
112 case CORBA::tk_sequence: {
113 printf("SequenceElem\n");
114 CORBA::Any* aSequenceSeq;
115 anAny >>= aSequenceSeq;
116 for (int i=0; i < aSeqLength; i++)
119 anArg <<= aSequenceSeq[i];
120 toString( &anArg, theRetStr );
121 if ( i < aSeqLength-1 ) theRetStr += QString(",");
126 printf("DefaultElem\n");
127 theRetStr += QString("< ? >");
131 theRetStr += QString("]");
134 case CORBA::tk_objref: {
135 /*CORBA::Object_ptr obj;
137 anAny >>= (CORBA::Any::to_object ) obj;
138 theRetStr += QString( _Orb->object_to_string( obj ) );
141 theRetStr += QString("object_to_string catched ");
143 theRetStr += QString("Objref");
147 theRetStr += QString("< ? >");
155 void toString(PyObject* theObject, QString& theRetStr)
157 if ( !theObject ) theRetStr += QString("< ? >");
159 std::ostringstream aStr;
160 if ( PyString_CheckExact(theObject) )
161 theRetStr += QString( PyString_AsString(theObject) );
162 else if ( PyLong_CheckExact(theObject) )
164 long aVal = PyLong_AsLong(theObject);
165 aStr << aVal << std::ends;
166 theRetStr += QString( aStr.str() );
168 else if ( PyInt_CheckExact(theObject) )
170 long aVal = PyInt_AsLong(theObject);
171 aStr << aVal << std::ends;
172 theRetStr += QString( aStr.str() );
174 else if ( PyBool_Check(theObject) )
175 theRetStr += QString( (theObject == Py_True) ? "true" : "false" );
176 else if ( PyFloat_CheckExact(theObject) )
178 double aVal = PyFloat_AsDouble(theObject);
179 aStr << aVal << std::ends;
180 theRetStr += QString( aStr.str() );
182 else if ( PyList_CheckExact(theObject) )
184 theRetStr += QString("[");
185 for (int i=0; i < PyList_Size(theObject); i++)
187 toString( PyList_GetItem(theObject, i), theRetStr );
188 if ( i < PyList_Size(theObject)-1 ) theRetStr += QString(",");
190 theRetStr += QString("]");
192 //else if ( ... ) // objref case
194 theRetStr += QString("< ? >");
197 void toString(YACS::ENGINE::Any* theAny, QString& theValue)
199 if ( !theAny ) theValue += QString("< ? >");
200 else if ( theAny->getType() )
202 DynType aKind = theAny->getType()->kind();
206 theValue += QString::number(theAny->getDoubleValue());
209 theValue += QString::number(theAny->getIntValue());
212 theValue += QString(theAny->getStringValue());
215 theValue += QString(theAny->getBoolValue()?"true":"false");
218 theValue += QString("Objref"); /// ?
221 SequenceAny* aSeqAny = dynamic_cast<SequenceAny*>( theAny );
224 theValue += QString("[");
225 for (int i=0; i < aSeqAny->size(); i++)
227 toString( (*aSeqAny)[i], theValue );
228 if ( i < aSeqAny->size()-1 ) theValue += QString(",");
230 theValue += QString("]");
236 theValue += QString("");
242 void toString(const std::string& value, const TypeCode * t,QString& theRetStr)
251 YACS::ENGINE::Any *ob=NULL;
252 doc = xmlParseMemory(value.c_str(), value.size());
258 cur = xmlDocGetRootElement(doc);
267 if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
271 ob=convertXmlNeutral(t,doc,cur);
287 toString(ob,theRetStr);