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_AttributeParameter.cxx
21 // Author : Sergey RUIN
25 #include "SALOMEDSImpl_AttributeParameter.hxx"
35 // Purpose: Each character in the string is replaced by 3 characters: '%' and hex number
36 // of the character (2 characters)
37 string convertString(const string& S)
39 int length = S.size();
40 const char *s = S.c_str();
41 char *c = new char[3], *buffer = new char[length*3+1];
42 buffer[length*3] = (char)0;
43 for(int i = 0, pos = 0; i<length; i++, pos+=3) {
46 sprintf(c, "%x", val);
57 //Restors a string converted by the function convertString
58 string restoreString(const string& S)
60 int length = S.size();
61 char *c = new char[3], *buffer = new char[length/3+1];
62 buffer[length/3] = (char)0;
63 const char *s = S.c_str();
64 for(int i = 0, pos = 0; i<length; i+=3, pos++) {
68 int val = strtol(c, NULL, 16);
69 buffer[pos] = (char)val;
78 //=======================================================================
81 * Purpose : Returns GUID of the attribute
83 //=======================================================================
84 const std::string& SALOMEDSImpl_AttributeParameter::GetID ()
86 static std::string ParemeterID ("BA75F3A1-E40B-46b8-8D24-B1D3C3BB1A8C");
90 //=======================================================================
93 * Purpose : Adds an attribute to the label
95 //=======================================================================
96 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_AttributeParameter::Set (const DF_Label& L)
98 SALOMEDSImpl_AttributeParameter* A = NULL;
99 if (!(A=(SALOMEDSImpl_AttributeParameter*)L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
100 A = new SALOMEDSImpl_AttributeParameter();
107 //=======================================================================
110 * Purpose : Associates a integer value with the ID
112 //=======================================================================
113 void SALOMEDSImpl_AttributeParameter::SetInt(const string& theID, const int& theValue)
117 if(theID.size() == 0) return;
121 _ints[theID] = theValue;
126 //=======================================================================
129 * Purpose : Returns a int value associated with the given ID
131 //=======================================================================
132 int SALOMEDSImpl_AttributeParameter::GetInt(const string& theID)
134 if(!IsSet(theID, PT_INTEGER)) throw DFexception("Invalid ID");
138 //=======================================================================
141 * Purpose : Associates a double value with the ID
143 //=======================================================================
144 void SALOMEDSImpl_AttributeParameter::SetReal(const string& theID, const double& theValue)
148 if(theID.size() == 0) return;
152 _reals[theID] = theValue;
157 //=======================================================================
160 * Purpose : Returns a double value associated with the given ID
162 //=======================================================================
163 double SALOMEDSImpl_AttributeParameter::GetReal(const string& theID)
165 if(!IsSet(theID, PT_REAL)) throw DFexception("Invalid ID");
166 return _reals[theID];
169 //=======================================================================
171 * Function : SetString
172 * Purpose : Associates a string with the ID
174 //=======================================================================
175 void SALOMEDSImpl_AttributeParameter::SetString(const string& theID, const string& theValue)
179 if(theID.size() == 0) return;
183 _strings[theID] = theValue;
188 //=======================================================================
190 * Function : GetString
191 * Purpose : Returns a string associated with the given ID
193 //=======================================================================
194 string SALOMEDSImpl_AttributeParameter::GetString(const string& theID)
196 if(!IsSet(theID, PT_STRING)) throw DFexception("Invalid ID");
197 return _strings[theID];
200 //=======================================================================
203 * Purpose : Associates a bool value with the ID
205 //=======================================================================
206 void SALOMEDSImpl_AttributeParameter::SetBool(const string& theID, const bool& theValue)
210 if(theID.size() == 0) return;
214 _bools[theID] = theValue;
219 //=======================================================================
222 * Purpose : Returns a bool value associated with the ID
224 //=======================================================================
225 bool SALOMEDSImpl_AttributeParameter::GetBool(const string& theID)
227 if(!IsSet(theID, PT_BOOLEAN)) throw DFexception("Invalid ID");
228 return _bools[theID];
231 //=======================================================================
233 * Function : SetRealArray
234 * Purpose : Associates an array of double values with the given ID
236 //=======================================================================
237 void SALOMEDSImpl_AttributeParameter::SetRealArray(const string& theID, const vector<double>& theArray)
241 if(theID.size() == 0) return;
245 _realarrays[theID] = theArray;
250 //=======================================================================
252 * Function : GetRealArray
253 * Purpose : Returns double values associated with the ID
255 //=======================================================================
256 vector<double> SALOMEDSImpl_AttributeParameter::GetRealArray(const string& theID)
258 if(!IsSet(theID, PT_REALARRAY)) throw DFexception("Invalid ID");
259 return _realarrays[theID];
263 //=======================================================================
265 * Function : SetIntArray
266 * Purpose : Associates an array of int values with the given ID
268 //=======================================================================
269 void SALOMEDSImpl_AttributeParameter::SetIntArray(const string& theID, const vector<int>& theArray)
273 if(theID.size() == 0) return;
277 _intarrays[theID] = theArray;
282 //=======================================================================
284 * Function : GetIntArray
285 * Purpose : Returns int values associated with the ID
287 //=======================================================================
288 vector<int> SALOMEDSImpl_AttributeParameter::GetIntArray(const string& theID)
290 if(!IsSet(theID, PT_INTARRAY)) throw DFexception("Invalid ID");
291 return _intarrays[theID];
295 //=======================================================================
297 * Function : SetStrArray
298 * Purpose : Associates an array of string values with the given ID
300 //=======================================================================
301 void SALOMEDSImpl_AttributeParameter::SetStrArray(const string& theID, const vector<string>& theArray)
305 if(theID.size() == 0) return;
309 _strarrays[theID] = theArray;
314 //=======================================================================
316 * Function : GetStrArray
317 * Purpose : Returns string values associated with the ID
319 //=======================================================================
320 vector<string> SALOMEDSImpl_AttributeParameter::GetStrArray(const string& theID)
322 if(!IsSet(theID, PT_STRARRAY)) throw DFexception("Invalid ID");
323 return _strarrays[theID];
327 //=======================================================================
330 * Purpose : Returns true if for the ID of given type was assigned \n
331 * a value in the attribute
333 //=======================================================================
334 bool SALOMEDSImpl_AttributeParameter::IsSet(const string& theID, const Parameter_Types theType)
338 if(_ints.find(theID) != _ints.end()) return true;
342 if(_reals.find(theID) != _reals.end()) return true;
346 if(_bools.find(theID) != _bools.end()) return true;
350 if(_strings.find(theID) != _strings.end()) return true;
354 if(_realarrays.find(theID) != _realarrays.end()) return true;
358 if(_intarrays.find(theID) != _intarrays.end()) return true;
362 if(_strarrays.find(theID) != _strarrays.end()) return true;
365 default: return false;
371 //=======================================================================
373 * Function : RemoveID
374 * Purpose : Removes a parameter with given ID
376 //=======================================================================
377 bool SALOMEDSImpl_AttributeParameter::RemoveID(const string& theID, const Parameter_Types theType)
384 if(_ints.erase(theID)) return true;
388 if(_reals.erase(theID)) return true;
392 if(_bools.erase(theID)) return true;
396 if(_strings.erase(theID)) return true;
400 if(_realarrays.erase(theID)) return true;
404 if(_intarrays.erase(theID)) return true;
408 if(_strarrays.erase(theID)) return true;
411 default: return false;
419 //=======================================================================
421 * Function : GetFather
422 * Purpose : Returns a father attribute for this attribute
424 //=======================================================================
425 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_AttributeParameter::GetFather()
427 SALOMEDSImpl_AttributeParameter* aFather;
428 DF_Label L = Label();
429 if(L.IsRoot()) return aFather;
433 if((aFather=(SALOMEDSImpl_AttributeParameter*)L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) break;
439 //=======================================================================
441 * Function : HasFather
442 * Purpose : Returns True if the attribute has a father attribute
444 //=======================================================================
445 bool SALOMEDSImpl_AttributeParameter::HasFather()
447 DF_Label L = Label();
448 if(L.IsRoot()) return false;
451 if(L.IsAttribute(SALOMEDSImpl_AttributeParameter::GetID())) return true;
457 //=======================================================================
460 * Purpose : Returns True is the attribute is highest in an hierachy
462 //=======================================================================
463 bool SALOMEDSImpl_AttributeParameter::IsRoot()
465 return ((HasFather())?false:true);
468 //=======================================================================
471 * Purpose : Clears the content of the attribute
473 //=======================================================================
474 void SALOMEDSImpl_AttributeParameter::Clear()
489 //=======================================================================
492 * Purpose : Returns an array of all ID's of the given type
494 //=======================================================================
495 vector<string> SALOMEDSImpl_AttributeParameter::GetIDs(const Parameter_Types theType)
498 vector<string> anArray;
504 anArray.resize(_ints.size());
505 for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++, i++)
506 anArray[i] = p->first;
512 anArray.resize(_reals.size());
513 for(map<string,double>::const_iterator p = _reals.begin(); p != _reals.end(); p++, i++)
514 anArray[i] = p->first;
520 anArray.resize(_bools.size());
521 for(map<string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++, i++)
522 anArray[i] = p->first;
527 if(_strings.size()) {
528 anArray.resize(_strings.size());
529 for(map<string,string>::const_iterator p = _strings.begin(); p!= _strings.end(); p++)
530 anArray[i] = p->first;
535 if(_realarrays.size()) {
536 anArray.resize(_realarrays.size());
537 for(map< string, vector<double> >::const_iterator p = _realarrays.begin(); p!= _realarrays.end(); p++)
538 anArray[i] = p->first;
543 if(_intarrays.size()) {
544 anArray.resize(_intarrays.size());
545 for(map< string, vector<int> >::const_iterator p = _intarrays.begin(); p!= _intarrays.end(); p++)
546 anArray[i] = p->first;
551 if(_strarrays.size()) {
552 anArray.resize(_strarrays.size());
553 for(map< string, vector<string> >::const_iterator p = _strarrays.begin(); p!= _strarrays.end(); p++)
554 anArray[i] = p->first;
558 default: return anArray;
564 //=======================================================================
567 * Purpose : Rteurns an GUID of the attribute
569 //=======================================================================
570 const std::string& SALOMEDSImpl_AttributeParameter::ID () const { return GetID(); }
573 DF_Attribute* SALOMEDSImpl_AttributeParameter::NewEmpty () const
575 return new SALOMEDSImpl_AttributeParameter();
578 //=======================================================================
581 * Purpose : Restors the content of the attribute from another
583 //=======================================================================
584 void SALOMEDSImpl_AttributeParameter::Restore(DF_Attribute* with)
586 SALOMEDSImpl_AttributeParameter* A = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(with);
595 for(map<string,int>::const_iterator p = A->_ints.begin(); p!= A->_ints.end(); p++)
596 if(p->first.size()) _ints[p->first] = p->second;
597 for(map<string,double>::const_iterator p = A->_reals.begin(); p!= A->_reals.end(); p++)
598 if(p->first.size()) _reals[p->first] = p->second;
599 for(map<string,bool>::const_iterator p = A->_bools.begin(); p!= A->_bools.end(); p++)
600 if(p->first.size()) _bools[p->first] = p->second;
601 for(map<string,string>::const_iterator p = A->_strings.begin(); p!= A->_strings.end(); p++)
602 if(p->first.size()) _strings[p->first] = p->second;
603 for(map< string,vector<double> >::const_iterator p = A->_realarrays.begin(); p!= A->_realarrays.end(); p++)
604 if(p->first.size()) _realarrays[p->first] = p->second;
605 for(map< string,vector<int> >::const_iterator p = A->_intarrays.begin(); p!= A->_intarrays.end(); p++)
606 if(p->first.size()) _intarrays[p->first] = p->second;
607 for(map< string,vector<string> >::const_iterator p = A->_strarrays.begin(); p!= A->_strarrays.end(); p++)
608 if(p->first.size()) _strarrays[p->first] = p->second;
611 //=======================================================================
614 * Purpose : Pastes the content of attribute to another
616 //=======================================================================
617 void SALOMEDSImpl_AttributeParameter::Paste (DF_Attribute* into)
622 //=======================================================================
625 * Purpose : Saves a content of the attribute as a string
627 //=======================================================================
628 string SALOMEDSImpl_AttributeParameter::Save()
631 char *tmpBuffer = new char[255];
633 buffer << _ints.size() << " ";
635 for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++) {
636 buffer << convertString(p->first) << " " << p->second << " ";
639 buffer << _reals.size() << " ";
640 for(map<string,double>::const_iterator p =_reals.begin(); p != _reals.end(); p++) {
641 sprintf(tmpBuffer, "%.64e", p->second);
642 buffer << convertString(p->first) << " " << tmpBuffer << " ";
645 buffer << _bools.size() << " ";
646 for(map<string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++) {
647 buffer << convertString(p->first) << " " << p->second << " ";
650 buffer << _strings.size() << " ";
651 for(map<string,string>::const_iterator p = _strings.begin(); p != _strings.end(); p++) {
652 buffer << convertString(p->first) << " " << convertString(p->second) << " ";
655 buffer << _realarrays.size() << " ";
656 for(map< string,vector<double> >::const_iterator p = _realarrays.begin(); p != _realarrays.end(); p++) {
657 vector<double> v(p->second);
658 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
660 for(int i = 0; i<v.size(); i++) {
661 sprintf(tmpBuffer, " %.64e ", v[i]);
666 buffer << _intarrays.size() << " ";
667 for(map< string,vector<int> >::const_iterator p = _intarrays.begin(); p != _intarrays.end(); p++) {
668 vector<int> v(p->second);
669 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
671 for(int i = 0; i<v.size(); i++) {
672 sprintf(tmpBuffer, " %d ", v[i]);
677 buffer << _strarrays.size() << " ";
678 for(map< string,vector<string> >::const_iterator p = _strarrays.begin(); p != _strarrays.end(); p++) {
679 vector<string> v(p->second);
680 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), v.size());
682 for(int i = 0; i<v.size(); i++) {
683 buffer << " " << convertString(v[i]) << " ";
689 string AS((char*)buffer.rdbuf()->str());
694 //=======================================================================
697 * Purpose : Restores the attribute from the string
699 //=======================================================================
700 void SALOMEDSImpl_AttributeParameter::Load(const string& theValue)
711 istrstream buffer(theValue.c_str(), theValue.size());
718 for(int i = 1; i<=size; i++) {
720 _ints[restoreString(id)] = val;
724 for(int i = 1; i<=size; i++) {
725 buffer >> id >> val2;
726 _reals[restoreString(id)] = val2;
730 for(int i = 1; i<=size; i++) {
732 _bools[restoreString(id)] = val;
736 for(int i = 1; i<=size; i++) {
738 _strings[restoreString(id)] = restoreString(s);
742 for(int i = 1; i<=size; i++) {
746 for(int j = 0; j<val; j++) {
750 _realarrays[restoreString(id)] = v;
754 for(int i = 1; i<=size; i++) {
758 for(int j = 0; j<val; j++) {
762 _intarrays[restoreString(id)] = v;
766 for(int i = 1; i<=size; i++) {
770 for(int j = 0; j<val; j++) {
772 v[j] = restoreString(s);
774 _strarrays[restoreString(id)] = v;