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