Salome HOME
DCQ: update msg file
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_AttributeTableOfReal_i.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOMEDS_AttributeTableOfReal_i.cxx
8 //  Author : Michael Ponikarov
9 //  Module : SALOME
10 //  $Header$
11
12 using namespace std;
13 #include "SALOMEDS_AttributeTableOfReal_i.hxx"
14 #include "SALOMEDS_SObject_i.hxx"
15 #include <TColStd_HSequenceOfReal.hxx>
16 #include <Standard_Failure.hxx>
17 #include <Standard_ErrorHandler.hxx>
18
19 #include <strstream>
20 #include <string>
21
22 #include "Utils_ExceptHandlers.hxx"
23 UNEXPECT_CATCH(ATR_IncorrectIndex, SALOMEDS::AttributeTableOfReal::IncorrectIndex);
24 UNEXPECT_CATCH(ATR_IncorrectArgumentLength, SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength);
25
26 #define SEPARATOR '\1'
27
28 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
29 {
30   TCollection_ExtendedString aString(theString);
31   int aPos = aString.Search(SEPARATOR);
32   if(aPos <= 0 || aPos == aString.Length() ) return TCollection_ExtendedString();
33   return aString.Split(aPos);
34 }
35
36 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
37 {
38   TCollection_ExtendedString aString(theString);
39   int aPos = aString.Search(SEPARATOR);
40   if(aPos < 1) return aString;
41   if(aPos == 1) return TCollection_ExtendedString();
42   aString.Split(aPos-1);
43   return aString;
44 }
45
46
47
48 void SALOMEDS_AttributeTableOfReal_i::SetTitle(const char* theTitle) {
49   CheckLocked();
50   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
51   CORBA::String_var aStr = CORBA::string_dup(theTitle);
52   aTable->SetTitle(TCollection_ExtendedString(aStr));
53 }
54
55 char* SALOMEDS_AttributeTableOfReal_i::GetTitle() {
56   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
57   CORBA::String_var c_s = CORBA::string_dup(TCollection_AsciiString(aTable->GetTitle()).ToCString());
58   return c_s._retn();
59 }
60
61 void SALOMEDS_AttributeTableOfReal_i::SetRowTitle(CORBA::Long theIndex, const char* theTitle)
62      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
63 {
64   Unexpect aCatch (ATR_IncorrectIndex);
65   CheckLocked();
66   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
67   if (theIndex <= 0 || theIndex > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
68   CORBA::String_var aStr = CORBA::string_dup(theTitle);
69   TCollection_ExtendedString aTitle(aStr);
70   TCollection_ExtendedString aUnit = getUnit(aTable->GetRowTitle(theIndex));
71   if(aUnit.Length() > 0) {
72     aTitle += SEPARATOR;
73     aTitle += aUnit;
74   }
75
76   aTable->SetRowTitle(theIndex, aTitle);
77 }
78
79 void SALOMEDS_AttributeTableOfReal_i::SetRowTitles(const SALOMEDS::StringSeq& theTitles)
80      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
81 {
82   Unexpect aCatch (ATR_IncorrectArgumentLength);
83   CheckLocked();
84   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
85   if (theTitles.length() != aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
86   for (int i = 0; i < theTitles.length(); i++) {
87     SetRowTitle(i + 1, theTitles[i]);
88   }
89 }
90
91 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetRowTitles() {
92   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
93   SALOMEDS::StringSeq_var aTitles = new SALOMEDS::StringSeq;
94   aTitles->length(aTable->GetNbRows());
95   for(int i = 0; i < aTitles->length(); i++)
96     aTitles[i] = CORBA::string_dup(TCollection_AsciiString(getTitle(aTable->GetRowTitle(i + 1))).ToCString());
97   return aTitles._retn();
98 }
99
100 void SALOMEDS_AttributeTableOfReal_i::SetColumnTitle(CORBA::Long theIndex, const char* theTitle)
101      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
102 {
103   Unexpect aCatch (ATR_IncorrectIndex);
104   CheckLocked();
105   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
106   if (theIndex <= 0 || theIndex > aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
107   CORBA::String_var aStr = CORBA::string_dup(theTitle);
108   aTable->SetColumnTitle(theIndex, TCollection_ExtendedString(aStr));
109 }
110
111 void SALOMEDS_AttributeTableOfReal_i::SetColumnTitles(const SALOMEDS::StringSeq& theTitles)
112      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
113 {
114   Unexpect aCatch(ATR_IncorrectArgumentLength);
115   CheckLocked();
116   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
117   if (theTitles.length() != aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
118   for (int i = 0; i < theTitles.length(); i++) {
119     SetColumnTitle(i + 1, theTitles[i]);
120   }
121 }
122
123 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetColumnTitles() {
124   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
125   SALOMEDS::StringSeq_var aTitles = new SALOMEDS::StringSeq;
126   aTitles->length(aTable->GetNbColumns());
127   for(int i = 0; i < aTitles->length(); i++)
128     aTitles[i] = CORBA::string_dup(TCollection_AsciiString(aTable->GetColumnTitle(i + 1)).ToCString());
129   return aTitles._retn();
130 }
131
132 //Units support
133 void SALOMEDS_AttributeTableOfReal_i::SetRowUnit(CORBA::Long theIndex, const char* theUnit)
134      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
135 {
136   Unexpect aCatch (ATR_IncorrectIndex);
137   CheckLocked();
138   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
139   if (theIndex <= 0 || theIndex > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
140   CORBA::String_var aStr = CORBA::string_dup(theUnit);
141   TCollection_ExtendedString aTitle = getTitle(aTable->GetRowTitle(theIndex));
142   TCollection_ExtendedString aUnit(aStr);
143   aTitle += SEPARATOR;
144   aTitle += aUnit;
145   aTable->SetRowTitle(theIndex, aTitle);
146 }
147
148 void SALOMEDS_AttributeTableOfReal_i::SetRowUnits(const SALOMEDS::StringSeq& theUnits)
149      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
150 {
151   Unexpect aCatch (ATR_IncorrectArgumentLength);
152   CheckLocked();
153   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
154   if (theUnits.length() != aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength();
155   for (int i = 0; i < theUnits.length(); i++) {
156     SetRowUnit(i + 1, theUnits[i]);
157   }
158 }
159
160 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfReal_i::GetRowUnits() {
161   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
162   SALOMEDS::StringSeq_var aUnits = new SALOMEDS::StringSeq;
163   aUnits->length(aTable->GetNbRows());
164   for(int i = 0; i < aUnits->length(); i++)
165     aUnits[i] = CORBA::string_dup(TCollection_AsciiString(getUnit(aTable->GetRowTitle(i + 1))).ToCString());
166   return aUnits._retn();
167 }
168
169
170 CORBA::Long SALOMEDS_AttributeTableOfReal_i::GetNbRows() {
171   return Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr)->GetNbRows();
172 }
173 CORBA::Long SALOMEDS_AttributeTableOfReal_i::GetNbColumns() {
174   return Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr)->GetNbColumns();
175 }
176
177 void SALOMEDS_AttributeTableOfReal_i::AddRow(const SALOMEDS::DoubleSeq& theData)
178      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
179 {
180   Unexpect aCatch(ATR_IncorrectArgumentLength);
181   CheckLocked();
182   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
183   
184   Handle(TColStd_HSequenceOfReal) aRow = new TColStd_HSequenceOfReal;
185   for (int i = 0; i < theData.length(); i++) aRow->Append(theData[i]);
186   aTable->SetRowData(aTable->GetNbRows() + 1, aRow);
187 }
188
189 void SALOMEDS_AttributeTableOfReal_i::SetRow(CORBA::Long theRow, const SALOMEDS::DoubleSeq& theData)
190      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength, SALOMEDS::AttributeTableOfReal::IncorrectIndex)
191 {
192   Unexpect aCatch(ATR_IncorrectArgumentLength);
193   CheckLocked();
194   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
195   
196   Handle(TColStd_HSequenceOfReal) aRow = new TColStd_HSequenceOfReal;
197   for (int i = 0; i < theData.length(); i++) aRow->Append(theData[i]);
198   aTable->SetRowData(theRow, aRow);
199 }
200
201 SALOMEDS::DoubleSeq* SALOMEDS_AttributeTableOfReal_i::GetRow(CORBA::Long theRow)
202      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
203 {
204   Unexpect aCatch(ATR_IncorrectIndex);
205   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
206   if (theRow <= 0 || theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
207
208   SALOMEDS::DoubleSeq_var CorbaSeq = new SALOMEDS::DoubleSeq;
209   Handle(TColStd_HSequenceOfReal) aRow = aTable->GetRowData(theRow);
210   CorbaSeq->length(aRow->Length());
211   for (int i = 0; i < aRow->Length(); i++) {
212     CorbaSeq[i] = aRow->Value(i + 1);
213   }
214   return CorbaSeq._retn();
215 }
216
217 void SALOMEDS_AttributeTableOfReal_i::AddColumn(const SALOMEDS::DoubleSeq& theData)
218      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength)
219 {
220   Unexpect aCatch(ATR_IncorrectArgumentLength);
221   CheckLocked();
222   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
223   
224   Handle(TColStd_HSequenceOfReal) aColumn = new TColStd_HSequenceOfReal;
225   for (int i = 0; i < theData.length(); i++) aColumn->Append(theData[i]);
226   aTable->SetColumnData(aTable->GetNbColumns() + 1, aColumn);
227 }
228
229 void SALOMEDS_AttributeTableOfReal_i::SetColumn(CORBA::Long theColumn, const SALOMEDS::DoubleSeq& theData)
230      throw (SALOMEDS::AttributeTableOfReal::IncorrectArgumentLength, SALOMEDS::AttributeTableOfReal::IncorrectIndex)
231 {
232   Unexpect aCatch(ATR_IncorrectArgumentLength);
233   CheckLocked();
234   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
235   
236   Handle(TColStd_HSequenceOfReal) aColumn = new TColStd_HSequenceOfReal;
237   for (int i = 0; i < theData.length(); i++) aColumn->Append(theData[i]);
238   aTable->SetColumnData(theColumn, aColumn);
239 }
240
241 SALOMEDS::DoubleSeq* SALOMEDS_AttributeTableOfReal_i::GetColumn(CORBA::Long theColumn)
242      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
243 {
244   Unexpect aCatch(ATR_IncorrectIndex);
245   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
246   if (theColumn <= 0 || theColumn > aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
247
248   SALOMEDS::DoubleSeq_var CorbaSeq = new SALOMEDS::DoubleSeq;
249   Handle(TColStd_HSequenceOfReal) aColumn = aTable->GetColumnData(theColumn);
250   CorbaSeq->length(aColumn->Length());
251   for (int i = 0; i < aColumn->Length(); i++) {
252     CorbaSeq[i] = aColumn->Value(i + 1);
253   }
254   return CorbaSeq._retn();
255 }
256
257 void SALOMEDS_AttributeTableOfReal_i::PutValue(CORBA::Double theValue, CORBA::Long theRow, CORBA::Long theColumn)
258      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
259 {
260   Unexpect aCatch(ATR_IncorrectIndex);
261   CheckLocked();
262   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
263
264   aTable->PutValue(theValue, theRow, theColumn);
265 }
266
267 CORBA::Boolean SALOMEDS_AttributeTableOfReal_i::HasValue(CORBA::Long theRow, CORBA::Long theColumn) {
268   return Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr)->HasValue(theRow, theColumn);
269 }
270
271 CORBA::Double SALOMEDS_AttributeTableOfReal_i::GetValue(CORBA::Long theRow, CORBA::Long theColumn)
272      throw (SALOMEDS::AttributeTableOfReal::IncorrectIndex)
273 {
274   Unexpect aCatch(ATR_IncorrectIndex);
275   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
276   if (theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
277
278   CORBA::Double aValue;
279
280   try {
281     aValue = aTable->GetValue(theRow, theColumn);
282   }
283   catch(Standard_Failure) {
284     throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
285   }
286
287   return aValue;
288 }
289
290
291 SALOMEDS::LongSeq* SALOMEDS_AttributeTableOfReal_i::GetRowSetIndices(CORBA::Long theRow) 
292 {
293   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
294
295   if(theRow <= 0 || theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfReal::IncorrectIndex();
296
297   SALOMEDS::LongSeq_var CorbaSeq = new SALOMEDS::LongSeq;
298   Handle(TColStd_HSequenceOfInteger) aSeq = aTable->GetSetRowIndices(theRow);
299   CorbaSeq->length(aSeq->Length());
300   for (int i = 0; i < aSeq->Length(); i++) {
301     CorbaSeq[i] = aSeq->Value(i + 1);
302   }
303   return CorbaSeq._retn(); 
304 }
305
306
307 void SALOMEDS_AttributeTableOfReal_i::SetNbColumns(CORBA::Long theNbColumns)
308 {
309   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
310   aTable->SetNbColumns(theNbColumns);
311 }
312
313 bool SALOMEDS_AttributeTableOfReal_i::ReadFromFile(const SALOMEDS::TMPFile& theStream) 
314 {
315   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
316
317   istrstream aStream((char*)&theStream[0], theStream.length());
318   return aTable->RestoreFromString(aStream);
319 }
320
321 SALOMEDS::TMPFile*  SALOMEDS_AttributeTableOfReal_i::SaveToFile()
322 {
323   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
324
325   ostrstream ostr;
326   string aString;
327   aTable->ConvertToString(ostr);
328
329   aString = ostr.rdbuf()->str();
330
331   char* aBuffer = (char*)CORBA::string_dup(aString.c_str());
332   int aBufferSize = strlen((char*)aBuffer);
333
334   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
335
336   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1);
337
338   return aStreamFile._retn();
339 }
340
341 char* SALOMEDS_AttributeTableOfReal_i::Store() {
342   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
343
344   ostrstream ostr;
345   aTable->ConvertToString(ostr);
346   string aString = ostr.rdbuf()->str();
347
348   CORBA::String_var aBuffer = CORBA::string_dup(aString.c_str());
349   return aBuffer._retn();
350 }
351
352 void SALOMEDS_AttributeTableOfReal_i::Restore(const char* value) {
353   Handle(SALOMEDS_TableOfRealAttribute) aTable = Handle(SALOMEDS_TableOfRealAttribute)::DownCast(_myAttr);
354
355   istrstream aStream(value, strlen(value));
356   aTable->RestoreFromString(aStream);
357 }