71be776bed15016ff3b0dcd898fbe8b3fc9621a3
[profile/ivi/layer-management.git] / LayerManagerControl / src / Expression.cpp
1 /***************************************************************************
2  *
3  * Copyright 2012 BMW Car IT GmbH
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  ****************************************************************************/
19 #include "Expression.h"
20 #include <iostream>
21 #include <sstream>
22 #include <stdio.h>
23 #include <string.h>  // memcpy
24
25 Expression::Expression(string name, Expression* parent)
26 : mName(name)
27 , mPreviousWord(parent)
28 , mFuncPtr(NULL)
29 {
30 }
31
32 void Expression::setVarValue(string value)
33 {
34     mVarValue = value;
35 }
36
37 bool Expression::isVar()
38 {
39     return mName[0] == '<';
40 }
41
42 string Expression::getString(string name)
43 {
44     string varName;
45     varName += "<";
46     varName += name;
47     varName += ">";
48
49     if (mName != varName)
50     {
51         if (mPreviousWord)
52         {
53             return mPreviousWord->getString(name);
54         }
55         else
56         {
57             return "";
58         }
59     }
60     return mVarValue;
61 }
62
63 unsigned int Expression::getUint(string name)
64 {
65     string stringVal = getString(name);
66
67     unsigned int value = 0;
68     sscanf(stringVal.c_str(), "%u", &value);
69
70     if (!value)
71     {
72         sscanf(stringVal.c_str(), "0x%x", &value);
73     }
74     return value;
75 }
76
77 void Expression::getUintArray(string name, unsigned int** array, unsigned int* count)
78 {
79     stringstream ss;
80     ss << getString(name);
81
82     unsigned int buffer[256]; // more than enough for all cases
83     *count = 0;
84
85     string stringVal;
86     while (getline( ss, stringVal, ',' ))
87     {
88         sscanf(stringVal.c_str(), "%u", &buffer[*count]);
89
90         if (!buffer[*count])
91         {
92             sscanf(stringVal.c_str(), "0x%x", &buffer[*count]);
93         }
94         ++(*count);
95     }
96
97     *array = new unsigned int[*count];
98     memcpy(*array, buffer, sizeof(unsigned int) * (*count));
99 }
100
101 int Expression::getInt(string name)
102 {
103     string stringVal = getString(name);
104
105     int value = 0;
106     sscanf(stringVal.c_str(), "%d", &value);
107
108     if (!value)
109     {
110         sscanf(stringVal.c_str(), "0x%x", &value);
111     }
112     return value;
113 }
114
115 double Expression::getDouble(string name)
116 {
117     string stringVal = getString(name);
118
119     double value = 0;
120     sscanf(stringVal.c_str(), "%lf", &value);
121     return value;
122 }
123
124 bool Expression::getBool(string name)
125 {
126     string stringVal = getString(name);
127     int value = 0;
128     return sscanf(stringVal.c_str(), "%d", &value) && value;
129 }
130
131 string Expression::getName()
132 {
133     return mName;
134 }
135
136 bool ExpressionCompare(Expression* a, Expression* b)
137 {
138     return a->getName() < b->getName();
139 }
140
141 void Expression::addNextExpression(Expression* word)
142 {
143     mNextWords.push_back(word);
144     mNextWords.sort(ExpressionCompare);
145 }
146
147 Expression* Expression::getNextExpression(string text)
148 {
149     Expression* varMatch = NULL;
150     Expression* nameMatch = NULL;
151
152     ExpressionList::const_iterator iter = mNextWords.begin();
153     ExpressionList::const_iterator end = mNextWords.end();
154     for (; iter != end; ++iter)
155     {
156         Expression* expr = *iter;
157
158         if (expr->getName() == text)
159         {
160             nameMatch = expr;
161         }
162
163         if (expr->isVar())
164         {
165             varMatch = expr;
166             varMatch->setVarValue(text);
167         }
168     }
169
170     return nameMatch ? nameMatch : (varMatch ? varMatch : NULL);
171 }
172
173 void Expression::printTree(int level)
174 {
175     for (int i = 0; i < level; ++i)
176     {
177         cout << ((i + 1 != level) ? "|  " : "|--");
178     }
179
180     stringstream name;
181     name << mName;
182
183     if (isExecutable())
184     {
185         name << "*";
186     }
187
188     cout << name.str() << endl;
189
190     ExpressionList::const_iterator iter = mNextWords.begin();
191     ExpressionList::const_iterator end = mNextWords.end();
192     for (; iter != end; ++iter)
193     {
194         (*iter)->printTree(level + 1);
195     }
196 }
197
198 void Expression::printList(string list)
199 {
200     if (mName != "[root]")
201     {
202         list += mName;
203         list += " ";
204         if (isExecutable())
205         {
206             cout << list << "\n";
207         }
208     }
209
210     ExpressionList::const_iterator iter = mNextWords.begin();
211     ExpressionList::const_iterator end = mNextWords.end();
212     for (; iter != end; ++iter)
213     {
214         (*iter)->printList(list);
215     }
216 }
217
218 bool Expression::isExecutable()
219 {
220     return mFuncPtr;
221 }
222
223 void Expression::execute()
224 {
225     (*mFuncPtr)(this);
226 }
227
228 void Expression::setFunc(callback funcPtr)
229 {
230     mFuncPtr = funcPtr;
231 }
232
233 Expression* Expression::getPreviousExpression()
234 {
235     return mPreviousWord;
236 }