Merge "Changed styles to use case-insensitive matching" into devel/master
[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 <cstring>
20 #include <algorithm>
21
22 // INTERNAL INCLUDES
23 #include "dali-toolkit/devel-api/builder/tree-node.h"
24 #include "dali-toolkit/internal/builder/tree-node-manipulator.h"
25
26 namespace
27 {
28
29 bool CaseInsensitiveCharacterCompare( unsigned char a, unsigned char b )
30 {
31   // Converts to lower case in the current locale.
32   return std::tolower( a ) == std::tolower( b );
33 }
34
35 /**
36  * return true if the lower cased ASCII strings are equal.
37  */
38 bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b )
39 {
40   bool result = false;
41   if( a.length() == b.length() )
42   {
43     result = std::equal( a.begin(), a.end(), b.begin(), CaseInsensitiveCharacterCompare );
44   }
45   return result;
46 }
47
48 } // anonymous namespace
49
50
51 namespace Dali
52 {
53
54 namespace Toolkit
55 {
56
57 TreeNode::TreeNode()
58   : mName(NULL),
59     mParent(NULL),
60     mNextSibling(NULL),
61     mFirstChild(NULL),
62     mLastChild(NULL),
63     mStringValue(NULL),
64     mType(TreeNode::IS_NULL),
65     mSubstituion(false)
66 {
67 }
68
69 TreeNode::~TreeNode()
70 {
71
72 }
73
74 const char* TreeNode::GetName() const
75 {
76   return mName;
77 }
78
79 TreeNode::NodeType TreeNode::GetType() const
80 {
81   return mType;
82 }
83
84 const char* TreeNode::GetString() const
85 {
86   return mStringValue;
87 }
88
89 bool TreeNode::HasSubstitution() const
90 {
91   return mSubstituion;
92 }
93
94 float TreeNode::GetFloat() const
95 {
96   return mFloatValue;
97 }
98
99 int TreeNode::GetInteger() const
100 {
101   return mIntValue;
102 }
103
104 bool TreeNode::GetBoolean() const
105 {
106   return mIntValue == 1 ? true : false;
107 }
108
109 size_t TreeNode::Size() const
110 {
111   size_t c = 0;
112   TreeNode* p = mFirstChild;
113   while(p)
114   {
115     c++;
116     p = p->mNextSibling;
117   }
118   return c;
119 }
120
121 size_t TreeNode::Count(const std::string& childName) const
122 {
123   const TreeNode* c = GetChild(childName);
124   if(c)
125   {
126     return c->Size();
127   }
128   else
129   {
130     return 0;
131   }
132 }
133
134 const TreeNode* TreeNode::GetChild(const std::string& childName) const
135 {
136   const TreeNode* p = mFirstChild;
137   while(p)
138   {
139     if(p->mName && (std::string(p->mName) == childName) )
140     {
141       return p;
142     }
143     p = p->mNextSibling;
144   }
145   return NULL;
146 }
147
148
149 const TreeNode* TreeNode::GetChildIgnoreCase(const std::string& childName) const
150 {
151   const TreeNode* p = mFirstChild;
152   while(p)
153   {
154     if(p->mName)
155     {
156       std::string nodeName(p->mName);
157       if( CaseInsensitiveStringCompare( nodeName, childName) )
158       {
159         return p;
160       }
161     }
162     p = p->mNextSibling;
163   }
164   return NULL;
165 }
166
167 const TreeNode* TreeNode::Find(const std::string& childName) const
168 {
169   if(mName && std::string(mName) == childName)
170   {
171     return this;
172   }
173   else
174   {
175     return Internal::FindIt(childName, this);
176   }
177 }
178
179 TreeNode::ConstIterator TreeNode::CBegin() const
180 {
181   return ConstIterator(mFirstChild);
182 }
183
184
185 TreeNode::ConstIterator TreeNode::CEnd() const
186 {
187   return ConstIterator(NULL);
188 }
189
190
191 TreeNode::ConstIterator::ConstIterator(TreeNode* v) : mNode(v)
192 {
193
194 }
195
196 TreeNode::ConstIterator& TreeNode::ConstIterator::operator ++()
197 {
198   if(mNode)
199   {
200     mNode = mNode->mNextSibling;
201   }
202   else
203   {
204     mNode = NULL;
205   }
206   return *this;
207 }
208
209 TreeNode::ConstIterator TreeNode::ConstIterator::operator ++(int)
210 {
211   TreeNode::ConstIterator ret(mNode);
212
213   if(mNode)
214   {
215     mNode = mNode->mNextSibling;
216   }
217   else
218   {
219     mNode = NULL;
220   }
221   return ret;
222 }
223
224 TreeNode::KeyNodePair TreeNode::ConstIterator::operator *()
225 {
226   return KeyNodePair(mNode->mName, *mNode);
227 }
228
229 bool TreeNode::ConstIterator::operator!=( const TreeNode::ConstIterator& rhs ) const
230 {
231   return mNode != rhs.mNode;
232 }
233
234 } // namespace Toolkit
235
236 } // namespace Dali