Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/Executor.cc @ 994

Last change on this file since 994 was 994, checked in by landauf, 16 years ago
  • added some symbols to the CommandExecutor: a) expression | expression: the pipe leads the output from the right expression into the left one b) expression > file: writes the output of the expression into a file c) expression < file: reads a file and uses it's content as input for the expression
  • added new console commands: a) echo text: returns the input b) read file: reads a file and returns the content c) write file text: writes text into a file d) append file text: appends text to a file
  • added stripEnclosingQuotes function to String.h, that removes enclosing quotes (if there are some). whitespaces outside the quotes are stripped, whitespaces inside the quotes stay. removes the quotes only if there is nothing else than whitespaces outside of them. what it changes: "expression" → expression what it let unchanged:
    • ex"press"ion
    • a"expression"b
    • a"expression"
    • "expression"b
    • express"ion
  • extended SubString: added some bools to determine the behaviour when dividing a string like the following up into pieces: mytext "this is a quoted area" blub (0, 1, 2)

this usually results in:
mytext / this is a quoted area / blub / 0, 1, 2

but now you can change it to:
mytext / "this is a quoted area" / blub / (0, 1, 2)

this is important if the string wents through several substring splitups and the quotes and brackets should stay.

File size: 8.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 *   Inspiration: Executor by Benjamin Grauer
27 */
28
29#include "Executor.h"
30#include "Language.h"
31#include "util/Math.h"
32
33namespace orxonox
34{
35    Executor::Executor(Functor* functor, const std::string& name, AccessLevel::Level level)
36    {
37        this->functor_ = functor;
38        this->name_ = name;
39        this->accessLevel_ = level;
40
41        this->bAddedDescription_ = false;
42        this->bAddedDescriptionReturnvalue_ = false;
43
44        this->bAddedDescriptionParam_[0] = false;
45        this->bAddedDescriptionParam_[1] = false;
46        this->bAddedDescriptionParam_[2] = false;
47        this->bAddedDescriptionParam_[3] = false;
48        this->bAddedDescriptionParam_[4] = false;
49
50        this->bAddedDefaultValue_[0] = false;
51        this->bAddedDefaultValue_[1] = false;
52        this->bAddedDefaultValue_[2] = false;
53        this->bAddedDefaultValue_[3] = false;
54        this->bAddedDefaultValue_[4] = false;
55    }
56
57    Executor::~Executor()
58    {
59        delete this->functor_;
60    }
61
62    bool Executor::parse(const std::string& params, const std::string& delimiter) const
63    {
64        EXECUTOR_PARSE(normal);
65    }
66
67    bool Executor::evaluate(const std::string& params, MultiTypeMath param[5], const std::string& delimiter) const
68    {
69        unsigned int paramCount = this->functor_->getParamCount();
70
71        if (paramCount == 1)
72        {
73            // only one param: check if there are params given, otherwise try to use default values
74            std::string temp = getStripped(params);
75            if ((temp != "") && (temp.size() != 0))
76            {
77                param[0] = params;
78                this->functor_->evaluateParam(0, param[0]);
79                return true;
80            }
81            else if (this->bAddedDefaultValue_[0])
82            {
83                param[0] = this->defaultValue_[0];
84                this->functor_->evaluateParam(0, param[0]);
85                return true;
86            }
87            return false;
88        }
89        else
90        {
91            // more than one param
92            SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0');
93
94            // if there are not enough params given, check if there are default values
95            for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++)
96                if (!this->bAddedDefaultValue_[i])
97                    return false;
98
99            // assign all given arguments to the multitypes
100            for (unsigned int i = 0; i < min(tokens.size(), (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++)
101                param[i] = tokens[i];
102
103            // fill the remaining multitypes with default values
104            for (unsigned int i = tokens.size(); i < min(paramCount, (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++)
105                param[i] = this->defaultValue_[i];
106
107            // evaluate the param types through the functor
108            for (unsigned int i = 0; i < min(paramCount, (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++)
109                this->functor_->evaluateParam(i, param[i]);
110
111            return true;
112        }
113    }
114
115    Executor& Executor::setDescription(const std::string& description)
116    {
117        if (!this->bAddedDescription_)
118        {
119            this->description_ = std::string("ExecutorDescription::" + this->name_ + "::function");
120            AddLanguageEntry(this->description_, description);
121            this->bAddedDescription_ = true;
122        }
123        return (*this);
124    }
125
126    const std::string& Executor::getDescription() const
127    {
128        return GetLocalisation(this->description_);
129    }
130
131    Executor& Executor::setDescriptionParam(int param, const std::string& description)
132    {
133        if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS)
134        {
135            if (!this->bAddedDescriptionParam_[param])
136            {
137                std::string paramnumber;
138                if (!Convert::ToString(&paramnumber, param))
139                    return (*this);
140
141                this->descriptionParam_[param] = std::string("ExecutorDescription::" + this->name_ + "::param" + paramnumber);
142                AddLanguageEntry(this->descriptionParam_[param], description);
143                this->bAddedDescriptionParam_[param] = true;
144            }
145        }
146        return (*this);
147    }
148
149    const std::string& Executor::getDescriptionParam(int param) const
150    {
151        if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS)
152            return GetLocalisation(this->descriptionParam_[param]);
153
154        return this->descriptionParam_[0];
155    }
156
157    Executor& Executor::setDescriptionReturnvalue(const std::string& description)
158    {
159        if (!this->bAddedDescriptionReturnvalue_)
160        {
161            this->descriptionReturnvalue_ = std::string("ExecutorDescription::" + this->name_ + "::returnvalue");
162            AddLanguageEntry(this->descriptionReturnvalue_, description);
163            this->bAddedDescriptionReturnvalue_ = true;
164        }
165        return (*this);
166    }
167
168    const std::string& Executor::getDescriptionReturnvalue(int param) const
169    {
170        return GetLocalisation(this->descriptionReturnvalue_);
171    }
172
173    Executor& Executor::setDefaultValues(const MultiTypeMath& param1)
174    {
175        this->defaultValue_[0] = param1;
176        this->bAddedDefaultValue_[0] = true;
177
178        return (*this);
179    }
180
181    Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2)
182    {
183        this->defaultValue_[0] = param1;
184        this->bAddedDefaultValue_[0] = true;
185        this->defaultValue_[1] = param2;
186        this->bAddedDefaultValue_[1] = true;
187
188        return (*this);
189    }
190
191    Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3)
192    {
193        this->defaultValue_[0] = param1;
194        this->bAddedDefaultValue_[0] = true;
195        this->defaultValue_[1] = param2;
196        this->bAddedDefaultValue_[1] = true;
197        this->defaultValue_[2] = param3;
198        this->bAddedDefaultValue_[2] = true;
199
200        return (*this);
201    }
202
203    Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4)
204    {
205        this->defaultValue_[0] = param1;
206        this->bAddedDefaultValue_[0] = true;
207        this->defaultValue_[1] = param2;
208        this->bAddedDefaultValue_[1] = true;
209        this->defaultValue_[2] = param3;
210        this->bAddedDefaultValue_[2] = true;
211        this->defaultValue_[3] = param4;
212        this->bAddedDefaultValue_[3] = true;
213
214        return (*this);
215    }
216
217    Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5)
218    {
219        this->defaultValue_[0] = param1;
220        this->bAddedDefaultValue_[0] = true;
221        this->defaultValue_[1] = param2;
222        this->bAddedDefaultValue_[1] = true;
223        this->defaultValue_[2] = param3;
224        this->bAddedDefaultValue_[2] = true;
225        this->defaultValue_[3] = param4;
226        this->bAddedDefaultValue_[3] = true;
227        this->defaultValue_[4] = param5;
228        this->bAddedDefaultValue_[4] = true;
229
230        return (*this);
231    }
232
233    Executor& Executor::setDefaultValue(unsigned int index, const MultiTypeMath& param)
234    {
235        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
236        {
237            this->defaultValue_[index] = param;
238            this->bAddedDefaultValue_[index] = true;
239        }
240        return (*this);
241    }
242
243    bool Executor::allDefaultValuesSet() const
244    {
245        for (unsigned int i = 0; i < this->functor_->getParamCount(); i++)
246            if (!this->bAddedDefaultValue_[i])
247                return false;
248
249        return true;
250    }
251}
Note: See TracBrowser for help on using the repository browser.