f8473adf2a910a898a00e151866c0f14ef2a3fb0
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / builder / tree-node.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <cctype>
20 #include <cstring>
21 #include <string>
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include "dali-toolkit/devel-api/builder/tree-node.h"
26 #include "dali-toolkit/internal/builder/tree-node-manipulator.h"
27
28
29 namespace Dali
30 {
31
32 bool CaseInsensitiveCharacterCompare( unsigned char a, unsigned char b )
33 {
34   // Converts to lower case in the current locale.
35   return std::tolower( a ) == std::tolower( b );
36 }
37
38 /**
39  * return true if the lower cased ASCII strings are equal.
40  */
41 bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b )
42 {
43   bool result = false;
44   if( a.length() == b.length() )
45   {
46     result = std::equal( a.begin(), a.end(), b.begin(), CaseInsensitiveCharacterCompare );
47   }
48   return result;
49 }
50
51 namespace Toolkit
52 {
53
54 TreeNode::TreeNode()
55   : mName(NULL),
56     mParent(NULL),
57     mNextSibling(NULL),
58     mFirstChild(NULL),
59     mLastChild(NULL),
60     mStringValue(NULL),
61     mType(TreeNode::IS_NULL),
62     mSubstituion(false)
63 {
64 }
65
66 TreeNode::~TreeNode()
67 {
68
69 }
70
71 const char* TreeNode::GetName() const
72 {
73   return mName;
74 }
75
76 TreeNode::NodeType TreeNode::GetType() const
77 {
78   return mType;
79 }
80
81 const char* TreeNode::GetString() const
82 {
83   return mStringValue;
84 }
85
86 bool TreeNode::HasSubstitution() const
87 {
88   return mSubstituion;
89 }
90
91 float TreeNode::GetFloat() const
92 {
93   return mFloatValue;
94 }
95
96 int TreeNode::GetInteger() const
97 {
98   return mIntValue;
99 }
100
101 bool TreeNode::GetBoolean() const
102 {
103   return mIntValue == 1 ? true : false;
104 }
105
106 size_t TreeNode::Size() const
107 {
108   size_t c = 0;
109   TreeNode* p = mFirstChild;
110   while(p)
111   {
112     c++;
113     p = p->mNextSibling;
114   }
115   return c;
116 }
117
118 size_t TreeNode::Count(const std::string& childName) const
119 {
120   const TreeNode* c = GetChild(childName);
121   if(c)
122   {
123     return c->Size();
124   }
125   else
126   {
127     return 0;
128   }
129 }
130
131 const TreeNode* TreeNode::GetChild(const std::string& childName) const
132 {
133   const TreeNode* p = mFirstChild;
134   while(p)
135   {
136     if(p->mName && (std::string(p->mName) == childName) )
137     {
138       return p;
139     }
140     p = p->mNextSibling;
141   }
142   return NULL;
143 }
144
145
146 const TreeNode* TreeNode::GetChildIgnoreCase(const std::string& childName) const
147 {
148   const TreeNode* p = mFirstChild;
149   while(p)
150   {
151     if(p->mName)
152     {
153       std::string nodeName(p->mName);
154       if( CaseInsensitiveStringCompare( nodeName, childName) )
155       {
156         return p;
157       }
158     }
159     p = p->mNextSibling;
160   }
161   return NULL;
162 }
163
164 const TreeNode* TreeNode::Find(const std::string& childName) const
165 {
166   if(mName && std::string(mName) == childName)
167   {
168     return this;
169   }
170   else
171   {
172     return Internal::FindIt(childName, this);
173   }
174 }
175
176 TreeNode::ConstIterator TreeNode::CBegin() const
177 {
178   return ConstIterator(mFirstChild);
179 }
180
181
182 TreeNode::ConstIterator TreeNode::CEnd() const
183 {
184   return ConstIterator(NULL);
185 }
186
187
188 TreeNode::ConstIterator::ConstIterator(TreeNode* v) : mNode(v)
189 {
190
191 }
192
193 TreeNode::ConstIterator& TreeNode::ConstIterator::operator ++()
194 {
195   if(mNode)
196   {
197     mNode = mNode->mNextSibling;
198   }
199   else
200   {
201     mNode = NULL;
202   }
203   return *this;
204 }
205
206 TreeNode::ConstIterator TreeNode::ConstIterator::operator ++(int)
207 {
208   TreeNode::ConstIterator ret(mNode);
209
210   if(mNode)
211   {
212     mNode = mNode->mNextSibling;
213   }
214   else
215   {
216     mNode = NULL;
217   }
218   return ret;
219 }
220
221 TreeNode::KeyNodePair TreeNode::ConstIterator::operator *()
222 {
223   return KeyNodePair(mNode->mName, *mNode);
224 }
225
226 bool TreeNode::ConstIterator::operator!=( const TreeNode::ConstIterator& rhs ) const
227 {
228   return mNode != rhs.mNode;
229 }
230
231 } // namespace Toolkit
232
233 } // namespace Dali