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