Salome HOME
Roll back invalid integration.
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeTableOfReal.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SALOMEDSImpl_AttributeTableOfReal.cxx
21 //  Author : Michael Ponikarov
22 //  Module : SALOME
23
24 #include <SALOMEDSImpl_AttributeTableOfReal.hxx>
25 #include <Standard_Failure.hxx>
26 #include <TColStd_DataMapIteratorOfDataMapOfIntegerReal.hxx>
27 #include <Standard_GUID.hxx>
28 #include <stdio.h>
29 #include <TColStd_HSequenceOfExtendedString.hxx>  
30
31 using namespace std;
32
33 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeTableOfReal, SALOMEDSImpl_GenericAttribute )
34 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeTableOfReal, SALOMEDSImpl_GenericAttribute )
35
36 #define SEPARATOR '\1'
37
38 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
39 {
40   TCollection_ExtendedString aString(theString);
41   int aPos = aString.Search(SEPARATOR);
42   if(aPos <= 0 || aPos == aString.Length() ) return TCollection_ExtendedString();
43   return aString.Split(aPos);
44 }
45
46 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
47 {
48   TCollection_ExtendedString aString(theString);
49   int aPos = aString.Search(SEPARATOR);
50   if(aPos < 1) return aString;
51   if(aPos == 1) return TCollection_ExtendedString();
52   aString.Split(aPos-1);
53   return aString;
54 }
55
56 const Standard_GUID& SALOMEDSImpl_AttributeTableOfReal::GetID() 
57 {
58   static Standard_GUID SALOMEDSImpl_AttributeTableOfRealID ("128371A1-8F52-11d6-A8A3-0001021E8C7F");
59   return SALOMEDSImpl_AttributeTableOfRealID;
60 }
61
62 Handle(SALOMEDSImpl_AttributeTableOfReal) SALOMEDSImpl_AttributeTableOfReal::Set(const TDF_Label& label) 
63 {
64   Handle(SALOMEDSImpl_AttributeTableOfReal) anAttr;
65   if (!label.FindAttribute(SALOMEDSImpl_AttributeTableOfReal::GetID(),anAttr)) {
66     anAttr = new SALOMEDSImpl_AttributeTableOfReal();
67     label.AddAttribute(anAttr);
68   }
69   return anAttr;
70 }
71
72 SALOMEDSImpl_AttributeTableOfReal::SALOMEDSImpl_AttributeTableOfReal() 
73 :SALOMEDSImpl_GenericAttribute("AttributeTableOfReal")
74 {
75   myRows = new TColStd_HSequenceOfExtendedString();
76   myCols = new TColStd_HSequenceOfExtendedString();
77   myNbRows = 0;
78   myNbColumns = 0;
79 }
80
81 void SALOMEDSImpl_AttributeTableOfReal::SetNbColumns(const Standard_Integer theNbColumns)
82 {
83   CheckLocked();  
84   Backup();
85   
86   TColStd_DataMapOfIntegerReal aMap;
87   aMap = myTable;
88   myTable.Clear();
89
90   TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(aMap);
91   for(; anIterator.More(); anIterator.Next()) {
92     int aRow = (int)(anIterator.Key()/myNbColumns) + 1;
93     int aCol = (int)(anIterator.Key() - myNbColumns*(aRow-1));
94     if(aCol == 0) { aCol = myNbColumns; aRow--; }
95     if(aCol > theNbColumns) continue;
96     int aKey = (aRow-1)*theNbColumns+aCol;
97     myTable.Bind(aKey, anIterator.Value());
98   }
99
100   myNbColumns = theNbColumns;
101
102   while (myCols->Length() < myNbColumns) { // append empty columns titles
103     myCols->Append(TCollection_ExtendedString(""));
104   }
105   
106   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
107 }
108
109 void SALOMEDSImpl_AttributeTableOfReal::SetTitle(const TCollection_ExtendedString& theTitle) 
110 {
111   CheckLocked();  
112   Backup();
113   myTitle = theTitle;
114   
115   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
116 }
117
118 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetTitle() const 
119 {
120   return myTitle;
121 }
122
123 void SALOMEDSImpl_AttributeTableOfReal::SetRowData(const Standard_Integer theRow,
124                                                    const Handle(TColStd_HSequenceOfReal)& theData) 
125 {
126   CheckLocked();  
127   if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
128
129   Backup();
130
131   while (myRows->Length() < theRow) { // append new row titles
132     myRows->Append(TCollection_ExtendedString(""));
133   }
134
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));
138   }
139
140   if(theRow > myNbRows) myNbRows = theRow;
141   
142   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
143 }
144
145 Handle(TColStd_HSequenceOfReal) SALOMEDSImpl_AttributeTableOfReal::GetRowData(const Standard_Integer theRow)
146 {
147   Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal();
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));
152      else
153        aSeq->Append(0.);
154   }
155   
156   return aSeq;
157 }
158
159
160 void SALOMEDSImpl_AttributeTableOfReal::SetRowTitle(const Standard_Integer theRow,
161                                                        const TCollection_ExtendedString& theTitle) 
162 {
163   CheckLocked();  
164   Backup();
165   TCollection_ExtendedString aTitle(theTitle), aUnit = GetRowUnit(theRow);
166   if(aUnit.Length()>0) {
167     aTitle += SEPARATOR;
168     aTitle += aUnit;
169   }
170   myRows->SetValue(theRow, aTitle);
171   
172   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
173 }
174
175 void SALOMEDSImpl_AttributeTableOfReal::SetRowUnit(const Standard_Integer theRow,
176                                                       const TCollection_ExtendedString& theUnit) 
177 {
178   CheckLocked();  
179   Backup();
180   TCollection_ExtendedString aTitle = GetRowTitle(theRow);
181   aTitle += SEPARATOR;
182   aTitle += theUnit;
183
184   myRows->SetValue(theRow, aTitle);
185   
186   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
187 }
188
189 void SALOMEDSImpl_AttributeTableOfReal::SetRowUnits(const Handle(TColStd_HSequenceOfExtendedString)& theUnits)
190 {
191   if (theUnits->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
192   int aLength = theUnits->Length(), i;
193   for(i = 1; i <= aLength; i++) SetRowUnit(i, theUnits->Value(i));
194   
195   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
196 }
197
198 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetRowUnits()
199 {
200   Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
201   int aLength = myRows->Length(), i;
202   for(i=1; i<=aLength; i++) aSeq->Append(getUnit(myRows->Value(i)));
203   return aSeq;
204 }
205
206 void SALOMEDSImpl_AttributeTableOfReal::SetRowTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
207 {
208   if (theTitles->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
209   int aLength = theTitles->Length(), i;
210   for(i = 1; i <= aLength; i++) SetRowTitle(i, theTitles->Value(i));
211   
212   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
213 }
214
215 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetRowTitles()
216 {
217   Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
218   int aLength = myRows->Length(), i;
219   for(i=1; i<=aLength; i++) aSeq->Append(getTitle(myRows->Value(i)));
220   return aSeq;
221 }
222
223
224 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetRowTitle(const Standard_Integer theRow) const 
225 {
226   return getTitle(myRows->Value(theRow));
227 }
228
229
230 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetRowUnit(const Standard_Integer theRow) const 
231 {
232   return getUnit(myRows->Value(theRow));
233 }
234
235 void SALOMEDSImpl_AttributeTableOfReal::SetColumnData(const Standard_Integer theColumn,
236                                                       const Handle(TColStd_HSequenceOfReal)& theData) 
237 {
238   CheckLocked();  
239   if(theColumn > myNbColumns) SetNbColumns(theColumn);
240
241   Backup();
242
243   Standard_Integer i, aLength = theData->Length();
244   for(i = 1; i <= aLength; i++) {
245     myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
246   }
247
248   if(aLength > myNbRows) {
249     myNbRows = aLength;
250     while (myRows->Length() < myNbRows) { // append empty row titles
251       myRows->Append(TCollection_ExtendedString(""));
252     }
253   }
254   
255   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
256 }
257
258
259 Handle(TColStd_HSequenceOfReal) SALOMEDSImpl_AttributeTableOfReal::GetColumnData(const Standard_Integer theColumn)
260 {
261   Handle(TColStd_HSequenceOfReal) aSeq = new TColStd_HSequenceOfReal;
262   
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));
268     else
269       aSeq->Append(0.);
270   }
271   
272   return aSeq;
273 }
274
275 void SALOMEDSImpl_AttributeTableOfReal::SetColumnTitle(const Standard_Integer theColumn,
276                                                        const TCollection_ExtendedString& theTitle) 
277 {
278   CheckLocked();  
279   Backup();
280   while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
281   myCols->SetValue(theColumn,theTitle);
282   
283   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
284 }
285
286 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfReal::GetColumnTitle(const Standard_Integer theColumn) const 
287 {
288   if(myCols.IsNull()) return "";
289   if(myCols->Length() < theColumn) return "";
290   return myCols->Value(theColumn);
291 }
292
293 void SALOMEDSImpl_AttributeTableOfReal::SetColumnTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
294 {
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));
298   
299   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
300 }
301
302 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfReal::GetColumnTitles()
303 {
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));
307   return aSeq;
308 }
309
310 Standard_Integer SALOMEDSImpl_AttributeTableOfReal::GetNbRows() const
311 {
312   return myNbRows;
313 }
314
315 Standard_Integer SALOMEDSImpl_AttributeTableOfReal::GetNbColumns() const
316 {
317   return myNbColumns;
318 }
319
320 void SALOMEDSImpl_AttributeTableOfReal::PutValue(const Standard_Real theValue,
321                                              const Standard_Integer theRow,
322                                              const Standard_Integer theColumn) 
323 {
324   CheckLocked();      
325   if(theColumn > myNbColumns) SetNbColumns(theColumn);
326
327   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
328   myTable.Bind(anIndex, theValue);
329
330   if(theRow > myNbRows) {
331     while (myRows->Length() < theRow) { // append empty row titles
332       myRows->Append(TCollection_ExtendedString(""));
333     }
334     myNbRows = theRow;
335   }
336   
337   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
338 }
339
340 Standard_Boolean SALOMEDSImpl_AttributeTableOfReal::HasValue(const Standard_Integer theRow,
341                                                          const Standard_Integer theColumn) 
342 {
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); 
347 }
348
349 Standard_Real SALOMEDSImpl_AttributeTableOfReal::GetValue(const Standard_Integer theRow,
350                                                       const Standard_Integer theColumn) 
351 {
352   if(theRow > myNbRows || theRow < 1) Standard_Failure::Raise("Invalid cell index");
353   if(theColumn > myNbColumns || theColumn < 1) Standard_Failure::Raise("Invalid cell index");
354
355   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
356   if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
357   
358   Standard_Failure::Raise("Invalid cell index");
359   return 0.;
360 }
361
362 const Standard_GUID& SALOMEDSImpl_AttributeTableOfReal::ID() const
363 {
364   return GetID();
365 }
366
367 void SALOMEDSImpl_AttributeTableOfReal::Restore(const Handle(TDF_Attribute)& with) 
368 {
369   Standard_Integer anIndex;
370   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(with);
371
372   myTable.Clear();
373   myCols->Clear();
374   myRows->Clear();
375
376   myTable = aTable->myTable;
377   myNbRows = aTable->myNbRows;
378   myNbColumns = aTable->myNbColumns;
379   myTitle = aTable->myTitle;
380   
381   for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
382     myRows->Append(aTable->GetRowTitle(anIndex));
383
384   for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++) 
385     myCols->Append(aTable->GetColumnTitle(anIndex));
386 }
387
388 Handle(TDF_Attribute) SALOMEDSImpl_AttributeTableOfReal::NewEmpty() const
389 {
390   return new SALOMEDSImpl_AttributeTableOfReal();
391 }
392
393 void SALOMEDSImpl_AttributeTableOfReal::Paste(const Handle(TDF_Attribute)& into,
394                                              const Handle(TDF_RelocationTable)&) const
395 {
396   Standard_Integer anIndex;
397   Handle(SALOMEDSImpl_AttributeTableOfReal) aTable = Handle(SALOMEDSImpl_AttributeTableOfReal)::DownCast(into);
398
399   aTable->myTable.Clear();
400   aTable->myCols->Clear();
401   aTable->myRows->Clear();
402
403   aTable->myTable = myTable;
404   aTable->myTitle = myTitle;
405   aTable->myNbRows = myNbRows;
406   aTable->myNbColumns = myNbColumns;
407
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));
412 }
413
414
415 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfReal::GetSetRowIndices(const Standard_Integer theRow)
416 {
417   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
418
419   Standard_Integer i, aShift = myNbColumns*(theRow-1);
420   for(i = 1; i <= myNbColumns; i++) {
421     if(myTable.IsBound(aShift + i)) aSeq->Append(i);
422   }
423   
424   return aSeq;
425 }
426
427 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfReal::GetSetColumnIndices(const Standard_Integer theColumn)
428 {
429   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
430
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);
435   }
436   
437   return aSeq;
438 }
439
440
441
442 void SALOMEDSImpl_AttributeTableOfReal::ConvertToString(ostrstream& theStream)
443 {
444   int i, j, l;
445
446   
447   //Title
448   l = myTitle.Length();
449   theStream << l << "\n";
450   for(i=1; i<=l; i++)
451     theStream << myTitle.Value(i) << "\n";
452
453   //Nb rows
454   theStream << myNbRows << "\n";
455
456   //Rows titles
457   for(i=1; i<=myNbRows; i++) {
458     l = myRows->Value(i).Length();
459     theStream << l << "\n";
460     for(j=1; j<=l; j++)
461       theStream << myRows->Value(i).Value(j) << "\n";
462   }
463
464   //Nb columns
465   theStream << myNbColumns << "\n";
466
467   //Columns titles
468   for(i=1; i<=myNbColumns; i++) {
469     l = myCols->Value(i).Length();
470     theStream << l << "\n";
471     for(j=1; j<=l; j++)
472       theStream << myCols->Value(i).Value(j) << "\n";
473   }
474
475   //Store the table values
476   l = myTable.Extent();
477   theStream << l << "\n";
478   char *aBuffer = new char[128];
479   TColStd_DataMapIteratorOfDataMapOfIntegerReal anIterator(myTable);
480   for(; anIterator.More(); anIterator.Next()) {
481     theStream << anIterator.Key() << "\n";
482     sprintf(aBuffer, "%.64e", anIterator.Value());
483     theStream << aBuffer << "\n";
484   }
485   
486   delete []aBuffer;
487
488   return;
489 }
490
491 bool SALOMEDSImpl_AttributeTableOfReal::RestoreFromString(istrstream& theStream)
492 {
493   Backup();
494
495   int i, j, l;
496
497   Standard_ExtCharacter anExtChar;
498   TCollection_ExtendedString aStr;
499
500   //Title
501   theStream >> l;
502
503   myTitle = TCollection_ExtendedString(l, 0);
504   for(i=1; i<=l; i++) {
505     theStream >> anExtChar;
506     myTitle.SetValue(i, anExtChar);
507   }
508
509   //Nb rows
510   theStream >> myNbRows;
511
512   //Rows titles
513   myRows->Clear();  
514   for(i=1; i<=myNbRows; i++) { 
515     theStream >> l;
516     aStr = TCollection_ExtendedString(l,0);
517     for(j=1; j<=l; j++) {
518       theStream >> anExtChar;
519       aStr.SetValue(j, anExtChar);
520     }
521     myRows->Append(aStr);
522   }
523
524   //Nb columns
525   theStream >> myNbColumns;
526
527   //Columns titles
528   myCols->Clear();
529   for(i=1; i<=myNbColumns; i++) {
530     theStream >> l;
531     aStr = TCollection_ExtendedString(l,0);
532     for(j=1; j<=l; j++) {
533       theStream >> anExtChar;
534       aStr.SetValue(j, anExtChar);
535     }
536     myCols->Append(aStr);
537   }
538
539   //Restore the table values
540   theStream >> l;
541   myTable.Clear();
542   for(i=1; i<=l; i++) {
543     Standard_Integer aKey;
544     Standard_Real aValue;
545     theStream >> aKey;
546     theStream >> aValue;
547     myTable.Bind(aKey, aValue);
548   }
549
550   return true;
551 }
552
553 TCollection_AsciiString SALOMEDSImpl_AttributeTableOfReal::Save() 
554 {
555   ostrstream ostr;
556   ConvertToString(ostr);
557   TCollection_AsciiString aString((char*)ostr.rdbuf()->str());
558   return aString;
559 }
560
561 void SALOMEDSImpl_AttributeTableOfReal::Load(const TCollection_AsciiString& value) 
562 {
563   istrstream aStream(value.ToCString(), strlen(value.ToCString()));
564   RestoreFromString(aStream);
565 }