Salome HOME
merge 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 : HYDROData_Entity( Geom_No )
37 {
38 }
39
40 HYDROData_StricklerTable::~HYDROData_StricklerTable()
41 {
42 }
43
44 const ObjectKind HYDROData_StricklerTable::GetKind() const
45 {
46     return KIND_STRICKLER_TABLE;
47 }
48
49 bool HYDROData_StricklerTable::Import( const TCollection_AsciiString& theFileName )
50 {
51     std::ifstream aStream( theFileName.ToCString(), std::ios::in | std::ios::binary );
52     if ( !aStream )
53         return false;
54
55     bool aRes = true;
56
57     const int aBufLen = 512;
58     char aBuf[aBufLen];
59     while( !aStream.eof() && aRes )
60     {
61         aStream.getline( aBuf, aBufLen );
62         TCollection_ExtendedString aStr( aBuf, Standard_True );
63
64         Standard_Integer aPos = aStr.SearchFromEnd( " " );
65         if ( aPos < 0 )
66             aRes = false;
67         else
68         {
69             TCollection_ExtendedString aValueStr = aStr.Split( aPos );
70             while ( aStr.Length() > 0 && ( aStr.Value( aStr.Length() ) == ' ' || aStr.Value( aStr.Length() ) == '\t' ) )
71                 aStr.Remove( aStr.Length() );
72             while ( aStr.Length() > 0 && ( aStr.Value( 1 ) == ' ' || aStr.Value( 1 ) == '\t' ) || aStr.Value( 1 ) < 0 )
73                 aStr.Remove( 1 );
74
75             if ( aStr.Length() > 0 && aStr.Value( aStr.Length() ) == '\"' )
76                 aStr.Remove( aStr.Length() );
77             if ( aStr.Length() > 0 && aStr.Value( 1 ) == '\"' )
78                 aStr.Remove( 1 );
79
80             if ( aValueStr.IsEmpty() || aStr.IsEmpty() )
81                 aRes = false;
82             else
83             {
84                 Standard_Real aValue = TCollection_AsciiString( aValueStr ).RealValue();
85                 Set( QString( (QChar*)aStr.ToExtString(), aStr.Length() ), aValue );
86             }
87         }
88     }
89
90     aStream.close();
91     return aRes;
92 }
93
94 bool HYDROData_StricklerTable::Export( const TCollection_AsciiString& theFileName )
95 {
96     Handle(TDataStd_NamedData) aMap = Map();
97     if ( aMap.IsNull() )
98         return false;
99
100     std::ofstream aStream( theFileName.ToCString(), std::ios::out | std::ios::binary );
101     if ( !aStream )
102         return false;
103
104     bool aRes = true;
105     for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More() && aRes; it.Next() )
106     {
107         TCollection_ExtendedString aLine = TCollection_ExtendedString( '\"' ) + it.Key() + TCollection_ExtendedString( '\"' ) +
108             TCollection_ExtendedString( ' ' ) + TCollection_ExtendedString( it.Value() );
109         Standard_PCharacter aBuf = (Standard_PCharacter)malloc( aLine.LengthOfCString() + 1 );
110         aStream.write( aBuf, aLine.ToUTF8CString( aBuf ) );
111         aStream.write( "\r\n", 2 );
112         free( aBuf );
113     }
114
115     aStream.close();
116     return aRes;
117 }
118
119 double HYDROData_StricklerTable::Get( const QString& theType, double theDefault ) const
120 {
121     Handle( TDataStd_NamedData ) aMap = Map();
122     TCollection_ExtendedString aType = toExtString( theType );
123     if( aMap->HasReal( aType ) )
124         return aMap->GetReal( aType );
125     else
126         return theDefault;
127 }
128
129 void HYDROData_StricklerTable::Set( const QString& theType, double theCoefficient )
130 {
131     Handle(TDataStd_NamedData) aMap = Map();
132     aMap->SetReal( toExtString( theType ), theCoefficient );
133 }
134
135 void HYDROData_StricklerTable::GetCoefficientRange( double& theMin, double& theMax ) const
136 {
137   theMin = 0;
138   theMax = 0;
139   
140   Handle(TDataStd_NamedData) aMap = Map();
141   Standard_Boolean isFirst = Standard_True;
142   for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() ) {
143     Standard_Real aValue = it.Value();
144     if ( theMin == 0 || aValue < theMin ) {
145       theMin = aValue;
146     }
147     if ( theMax == 0 || aValue > theMax ) {
148       theMax = aValue;
149     }
150   }
151 }
152
153 QStringList HYDROData_StricklerTable::GetTypes() const
154 {
155     QStringList aSeq;
156     Handle(TDataStd_NamedData) aMap = Map();
157     if ( !aMap.IsNull() )
158     {
159         for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
160             aSeq.append( toQString( it.Key() ) );
161     }
162     return aSeq;
163 }
164
165 bool HYDROData_StricklerTable::HasType( const QString& theType ) const
166 {
167   Handle(TDataStd_NamedData) aMap = Map();
168   
169   TCollection_ExtendedString aType = toExtString( theType );
170   return !aMap.IsNull() && aMap->HasReal( aType );
171 }
172
173 void HYDROData_StricklerTable::Clear()
174 {
175     Handle(TDataStd_NamedData) aMap = Map();
176     if ( !aMap.IsNull() )
177         aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
178 }
179
180 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
181 {
182     QStringList aResList = dumpObjectCreation( theTreatedObjects );
183     QString aPyName = GetObjPyName();
184
185     aResList << QString( "" );
186     Handle(TDataStd_NamedData) aMap = Map();
187     if ( !aMap.IsNull() )
188     {
189         for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
190         {
191             TCollection_ExtendedString aType = it.Key();
192             Standard_Real aValue = it.Value();
193             aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
194         }
195     }
196     aResList << QString( "" );
197     aResList << QString( "%1.Update();" ).arg( aPyName );
198
199     return aResList;
200 }
201
202 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
203 {
204     TDF_Label aLabel = myLab.FindChild( DataTag_Table );
205     Handle( TDataStd_NamedData ) aMap;
206     if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
207         aMap = TDataStd_NamedData::Set( aLabel );
208     return aMap;
209 }
210
211 TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
212 {
213     TCollection_ExtendedString aRes;
214     if( !theStr.isEmpty() )
215     {
216             Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
217             memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
218             ((short*)extStr)[theStr.length()] = '\0';
219
220             aRes = TCollection_ExtendedString( extStr );
221             delete [] extStr;
222     }
223     return aRes;
224 }
225
226 QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
227 {
228     return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
229 }