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"
29 #define SEPARATOR '\1'
30 typedef map<int, int>::const_iterator MI;
32 static std::string getUnit(std::string theString)
34 std::string aString(theString);
35 int aPos = aString.find(SEPARATOR);
36 if(aPos <= 0 || aPos == aString.size() ) return std::string();
37 return aString.substr(aPos+1, aString.size());
40 static std::string getTitle(std::string theString)
42 std::string aString(theString);
43 int aPos = aString.find(SEPARATOR);
44 if(aPos < 1) return aString;
45 if(aPos == 1) return std::string();
46 return aString.substr(0, aPos);
49 const std::string& SALOMEDSImpl_AttributeTableOfInteger::GetID()
51 static std::string SALOMEDSImpl_AttributeTableOfIntegerID ("128371A0-8F52-11d6-A8A3-0001021E8C7F");
52 return SALOMEDSImpl_AttributeTableOfIntegerID;
55 SALOMEDSImpl_AttributeTableOfInteger* SALOMEDSImpl_AttributeTableOfInteger::Set(const DF_Label& label)
57 SALOMEDSImpl_AttributeTableOfInteger* A = NULL;
58 if (!(A=(SALOMEDSImpl_AttributeTableOfInteger*)label.FindAttribute(SALOMEDSImpl_AttributeTableOfInteger::GetID()))) {
59 A = new SALOMEDSImpl_AttributeTableOfInteger();
60 label.AddAttribute(A);
65 SALOMEDSImpl_AttributeTableOfInteger::SALOMEDSImpl_AttributeTableOfInteger()
66 :SALOMEDSImpl_GenericAttribute("AttributeTableOfInteger")
72 void SALOMEDSImpl_AttributeTableOfInteger::SetNbColumns(const int theNbColumns)
82 for(MI p = aMap.begin(); p != aMap.end(); p++) {
83 int aRow = (int)(p->first/myNbColumns) + 1;
84 int aCol = (int)(p->first - myNbColumns*(aRow-1));
85 if(aCol == 0) { aCol = myNbColumns; aRow--; }
86 if(aCol > theNbColumns) continue;
87 int aKey = (aRow-1)*theNbColumns+aCol;
88 myTable[aKey] = p->second;
91 myNbColumns = theNbColumns;
93 while (myCols.size() < myNbColumns) { // append empty columns titles
94 myCols.push_back(std::string(""));
97 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
101 void SALOMEDSImpl_AttributeTableOfInteger::SetTitle(const std::string& theTitle)
107 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
110 std::string SALOMEDSImpl_AttributeTableOfInteger::GetTitle() const
115 void SALOMEDSImpl_AttributeTableOfInteger::SetRowData(const int theRow,
116 const vector<int>& theData)
119 if(theData.size() > myNbColumns) SetNbColumns(theData.size());
123 while (myRows.size() < theRow) { // append new row titles
124 myRows.push_back(std::string(""));
127 int i, aShift = (theRow-1)*myNbColumns, aLength = theData.size();
128 for(i = 1; i <= aLength; i++) {
129 myTable[aShift + i] = theData[i-1];
132 if(theRow > myNbRows) myNbRows = theRow;
134 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
137 vector<int> SALOMEDSImpl_AttributeTableOfInteger::GetRowData(const int theRow)
140 int i, aShift = (theRow-1)*myNbColumns;
141 for(i = 1; i <= myNbColumns; i++) {
142 if(myTable.find(aShift+i) != myTable.end())
143 aSeq.push_back(myTable[aShift+i]);
151 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitle(const int theRow,
152 const std::string& theTitle)
156 string aTitle(theTitle), aUnit = GetRowUnit(theRow);
161 myRows[theRow-1] = aTitle;
163 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
166 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnit(const int theRow,
167 const std::string& theUnit)
171 std::string aTitle = GetRowTitle(theRow);
175 myRows[theRow-1] = aTitle;
177 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
180 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnits(const vector<string>& theUnits)
182 if (theUnits.size() != GetNbRows()) throw DFexception("Invalid number of rows");
183 int aLength = theUnits.size(), i;
184 for(i = 1; i <= aLength; i++) SetRowUnit(i, theUnits[i-1]);
186 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
189 vector<string> SALOMEDSImpl_AttributeTableOfInteger::GetRowUnits()
192 int aLength = myRows.size(), i;
193 for(i=0; i<aLength; i++) aSeq.push_back(getUnit(myRows[i]));
197 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitles(const vector<string>& theTitles)
199 if (theTitles.size() != GetNbRows()) throw DFexception("Invalid number of rows");
200 int aLength = theTitles.size(), i;
201 for(i = 1; i <= aLength; i++) SetRowTitle(i, theTitles[i-1]);
203 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
206 vector<string> SALOMEDSImpl_AttributeTableOfInteger::GetRowTitles()
209 int aLength = myRows.size(), i;
210 for(i=0; i<aLength; i++) aSeq.push_back(getTitle(myRows[i]));
215 std::string SALOMEDSImpl_AttributeTableOfInteger::GetRowTitle(const int theRow) const
217 return getTitle(myRows[theRow-1]);
221 std::string SALOMEDSImpl_AttributeTableOfInteger::GetRowUnit(const int theRow) const
223 return getUnit(myRows[theRow-1]);
227 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnData(const int theColumn,
228 const vector<int>& theData)
231 if(theColumn > myNbColumns) SetNbColumns(theColumn);
235 int i, aLength = theData.size();
236 for(i = 1; i <= aLength; i++) {
237 myTable[myNbColumns*(i-1)+theColumn] = theData[i-1];
240 if(aLength > myNbRows) {
242 while (myRows.size() < myNbRows) { // append empty row titles
243 myRows.push_back(std::string(""));
247 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
251 vector<int> SALOMEDSImpl_AttributeTableOfInteger::GetColumnData(const int theColumn)
255 for(i = 1; i <= myNbRows; i++) {
256 anIndex = myNbColumns*(i-1) + theColumn;
257 if(myTable.find(anIndex) != myTable.end())
258 aSeq.push_back(myTable[anIndex]);
266 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitle(const int theColumn,
267 const std::string& theTitle)
271 while(myCols.size() < theColumn) myCols.push_back(std::string(""));
272 myCols[theColumn-1] = theTitle;
274 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
277 std::string SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitle(const int theColumn) const
279 if(myCols.empty()) return "";
280 if(myCols.size() < theColumn) return "";
281 return myCols[theColumn-1];
284 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitles(const vector<string>& theTitles)
286 if (theTitles.size() != myNbColumns) throw DFexception("Invalid number of columns");
287 int aLength = theTitles.size(), i;
288 for(i = 0; i < aLength; i++) myCols[i] = theTitles[i];
290 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
293 vector<string> SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitles()
296 int aLength = myCols.size(), i;
297 for(i=0; i<aLength; i++) aSeq.push_back(myCols[i]);
301 int SALOMEDSImpl_AttributeTableOfInteger::GetNbRows() const
306 int SALOMEDSImpl_AttributeTableOfInteger::GetNbColumns() const
311 void SALOMEDSImpl_AttributeTableOfInteger::PutValue(const int theValue,
316 if(theColumn > myNbColumns) SetNbColumns(theColumn);
318 int anIndex = (theRow-1)*myNbColumns + theColumn;
319 myTable[anIndex] = theValue;
321 if(theRow > myNbRows) {
322 while (myRows.size() < theRow) { // append empty row titles
323 myRows.push_back(std::string(""));
328 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
331 bool SALOMEDSImpl_AttributeTableOfInteger::HasValue(const int theRow,
334 if(theRow > myNbRows || theRow < 1) return false;
335 if(theColumn > myNbColumns || theColumn < 1) return false;
336 int anIndex = (theRow-1)*myNbColumns + theColumn;
337 return (myTable.find(anIndex) != myTable.end());
340 int SALOMEDSImpl_AttributeTableOfInteger::GetValue(const int theRow,
343 if(theRow > myNbRows || theRow < 1) throw DFexception("Invalid cell index");
344 if(theColumn > myNbColumns || theColumn < 1) DFexception("Invalid cell index");
346 int anIndex = (theRow-1)*myNbColumns + theColumn;
347 if(myTable.find(anIndex) != myTable.end()) return myTable[anIndex];
349 throw DFexception("Invalid cell index");
353 const std::string& SALOMEDSImpl_AttributeTableOfInteger::ID() const
358 void SALOMEDSImpl_AttributeTableOfInteger::Restore(DF_Attribute* with)
361 SALOMEDSImpl_AttributeTableOfInteger* aTable = dynamic_cast<SALOMEDSImpl_AttributeTableOfInteger*>(with);
362 if(!aTable) throw DFexception("Can't Restore from a null attribute");
368 myTable = aTable->myTable;
369 myNbRows = aTable->myNbRows;
370 myNbColumns = aTable->myNbColumns;
371 myTitle = aTable->myTitle;
373 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
374 myRows.push_back(aTable->GetRowTitle(anIndex));
376 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
377 myCols.push_back(aTable->GetColumnTitle(anIndex));
380 DF_Attribute* SALOMEDSImpl_AttributeTableOfInteger::NewEmpty() const
382 return new SALOMEDSImpl_AttributeTableOfInteger();
385 void SALOMEDSImpl_AttributeTableOfInteger::Paste(DF_Attribute* into)
388 SALOMEDSImpl_AttributeTableOfInteger* aTable = dynamic_cast<SALOMEDSImpl_AttributeTableOfInteger*>(into);
389 if(!aTable) throw DFexception("Can't Paste into a null attribute");
391 aTable->myTable.clear();
392 aTable->myCols.clear();
393 aTable->myRows.clear();
395 aTable->myTable = myTable;
396 aTable->myTitle = myTitle;
397 aTable->myNbRows = myNbRows;
398 aTable->myNbColumns = myNbColumns;
400 for(anIndex = 1; anIndex <= GetNbRows();anIndex++)
401 aTable->myRows.push_back(GetRowTitle(anIndex));
402 for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++)
403 aTable->myCols.push_back(GetColumnTitle(anIndex));
407 vector<int> SALOMEDSImpl_AttributeTableOfInteger::GetSetRowIndices(const int theRow)
411 int i, aShift = myNbColumns*(theRow-1);
412 for(i = 1; i <= myNbColumns; i++) {
413 if(myTable.find(aShift + i) != myTable.end()) aSeq.push_back(i);
419 vector<int> SALOMEDSImpl_AttributeTableOfInteger::GetSetColumnIndices(const int theColumn)
424 for(i = 1; i <= myNbRows; i++) {
425 anIndex = myNbColumns*(i-1)+theColumn;
426 if(myTable.find(anIndex) != myTable.end()) aSeq.push_back(i);
433 string SALOMEDSImpl_AttributeTableOfInteger::Save()
435 ostrstream theStream;
438 theStream.precision(64);
442 theStream << l << "\n";
444 theStream << myTitle[i] << "\n";
447 theStream << myNbRows << "\n";
450 for(i=0; i<myNbRows; i++) {
451 l = myRows[i].size();
452 theStream << l << "\n";
454 theStream << myRows[i][j] << "\n";
458 theStream << myNbColumns << "\n";
461 for(i=0; i<myNbColumns; i++) {
462 l = myCols[i].size();
463 theStream << l << "\n";
465 theStream << myCols[i][j] << "\n";
468 //Store the table values
470 theStream << l << "\n";
471 for(MI p = myTable.begin(); p != myTable.end(); p++) {
472 theStream << p->first << "\n";
473 theStream << p->second << "\n";
476 string aString((char*)theStream.rdbuf()->str());
480 void SALOMEDSImpl_AttributeTableOfInteger::Load(const string& value)
482 istrstream theStream(value.c_str(), strlen(value.c_str()));
493 myTitle = std::string(l, 0);
495 theStream >> anExtChar;
496 myTitle[i] = anExtChar;
500 theStream >> myNbRows;
504 for(i=1; i<=myNbRows; i++) {
506 aStr = std::string(l,0);
508 theStream >> anExtChar;
511 myRows.push_back(aStr);
515 theStream >> myNbColumns;
519 for(i=1; i<=myNbColumns; i++) {
521 aStr = std::string(l,0);
523 theStream >> anExtChar;
526 myCols.push_back(aStr);
529 //Restore the table values
532 for(i=1; i<=l; i++) {
536 myTable[aKey] = aValue;