Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' 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( QString( (QChar*)aStr.ToExtString(), aStr.Length() ), 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 QString& theType, double theDefault ) const
119 {
120     Handle( TDataStd_NamedData ) aMap = Map();
121     TCollection_ExtendedString aType = toExtString( theType );
122     if( aMap->HasReal( aType ) )
123         return aMap->GetReal( aType );
124     else
125         return theDefault;
126 }
127
128 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
129 {
130     Handle(TDataStd_NamedData) aMap = Map();
131     aMap->SetReal( toExtString( theType ), theCoefficient );
132 }
133
134 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
135 {
136   theMin = 0;
137   theMax = 0;
138   
139   Handle(TDataStd_NamedData) aMap = Map();
140   Standard_Boolean isFirst = Standard_True;
141   for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) {
142     Standard_Real aValue = it.Value();
143     if ( theMin == 0 || aValue < theMin ) {
144       theMin = aValue;
145     }
146     if ( theMax == 0 || aValue > theMax ) {
147       theMax = aValue;
148     }
149   }
150 }
151
152 QStringList HYDROData_StricklerTable::GetTypes() const
153 {
154     QStringList aSeq;
155     Handle(TDataStd_NamedData) aMap = Map();
156     if ( !aMap.IsNull() )
157     {
158         for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
159             aSeq.append( toQString( it.Key() ) );
160     }
161     return aSeq;
162 }
163
164 bool HYDROData_StricklerTable::HasType( const TCollection_ExtendedString& theType ) const
165 {
166   Handle(TDataStd_NamedData) aMap = Map();
167   
168   return !aMap.IsNull() && aMap->HasReal( theType );
169 }
170
171 void HYDROData_StricklerTable::Clear()
172 {
173     Handle(TDataStd_NamedData) aMap = Map();
174     if ( !aMap.IsNull() )
175         aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
176 }
177
178 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
179 {
180     QStringList aResList = dumpObjectCreation( theTreatedObjects );
181     QString aPyName = GetObjPyName();
182
183     aResList << QString( "" );
184     Handle(TDataStd_NamedData) aMap = Map();
185     if ( !aMap.IsNull() )
186     {
187         for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
188         {
189             TCollection_ExtendedString aType = it.Key();
190             Standard_Real aValue = it.Value();
191             aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
192         }
193     }
194     aResList << QString( "" );
195     aResList << QString( "%1.Update();" ).arg( aPyName );
196
197     return aResList;
198 }
199
200 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
201 {
202     TDF_Label aLabel = myLab.FindChild( DataTag_Table );
203     Handle( TDataStd_NamedData ) aMap;
204     if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
205         aMap = TDataStd_NamedData::Set( aLabel );
206     return aMap;
207 }
208
209 TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
210 {
211     TCollection_ExtendedString aRes;
212     if( !theStr.isEmpty() )
213     {
214             Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
215             memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
216             ((short*)extStr)[theStr.length()] = '\0';
217
218             aRes = TCollection_ExtendedString( extStr );
219             delete [] extStr;
220     }
221     return aRes;
222 }
223
224 QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
225 {
226     return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
227 }