1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SALOMEDSImpl_AttributeTableOfInteger.cxx
23 // Author : Michael Ponikarov
26 #include "SALOMEDSImpl_AttributeTableOfInteger.hxx"
31 #define SEPARATOR '\1'
32 typedef map<int, int>::const_iterator MI;
34 static std::string getUnit(std::string theString)
36 std::string aString(theString);
37 int aPos = aString.find(SEPARATOR);
38 if(aPos <= 0 || aPos == aString.size() ) return std::string();
39 return aString.substr(aPos+1, aString.size());
42 static std::string getTitle(std::string theString)
44 std::string aString(theString);
45 int aPos = aString.find(SEPARATOR);
46 if(aPos < 1) return aString;
47 if(aPos == 0) return std::string();
48 return aString.substr(0, aPos);
51 const std::string& SALOMEDSImpl_AttributeTableOfInteger::GetID()
53 static std::string SALOMEDSImpl_AttributeTableOfIntegerID ("128371A0-8F52-11d6-A8A3-0001021E8C7F");
54 return SALOMEDSImpl_AttributeTableOfIntegerID;
57 SALOMEDSImpl_AttributeTableOfInteger* SALOMEDSImpl_AttributeTableOfInteger::Set(const DF_Label& label)
59 SALOMEDSImpl_AttributeTableOfInteger* A = NULL;
60 if (!(A=(SALOMEDSImpl_AttributeTableOfInteger*)label.FindAttribute(SALOMEDSImpl_AttributeTableOfInteger::GetID()))) {
61 A = new SALOMEDSImpl_AttributeTableOfInteger();
62 label.AddAttribute(A);
67 SALOMEDSImpl_AttributeTableOfInteger::SALOMEDSImpl_AttributeTableOfInteger()
68 :SALOMEDSImpl_GenericAttribute("AttributeTableOfInteger")
74 void SALOMEDSImpl_AttributeTableOfInteger::SetNbColumns(const int theNbColumns)
84 for(MI p = aMap.begin(); p != aMap.end(); p++) {
85 int aRow = (int)(p->first/myNbColumns) + 1;
86 int aCol = (int)(p->first - myNbColumns*(aRow-1));
87 if(aCol == 0) { aCol = myNbColumns; aRow--; }
88 if(aCol > theNbColumns) continue;
89 int aKey = (aRow-1)*theNbColumns+aCol;
90 myTable[aKey] = p->second;
93 myNbColumns = theNbColumns;
95 while (myCols.size() < myNbColumns) { // append empty columns titles
96 myCols.push_back(std::string(""));
99 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
103 void SALOMEDSImpl_AttributeTableOfInteger::SetTitle(const std::string& theTitle)
109 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
112 std::string SALOMEDSImpl_AttributeTableOfInteger::GetTitle() const
117 void SALOMEDSImpl_AttributeTableOfInteger::SetRowData(const int theRow,
118 const vector<int>& theData)
121 if(theData.size() > myNbColumns) SetNbColumns(theData.size());
125 while (myRows.size() < theRow) { // append new row titles
126 myRows.push_back(std::string(""));
129 int i, aShift = (theRow-1)*myNbColumns, aLength = theData.size();
130 for(i = 1; i <= aLength; i++) {
131 myTable[aShift + i] = theData[i-1];
134 if(theRow > myNbRows) myNbRows = theRow;
136 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
139 vector<int> SALOMEDSImpl_AttributeTableOfInteger::GetRowData(const int theRow)
142 int i, aShift = (theRow-1)*myNbColumns;
143 for(i = 1; i <= myNbColumns; i++) {
144 if(myTable.find(aShift+i) != myTable.end())
145 aSeq.push_back(myTable[aShift+i]);
153 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitle(const int theRow,
154 const std::string& theTitle)
158 string aTitle(theTitle), aUnit = GetRowUnit(theRow);
163 myRows[theRow-1] = aTitle;
165 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
168 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnit(const int theRow,
169 const std::string& theUnit)
173 std::string aTitle = GetRowTitle(theRow);
177 myRows[theRow-1] = aTitle;
179 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
182 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnits(const vector<string>& theUnits)
184 if (theUnits.size() != GetNbRows()) throw DFexception("Invalid number of rows");
185 int aLength = theUnits.size(), i;
186 for(i = 1; i <= aLength; i++) SetRowUnit(i, theUnits[i-1]);
188 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
191 vector<string> SALOMEDSImpl_AttributeTableOfInteger::GetRowUnits()
194 int aLength = myRows.size(), i;
195 for(i=0; i<aLength; i++) aSeq.push_back(getUnit(myRows[i]));
199 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitles(const vector<string>& theTitles)
201 if (theTitles.size() != GetNbRows()) throw DFexception("Invalid number of rows");
202 int aLength = theTitles.size(), i;
203 for(i = 1; i <= aLength; i++) SetRowTitle(i, theTitles[i-1]);
205 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
208 vector<string> SALOMEDSImpl_AttributeTableOfInteger::GetRowTitles()
211 int aLength = myRows.size(), i;
212 for(i=0; i<aLength; i++) aSeq.push_back(getTitle(myRows[i]));
217 std::string SALOMEDSImpl_AttributeTableOfInteger::GetRowTitle(const int theRow) const
219 return getTitle(myRows[theRow-1]);
223 std::string SALOMEDSImpl_AttributeTableOfInteger::GetRowUnit(const int theRow) const
225 return getUnit(myRows[theRow-1]);
229 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnData(const int theColumn,
230 const vector<int>& theData)
233 if(theColumn > myNbColumns) SetNbColumns(theColumn);
237 int i, aLength = theData.size();
238 for(i = 1; i <= aLength; i++) {
239 myTable[myNbColumns*(i-1)+theColumn] = theData[i-1];
242 if(aLength > myNbRows) {
244 while (myRows.size() < myNbRows) { // append empty row titles
245 myRows.push_back(std::string(""));
249 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
253 vector<int> SALOMEDSImpl_AttributeTableOfInteger::GetColumnData(const int theColumn)
257 for(i = 1; i <= myNbRows; i++) {
258 anIndex = myNbColumns*(i-1) + theColumn;
259 if(myTable.find(anIndex) != myTable.end())
260 aSeq.push_back(myTable[anIndex]);
268 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitle(const int theColumn,
269 const std::string& theTitle)
273 while(myCols.size() < theColumn) myCols.push_back(std::string(""));
274 myCols[theColumn-1] = theTitle;
276 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
279 std::string SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitle(const int theColumn) const
281 if(myCols.empty()) return "";
282 if(myCols.size() < theColumn) return "";
283 return myCols[theColumn-1];
286 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitles(const vector<string>& theTitles)
288 if (theTitles.size() != myNbColumns) throw DFexception("Invalid number of columns");
289 int aLength = theTitles.size(), i;
290 for(i = 0; i < aLength; i++) myCols[i] = theTitles[i];
292 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
295 vector<string> SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitles()
298 int aLength = myCols.size(), i;
299 for(i=0; i<aLength; i++) aSeq.push_back(myCols[i]);
303 int SALOMEDSImpl_AttributeTableOfInteger::GetNbRows() const
308 int SALOMEDSImpl_AttributeTableOfInteger::GetNbColumns() const
313 void SALOMEDSImpl_AttributeTableOfInteger::PutValue(const int theValue,
318 if(theColumn > myNbColumns) SetNbColumns(theColumn);
320 int anIndex = (theRow-1)*myNbColumns + theColumn;
321 myTable[anIndex] = theValue;
323 if(theRow > myNbRows) {
324 while (myRows.size() < theRow) { // append empty row titles
325 myRows.push_back(std::string(""));
330 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
333 bool SALOMEDSImpl_AttributeTableOfInteger::HasValue(const int theRow,
336 if(theRow > myNbRows || theRow < 1) return false;
337 if(theColumn > myNbColumns || theColumn < 1) return false;
338 int anIndex = (theRow-1)*myNbColumns + theColumn;
339 return (myTable.find(anIndex) != myTable.end());
342 int SALOMEDSImpl_AttributeTableOfInteger::GetValue(const int theRow,
345 if(theRow > myNbRows || theRow < 1) throw DFexception("Invalid cell index");
346 if(theColumn > myNbColumns || theColumn < 1) DFexception("Invalid cell index");
348 int anIndex = (theRow-1)*myNbColumns + theColumn;
349 if(myTable.find(anIndex) != myTable.end()) return myTable[anIndex];
351 throw DFexception("Invalid cell index");
355 const std::string& SALOMEDSImpl_AttributeTableOfInteger::ID() const
360 void SALOMEDSImpl_AttributeTableOfInteger::Restore(DF_Attribute* with)
363 SALOMEDSImpl_AttributeTableOfInteger* aTable = dynamic_cast<SALOMEDSImpl_AttributeTableOfInteger*>(with);
364 if(!aTable) throw DFexception("Can't Restore from a null attribute");
370 myTable = aTable->myTable;
371 myNbRows = aTable->myNbRows;
372 myNbColumns = aTable->myNbColumns;
373 myTitle = aTable->myTitle;
375 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
376 myRows.push_back(aTable->GetRowTitle(anIndex));
378 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
379 myCols.push_back(aTable->GetColumnTitle(anIndex));
382 DF_Attribute* SALOMEDSImpl_AttributeTableOfInteger::NewEmpty() const
384 return new SALOMEDSImpl_AttributeTableOfInteger();
387 void SALOMEDSImpl_AttributeTableOfInteger::Paste(DF_Attribute* into)
390 SALOMEDSImpl_AttributeTableOfInteger* aTable = dynamic_cast<SALOMEDSImpl_AttributeTableOfInteger*>(into);
391 if(!aTable) throw DFexception("Can't Paste into a null attribute");
393 aTable->myTable.clear();
394 aTable->myCols.clear();
395 aTable->myRows.clear();
397 aTable->myTable = myTable;
398 aTable->myTitle = myTitle;
399 aTable->myNbRows = myNbRows;
400 aTable->myNbColumns = myNbColumns;
402 for(anIndex = 1; anIndex <= GetNbRows();anIndex++)
403 aTable->myRows.push_back(GetRowTitle(anIndex));
404 for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++)
405 aTable->myCols.push_back(GetColumnTitle(anIndex));
409 vector<int> SALOMEDSImpl_AttributeTableOfInteger::GetSetRowIndices(const int theRow)
413 int i, aShift = myNbColumns*(theRow-1);
414 for(i = 1; i <= myNbColumns; i++) {
415 if(myTable.find(aShift + i) != myTable.end()) aSeq.push_back(i);
421 vector<int> SALOMEDSImpl_AttributeTableOfInteger::GetSetColumnIndices(const int theColumn)
426 for(i = 1; i <= myNbRows; i++) {
427 anIndex = myNbColumns*(i-1)+theColumn;
428 if(myTable.find(anIndex) != myTable.end()) aSeq.push_back(i);
435 string SALOMEDSImpl_AttributeTableOfInteger::Save()
438 char* buffer = new char[1024];
443 sprintf(buffer, "%d\n", l);
446 aString += myTitle[i];
451 sprintf(buffer, "%d\n", myNbRows);
455 for(i=0; i<myNbRows; i++) {
456 l = myRows[i].size();
457 sprintf(buffer, "%d\n", l);
460 aString += myRows[i][j];
466 sprintf(buffer, "%d\n", myNbColumns);
470 for(i=0; i<myNbColumns; i++) {
471 l = myCols[i].size();
472 sprintf(buffer, "%d\n", l);
475 aString += myCols[i][j];
480 //Store the table values
482 sprintf(buffer, "%d\n", l);
484 for(MI p = myTable.begin(); p != myTable.end(); p++) {
485 sprintf(buffer, "%d\n%d\n", p->first, p->second);
495 void SALOMEDSImpl_AttributeTableOfInteger::Load(const string& value)
498 int i, j, l, pos, aSize = (int)value.size();
499 for(i = 0, pos = 0; i<aSize; i++) {
500 if(value[i] == '\n') {
501 v.push_back(value.substr(pos, i-pos));
512 l = strtol(v[pos++].c_str(), NULL, 10);
514 myTitle = std::string(l, 0);
516 myTitle[i] = v[pos++][0];
520 myNbRows = strtol(v[pos++].c_str(), NULL, 10);
524 for(i=1; i<=myNbRows; i++) {
525 l = strtol(v[pos++].c_str(), NULL, 10);
526 aStr = std::string(l,0);
528 aStr[j] = v[pos++][0];
530 myRows.push_back(aStr);
534 myNbColumns = strtol(v[pos++].c_str(), NULL, 10);
538 for(i=1; i<=myNbColumns; i++) {
539 l = strtol(v[pos++].c_str(), NULL, 10);
540 aStr = std::string(l,0);
542 aStr[j] = v[pos++][0];
544 myCols.push_back(aStr);
547 //Restore the table values
548 l = strtol(v[pos++].c_str(), NULL, 10);
550 for(i=1; i<=l; i++) {
551 int aKey = strtol(v[pos++].c_str(), NULL, 10);
552 int aValue = strtol(v[pos++].c_str(), NULL, 10);
553 myTable[aKey] = aValue;