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