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