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