1 // Copyright (C) 2007-2008 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.
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
22 // File : SALOMEDSImpl_AttributeParameter.cxx
23 // Author : Sergey RUIN
26 #include "SALOMEDSImpl_AttributeParameter.hxx"
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)
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) {
48 sprintf(c, "%.2x", val);
59 //Restors a string converted by the function convertString
60 string restoreString(const string& S)
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++) {
70 int val = strtol(c, NULL, 16);
71 buffer[pos] = (char)val;
80 //=======================================================================
83 * Purpose : Returns GUID of the attribute
85 //=======================================================================
86 const std::string& SALOMEDSImpl_AttributeParameter::GetID ()
88 static std::string ParemeterID ("BA75F3A1-E40B-46b8-8D24-B1D3C3BB1A8C");
92 //=======================================================================
95 * Purpose : Adds an attribute to the label
97 //=======================================================================
98 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_AttributeParameter::Set (const DF_Label& L)
100 SALOMEDSImpl_AttributeParameter* A = NULL;
101 if (!(A=(SALOMEDSImpl_AttributeParameter*)L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
102 A = new SALOMEDSImpl_AttributeParameter();
109 //=======================================================================
112 * Purpose : Associates a integer value with the ID
114 //=======================================================================
115 void SALOMEDSImpl_AttributeParameter::SetInt(const string& theID, const int& theValue)
119 if(theID.size() == 0) return;
123 _ints[theID] = theValue;
128 //=======================================================================
131 * Purpose : Returns a int value associated with the given ID
133 //=======================================================================
134 int SALOMEDSImpl_AttributeParameter::GetInt(const string& theID)
136 if(!IsSet(theID, PT_INTEGER)) throw DFexception("Invalid ID");
140 //=======================================================================
143 * Purpose : Associates a double value with the ID
145 //=======================================================================
146 void SALOMEDSImpl_AttributeParameter::SetReal(const string& theID, const double& theValue)
150 if(theID.size() == 0) return;
154 _reals[theID] = theValue;
159 //=======================================================================
162 * Purpose : Returns a double value associated with the given ID
164 //=======================================================================
165 double SALOMEDSImpl_AttributeParameter::GetReal(const string& theID)
167 if(!IsSet(theID, PT_REAL)) throw DFexception("Invalid ID");
168 return _reals[theID];
171 //=======================================================================
173 * Function : SetString
174 * Purpose : Associates a string with the ID
176 //=======================================================================
177 void SALOMEDSImpl_AttributeParameter::SetString(const string& theID, const string& theValue)
181 if(theID.size() == 0) return;
185 _strings[theID] = theValue;
190 //=======================================================================
192 * Function : GetString
193 * Purpose : Returns a string associated with the given ID
195 //=======================================================================
196 string SALOMEDSImpl_AttributeParameter::GetString(const string& theID)
198 if(!IsSet(theID, PT_STRING)) throw DFexception("Invalid ID");
199 return _strings[theID];
202 //=======================================================================
205 * Purpose : Associates a bool value with the ID
207 //=======================================================================
208 void SALOMEDSImpl_AttributeParameter::SetBool(const string& theID, const bool& theValue)
212 if(theID.size() == 0) return;
216 _bools[theID] = theValue;
221 //=======================================================================
224 * Purpose : Returns a bool value associated with the ID
226 //=======================================================================
227 bool SALOMEDSImpl_AttributeParameter::GetBool(const string& theID)
229 if(!IsSet(theID, PT_BOOLEAN)) throw DFexception("Invalid ID");
230 return _bools[theID];
233 //=======================================================================
235 * Function : SetRealArray
236 * Purpose : Associates an array of double values with the given ID
238 //=======================================================================
239 void SALOMEDSImpl_AttributeParameter::SetRealArray(const string& theID, const vector<double>& theArray)
243 if(theID.size() == 0) return;
247 _realarrays[theID] = theArray;
252 //=======================================================================
254 * Function : GetRealArray
255 * Purpose : Returns double values associated with the ID
257 //=======================================================================
258 vector<double> SALOMEDSImpl_AttributeParameter::GetRealArray(const string& theID)
260 if(!IsSet(theID, PT_REALARRAY)) throw DFexception("Invalid ID");
261 return _realarrays[theID];
265 //=======================================================================
267 * Function : SetIntArray
268 * Purpose : Associates an array of int values with the given ID
270 //=======================================================================
271 void SALOMEDSImpl_AttributeParameter::SetIntArray(const string& theID, const vector<int>& theArray)
275 if(theID.size() == 0) return;
279 _intarrays[theID] = theArray;
284 //=======================================================================
286 * Function : GetIntArray
287 * Purpose : Returns int values associated with the ID
289 //=======================================================================
290 vector<int> SALOMEDSImpl_AttributeParameter::GetIntArray(const string& theID)
292 if(!IsSet(theID, PT_INTARRAY)) throw DFexception("Invalid ID");
293 return _intarrays[theID];
297 //=======================================================================
299 * Function : SetStrArray
300 * Purpose : Associates an array of string values with the given ID
302 //=======================================================================
303 void SALOMEDSImpl_AttributeParameter::SetStrArray(const string& theID, const vector<string>& theArray)
307 if(theID.size() == 0) return;
311 _strarrays[theID] = theArray;
316 //=======================================================================
318 * Function : GetStrArray
319 * Purpose : Returns string values associated with the ID
321 //=======================================================================
322 vector<string> SALOMEDSImpl_AttributeParameter::GetStrArray(const string& theID)
324 if(!IsSet(theID, PT_STRARRAY)) throw DFexception("Invalid ID");
325 return _strarrays[theID];
329 //=======================================================================
332 * Purpose : Returns true if for the ID of given type was assigned \n
333 * a value in the attribute
335 //=======================================================================
336 bool SALOMEDSImpl_AttributeParameter::IsSet(const string& theID, const Parameter_Types theType)
340 if(_ints.find(theID) != _ints.end()) return true;
344 if(_reals.find(theID) != _reals.end()) return true;
348 if(_bools.find(theID) != _bools.end()) return true;
352 if(_strings.find(theID) != _strings.end()) return true;
356 if(_realarrays.find(theID) != _realarrays.end()) return true;
360 if(_intarrays.find(theID) != _intarrays.end()) return true;
364 if(_strarrays.find(theID) != _strarrays.end()) return true;
367 default: return false;
373 //=======================================================================
375 * Function : RemoveID
376 * Purpose : Removes a parameter with given ID
378 //=======================================================================
379 bool SALOMEDSImpl_AttributeParameter::RemoveID(const string& theID, const Parameter_Types theType)
386 if(_ints.erase(theID)) return true;
390 if(_reals.erase(theID)) return true;
394 if(_bools.erase(theID)) return true;
398 if(_strings.erase(theID)) return true;
402 if(_realarrays.erase(theID)) return true;
406 if(_intarrays.erase(theID)) return true;
410 if(_strarrays.erase(theID)) return true;
413 default: return false;
421 //=======================================================================
423 * Function : GetFather
424 * Purpose : Returns a father attribute for this attribute
426 //=======================================================================
427 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_AttributeParameter::GetFather()
429 SALOMEDSImpl_AttributeParameter* aFather;
430 DF_Label L = Label();
431 if(L.IsRoot()) return aFather;
435 if((aFather=(SALOMEDSImpl_AttributeParameter*)L.FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) break;
441 //=======================================================================
443 * Function : HasFather
444 * Purpose : Returns True if the attribute has a father attribute
446 //=======================================================================
447 bool SALOMEDSImpl_AttributeParameter::HasFather()
449 DF_Label L = Label();
450 if(L.IsRoot()) return false;
453 if(L.IsAttribute(SALOMEDSImpl_AttributeParameter::GetID())) return true;
459 //=======================================================================
462 * Purpose : Returns True is the attribute is highest in an hierachy
464 //=======================================================================
465 bool SALOMEDSImpl_AttributeParameter::IsRoot()
467 return ((HasFather())?false:true);
470 //=======================================================================
473 * Purpose : Clears the content of the attribute
475 //=======================================================================
476 void SALOMEDSImpl_AttributeParameter::Clear()
491 //=======================================================================
494 * Purpose : Returns an array of all ID's of the given type
496 //=======================================================================
497 vector<string> SALOMEDSImpl_AttributeParameter::GetIDs(const Parameter_Types theType)
500 vector<string> anArray;
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;
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;
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;
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;
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;
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;
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;
560 default: return anArray;
566 //=======================================================================
569 * Purpose : Rteurns an GUID of the attribute
571 //=======================================================================
572 const std::string& SALOMEDSImpl_AttributeParameter::ID () const { return GetID(); }
575 DF_Attribute* SALOMEDSImpl_AttributeParameter::NewEmpty () const
577 return new SALOMEDSImpl_AttributeParameter();
580 //=======================================================================
583 * Purpose : Restors the content of the attribute from another
585 //=======================================================================
586 void SALOMEDSImpl_AttributeParameter::Restore(DF_Attribute* with)
588 SALOMEDSImpl_AttributeParameter* A = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(with);
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;
613 //=======================================================================
616 * Purpose : Pastes the content of attribute to another
618 //=======================================================================
619 void SALOMEDSImpl_AttributeParameter::Paste (DF_Attribute* into)
624 //=======================================================================
627 * Purpose : Saves a content of the attribute as a string
629 //=======================================================================
630 string SALOMEDSImpl_AttributeParameter::Save()
632 ostringstream buffer;
633 char *tmpBuffer = new char[255];
635 buffer << _ints.size() << " ";
637 for(map<string,int>::const_iterator p = _ints.begin(); p != _ints.end(); p++) {
638 buffer << convertString(p->first) << " " << p->second << " ";
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 << " ";
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 << " ";
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) << " ";
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());
662 for(int i = 0; i<v.size(); i++) {
663 sprintf(tmpBuffer, " %.64e ", v[i]);
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());
673 for(int i = 0; i<v.size(); i++) {
674 sprintf(tmpBuffer, " %d ", v[i]);
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());
684 for(int i = 0; i<v.size(); i++) {
685 buffer << " " << convertString(v[i]) << " ";
691 string AS = buffer.str();
696 //=======================================================================
699 * Purpose : Restores the attribute from the string
701 //=======================================================================
702 void SALOMEDSImpl_AttributeParameter::Load(const string& theValue)
713 istringstream buffer(theValue.c_str());
720 for(int i = 1; i<=size; i++) {
722 _ints[restoreString(id)] = val;
726 for(int i = 1; i<=size; i++) {
727 buffer >> id >> val2;
728 _reals[restoreString(id)] = val2;
732 for(int i = 1; i<=size; i++) {
734 _bools[restoreString(id)] = val;
738 for(int i = 1; i<=size; i++) {
740 _strings[restoreString(id)] = restoreString(s);
744 for(int i = 1; i<=size; i++) {
748 for(int j = 0; j<val; j++) {
752 _realarrays[restoreString(id)] = v;
756 for(int i = 1; i<=size; i++) {
760 for(int j = 0; j<val; j++) {
764 _intarrays[restoreString(id)] = v;
768 for(int i = 1; i<=size; i++) {
772 for(int j = 0; j<val; j++) {
774 v[j] = restoreString(s);
776 _strarrays[restoreString(id)] = v;