Salome HOME
279ecaea75d844343183b49c4fa9d3566432a05b
[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 QStringList HYDROData_StricklerTable::GetTypes() const
135 {
136     QStringList aSeq;
137     Handle(TDataStd_NamedData) aMap = Map();
138     if ( !aMap.IsNull() )
139     {
140         for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
141             aSeq.append( toQString( it.Key() ) );
142     }
143     return aSeq;
144 }
145
146 void HYDROData_StricklerTable::Clear()
147 {
148     Handle(TDataStd_NamedData) aMap = Map();
149     if ( !aMap.IsNull() )
150         aMap->ChangeReals( TDataStd_DataMapOfStringReal() );
151 }
152
153 QStringList HYDROData_StricklerTable::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
154 {
155     QStringList aResList = dumpObjectCreation( theTreatedObjects );
156     QString aPyName = GetObjPyName();
157
158     aResList << QString( "" );
159     Handle(TDataStd_NamedData) aMap = Map();
160     if ( !aMap.IsNull() )
161     {
162         for ( TDataStd_DataMapIteratorOfDataMapOfStringReal it( aMap->GetRealsContainer() ); it.More(); it.Next() )
163         {
164             TCollection_ExtendedString aType = it.Key();
165             Standard_Real aValue = it.Value();
166             aResList << QString( "%1.Set( \"%2\", %3 );" ).arg( aPyName ).arg( QString( (QChar*)aType.ToExtString(), aType.Length() ) ).arg( aValue );
167         }
168     }
169     aResList << QString( "" );
170     aResList << QString( "%1.Update();" ).arg( aPyName );
171
172     return aResList;
173 }
174
175 Handle(TDataStd_NamedData) HYDROData_StricklerTable::Map() const
176 {
177     TDF_Label aLabel = myLab.FindChild( DataTag_Table );
178     Handle( TDataStd_NamedData ) aMap;
179     if( !aLabel.FindAttribute( TDataStd_NamedData::GetID(), aMap ) )
180         aMap = TDataStd_NamedData::Set( aLabel );
181     return aMap;
182 }
183
184 TCollection_ExtendedString HYDROData_StricklerTable::toExtString( const QString& theStr ) const
185 {
186     TCollection_ExtendedString aRes;
187     if( !theStr.isEmpty() )
188     {
189             Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
190             memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
191             ((short*)extStr)[theStr.length()] = '\0';
192
193             aRes = TCollection_ExtendedString( extStr );
194             delete [] extStr;
195     }
196     return aRes;
197 }
198
199 QString HYDROData_StricklerTable::toQString( const TCollection_ExtendedString& theStr ) const
200 {
201     return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
202 }