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