Salome HOME
Merge branch 'BR_v14_rc' of ssh://git.salome-platform.org/modules/hydro into BR_v14_rc
[modules/hydro.git] / src / HYDROData / HYDROData_StricklerTable.cxx
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, or (at your option) any later version.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROData_StricklerTable.h>
20
21 #include <TDataStd_NamedData.hxx>
22
23 #include <TDataStd_DataMapOfStringReal.hxx>
24 #include <TDataStd_DataMapIteratorOfDataMapOfStringReal.hxx>
25
26 #include <TCollection_AsciiString.hxx>
27 #include <TCollection_ExtendedString.hxx>
28
29 #include <QtCore/QString>
30 #include <QtCore/QStringList>
31
32 IMPLEMENT_STANDARD_HANDLE( HYDROData_StricklerTable, HYDROData_Entity )
33     IMPLEMENT_STANDARD_RTTIEXT( HYDROData_StricklerTable, HYDROData_Entity )
34
35     HYDROData_StricklerTable::HYDROData_StricklerTable()
36 {
37 }
38
39 HYDROData_StricklerTable::~HYDROData_StricklerTable()
40 {
41 }
42
43 const ObjectKind HYDROData_StricklerTable::GetKind() const
44 {
45     return KIND_STRICKLER_TABLE;
46 }
47
48 bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
49 {
50     std::ifstream aStream( theFileName.ToCString(), std::ios::in | std::ios::binary );
51     if ( !aStream )
52         return false;
53
54     bool aRes = true;
55
56     const int aBufLen = 512;
57     char aBuf[aBufLen];
58     while( !aStream.eof() && aRes )
59     {
60         aStream.getline( aBuf, aBufLen );
61         TCollection_ExtendedString aStr( aBuf, Standard_True );
62
63         Standard_Integer aPos = aStr.SearchFromEnd( " " );
64         if ( aPos < 0 )
65             aRes = false;
66         else
67         {
68             TCollection_ExtendedString aValueStr = aStr.Split( aPos );
69             while ( aStr.Length() > 0 && ( aStr.Value( aStr.Length() ) == ' ' || aStr.Value( aStr.Length() ) == '\t' ) )
70                 aStr.Remove( aStr.Length() );
71             while ( aStr.Length() > 0 && ( aStr.Value( 1 ) == ' ' || aStr.Value( 1 ) == '\t' ) || aStr.Value( 1 ) < 0 )
72                 aStr.Remove( 1 );
73
74             if ( aStr.Length() > 0 && aStr.Value( aStr.Length() ) == '\"' )
75                 aStr.Remove( aStr.Length() );
76             if ( aStr.Length() > 0 && aStr.Value( 1 ) == '\"' )
77                 aStr.Remove( 1 );
78
79             if ( aValueStr.IsEmpty() || aStr.IsEmpty() )
80                 aRes = false;
81             else
82             {
83                 Standard_Real aValue = TCollection_AsciiString( aValueStr ).RealValue();
84                 Set( aStr, aValue );
85             }
86         }
87     }
88
89     aStream.close();
90     return aRes;
91 }
92
93 bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
94 {
95     Handle(TDataStd_NamedData) aMap = Map();
96     if ( aMap.IsNull() )
97         return false;
98
99     std::ofstream aStream( theFileName.ToCString(), std::ios::out | std::ios::binary );
100     if ( !aStream )
101         return false;
102
103     bool aRes = true;
104     for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
105     {
106         TCollection_ExtendedString aLine = TCollection_ExtendedString( '\"' ) + it.Key() + TCollection_ExtendedString( '\"' ) +
107             TCollection_ExtendedString( ' ' ) + TCollection_ExtendedString( it.Value() );
108         Standard_PCharacter aBuf = (Standard_PCharacter)malloc( aLine.LengthOfCString() + 1 );
109         aStream.write( aBuf, aLine.ToUTF8CString( aBuf ) );
110         aStream.write( "\r\n", 2 );
111         free( aBuf );
112     }
113
114     aStream.close();
115     return aRes;
116 }
117
118 double HYDROData_StricklerTable::Get( const TCollection_ExtendedString& theType, double theDefault ) const
119 {
120     Handle( TDataStd_NamedData ) aMap = Map();
121     if( aMap->HasReal( theType ) )
122         return aMap->GetReal( theType );
123     else
124         return theDefault;
125 }
126
127 void HYDROData_StricklerTable::Set( const TCollection_ExtendedString& theType, double theCoefficient )
128 {
129     Handle(TDataStd_NamedData) aMap = Map();
130     aMap->SetReal( theType, theCoefficient );
131 }
132
133 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
134 {
135   theMin = 0;
136   theMax = 0;
137   
138   Handle(TDataStd_NamedData) aMap = Map();
139   Standard_Boolean isFirst = Standard_True;
140   for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) {
141     Standard_Real aValue = it.Value();
142     if ( theMin == 0 || aValue < theMin ) {
143       theMin = aValue;
144     }
145     if ( theMax == 0 || aValue > theMax ) {
146       theMax = aValue;
147     }
148   }
149 }
150
151 TColStd_SequenceOfExtendedString HYDROData_StricklerTable::GetTypes() const
152 {
153     TColStd_SequenceOfExtendedString aSeq;
154     Handle(TDataStd_NamedData) aMap = Map();
155     if ( !aMap.IsNull() )
156     {
157         for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
158             aSeq.Append( it.Key() );
159     }
160     return aSeq;
161 }
162
163 bool HYDROData_StricklerTable::HasType( const TCollection_ExtendedString& theType ) const
164 {
165   Handle(TDataStd_NamedData) aMap = Map();
166   
167   return !aMap.IsNull() && aMap->HasReal( theType );
168 }
169
170 void HYDROData_StricklerTable::Clear()
171 {
172     Handle(TDataStd_NamedData) aMap = Map();
173     if ( !aMap.IsNull() )
174         aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
175 }
176
177 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
178 {
179     QStringList aResList = dumpObjectCreation( theTreatedObjects );
180     QString aPyName = GetObjPyName();
181
182     aResList << QString( "" );
183     Handle(TDataStd_NamedData) aMap = Map();
184     if ( !aMap.IsNull() )
185     {
186         for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
187         {
188             TCollection_ExtendedString aType = it.Key();
189             Standard_Real aValue = it.Value();
190             aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
191         }
192     }
193     aResList << QString( "" );
194     aResList << QString( "%1.Update();" ).arg( aPyName );
195
196     return aResList;
197 }
198
199 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
200 {
201     TDF_Label aLabel = myLab.FindChild( DataTag_Table );
202     Handle( TDataStd_NamedData ) aMap;
203     if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
204         aMap = TDataStd_NamedData::Set( aLabel );
205     return aMap;
206 }