Salome HOME
First stable version after merging with V3_2_2
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeParameter.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_AttributeParameter.cxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24
25 #include "SALOMEDSImpl_AttributeParameter.hxx"
26 #include <Standard_Failure.hxx>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <strstream>
31
32 using namespace std;
33
34
35
36 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeParameter, SALOMEDSImpl_GenericAttribute )
37 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeParameter, SALOMEDSImpl_GenericAttribute )
38
39
40 // Purpose: Each character in the string is replaced by 3 characters: '%' and hex number 
41 //          of the character (2 characters)
42 string convertString(const string& S)
43 {
44   int length = S.size();
45   const char *s = S.c_str();
46   char *c = new char[3], *buffer = new char[length*3+1];
47   buffer[length*3] = (char)0;
48   for(int i = 0, pos = 0; i<length; i++, pos+=3) {
49     int val = (int)s[i];
50     buffer[pos] = '%';
51     sprintf(c, "%x", val);
52     buffer[pos+1] = c[0]; 
53     buffer[pos+2] = c[1];
54   }
55
56   string RS(buffer); 
57   delete c;
58   delete buffer;
59   return RS;
60 }
61
62 //Restors a string converted by the function convertString
63 string restoreString(const string& S)
64 {
65   int length = S.size();
66   char *c = new char[3], *buffer = new char[length/3+1];
67   buffer[length/3] = (char)0;
68   const char *s = S.c_str();
69   for(int i = 0, pos = 0; i<length; i+=3, pos++) {
70     c[0] = s[i+1];
71     c[1] = s[i+2];
72     c[2] = (char)0;
73     int val = strtol(c, NULL, 16);
74     buffer[pos] = (char)val;
75   }
76
77   string RS(buffer); 
78   delete c;
79   delete buffer;
80   return RS;
81 }
82
83 //=======================================================================
84 /*!
85  * Function : GetID
86  * Purpose  : Returns GUID of the attribute
87  */
88 //=======================================================================
89 const Standard_GUID& SALOMEDSImpl_AttributeParameter::GetID ()
90 {
91   static Standard_GUID ParemeterID ("BA75F3A1-E40B-46b8-8D24-B1D3C3BB1A8C");
92   return ParemeterID;
93 }   
94
95 //=======================================================================
96 /*!
97  * Function : Set
98  * Purpose  : Adds an attribute to the label
99  */
100 //=======================================================================
101 Handle(SALOMEDSImpl_AttributeParameter) SALOMEDSImpl_AttributeParameter::Set (const TDF_Label& L) 
102 {
103   Handle(SALOMEDSImpl_AttributeParameter) A;
104   if (!L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID(), A)) {
105     A = new  SALOMEDSImpl_AttributeParameter(); 
106     L.AddAttribute(A);
107   }
108
109   return A;
110 }
111
112 //=======================================================================
113 /*!
114  * Function : SetInt
115  * Purpose  : Associates a integer value with the ID
116  */
117 //=======================================================================
118 void SALOMEDSImpl_AttributeParameter::SetInt(const string& theID, const int& theValue)
119 {
120   CheckLocked();
121
122   Backup();
123
124   _ints[theID] = theValue;
125
126   SetModifyFlag(); 
127 }
128
129 //=======================================================================
130 /*!
131  * Function : GetInt
132  * Purpose  : Returns a int value associated with the given ID
133  */
134 //=======================================================================
135 int SALOMEDSImpl_AttributeParameter::GetInt(const string& theID)
136 {
137   if(!IsSet(theID, PT_INTEGER)) Standard_Failure::Raise("Invalid ID");
138   return _ints[theID];
139 }
140
141 //=======================================================================
142 /*!
143  * Function : SetReal
144  * Purpose  : Associates a double value with the ID
145  */
146 //=======================================================================
147 void SALOMEDSImpl_AttributeParameter::SetReal(const string& theID, const double& theValue)
148 {
149   CheckLocked();
150
151   Backup();
152
153   _reals[theID] = theValue;
154
155   SetModifyFlag(); 
156 }
157
158 //=======================================================================
159 /*!
160  * Function : GetReal
161  * Purpose  : Returns a double value associated with the given ID
162  */
163 //=======================================================================
164 double SALOMEDSImpl_AttributeParameter::GetReal(const string& theID)
165 {
166   if(!IsSet(theID, PT_REAL)) Standard_Failure::Raise("Invalid ID");
167   return _reals[theID];
168 }
169
170 //=======================================================================
171 /*!
172  * Function : SetString
173  * Purpose  : Associates a string with the ID
174  */
175 //=======================================================================
176 void SALOMEDSImpl_AttributeParameter::SetString(const string& theID, const string& theValue)
177 {
178   CheckLocked();
179
180   Backup();
181
182   _strings[theID] = theValue;
183
184   SetModifyFlag(); 
185 }
186
187 //=======================================================================
188 /*!
189  * Function : GetString
190  * Purpose  : Returns a string associated with the given ID
191  */
192 //=======================================================================
193 string SALOMEDSImpl_AttributeParameter::GetString(const string& theID)
194 {
195   if(!IsSet(theID, PT_STRING)) Standard_Failure::Raise("Invalid ID");
196   return _strings[theID];
197 }
198
199 //=======================================================================
200 /*!
201  * Function : SetBool
202  * Purpose  : Associates a bool value with the ID
203  */
204 //=======================================================================
205 void SALOMEDSImpl_AttributeParameter::SetBool(const string& theID, const bool& theValue)
206 {
207   CheckLocked();
208
209   Backup();
210
211   _bools[theID] = theValue;
212
213   SetModifyFlag(); 
214 }
215
216 //=======================================================================
217 /*!
218  * Function : GetBool
219  * Purpose  : Returns a bool value associated with the ID
220  */
221 //=======================================================================
222 bool SALOMEDSImpl_AttributeParameter::GetBool(const string& theID)
223 {
224   if(!IsSet(theID, PT_BOOLEAN)) Standard_Failure::Raise("Invalid ID");
225   return _bools[theID];
226 }
227
228 //=======================================================================
229 /*!
230  * Function : SetRealArray
231  * Purpose  : Associates an array of double values with the given ID
232  */
233 //=======================================================================
234 void SALOMEDSImpl_AttributeParameter::SetRealArray(const string& theID, const vector<double>& theArray)
235 {
236   CheckLocked();
237
238   Backup();
239
240   _realarrays[theID] = theArray;
241
242   SetModifyFlag(); 
243 }
244
245 //=======================================================================
246 /*!
247  * Function : GetRealArray
248  * Purpose  : Returns double values associated with the ID
249  */
250 //=======================================================================
251 vector<double> SALOMEDSImpl_AttributeParameter::GetRealArray(const string& theID)
252 {
253   if(!IsSet(theID, PT_REALARRAY)) Standard_Failure::Raise("Invalid ID");
254   return _realarrays[theID];
255 }
256  
257
258 //=======================================================================
259 /*!
260  * Function : SetIntArray
261  * Purpose  : Associates an array of int values with the given ID
262  */
263 //=======================================================================
264 void SALOMEDSImpl_AttributeParameter::SetIntArray(const string& theID, const vector<int>& theArray)
265 {
266   CheckLocked();
267
268   Backup();
269
270   _intarrays[theID] = theArray;
271
272   SetModifyFlag(); 
273 }
274
275 //=======================================================================
276 /*!
277  * Function : GetIntArray
278  * Purpose  : Returns int values associated with the ID
279  */
280 //=======================================================================
281 vector<int> SALOMEDSImpl_AttributeParameter::GetIntArray(const string& theID)
282 {
283   if(!IsSet(theID, PT_INTARRAY)) Standard_Failure::Raise("Invalid ID");
284   return _intarrays[theID];
285 }
286  
287
288 //=======================================================================
289 /*!
290  * Function : SetStrArray
291  * Purpose  : Associates an array of string values with the given ID
292  */
293 //=======================================================================
294 void SALOMEDSImpl_AttributeParameter::SetStrArray(const string& theID, const vector<string>& theArray)
295 {
296   CheckLocked();
297
298   Backup();
299
300   _strarrays[theID] = theArray;
301
302   SetModifyFlag(); 
303 }
304
305 //=======================================================================
306 /*!
307  * Function : GetStrArray
308  * Purpose  : Returns string values associated with the ID
309  */
310 //=======================================================================
311 vector<string> SALOMEDSImpl_AttributeParameter::GetStrArray(const string& theID)
312 {
313   if(!IsSet(theID, PT_STRARRAY)) Standard_Failure::Raise("Invalid ID");
314   return _strarrays[theID];
315 }
316  
317
318 //=======================================================================
319 /*!
320  * Function : IsSet
321  * Purpose  : Returns true if for the ID of given type was assigned \n
322  *            a value in the attribute
323  */
324 //=======================================================================
325 bool SALOMEDSImpl_AttributeParameter::IsSet(const string& theID, const Parameter_Types theType)
326 {
327   switch(theType) {
328   case PT_INTEGER: {
329     if(_ints.find(theID) != _ints.end()) return true;
330     break;
331   }
332   case PT_REAL: {
333     if(_reals.find(theID) != _reals.end()) return true;
334     break;
335   }
336   case PT_BOOLEAN: {
337     if(_bools.find(theID) != _bools.end()) return true;
338     break;
339   }
340   case PT_STRING: {
341     if(_strings.find(theID) != _strings.end()) return true;
342     break;
343   }
344   case PT_REALARRAY: {
345     if(_realarrays.find(theID) != _realarrays.end()) return true;
346     break;
347   }
348   case PT_INTARRAY: {
349     if(_intarrays.find(theID) != _intarrays.end()) return true;
350     break;
351   }
352   case PT_STRARRAY: {
353     if(_strarrays.find(theID) != _strarrays.end()) return true;
354     break;
355   }
356   default: return false;
357   }
358
359   return false;
360 }
361
362 //=======================================================================
363 /*!
364  * Function : RemoveID
365  * Purpose  : Removes a parameter with given ID
366  */
367 //=======================================================================
368 bool SALOMEDSImpl_AttributeParameter::RemoveID(const string& theID, const Parameter_Types theType)
369 {
370   Backup();
371   SetModifyFlag(); 
372
373   switch(theType) {
374   case PT_INTEGER: {
375     if(_ints.erase(theID)) return true;
376     break;
377   }
378   case PT_REAL: {
379     if(_reals.erase(theID)) return true;
380     break;
381   }
382   case PT_BOOLEAN: {
383     if(_bools.erase(theID)) return true;
384     break;
385   }
386   case PT_STRING: {
387     if(_strings.erase(theID)) return true;
388     break;
389   }
390   case PT_REALARRAY: {
391     if(_realarrays.erase(theID)) return true;
392     break;
393   }
394   case PT_INTARRAY: {
395     if(_intarrays.erase(theID)) return true;
396     break;
397   }
398   case PT_STRARRAY: {
399     if(_strarrays.erase(theID)) return true;
400     break;
401   }
402   default: return false;
403   }
404
405
406   return false;
407 }
408
409
410 //=======================================================================
411 /*!
412  * Function : GetFather
413  * Purpose  : Returns a father attribute for this attribute
414  */
415 //=======================================================================
416 Handle(SALOMEDSImpl_AttributeParameter) SALOMEDSImpl_AttributeParameter::GetFather()
417 {
418   Handle(SALOMEDSImpl_AttributeParameter) aFather;
419   TDF_Label L = Label();
420   if(L.IsRoot()) return aFather;
421
422   while(!L.IsRoot()) {
423     L = L.Father();
424     if(L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID(), aFather)) break; 
425   }
426
427   return aFather;
428 }
429
430 //=======================================================================
431 /*!
432  * Function : HasFather
433  * Purpose  : Returns True if the attribute has a father attribute
434  */
435 //=======================================================================
436 bool SALOMEDSImpl_AttributeParameter::HasFather()
437 {
438   TDF_Label L = Label();
439   if(L.IsRoot()) return false;
440   while(!L.IsRoot()) {
441     L = L.Father();
442     if(L.IsAttribute(SALOMEDSImpl_AttributeParameter::GetID())) return true; 
443   }
444
445   return false;
446 }
447
448 //=======================================================================
449 /*!
450  * Function : IsRoot
451  * Purpose  : Returns True is the attribute is highest in an hierachy
452  */
453 //=======================================================================
454 bool SALOMEDSImpl_AttributeParameter::IsRoot()
455 {
456   return ((HasFather())?false:true);
457 }           
458
459 //=======================================================================
460 /*!
461  * Function : Clear
462  * Purpose  : Clears the content of the attribute
463  */
464 //=======================================================================
465 void SALOMEDSImpl_AttributeParameter::Clear()
466 {
467   Backup();
468
469   _ints.clear();
470   _reals.clear();
471   _bools.clear();
472   _strings.clear();
473   _realarrays.clear();
474   _intarrays.clear();
475   _strarrays.clear();
476
477   SetModifyFlag(); 
478 }
479
480 //=======================================================================
481 /*!
482  * Function : GetIDs
483  * Purpose  : Returns an array of all ID's of the given type
484  */
485 //=======================================================================
486 vector<string> SALOMEDSImpl_AttributeParameter::GetIDs(const Parameter_Types theType)
487 {
488   
489   vector<string> anArray;
490   int i = 0;
491
492   switch(theType) {
493   case PT_INTEGER: {
494     if(_ints.size()) {
495       anArray.resize(_ints.size());
496       for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++, i++) 
497         anArray[i] =  p->first;
498     }
499     break;
500   }
501   case PT_REAL: {
502     if(_reals.size()) {
503       anArray.resize(_reals.size());
504       for(map<string,double>::const_iterator p = _reals.begin(); p != _reals.end(); p++, i++) 
505         anArray[i] = p->first;
506     }
507     break;
508   }
509   case PT_BOOLEAN: {
510     if(_bools.size()) {
511       anArray.resize(_bools.size());
512       for(map<string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++, i++) 
513         anArray[i] = p->first;
514     }
515     break;
516   }
517   case PT_STRING: {
518     if(_strings.size()) {
519       anArray.resize(_strings.size());
520       for(map<string,string>::const_iterator p = _strings.begin(); p!= _strings.end(); p++) 
521         anArray[i] = p->first;
522     }
523     break;
524   }
525   case PT_REALARRAY: {
526     if(_realarrays.size()) {
527       anArray.resize(_realarrays.size());
528       for(map< string, vector<double> >::const_iterator p = _realarrays.begin(); p!= _realarrays.end(); p++) 
529         anArray[i] = p->first;
530     }
531     break;
532   }
533   case PT_INTARRAY: {
534     if(_intarrays.size()) {
535       anArray.resize(_intarrays.size());
536       for(map< string, vector<int> >::const_iterator p = _intarrays.begin(); p!= _intarrays.end(); p++) 
537         anArray[i] = p->first;
538     }
539     break;
540   }
541   case PT_STRARRAY: {
542     if(_strarrays.size()) {
543       anArray.resize(_strarrays.size());
544       for(map< string, vector<string> >::const_iterator p = _strarrays.begin(); p!= _strarrays.end(); p++) 
545         anArray[i] = p->first;
546     }
547     break;
548   }
549   default: return anArray;
550   }
551
552   return anArray;
553 }
554
555 //=======================================================================
556 /*!
557  * Function : ID
558  * Purpose  : Rteurns an GUID of the attribute
559  */
560 //=======================================================================
561 const Standard_GUID& SALOMEDSImpl_AttributeParameter::ID () const { return GetID(); } 
562
563
564 Handle(TDF_Attribute) SALOMEDSImpl_AttributeParameter::NewEmpty () const
565 {  
566   return new SALOMEDSImpl_AttributeParameter(); 
567 }
568
569 //=======================================================================
570 /*!
571  * Function : Restore
572  * Purpose  : Restors the content of the attribute from another
573  */
574 //=======================================================================
575 void SALOMEDSImpl_AttributeParameter::Restore(const Handle(TDF_Attribute)& with) 
576 {
577   Handle(SALOMEDSImpl_AttributeParameter) A = Handle(SALOMEDSImpl_AttributeParameter)::DownCast (with);
578   _ints.clear();
579   _reals.clear();
580   _bools.clear();
581   _strings.clear();
582   _realarrays.clear();
583   _intarrays.clear();
584   _strarrays.clear();
585   
586   for(map<string,int>::const_iterator p = A->_ints.begin(); p!= A->_ints.end(); p++) _ints[p->first] = p->second;
587   for(map<string,double>::const_iterator p = A->_reals.begin(); p!= A->_reals.end(); p++) _reals[p->first] = p->second;
588   for(map<string,bool>::const_iterator p = A->_bools.begin(); p!= A->_bools.end(); p++) _bools[p->first] = p->second;
589   for(map<string,string>::const_iterator p = A->_strings.begin(); p!= A->_strings.end(); p++) 
590     _strings[p->first] = p->second;
591   for(map< string,vector<double> >::const_iterator p = A->_realarrays.begin(); p!= A->_realarrays.end(); p++) 
592     _realarrays[p->first] = p->second;  
593   for(map< string,vector<int> >::const_iterator p = A->_intarrays.begin(); p!= A->_intarrays.end(); p++) 
594     _intarrays[p->first] = p->second;  
595   for(map< string,vector<string> >::const_iterator p = A->_strarrays.begin(); p!= A->_strarrays.end(); p++) 
596     _strarrays[p->first] = p->second; 
597 }
598
599 //=======================================================================
600 /*!
601  * Function : Paste
602  * Purpose  : Pastes the content of attribute to another
603  */
604 //=======================================================================
605 void SALOMEDSImpl_AttributeParameter::Paste (const Handle(TDF_Attribute)& into,
606                                              const Handle(TDF_RelocationTable)& RT) const
607 {
608   into->Restore(this);
609 }
610
611 //=======================================================================
612 /*!
613  * Function : Save
614  * Purpose  : Saves a content of the attribute as a string
615  */
616 //=======================================================================
617 TCollection_AsciiString SALOMEDSImpl_AttributeParameter::Save() 
618
619   ostrstream buffer;
620   char *tmpBuffer = new char[255];
621
622   buffer << _ints.size() << " ";
623
624   for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++) {
625     buffer << convertString(p->first) << " " << p->second << " ";
626   }
627
628   buffer << _reals.size() << " ";
629   for(map<string,double>::const_iterator p =_reals.begin(); p != _reals.end(); p++) {
630     sprintf(tmpBuffer, "%.64e", p->second);
631     buffer << convertString(p->first) << " " << tmpBuffer << " ";
632   }
633
634   buffer << _bools.size() << " ";
635   for(map<string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++) {
636      buffer << convertString(p->first) << " " << p->second << " ";
637   }
638
639   buffer << _strings.size() << " ";
640   for(map<string,string>::const_iterator p = _strings.begin(); p != _strings.end(); p++) {
641     buffer << convertString(p->first) << " " <<  convertString(p->second) << " ";
642   }
643
644   buffer << _realarrays.size() << " ";
645   for(map< string,vector<double> >::const_iterator p = _realarrays.begin(); p != _realarrays.end(); p++) {
646     vector<double> v(p->second);
647     sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
648     buffer << tmpBuffer;
649     for(int i = 0; i<v.size(); i++) {
650       sprintf(tmpBuffer, " %.64e ", v[i]);
651       buffer << tmpBuffer;
652     }
653   }
654
655   buffer << _intarrays.size() << " ";
656   for(map< string,vector<int> >::const_iterator p = _intarrays.begin(); p != _intarrays.end(); p++) {
657     vector<int> v(p->second);
658     sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
659     buffer << tmpBuffer;
660     for(int i = 0; i<v.size(); i++) {
661       sprintf(tmpBuffer, " %d ", v[i]);
662       buffer << tmpBuffer;
663     }
664   }
665
666   buffer << _strarrays.size() << " ";
667   for(map< string,vector<string> >::const_iterator p = _strarrays.begin(); p != _strarrays.end(); p++) {
668     vector<string> v(p->second);
669     sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
670     buffer << tmpBuffer;
671     for(int i = 0; i<v.size(); i++) {
672       buffer << " " << convertString(v[i]) << " ";
673     }
674   }
675
676   delete tmpBuffer;
677
678   TCollection_AsciiString AS((char*)buffer.rdbuf()->str());
679
680   return AS; 
681 }
682
683 //=======================================================================
684 /*!
685  * Function : Load
686  * Purpose  : Restores the attribute from the string
687  */
688 //=======================================================================
689 void SALOMEDSImpl_AttributeParameter::Load(const TCollection_AsciiString& theValue) 
690
691   Backup();
692
693   _ints.clear();
694   _reals.clear();
695   _bools.clear();
696   _strings.clear();
697   _realarrays.clear();
698   _intarrays.clear();
699
700   istrstream buffer(theValue.ToCString(), strlen(theValue.ToCString()));
701
702   int size, val, ival;
703   double val2;
704   string s, id;
705
706   buffer >> size;
707   for(int i = 1; i<=size; i++) {
708     buffer >> id >> val;
709     _ints[restoreString(id)] = val;
710   }
711
712   buffer >> size;
713   for(int i = 1; i<=size; i++) {
714     buffer >> id >> val2;
715     _reals[restoreString(id)] = val2;
716   }
717
718   buffer >> size;
719   for(int i = 1; i<=size; i++) {
720     buffer >> id >> val;
721     _bools[restoreString(id)] = val;
722   }
723
724   buffer >> size;
725   for(int i = 1; i<=size; i++) {
726     buffer >> id >> s;
727     _strings[restoreString(id)] = restoreString(s);
728   }
729   
730   buffer >> size;
731   for(int i = 1; i<=size; i++) {
732     buffer >> id >> val;
733     vector<double> v;
734     v.resize(val);
735     for(int j = 0; j<val; j++) {
736       buffer >> val2;
737       v[j] = val2;
738     }
739     _realarrays[restoreString(id)] = v;
740   }
741
742   buffer >> size;
743   for(int i = 1; i<=size; i++) {
744     buffer >> id >> val;
745     vector<int> v;
746     v.resize(val);
747     for(int j = 0; j<val; j++) {
748       buffer >> ival;
749       v[j] = ival;
750     }
751     _intarrays[restoreString(id)] = v;
752   }
753   
754   buffer >> size;
755   for(int i = 1; i<=size; i++) {
756     buffer >> id >> val;
757     vector<string> v;
758     v.resize(val);
759     for(int j = 0; j<val; j++) {
760       buffer >> s;
761       v[j] = restoreString(s);
762     }
763     _strarrays[restoreString(id)] = v;
764   }
765