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