1 /***************************************************************************
3 * Copyright 2012 BMW Car IT GmbH
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 ****************************************************************************/
19 #include "Expression.h"
23 #include <string.h> // memcpy
25 Expression::Expression(string name, Expression* parent)
27 , mPreviousWord(parent)
32 void Expression::setVarValue(string value)
37 bool Expression::isVar()
39 return mName[0] == '<';
42 string Expression::getString(string name)
53 return mPreviousWord->getString(name);
63 unsigned int Expression::getUint(string name)
65 string stringVal = getString(name);
67 unsigned int value = 0;
68 sscanf(stringVal.c_str(), "%u", &value);
72 sscanf(stringVal.c_str(), "0x%x", &value);
77 void Expression::getUintArray(string name, unsigned int** array, unsigned int* count)
80 ss << getString(name);
82 unsigned int buffer[256]; // more than enough for all cases
86 while (getline( ss, stringVal, ',' ))
88 sscanf(stringVal.c_str(), "%u", &buffer[*count]);
92 sscanf(stringVal.c_str(), "0x%x", &buffer[*count]);
97 *array = new unsigned int[*count];
98 memcpy(*array, buffer, sizeof(unsigned int) * (*count));
101 int Expression::getInt(string name)
103 string stringVal = getString(name);
106 sscanf(stringVal.c_str(), "%d", &value);
110 sscanf(stringVal.c_str(), "0x%x", &value);
115 double Expression::getDouble(string name)
117 string stringVal = getString(name);
120 sscanf(stringVal.c_str(), "%lf", &value);
124 bool Expression::getBool(string name)
126 string stringVal = getString(name);
128 return sscanf(stringVal.c_str(), "%d", &value) && value;
131 string Expression::getName()
136 bool ExpressionCompare(Expression* a, Expression* b)
138 return a->getName() < b->getName();
141 void Expression::addNextExpression(Expression* word)
143 mNextWords.push_back(word);
144 mNextWords.sort(ExpressionCompare);
147 Expression* Expression::getNextExpression(string text)
149 Expression* varMatch = NULL;
150 Expression* nameMatch = NULL;
152 ExpressionList::const_iterator iter = mNextWords.begin();
153 ExpressionList::const_iterator end = mNextWords.end();
154 for (; iter != end; ++iter)
156 Expression* expr = *iter;
158 if (expr->getName() == text)
166 varMatch->setVarValue(text);
170 return nameMatch ? nameMatch : (varMatch ? varMatch : NULL);
173 void Expression::printTree(int level)
175 for (int i = 0; i < level; ++i)
177 cout << ((i + 1 != level) ? "| " : "|--");
188 cout << name.str() << endl;
190 ExpressionList::const_iterator iter = mNextWords.begin();
191 ExpressionList::const_iterator end = mNextWords.end();
192 for (; iter != end; ++iter)
194 (*iter)->printTree(level + 1);
198 void Expression::printList(string list)
200 if (mName != "[root]")
206 cout << list << "\n";
210 ExpressionList::const_iterator iter = mNextWords.begin();
211 ExpressionList::const_iterator end = mNextWords.end();
212 for (; iter != end; ++iter)
214 (*iter)->printList(list);
218 bool Expression::isExecutable()
223 void Expression::execute()
228 void Expression::setFunc(callback funcPtr)
233 Expression* Expression::getPreviousExpression()
235 return mPreviousWord;