1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSImpl_AttributeTableOfInteger.cxx
21 // Author : Michael Ponikarov
24 #include <SALOMEDSImpl_AttributeTableOfInteger.hxx>
25 #include <Standard_Failure.hxx>
26 #include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx>
27 #include <Standard_GUID.hxx>
28 #include <TColStd_HSequenceOfExtendedString.hxx>
32 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeTableOfInteger, SALOMEDSImpl_GenericAttribute )
33 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeTableOfInteger, SALOMEDSImpl_GenericAttribute )
35 #define SEPARATOR '\1'
37 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
39 TCollection_ExtendedString aString(theString);
40 int aPos = aString.Search(SEPARATOR);
41 if(aPos <= 0 || aPos == aString.Length() ) return TCollection_ExtendedString();
42 return aString.Split(aPos);
45 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
47 TCollection_ExtendedString aString(theString);
48 int aPos = aString.Search(SEPARATOR);
49 if(aPos < 1) return aString;
50 if(aPos == 1) return TCollection_ExtendedString();
51 aString.Split(aPos-1);
55 const Standard_GUID& SALOMEDSImpl_AttributeTableOfInteger::GetID()
57 static Standard_GUID SALOMEDSImpl_AttributeTableOfIntegerID ("128371A0-8F52-11d6-A8A3-0001021E8C7F");
58 return SALOMEDSImpl_AttributeTableOfIntegerID;
61 Handle(SALOMEDSImpl_AttributeTableOfInteger) SALOMEDSImpl_AttributeTableOfInteger::Set(const TDF_Label& label)
63 Handle(SALOMEDSImpl_AttributeTableOfInteger) anAttr;
64 if (!label.FindAttribute(SALOMEDSImpl_AttributeTableOfInteger::GetID(),anAttr)) {
65 anAttr = new SALOMEDSImpl_AttributeTableOfInteger();
66 label.AddAttribute(anAttr);
71 SALOMEDSImpl_AttributeTableOfInteger::SALOMEDSImpl_AttributeTableOfInteger()
72 :SALOMEDSImpl_GenericAttribute("AttributeTableOfInteger")
74 myRows = new TColStd_HSequenceOfExtendedString();
75 myCols = new TColStd_HSequenceOfExtendedString();
80 void SALOMEDSImpl_AttributeTableOfInteger::SetNbColumns(const Standard_Integer theNbColumns)
85 TColStd_DataMapOfIntegerInteger aMap;
89 TColStd_DataMapIteratorOfDataMapOfIntegerInteger anIterator(aMap);
90 for(; anIterator.More(); anIterator.Next()) {
91 int aRow = (int)(anIterator.Key()/myNbColumns) + 1;
92 int aCol = (int)(anIterator.Key() - myNbColumns*(aRow-1));
93 if(aCol == 0) { aCol = myNbColumns; aRow--; }
94 if(aCol > theNbColumns) continue;
95 int aKey = (aRow-1)*theNbColumns+aCol;
96 myTable.Bind(aKey, anIterator.Value());
99 myNbColumns = theNbColumns;
101 while (myCols->Length() < myNbColumns) { // append empty columns titles
102 myCols->Append(TCollection_ExtendedString(""));
105 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
109 void SALOMEDSImpl_AttributeTableOfInteger::SetTitle(const TCollection_ExtendedString& theTitle)
115 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
118 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetTitle() const
123 void SALOMEDSImpl_AttributeTableOfInteger::SetRowData(const Standard_Integer theRow,
124 const Handle(TColStd_HSequenceOfInteger)& theData)
127 if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
131 while (myRows->Length() < theRow) { // append new row titles
132 myRows->Append(TCollection_ExtendedString(""));
135 Standard_Integer i, aShift = (theRow-1)*myNbColumns, aLength = theData->Length();
136 for(i = 1; i <= aLength; i++) {
137 myTable.Bind(aShift + i, theData->Value(i));
140 if(theRow > myNbRows) myNbRows = theRow;
142 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
145 Handle(TColStd_HSequenceOfInteger) SALOMEDSImpl_AttributeTableOfInteger::GetRowData(const Standard_Integer theRow)
147 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger();
148 Standard_Integer i, aShift = (theRow-1)*myNbColumns;
149 for(i = 1; i <= myNbColumns; i++) {
150 if(myTable.IsBound(aShift+i))
151 aSeq->Append(myTable.Find(aShift+i));
159 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitle(const Standard_Integer theRow,
160 const TCollection_ExtendedString& theTitle)
164 TCollection_ExtendedString aTitle(theTitle), aUnit = GetRowUnit(theRow);
165 if(aUnit.Length()>0) {
169 myRows->SetValue(theRow, aTitle);
171 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
174 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnit(const Standard_Integer theRow,
175 const TCollection_ExtendedString& theUnit)
179 TCollection_ExtendedString aTitle = GetRowTitle(theRow);
183 myRows->SetValue(theRow, aTitle);
185 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
188 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnits(const Handle(TColStd_HSequenceOfExtendedString)& theUnits)
190 if (theUnits->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
191 int aLength = theUnits->Length(), i;
192 for(i = 1; i <= aLength; i++) SetRowUnit(i, theUnits->Value(i));
194 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
197 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetRowUnits()
199 Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
200 int aLength = myRows->Length(), i;
201 for(i=1; i<=aLength; i++) aSeq->Append(getUnit(myRows->Value(i)));
205 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
207 if (theTitles->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
208 int aLength = theTitles->Length(), i;
209 for(i = 1; i <= aLength; i++) SetRowTitle(i, theTitles->Value(i));
211 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
214 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetRowTitles()
216 Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
217 int aLength = myRows->Length(), i;
218 for(i=1; i<=aLength; i++) aSeq->Append(getTitle(myRows->Value(i)));
223 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetRowTitle(const Standard_Integer theRow) const
225 return getTitle(myRows->Value(theRow));
229 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetRowUnit(const Standard_Integer theRow) const
231 return getUnit(myRows->Value(theRow));
235 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnData(const Standard_Integer theColumn,
236 const Handle(TColStd_HSequenceOfInteger)& theData)
239 if(theColumn > myNbColumns) SetNbColumns(theColumn);
243 Standard_Integer i, aLength = theData->Length();
244 for(i = 1; i <= aLength; i++) {
245 myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
248 if(aLength > myNbRows) {
250 while (myRows->Length() < myNbRows) { // append empty row titles
251 myRows->Append(TCollection_ExtendedString(""));
255 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
259 Handle(TColStd_HSequenceOfInteger) SALOMEDSImpl_AttributeTableOfInteger::GetColumnData(const Standard_Integer theColumn)
261 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
263 Standard_Integer i, anIndex;
264 for(i = 1; i <= myNbRows; i++) {
265 anIndex = myNbColumns*(i-1) + theColumn;
266 if(myTable.IsBound(anIndex))
267 aSeq->Append(myTable.Find(anIndex));
275 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitle(const Standard_Integer theColumn,
276 const TCollection_ExtendedString& theTitle)
280 while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
281 myCols->SetValue(theColumn,theTitle);
283 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
286 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitle(const Standard_Integer theColumn) const
288 if(myCols.IsNull()) return "";
289 if(myCols->Length() < theColumn) return "";
290 return myCols->Value(theColumn);
293 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
295 if (theTitles->Length() != myNbColumns) Standard_Failure::Raise("Invalid number of columns");
296 int aLength = theTitles->Length(), i;
297 for(i = 1; i <= aLength; i++) myCols->SetValue(i, theTitles->Value(i));
299 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
302 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitles()
304 Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
305 int aLength = myCols->Length(), i;
306 for(i=1; i<=aLength; i++) aSeq->Append(myCols->Value(i));
310 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetNbRows() const
315 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetNbColumns() const
320 void SALOMEDSImpl_AttributeTableOfInteger::PutValue(const Standard_Integer theValue,
321 const Standard_Integer theRow,
322 const Standard_Integer theColumn)
325 if(theColumn > myNbColumns) SetNbColumns(theColumn);
327 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
328 myTable.Bind(anIndex, theValue);
330 if(theRow > myNbRows) {
331 while (myRows->Length() < theRow) { // append empty row titles
332 myRows->Append(TCollection_ExtendedString(""));
337 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
340 Standard_Boolean SALOMEDSImpl_AttributeTableOfInteger::HasValue(const Standard_Integer theRow,
341 const Standard_Integer theColumn)
343 if(theRow > myNbRows || theRow < 1) return Standard_False;
344 if(theColumn > myNbColumns || theColumn < 1) return Standard_False;
345 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
346 return myTable.IsBound(anIndex);
349 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetValue(const Standard_Integer theRow,
350 const Standard_Integer theColumn)
352 if(theRow > myNbRows || theRow < 1) Standard_Failure::Raise("Invalid cell index");
353 if(theColumn > myNbColumns || theColumn < 1) Standard_Failure::Raise("Invalid cell index");
355 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
356 if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
358 Standard_Failure::Raise("Invalid cell index");
362 const Standard_GUID& SALOMEDSImpl_AttributeTableOfInteger::ID() const
367 void SALOMEDSImpl_AttributeTableOfInteger::Restore(const Handle(TDF_Attribute)& with)
369 Standard_Integer anIndex;
370 Handle(SALOMEDSImpl_AttributeTableOfInteger) aTable = Handle(SALOMEDSImpl_AttributeTableOfInteger)::DownCast(with);
376 myTable = aTable->myTable;
377 myNbRows = aTable->myNbRows;
378 myNbColumns = aTable->myNbColumns;
379 myTitle = aTable->myTitle;
381 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
382 myRows->Append(aTable->GetRowTitle(anIndex));
384 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
385 myCols->Append(aTable->GetColumnTitle(anIndex));
388 Handle(TDF_Attribute) SALOMEDSImpl_AttributeTableOfInteger::NewEmpty() const
390 return new SALOMEDSImpl_AttributeTableOfInteger();
393 void SALOMEDSImpl_AttributeTableOfInteger::Paste(const Handle(TDF_Attribute)& into,
394 const Handle(TDF_RelocationTable)&) const
396 Standard_Integer anIndex;
397 Handle(SALOMEDSImpl_AttributeTableOfInteger) aTable = Handle(SALOMEDSImpl_AttributeTableOfInteger)::DownCast(into);
399 aTable->myTable.Clear();
400 aTable->myCols->Clear();
401 aTable->myRows->Clear();
403 aTable->myTable = myTable;
404 aTable->myTitle = myTitle;
405 aTable->myNbRows = myNbRows;
406 aTable->myNbColumns = myNbColumns;
408 for(anIndex = 1; anIndex <= GetNbRows();anIndex++)
409 aTable->myRows->Append(GetRowTitle(anIndex));
410 for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++)
411 aTable->myCols->Append(GetColumnTitle(anIndex));
415 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfInteger::GetSetRowIndices(const Standard_Integer theRow)
417 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
419 Standard_Integer i, aShift = myNbColumns*(theRow-1);
420 for(i = 1; i <= myNbColumns; i++) {
421 if(myTable.IsBound(aShift + i)) aSeq->Append(i);
427 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfInteger::GetSetColumnIndices(const Standard_Integer theColumn)
429 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
431 Standard_Integer i, anIndex;
432 for(i = 1; i <= myNbRows; i++) {
433 anIndex = myNbColumns*(i-1)+theColumn;
434 if(myTable.IsBound(anIndex)) aSeq->Append(i);
441 void SALOMEDSImpl_AttributeTableOfInteger::ConvertToString(ostrstream& theStream)
445 theStream.precision(64);
448 l = myTitle.Length();
449 theStream << l << "\n";
451 theStream << myTitle.Value(i) << "\n";
454 theStream << myNbRows << "\n";
457 for(i=1; i<=myNbRows; i++) {
458 l = myRows->Value(i).Length();
459 theStream << l << "\n";
461 theStream << myRows->Value(i).Value(j) << "\n";
465 theStream << myNbColumns << "\n";
468 for(i=1; i<=myNbColumns; i++) {
469 l = myCols->Value(i).Length();
470 theStream << l << "\n";
472 theStream << myCols->Value(i).Value(j) << "\n";
475 //Store the table values
476 l = myTable.Extent();
477 theStream << l << "\n";
478 TColStd_DataMapIteratorOfDataMapOfIntegerInteger anIterator(myTable);
479 for(; anIterator.More(); anIterator.Next()) {
480 theStream << anIterator.Key() << "\n";
481 theStream << anIterator.Value() << "\n";
487 bool SALOMEDSImpl_AttributeTableOfInteger::RestoreFromString(istrstream& theStream)
493 Standard_ExtCharacter anExtChar;
494 TCollection_ExtendedString aStr;
499 myTitle = TCollection_ExtendedString(l, 0);
500 for(i=1; i<=l; i++) {
501 theStream >> anExtChar;
502 myTitle.SetValue(i, anExtChar);
506 theStream >> myNbRows;
510 for(i=1; i<=myNbRows; i++) {
512 aStr = TCollection_ExtendedString(l,0);
513 for(j=1; j<=l; j++) {
514 theStream >> anExtChar;
515 aStr.SetValue(j, anExtChar);
517 myRows->Append(aStr);
521 theStream >> myNbColumns;
525 for(i=1; i<=myNbColumns; i++) {
527 aStr = TCollection_ExtendedString(l,0);
528 for(j=1; j<=l; j++) {
529 theStream >> anExtChar;
530 aStr.SetValue(j, anExtChar);
532 myCols->Append(aStr);
535 //Restore the table values
538 for(i=1; i<=l; i++) {
542 myTable.Bind(aKey, aValue);
549 TCollection_AsciiString SALOMEDSImpl_AttributeTableOfInteger::Save()
552 ConvertToString(ostr);
553 TCollection_AsciiString aString((char*)ostr.rdbuf()->str());
557 void SALOMEDSImpl_AttributeTableOfInteger::Load(const TCollection_AsciiString& value)
559 istrstream aStream(value.ToCString(), strlen(value.ToCString()));
560 RestoreFromString(aStream);