Salome HOME
Copyrights update
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_AttributeTableOfString_i.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 //  File   : SALOMEDS_AttributeTableOfString_i.cxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24
25 #include <TCollection_ExtendedString.hxx>
26 #include <TCollection_AsciiString.hxx>
27
28 #include "SALOMEDS_AttributeTableOfString_i.hxx"
29 #include "SALOMEDS.hxx"
30
31 #include <TColStd_HSequenceOfExtendedString.hxx>
32 #include <Standard_Failure.hxx>
33 #include <Standard_ErrorHandler.hxx>
34
35 #include <strstream>
36 #include <string>
37
38 #include "Utils_ExceptHandlers.hxx"
39
40 using namespace std;
41
42 UNEXPECT_CATCH(ATS_IncorrectIndex, SALOMEDS::AttributeTableOfString::IncorrectIndex);
43 UNEXPECT_CATCH(ATS_IncorrectArgumentLength, SALOMEDS::AttributeTableOfString::IncorrectArgumentLength);
44
45 void SALOMEDS_AttributeTableOfString_i::SetTitle(const char* theTitle) 
46 {
47   SALOMEDS::Locker lock;
48   CheckLocked();
49   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
50   CORBA::String_var aStr = CORBA::string_dup(theTitle);
51   aTable->SetTitle(TCollection_ExtendedString(aStr));
52 }
53
54 char* SALOMEDS_AttributeTableOfString_i::GetTitle() 
55 {
56   SALOMEDS::Locker lock;
57   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
58   CORBA::String_var c_s = CORBA::string_dup(TCollection_AsciiString(aTable->GetTitle()).ToCString());
59   return c_s._retn();
60 }
61
62 void SALOMEDS_AttributeTableOfString_i::SetRowTitle(CORBA::Long theIndex, const char* theTitle)
63      throw (SALOMEDS::AttributeTableOfString::IncorrectIndex)
64 {
65   SALOMEDS::Locker lock;
66   Unexpect aCatch(ATS_IncorrectIndex);
67   CheckLocked();
68   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
69   if (theIndex <= 0 || theIndex > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfString::IncorrectIndex();
70
71   aTable->SetRowTitle(theIndex, TCollection_ExtendedString((char*)theTitle));
72 }
73
74 void SALOMEDS_AttributeTableOfString_i::SetRowTitles(const SALOMEDS::StringSeq& theTitles)
75      throw (SALOMEDS::AttributeTableOfString::IncorrectArgumentLength)
76 {
77   SALOMEDS::Locker lock;
78   Unexpect aCatch(ATS_IncorrectArgumentLength);
79   CheckLocked();
80   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
81   if (theTitles.length() != aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfString::IncorrectArgumentLength();
82   for (int i = 0; i < theTitles.length(); i++) {
83     aTable->SetRowTitle(i + 1, TCollection_ExtendedString((char*)theTitles[i].in()));
84   }
85 }
86
87 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfString_i::GetRowTitles() 
88 {
89   SALOMEDS::Locker lock;
90   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
91   SALOMEDS::StringSeq_var aTitles = new SALOMEDS::StringSeq;
92   aTitles->length(aTable->GetNbRows());
93   for(int i = 0; i < aTitles->length(); i++)
94     aTitles[i] =CORBA::string_dup(TCollection_AsciiString(aTable->GetRowTitle(i + 1)).ToCString());
95   return aTitles._retn();
96 }
97
98 void SALOMEDS_AttributeTableOfString_i::SetColumnTitle(CORBA::Long theIndex, const char* theTitle)
99      throw (SALOMEDS::AttributeTableOfString::IncorrectIndex)
100 {
101   SALOMEDS::Locker lock;
102   Unexpect aCatch(ATS_IncorrectIndex);
103   CheckLocked();
104   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
105   if (theIndex <= 0 || theIndex > aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfString::IncorrectIndex();
106   aTable->SetColumnTitle(theIndex, TCollection_ExtendedString((char*)theTitle));
107 }
108
109 void SALOMEDS_AttributeTableOfString_i::SetColumnTitles(const SALOMEDS::StringSeq& theTitles)
110      throw (SALOMEDS::AttributeTableOfString::IncorrectArgumentLength)
111 {
112   SALOMEDS::Locker lock;
113   Unexpect aCatch(ATS_IncorrectArgumentLength);
114   CheckLocked();
115   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
116   if (theTitles.length() != aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfString::IncorrectArgumentLength();
117   for (int i = 0; i < theTitles.length(); i++) {
118     aTable->SetColumnTitle(i + 1, TCollection_ExtendedString((char*)theTitles[i].in()));
119   }
120 }
121
122 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfString_i::GetColumnTitles() 
123 {
124   SALOMEDS::Locker lock;
125   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
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_AttributeTableOfString_i::SetRowUnit(CORBA::Long theIndex, const char* theUnit)
135      throw (SALOMEDS::AttributeTableOfString::IncorrectIndex)
136 {
137   SALOMEDS::Locker lock;
138   Unexpect aCatch(ATS_IncorrectIndex);
139   CheckLocked();
140   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
141   if (theIndex <= 0 || theIndex > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfString::IncorrectIndex();
142   aTable->SetRowUnit(theIndex, TCollection_ExtendedString((char*)theUnit));
143 }
144
145 void SALOMEDS_AttributeTableOfString_i::SetRowUnits(const SALOMEDS::StringSeq& theUnits)
146      throw (SALOMEDS::AttributeTableOfString::IncorrectArgumentLength)
147 {
148   SALOMEDS::Locker lock;
149   Unexpect aCatch(ATS_IncorrectArgumentLength);
150   CheckLocked();
151   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
152   if (theUnits.length() != aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfString::IncorrectArgumentLength();
153   for (int i = 0; i < theUnits.length(); i++) {
154     aTable->SetRowUnit(i + 1, TCollection_ExtendedString((char*)theUnits[i].in()));
155   }
156 }
157
158 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfString_i::GetRowUnits() 
159 {
160   SALOMEDS::Locker lock;
161   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
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(aTable->GetRowUnit(i + 1)).ToCString());
166   return aUnits._retn();
167 }
168
169
170 CORBA::Long SALOMEDS_AttributeTableOfString_i::GetNbRows() 
171 {
172   SALOMEDS::Locker lock;
173   return Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl)->GetNbRows();
174 }
175
176 CORBA::Long SALOMEDS_AttributeTableOfString_i::GetNbColumns() 
177 {
178   SALOMEDS::Locker lock;
179   return Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl)->GetNbColumns();
180 }
181
182 void SALOMEDS_AttributeTableOfString_i::AddRow(const SALOMEDS::StringSeq& theData)
183      throw (SALOMEDS::AttributeTableOfString::IncorrectArgumentLength)
184 {
185   SALOMEDS::Locker lock;
186   Unexpect aCatch(ATS_IncorrectArgumentLength);
187   CheckLocked();
188   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
189   
190   Handle(TColStd_HSequenceOfExtendedString) aRow = new TColStd_HSequenceOfExtendedString;
191   for (int i = 0; i < theData.length(); i++) aRow->Append(TCollection_AsciiString(CORBA::string_dup(theData[i])));
192   aTable->SetRowData(aTable->GetNbRows() + 1, aRow);
193 }
194
195 void SALOMEDS_AttributeTableOfString_i::SetRow(CORBA::Long theRow, const SALOMEDS::StringSeq& theData)
196      throw (SALOMEDS::AttributeTableOfString::IncorrectArgumentLength, SALOMEDS::AttributeTableOfString::IncorrectIndex)
197 {
198   SALOMEDS::Locker lock;
199   Unexpect aCatch(ATS_IncorrectArgumentLength);
200   CheckLocked();
201   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
202   
203   Handle(TColStd_HSequenceOfExtendedString) aRow = new TColStd_HSequenceOfExtendedString;
204   for (int i = 0; i < theData.length(); i++) aRow->Append(TCollection_AsciiString(CORBA::string_dup(theData[i])));
205   aTable->SetRowData(theRow, aRow);
206 }
207
208 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfString_i::GetRow(CORBA::Long theRow)
209      throw (SALOMEDS::AttributeTableOfString::IncorrectIndex)
210 {
211   SALOMEDS::Locker lock;
212   Unexpect aCatch(ATS_IncorrectIndex);
213   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
214   if (theRow <= 0 || theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfString::IncorrectIndex();
215
216   SALOMEDS::StringSeq_var CorbaSeq = new SALOMEDS::StringSeq;
217   Handle(TColStd_HSequenceOfExtendedString) aRow = aTable->GetRowData(theRow);
218   CorbaSeq->length(aRow->Length());
219   for (int i = 0; i < aRow->Length(); i++) {
220     CorbaSeq[i] = CORBA::string_dup((TCollection_AsciiString(aRow->Value(i + 1))).ToCString());
221   }
222   return CorbaSeq._retn();
223 }
224
225 void SALOMEDS_AttributeTableOfString_i::AddColumn(const SALOMEDS::StringSeq& theData)
226      throw (SALOMEDS::AttributeTableOfString::IncorrectArgumentLength)
227 {
228   SALOMEDS::Locker lock;
229   Unexpect aCatch(ATS_IncorrectArgumentLength);
230   CheckLocked();
231   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
232   
233   Handle(TColStd_HSequenceOfExtendedString) aColumn = new TColStd_HSequenceOfExtendedString;
234   for (int i = 0; i < theData.length(); i++) aColumn->Append(TCollection_AsciiString(CORBA::string_dup(theData[i])));
235   aTable->SetColumnData(aTable->GetNbColumns() + 1, aColumn);
236 }
237
238 void SALOMEDS_AttributeTableOfString_i::SetColumn(CORBA::Long theColumn, const SALOMEDS::StringSeq& theData)
239      throw (SALOMEDS::AttributeTableOfString::IncorrectArgumentLength, SALOMEDS::AttributeTableOfString::IncorrectIndex)
240 {
241   SALOMEDS::Locker lock;
242   Unexpect aCatch(ATS_IncorrectArgumentLength);
243   CheckLocked();
244   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
245   
246   Handle(TColStd_HSequenceOfExtendedString) aColumn = new TColStd_HSequenceOfExtendedString;
247   for (int i = 0; i < theData.length(); i++) aColumn->Append(TCollection_AsciiString(CORBA::string_dup(theData[i])));
248   aTable->SetColumnData(theColumn, aColumn);
249 }
250
251 SALOMEDS::StringSeq* SALOMEDS_AttributeTableOfString_i::GetColumn(CORBA::Long theColumn)
252      throw (SALOMEDS::AttributeTableOfString::IncorrectIndex)
253 {
254   SALOMEDS::Locker lock;
255   Unexpect aCatch(ATS_IncorrectIndex);
256   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
257   if (theColumn <= 0 || theColumn > aTable->GetNbColumns()) throw SALOMEDS::AttributeTableOfString::IncorrectIndex();
258
259   SALOMEDS::StringSeq_var CorbaSeq = new SALOMEDS::StringSeq;
260   Handle(TColStd_HSequenceOfExtendedString) aColumn = aTable->GetColumnData(theColumn);
261   CorbaSeq->length(aColumn->Length());
262   for (int i = 0; i < aColumn->Length(); i++) {
263     CorbaSeq[i] = CORBA::string_dup((TCollection_AsciiString(aColumn->Value(i + 1))).ToCString());
264   }
265   return CorbaSeq._retn();
266 }
267
268 void SALOMEDS_AttributeTableOfString_i::PutValue(const char* theValue, CORBA::Long theRow, CORBA::Long theColumn)
269      throw (SALOMEDS::AttributeTableOfString::IncorrectIndex)
270 {
271   SALOMEDS::Locker lock;
272   Unexpect aCatch(ATS_IncorrectIndex);
273   CheckLocked();
274   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
275   char* aValue = CORBA::string_dup(theValue);
276
277   aTable->PutValue(aValue, theRow, theColumn);
278 }
279
280 CORBA::Boolean SALOMEDS_AttributeTableOfString_i::HasValue(CORBA::Long theRow, CORBA::Long theColumn) 
281 {
282   SALOMEDS::Locker lock;
283   return Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl)->HasValue(theRow, theColumn);
284 }
285
286 char* SALOMEDS_AttributeTableOfString_i::GetValue(CORBA::Long theRow, CORBA::Long theColumn)
287      throw (SALOMEDS::AttributeTableOfString::IncorrectIndex)
288 {
289   SALOMEDS::Locker lock;
290   Unexpect aCatch(ATS_IncorrectIndex);
291   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
292   if (theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfString::IncorrectIndex();
293
294   TCollection_AsciiString aValue;
295
296   try {
297     aValue = aTable->GetValue(theRow, theColumn);
298   }
299   catch(Standard_Failure) {
300     throw SALOMEDS::AttributeTableOfString::IncorrectIndex();
301   }
302
303   return CORBA::string_dup(aValue.ToCString());
304 }
305
306
307 SALOMEDS::LongSeq* SALOMEDS_AttributeTableOfString_i::GetRowSetIndices(CORBA::Long theRow) 
308 {
309   SALOMEDS::Locker lock;
310   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
311
312   if(theRow <= 0 || theRow > aTable->GetNbRows()) throw SALOMEDS::AttributeTableOfString::IncorrectIndex();
313
314   SALOMEDS::LongSeq_var CorbaSeq = new SALOMEDS::LongSeq;
315   Handle(TColStd_HSequenceOfInteger) aSeq = aTable->GetSetRowIndices(theRow);
316   CorbaSeq->length(aSeq->Length());
317   for (int i = 0; i < aSeq->Length(); i++) {
318     CorbaSeq[i] = aSeq->Value(i + 1);
319   }
320   return CorbaSeq._retn(); 
321 }
322
323
324 void SALOMEDS_AttributeTableOfString_i::SetNbColumns(CORBA::Long theNbColumns)
325 {
326   SALOMEDS::Locker lock;
327   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
328   aTable->SetNbColumns(theNbColumns);
329 }
330
331 bool SALOMEDS_AttributeTableOfString_i::ReadFromFile(const SALOMEDS::TMPFile& theStream) 
332 {
333   SALOMEDS::Locker lock;
334   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
335
336   istrstream aStream((char*)&theStream[0], theStream.length());
337   return aTable->RestoreFromString(aStream);
338 }
339
340 SALOMEDS::TMPFile*  SALOMEDS_AttributeTableOfString_i::SaveToFile()
341 {
342   SALOMEDS::Locker lock;
343   Handle(SALOMEDSImpl_AttributeTableOfString) aTable = Handle(SALOMEDSImpl_AttributeTableOfString)::DownCast(_impl);
344
345   ostrstream ostr;
346   aTable->ConvertToString(ostr);
347   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)ostr.rdbuf()->str();
348   unsigned long aSize = ostr.pcount();
349   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(aSize, aSize, anOctetBuf, 1);
350   return aStreamFile._retn();
351 }
352