1 // Copyright (C) 2007-2023 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, or (at your option) any later version.
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
23 // File : SALOMEDSImpl_AttributeParameter.cxx
24 // Author : Sergey RUIN
27 #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 std::string convertString(const std::string& S)
39 size_t length = S.size();
42 char c[3], *buffer = new char[length*3+1];
43 buffer[length*3] = (char)0;
44 for(int i = 0, pos = 0; i<(int)length; i++, pos+=3) { //TODO: mismatch signed/unsigned
47 sprintf(c, "%.2x", val);
52 std::string RS(buffer);
57 //Restors a string converted by the function convertString
58 std::string restoreString(const std::string& S)
60 size_t length = S.size();
63 char c[3], *buffer = new char[length/3+1];
64 buffer[length/3] = (char)0;
65 for(int i = 0, pos = 0; i<(int)length; i+=3, pos++) { //TODO: mismatch signed/unsigned
69 int val = strtol(c, NULL, 16);
70 buffer[pos] = (char)val;
73 std::string RS(buffer);
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 std::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 std::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 std::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 std::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 std::string& theID, const std::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 std::string SALOMEDSImpl_AttributeParameter::GetString(const std::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 std::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 std::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 std::string& theID, const std::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 std::vector<double> SALOMEDSImpl_AttributeParameter::GetRealArray(const std::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 std::string& theID, const std::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 std::vector<int> SALOMEDSImpl_AttributeParameter::GetIntArray(const std::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 std::string& theID, const std::vector<std::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 std::vector<std::string> SALOMEDSImpl_AttributeParameter::GetStrArray(const std::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 std::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 std::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 = 0;
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 hierarchy
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 std::vector<std::string> SALOMEDSImpl_AttributeParameter::GetIDs(const Parameter_Types theType)
498 std::vector<std::string> anArray;
504 anArray.resize(_ints.size());
505 for(std::map<std::string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++, i++)
506 anArray[i] = p->first;
512 anArray.resize(_reals.size());
513 for(std::map<std::string,double>::const_iterator p = _reals.begin(); p != _reals.end(); p++, i++)
514 anArray[i] = p->first;
520 anArray.resize(_bools.size());
521 for(std::map<std::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(std::map<std::string,std::string>::const_iterator p = _strings.begin(); p!= _strings.end(); p++, i++)
530 anArray[i] = p->first;
535 if(_realarrays.size()) {
536 anArray.resize(_realarrays.size());
537 for(std::map< std::string, std::vector<double> >::const_iterator p = _realarrays.begin(); p!= _realarrays.end(); p++, i++)
538 anArray[i] = p->first;
543 if(_intarrays.size()) {
544 anArray.resize(_intarrays.size());
545 for(std::map< std::string, std::vector<int> >::const_iterator p = _intarrays.begin(); p!= _intarrays.end(); p++, i++)
546 anArray[i] = p->first;
551 if(_strarrays.size()) {
552 anArray.resize(_strarrays.size());
553 for(std::map< std::string, std::vector<std::string> >::const_iterator p = _strarrays.begin(); p!= _strarrays.end(); p++, i++)
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(std::map<std::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(std::map<std::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(std::map<std::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(std::map<std::string,std::string>::const_iterator p = A->_strings.begin(); p!= A->_strings.end(); p++)
602 if(p->first.size()) _strings[p->first] = p->second;
603 for(std::map< std::string,std::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(std::map< std::string,std::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(std::map< std::string,std::vector<std::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 std::string SALOMEDSImpl_AttributeParameter::Save()
630 std::ostringstream buffer;
631 char *tmpBuffer = new char[255];
633 buffer << _ints.size() << " ";
634 for(std::map<std::string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++) {
635 buffer << convertString(p->first) << " " << p->second << " ";
638 buffer << _reals.size() << " ";
639 for(std::map<std::string,double>::const_iterator p =_reals.begin(); p != _reals.end(); p++) {
640 sprintf(tmpBuffer, "%.64e", p->second);
641 buffer << convertString(p->first) << " " << tmpBuffer << " ";
644 buffer << _bools.size() << " ";
645 for(std::map<std::string,bool>::const_iterator p = _bools.begin(); p != _bools.end(); p++) {
646 buffer << convertString(p->first) << " " << p->second << " ";
649 buffer << _strings.size() << " ";
650 for(std::map<std::string,std::string>::const_iterator p = _strings.begin(); p != _strings.end(); p++) {
651 buffer << convertString(p->first) << " " << convertString(p->second) << " ";
654 buffer << _realarrays.size() << " ";
655 for(std::map< std::string,std::vector<double> >::const_iterator p = _realarrays.begin(); p != _realarrays.end(); p++) {
656 std::vector<double> v(p->second);
657 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), (int)v.size());
659 for(size_t i = 0; i<v.size(); i++) {
660 sprintf(tmpBuffer, " %.64e ", v[i]);
665 buffer << _intarrays.size() << " ";
666 for(std::map< std::string,std::vector<int> >::const_iterator p = _intarrays.begin(); p != _intarrays.end(); p++) {
667 std::vector<int> v(p->second);
668 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), (int)v.size());
670 for(size_t i = 0; i<v.size(); i++) {
671 sprintf(tmpBuffer, " %d ", v[i]);
676 buffer << _strarrays.size() << " ";
677 for(std::map< std::string,std::vector<std::string> >::const_iterator p = _strarrays.begin(); p != _strarrays.end(); p++) {
678 std::vector<std::string> v(p->second);
679 sprintf(tmpBuffer, " %s %d ", convertString(p->first).c_str(), (int)v.size());
681 for(size_t i = 0; i<v.size(); i++) {
682 buffer << " " << convertString(v[i]) << " ";
688 std::string AS = buffer.str();
693 //=======================================================================
696 * Purpose : Restores the attribute from the string
698 //=======================================================================
699 void SALOMEDSImpl_AttributeParameter::Load(const std::string& theValue)
710 std::istringstream buffer(theValue.c_str());
717 for(int i = 1; i<=size; i++) {
719 _ints[restoreString(id)] = val;
723 for(int i = 1; i<=size; i++) {
724 buffer >> id >> val2;
725 _reals[restoreString(id)] = val2;
729 for(int i = 1; i<=size; i++) {
731 _bools[restoreString(id)] = val;
735 for(int i = 1; i<=size; i++) {
737 _strings[restoreString(id)] = restoreString(s);
741 for(int i = 1; i<=size; i++) {
743 std::vector<double> v;
745 for(int j = 0; j<val; j++) {
749 _realarrays[restoreString(id)] = v;
753 for(int i = 1; i<=size; i++) {
757 for(int j = 0; j<val; j++) {
761 _intarrays[restoreString(id)] = v;
765 for(int i = 1; i<=size; i++) {
767 std::vector<std::string> v;
769 for(int j = 0; j<val; j++) {
771 v[j] = restoreString(s);
773 _strarrays[restoreString(id)] = v;