Salome HOME
Fix pb. with loading of last module (in list).
[modules/kernel.git] / src / DF / DF_Label.cxx
1 #include "DF_definitions.hxx"
2 #include "DF_Label.hxx"
3 #include "DF_Document.hxx"
4 #include "DF_Attribute.hxx"
5 #include "DF_ChildIterator.hxx"
6
7 #include <algorithm>
8
9 using namespace std;
10
11 //Class DF_Label defines a persistence reference in DF_Document that contains a tree of Labels.
12 //This reference is named "entry" and is a sequence of tags divided by ":". The root entry is "0:".
13 //For example "0:1:1" corresponds the following structure
14 // 0_
15 //   |
16 //   |_1_
17 //       |
18 //       |_ 1
19
20 DF_Label DF_Label::Label(const DF_Label& theLabel, const string& theEntry, bool isCreated)
21 {
22   if(theLabel.IsNull()) return DF_Label();
23   
24   DF_Label aLabel = theLabel.Root();
25   if(theEntry == "0:") return aLabel;
26   if(theEntry == "0:1") return theLabel.GetDocument()->Main();
27
28   char* cc = (char*)theEntry.c_str();
29   int n = 0;
30   vector<int> tags;
31
32   while (*cc != '\0') {
33     while ( *cc >= '0' && *cc <= '9') {
34       n = 10*n + (*cc - '0');
35       ++cc;
36     }
37     if (*cc == ':' || *cc == '\0') {
38       tags.push_back(n);
39       n = 0;
40       if (*cc != '\0') ++cc;
41     }
42     else {
43       tags.clear();
44       break;
45     }
46   }
47
48   if(!tags.size()) return DF_Label();
49   
50   for(int i = 1, len = tags.size(); !aLabel.IsNull() && i<len; i++)
51     aLabel = aLabel.FindChild(tags[i], isCreated);
52
53   return aLabel;
54 }
55
56 DF_Label::DF_Label(DF_LabelNode* theNode)
57   :_node(theNode)
58 {
59 }
60
61 //Constructor
62 DF_Label::DF_Label()
63 {
64   _node = NULL;
65 }
66
67 //Copy constructor
68 DF_Label::DF_Label(const DF_Label& theLabel)
69 {
70   _node = theLabel._node;
71 }
72
73 DF_Label& DF_Label::operator=(const DF_Label& theLabel)
74 {
75   _node = theLabel._node;
76   return *this;
77 }
78
79 //Destructor
80 DF_Label::~DF_Label()
81 {
82   _node = NULL;
83 }
84
85 //Returns a smart pointer to Document which contains this Label
86 DF_Document* DF_Label::GetDocument() const
87 {
88   if(!_node) return NULL;
89   return _node->_document;
90 }
91
92 //Returns true if theLabel equals to this label
93 bool DF_Label::operator==(const DF_Label& theLabel)
94 {
95   if(IsNull() || theLabel.IsNull()) return false;
96   return (theLabel.Entry() == Entry());
97 }
98
99 //Returns true if theLabel doesn't equals to this label
100 bool DF_Label::operator!=(const DF_Label& theLabel)
101 {
102   if(IsNull() || theLabel.IsNull()) return true;
103   return (theLabel.Entry() != Entry());
104 }
105
106
107 //Returns a tag of this Label
108 int DF_Label::Tag() const
109 {
110   if(!_node) return -1;
111   return _node->_tag;
112 }
113
114 //Returns true if this Label is attached to the tree in the Document.
115 bool DF_Label::IsAttached()
116 {
117   if(!_node) return false;
118   return (bool)(_node->_document);
119 }
120
121 //Searches an Attribute with given ID located on this Label.
122 //Returns true if the Attribute is found.
123 DF_Attribute* DF_Label::FindAttribute(const std::string& theID) const
124 {
125   if(!_node) return NULL;
126
127   if(_node->_attributes.find(theID) == _node->_attributes.end()) return NULL;
128   return _node->_attributes[theID];
129 }
130
131 //Returns true if there is an Attribute with given ID on this Label.
132 bool DF_Label::IsAttribute(const std::string& theID) const
133 {
134   if(!_node) return false;
135
136   return (_node->_attributes.find(theID) != _node->_attributes.end());
137 }
138
139 //Adds theAttribute to the Label where this Attribute is located.
140 //Returns true if theAttribute was added.
141 bool DF_Label::AddAttribute(DF_Attribute* theAttribute) const
142 {
143   if(!_node) return false;
144
145   if(_node->_attributes.find(theAttribute->ID()) != _node->_attributes.end()) return false;
146   theAttribute->_node = _node;
147   _node->_attributes[theAttribute->ID()] = theAttribute;
148
149   return true;
150 }
151
152 //Forgets an Attribute with given ID located on the this Label.
153 bool DF_Label::ForgetAttribute(const std::string& theID) const
154 {
155   if(!_node) return false;
156
157   if(_node->_attributes.find(theID) == _node->_attributes.end()) return false;
158   DF_Attribute* attr = _node->_attributes[theID];
159   _node->_attributes.erase(theID);
160   delete attr;
161
162   return true;
163 }
164
165 //Forgets all Attributes located on this Label.
166 bool DF_Label::ForgetAllAttributes(bool clearChildren) const
167 {
168   if(!_node) return false;
169
170   vector<DF_Attribute*> va = GetAttributes();
171   _node->_attributes.clear();
172
173   for(int i = 0, len = va.size(); i<len; i++) 
174     delete va[i];
175
176   if(clearChildren) {
177     DF_ChildIterator CI(*this, true);
178     for(; CI.More(); CI.Next()) 
179       CI.Value().ForgetAllAttributes(true);
180   }
181
182   return true;
183 }
184
185 //Returns Father of this Label.
186 DF_Label DF_Label::Father() const
187 {
188   if(!_node) return DF_Label();
189
190   return _node->_father;
191 }
192
193 //Returns is this Label is not initialized
194 bool DF_Label::IsNull() const
195 {
196   return (!_node || (_node->_document == NULL));
197 }
198
199 //Returns is this Label is a Root label
200 bool DF_Label::IsRoot() const
201 {
202   if(IsNull() || Father().IsNull()) return true;
203   return false;
204 }
205
206
207 //Returns true if this Label has Attributes.
208 bool DF_Label::HasAttributes() const
209 {
210   if(!_node) return false;
211
212   return !(_node->_attributes.empty());
213 }
214
215 //Returns a list of Attributes of this Label.
216 vector<DF_Attribute*> DF_Label::GetAttributes() const
217 {
218   vector<DF_Attribute*> attributes;
219   if(!_node) return attributes;
220   
221   typedef map<string, DF_Attribute*>::const_iterator AI;
222   vector<string> sorted;
223   for(AI p = _node->_attributes.begin(); p!=_node->_attributes.end(); p++)
224     sorted.push_back(p->first);
225     
226   sort(sorted.begin(), sorted.end());
227   int len = sorted.size();    
228   for(int i = 0; i<len; i++)
229     attributes.push_back(_node->_attributes[sorted[i]]);
230
231   return attributes;
232 }
233
234 //Returns true if this Label has a child Label.
235 bool DF_Label::HasChild() const
236 {
237   if(!_node) return false;
238
239   return (bool)(_node->_firstChild);
240 }
241
242 //Returns a number of child Labels.
243 int DF_Label::NbChildren() const
244 {
245   if(!_node) return -1;
246
247   if(!_node->_firstChild) return 0;
248   int nb = 1;
249   DF_LabelNode* next = _node->_firstChild->_next;
250   while(next) {
251     nb++;
252     next = next->_next;
253   }
254
255   return nb;
256 }
257
258 //Returns the depth (a number of fathers required to identify the Label) of this Label in the tree.
259 int DF_Label::Depth() const
260 {
261   if(!_node) return -1;
262
263   return _node->_depth;
264 }
265
266 //Returns true if this Label is a descendant of theLabel.
267 bool DF_Label::IsDescendant(const DF_Label& theLabel)
268 {
269   if(!_node) return false;
270
271   DF_LabelNode* father = _node->_father;
272   if(!father) return false;
273
274   while(father) {
275     if(father == theLabel._node) return true;
276     father = father->_father;
277   }
278
279   return false;
280 }
281
282 //Returns the root Label of a Label tree to which this Label belongs.
283 DF_Label DF_Label::Root() const
284 {
285   if(!_node) return DF_Label();
286
287   return _node->_document->Main().Father();
288 }
289
290 //Finds a child Label of this Label with a given tag. If isCreate = true and there is no child
291 //Label with the given tag, the child Label is created.
292 DF_Label DF_Label::FindChild(int theTag, bool isCreate)
293 {
294   if(!_node || IsNull()) return DF_Label();
295
296   DF_LabelNode *aLabel = NULL, *aPrevious = NULL, *aNext = NULL;
297   if(!_node->_firstChild && !isCreate) return DF_Label();
298
299   if(_node->_firstChild && _node->_firstChild->_tag == theTag) 
300     return DF_Label(_node->_firstChild);    
301
302   if(_node->_lastChild) {
303     if( _node->_lastChild->_tag == theTag) {
304         return DF_Label(_node->_lastChild);
305     }
306     else if( _node->_lastChild->_tag < theTag) { //Incremental addition of children
307         aPrevious = _node->_lastChild;     
308     }   
309   }
310   else {
311     aLabel = _node->_firstChild;
312     while(aLabel) {
313       if(aLabel->_tag == theTag) return DF_Label(aLabel);
314       if(aLabel->_tag > theTag) {
315         aNext = aLabel;
316         break;
317       }
318       if(aLabel->_tag < theTag) aPrevious = aLabel;
319       aLabel = aLabel->_next;
320     }
321   }
322   
323   if(!isCreate) return DF_Label();
324
325   DF_LabelNode* aChild = new DF_LabelNode();
326   aChild->_father = this->_node;
327   aChild->_document = _node->_document;
328   aChild->_tag = theTag;
329   aChild->_depth = _node->_depth+1;
330   if(aNext) {
331     aChild->_previous = aNext->_previous;
332     aChild->_next = aNext;
333     aNext->_previous = aChild;
334   }
335   if(aPrevious) {
336     aChild->_previous = aPrevious;
337     aChild->_next = aPrevious->_next;
338     aPrevious->_next = aChild;
339   }
340     
341   if(!_node->_firstChild || (aNext && aNext == _node->_firstChild) ) _node->_firstChild = aChild;
342   if(!aNext) {
343     _node->_lastChild = aChild;
344   }
345   
346   return aChild;
347 }
348
349 //Creates a new child Label of this Label.
350 DF_Label DF_Label::NewChild()
351 {
352   if(!_node || IsNull()) return DF_Label();
353
354   int tag = 1;
355   if(_node->_lastChild) tag = _node->_lastChild->_tag+1;
356   
357   return FindChild(tag, true);
358 }
359
360 //Returns a string entry of this Label
361 string DF_Label::Entry() const
362 {
363   string entry = "";
364   vector<int> vi;
365   DF_LabelNode* father = this->_node;
366   while(father) {
367     vi.push_back(father->_tag);
368     father = father->_father;
369   }
370
371   int len = vi.size();
372   if(len == 1) {
373     entry = "0:";
374   }
375   else {
376     char buffer[128];
377     for(int i = len-1; i>=0; i--) {
378       int tag = vi[i];
379       sprintf(buffer, "%d", tag);
380       entry+=string(buffer);
381       if(i) entry += ":";
382     }
383   }
384
385   return entry;
386 }
387
388 bool DF_Label::IsEqual(const DF_Label& theLabel)
389 {
390   if(theLabel.IsNull() || IsNull()) return false;
391   DF_Label L(theLabel);
392   return (L.Entry() == Entry());
393 }
394
395
396 void DF_Label::Nullify() 
397 {
398   delete _node;
399   _node = NULL;
400 }
401
402 void DF_Label::dump()
403 {
404   if(!_node) cout << "DF_Label addr : " << this << " NULL " << endl;
405   else {
406     cout << "DF_Label addr : " << this->_node << " entry : " << Entry() << endl;
407     if(_node->_father) cout << " Father : " << _node->_father << " entry : " << Father().Entry() << endl;
408     else cout << " Father : NULL " << endl;
409
410     if(_node->_firstChild) cout << " FirstChild : " << _node->_firstChild << " entry : " << DF_Label(_node->_firstChild).Entry() << endl;
411     else cout << " FirstChild : NULL " << endl;
412
413     if(_node->_lastChild) cout << " LastChild : " << _node->_lastChild << " entry : " << DF_Label(_node->_lastChild).Entry() << endl;
414     else cout << " LastChild : NULL " << endl;
415
416     if(_node->_previous) cout << " Previous : " << _node->_previous << " entry : " << DF_Label(_node->_previous).Entry() << endl;
417     else cout << " Previous : NULL " << endl;
418
419     if(_node->_next) cout << " Next : " << _node->_next << " entry : " << DF_Label(_node->_next).Entry() << endl;
420     else cout << " Next : NULL " << endl;
421   }
422 }
423
424
425 /*
426  ###############################################
427             DF_LabelNode methods
428  ###############################################
429 */
430
431 DF_LabelNode::DF_LabelNode()
432 {
433   _depth = 0;
434   _tag = 0;
435   _attributes.clear();
436   _document = NULL;
437   _father = NULL;
438   _firstChild = NULL;
439   _lastChild = NULL;
440   _previous = NULL;
441   _next = NULL;
442 }
443
444 DF_LabelNode::~DF_LabelNode()
445 {
446   vector<DF_Attribute*> va;
447   typedef map<string, DF_Attribute*>::const_iterator AI;
448   for(AI p = _attributes.begin(); p!=_attributes.end(); p++)
449     va.push_back(p->second);
450
451   for(int i = 0, len = va.size(); i<len; i++) 
452     delete va[i];
453
454   _attributes.clear();
455 }
456
457
458 void DF_LabelNode::Reset()
459 {
460   _depth = 0;
461   _tag = 0;
462
463   vector<DF_Attribute*> va;
464   typedef map<string, DF_Attribute*>::const_iterator AI;
465   for(AI p = _attributes.begin(); p!=_attributes.end(); p++)
466     va.push_back(p->second);
467
468   for(int i = 0, len = va.size(); i<len; i++) 
469     delete va[i];
470
471   _attributes.clear();
472   _document = NULL;
473   _father = NULL;
474   _firstChild = NULL;
475   _lastChild = NULL;
476   _previous = NULL;
477   _next = NULL;  
478 }