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