//
#include <HYDROData_StricklerTable.h>
+
#include <TDataStd_NamedData.hxx>
+#include <TDataStd_DataMapOfStringReal.hxx>
+#include <TDataStd_DataMapIteratorOfDataMapOfStringReal.hxx>
+
+#include <TCollection_AsciiString.hxx>
+#include <TCollection_ExtendedString.hxx>
+
+#include <QtCore/QString>
+#include <QtCore/QStringList>
+
IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
-IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
+ IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
-HYDROData_StricklerTable::HYDROData_StricklerTable()
+ HYDROData_StricklerTable::HYDROData_StricklerTable()
{
}
const ObjectKind HYDROData_StricklerTable::GetKind() const
{
- return KIND_STRICKLER_TABLE;
+ return KIND_STRICKLER_TABLE;
}
bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
{
- FILE* aFile = fopen( theFileName.ToCString(), "r" );
- if( !aFile )
- return false;
-
- const int aBufLen = 256;
- char aBuf[aBufLen];
- while( !feof( aFile ) )
- {
- fgets( aBuf, aBufLen, aFile );
- std::string aStr = aBuf;
- }
- return false;
+ std::ifstream aStream( theFileName.ToCString(), std::ios::in | std::ios::binary );
+ if ( !aStream )
+ return false;
+
+ bool aRes = true;
+
+ const int aBufLen = 512;
+ char aBuf[aBufLen];
+ while( !aStream.eof() && aRes )
+ {
+ aStream.getline( aBuf, aBufLen );
+ TCollection_ExtendedString aStr( aBuf, Standard_True );
+
+ Standard_Integer aPos = aStr.SearchFromEnd( " " );
+ if ( aPos < 0 )
+ aRes = false;
+ else
+ {
+ TCollection_ExtendedString aValueStr = aStr.Split( aPos );
+ while ( aStr.Length() > 0 && ( aStr.Value( aStr.Length() ) == ' ' || aStr.Value( aStr.Length() ) == '\t' ) )
+ aStr.Remove( aStr.Length() );
+ while ( aStr.Length() > 0 && ( aStr.Value( 1 ) == ' ' || aStr.Value( 1 ) == '\t' ) || aStr.Value( 1 ) < 0 )
+ aStr.Remove( 1 );
+
+ if ( aStr.Length() > 0 && aStr.Value( aStr.Length() ) == '\"' )
+ aStr.Remove( aStr.Length() );
+ if ( aStr.Length() > 0 && aStr.Value( 1 ) == '\"' )
+ aStr.Remove( 1 );
+
+ if ( aValueStr.IsEmpty() || aStr.IsEmpty() )
+ aRes = false;
+ else
+ {
+ Standard_Real aValue = TCollection_AsciiString( aValueStr ).RealValue();
+ Set( QString( (QChar*)aStr.ToExtString(), aStr.Length() ), aValue );
+ }
+ }
+ }
+
+ aStream.close();
+ return aRes;
}
bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
{
- //TODO
- return false;
+ Handle(TDataStd_NamedData) aMap = Map();
+ if ( aMap.IsNull() )
+ return false;
+
+ std::ofstream aStream( theFileName.ToCString(), std::ios::out | std::ios::binary );
+ if ( !aStream )
+ return false;
+
+ bool aRes = true;
+ for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
+ {
+ TCollection_ExtendedString aLine = TCollection_ExtendedString( '\"' ) + it.Key() + TCollection_ExtendedString( '\"' ) +
+ TCollection_ExtendedString( ' ' ) + TCollection_ExtendedString( it.Value() );
+ Standard_PCharacter aBuf = (Standard_PCharacter)malloc( aLine.LengthOfCString() + 1 );
+ aStream.write( aBuf, aLine.ToUTF8CString( aBuf ) );
+ aStream.write( "\r\n", 2 );
+ free( aBuf );
+ }
+
+ aStream.close();
+ return aRes;
+}
+
+double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
+{
+ Handle( TDataStd_NamedData ) aMap = Map();
+ TCollection_ExtendedString aType = toExtString( theType );
+ if( aMap->HasReal( aType ) )
+ return aMap->GetReal( aType );
+ else
+ return theDefault;
}
-double HYDROData_StricklerTable::Get( const TCollection_ExtendedString& theType, double theDefault ) const
+void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
{
- Handle( TDataStd_NamedData ) aMap = Map();
- if( aMap->HasReal( theType ) )
- return aMap->GetReal( theType );
- else
- return theDefault;
+ Handle(TDataStd_NamedData) aMap = Map();
+ aMap->SetReal( toExtString( theType ), theCoefficient );
}
-void HYDROData_StricklerTable::Set( const TCollection_ExtendedString& theType, double theCoefficient )
+void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
{
- Handle( TDataStd_NamedData ) aMap = Map();
- aMap->SetReal( theType, theCoefficient );
+ theMin = 0;
+ theMax = 0;
+
+ Handle(TDataStd_NamedData) aMap = Map();
+ Standard_Boolean isFirst = Standard_True;
+ for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) {
+ Standard_Real aValue = it.Value();
+ if ( theMin == 0 || aValue < theMin ) {
+ theMin = aValue;
+ }
+ if ( theMax == 0 || aValue > theMax ) {
+ theMax = aValue;
+ }
+ }
+}
+
+QStringList HYDROData_StricklerTable::GetTypes() const
+{
+ QStringList aSeq;
+ Handle(TDataStd_NamedData) aMap = Map();
+ if ( !aMap.IsNull() )
+ {
+ for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
+ aSeq.append( toQString( it.Key() ) );
+ }
+ return aSeq;
+}
+
+bool HYDROData_StricklerTable::HasType( const QString& theType ) const
+{
+ Handle(TDataStd_NamedData) aMap = Map();
+
+ TCollection_ExtendedString aType = toExtString( theType );
+ return !aMap.IsNull() && aMap->HasReal( aType );
+}
+
+void HYDROData_StricklerTable::Clear()
+{
+ Handle(TDataStd_NamedData) aMap = Map();
+ if ( !aMap.IsNull() )
+ aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
+}
+
+QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+{
+ QStringList aResList = dumpObjectCreation( theTreatedObjects );
+ QString aPyName = GetObjPyName();
+
+ aResList << QString( "" );
+ Handle(TDataStd_NamedData) aMap = Map();
+ if ( !aMap.IsNull() )
+ {
+ for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
+ {
+ TCollection_ExtendedString aType = it.Key();
+ Standard_Real aValue = it.Value();
+ aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
+ }
+ }
+ aResList << QString( "" );
+ aResList << QString( "%1.Update();" ).arg( aPyName );
+
+ return aResList;
+}
+
+Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
+{
+ TDF_Label aLabel = myLab.FindChild( DataTag_Table );
+ Handle( TDataStd_NamedData ) aMap;
+ if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
+ aMap = TDataStd_NamedData::Set( aLabel );
+ return aMap;
+}
+
+TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
+{
+ TCollection_ExtendedString aRes;
+ if( !theStr.isEmpty() )
+ {
+ Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
+ memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
+ ((short*)extStr)[theStr.length()] = '\0';
+
+ aRes = TCollection_ExtendedString( extStr );
+ delete [] extStr;
+ }
+ return aRes;
}
-Handle( TDataStd_NamedData ) HYDROData_StricklerTable::Map() const
+QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
{
- TDF_Label aLabel = myLab.FindChild( DataTag_Table );
- Handle( TDataStd_NamedData ) aMap;
- if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
- aMap = TDataStd_NamedData::Set( aLabel );
- return aMap;
+ return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
}