Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/CommandEvaluation.cc @ 1383

Last change on this file since 1383 was 1351, checked in by landauf, 17 years ago

changed large parts of CommandExecutor and CommandEvaluation. very unfinished.

File size: 11.1 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "CommandEvaluation.h"
30#include "ConsoleCommand.h"
31#include "Debug.h"
32
33namespace orxonox
34{
35    CommandEvaluation::CommandEvaluation()
36    {
37        this->initialize("");
38        this->state_ = CS_Uninitialized;
39    }
40
41    void CommandEvaluation::initialize(const std::string& command)
42    {
43        this->bNewCommand_ = true;
44
45        this->originalCommand_ = command;
46        this->command_ = command;
47        this->originalCommandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
48        this->commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
49
50        this->additionalParameter_ = "";
51
52        this->bEvaluatedParams_ = false;
53
54        this->listOfPossibleFunctionClasses_.clear();
55        this->listOfPossibleFunctions_.clear();
56
57        this->functionclass_ = 0;
58        this->function_ = 0;
59
60        this->errorMessage_ = "";
61        this->state_ = CS_Empty;
62    }
63
64    bool CommandEvaluation::isValid() const
65    {
66        if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
67        {
68            return this->function_;
69        }
70        else if (this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
71        {
72            return (this->functionclass_ && this->function_);
73        }
74        else
75        {
76            return false;
77        }
78    }
79
80    bool CommandEvaluation::execute() const
81    {
82        if (!this->isValid())
83            return false;
84
85        if (this->bEvaluatedParams_ && this->function_)
86        {
87            COUT(4) << "CE_execute (evaluation): " << this->function_->getName() << " " << this->param_[0] << " " << this->param_[1] << " " << this->param_[2] << " " << this->param_[3] << " " << this->param_[4] << std::endl;
88            (*this->function_)(this->param_[0], this->param_[1], this->param_[2], this->param_[3], this->param_[4]);
89            return true;
90        }
91
92        COUT(4) << "CE_execute: " << this->originalCommand_ << "\n";
93
94        unsigned int startindex = this->getStartindex();
95        if (this->originalCommandTokens_.size() > startindex)
96            return evaluation.shortcut_->parse(removeSlashes(this->originalCommandTokens_.subSet(startindex).join() + evaluation.getAdditionalParameter()));
97        else
98            return evaluation.shortcut_->parse(removeSlashes(evaluation.additionalParameter_));
99    }
100
101    std::string CommandEvaluation::complete() const
102    {
103        switch (this->state_)
104        {
105            case CS_Empty:
106                std::list<std::pair<const std::string*, const std::string*> > temp;
107                if (evaluation.state_ == CS_Empty)
108                {
109                    temp.insert(temp.end(), this->listOfPossibleFunctions_.begin(), this->listOfPossibleFunctions_.end());
110                    temp.insert(temp.end(), this->listOfPossibleFunctionClasses_.begin(), this->listOfPossibleFunctionClasses_.end());
111                }
112                return (CommandEvaluation::getCommonBegin(temp));
113                break;
114            case CS_Shortcut_Params:
115                if (this->function_)
116                    return (this->function_->getName() + " ");
117                break;
118            case CS_Shortcut_Finished:
119                if (this->function_)
120                {
121                    if (this->function_->getParamCount() == 0)
122                        return (this->function_->getName());
123                    else if (this->originalCommandTokens_.size() > 1)
124                        return (this->function_->getName() + " " + this->originalCommandTokens_.subSet(1).join());
125                }
126                break;
127            case CS_Function:
128                if (this->functionclass_)
129                    return (this->functionclass_->getName() + " " + CommandEvaluation::getCommonBegin(this->listOfPossibleFunctions_));
130                break;
131            case CS_Function_Params:
132                if (this->functionclass_ && this->function_)
133                    return (this->functionclass_->getName() + " " + this->function_->getName() + " ");
134                break;
135            case CS_Function_Finished:
136                if (this->functionclass_ && this->function_)
137                {
138                    if (this->function_->getParamCount() == 0)
139                        return (this->functionclass_->getName() + " " + this->unction_->getName());
140                    else if (this->originalCommandTokens_.size() > 2)
141                        return (this->functionclass_->getName() + " " + this->function_->getName() + " " + this->originalCommandTokens_.subSet(2).join());
142                }
143                break;
144        }
145
146        return this->originalCommand_;
147    }
148
149    std::string CommandEvaluation::hint() const
150    {
151        switch (this->state_)
152        {
153            case CS_Empty:
154                return (CommandEvaluation::dump(this->listOfPossibleFunctions_) + "\n" + CommandExecutor::dump(this->listOfPossibleFunctionClasses_));
155                break;
156            case CS_Function:
157                return CommandEvaluation::dump(this->listOfPossibleFunctions_);
158                break;
159            case CS_Shortcut_Params:
160            case CS_Shortcut_Finished:
161            case CS_Function_Params:
162            case CS_Function_Finished:
163                if (this->function_)
164                    return CommandEvaluation::dump(this->function_);
165                break;
166            case CS_Error:
167                return this->errorMessage_;
168                break;
169        }
170
171        return "";
172    }
173
174    void CommandEvaluation::evaluateParams()
175    {
176        this->bEvaluatedParams_ = false;
177        this->evaluatedExecutor_ = 0;
178
179        for (unsigned int i = 0; i < MAX_FUNCTOR_ARGUMENTS; i++)
180            this->param_[i] = MT_null;
181
182        if (!this->isValid())
183            return;
184
185        unsigned int startindex = this->getStartindex();
186
187        if (this->originalCommandTokens_.size() <= startindex)
188        {
189            if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " "))
190            {
191                this->bEvaluatedParams_ = true;
192                this->evaluatedExecutor_ = this->function_;
193            }
194        }
195        else if (this->originalCommandTokens_.size() > startindex)
196        {
197            if (this->function_->evaluate(this->originalCommandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " "))
198            {
199                this->bEvaluatedParams_ = true;
200                this->evaluatedExecutor_ = this->function_;
201            }
202        }
203    }
204
205    void CommandEvaluation::setEvaluatedParameter(unsigned int index, MultiTypeMath param)
206    {
207        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
208            this->param_[index] = param;
209    }
210
211    MultiTypeMath CommandEvaluation::getEvaluatedParameter(unsigned int index) const
212    {
213        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
214            return this->param_[index];
215
216        return MT_null;
217    }
218
219    bool CommandEvaluation::hasReturnvalue() const
220    {
221        if (this->function_)
222            return this->function_->hasReturnvalue();
223
224        return MT_null;
225    }
226
227    MultiTypeMath CommandEvaluation::getReturnvalue() const
228    {
229        if (this->function_)
230            return this->function_->getReturnvalue();
231
232        return MultiTypeMath();
233    }
234
235
236    unsigned int CommandEvaluation::getStartindex() const
237    {
238        if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
239            return 1;
240        else if (this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
241            return 2;
242        else
243            return 0;
244    }
245
246    std::string CommandEvaluation::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list)
247    {
248        if (list.size() == 0)
249        {
250            return "";
251        }
252        else if (list.size() == 1)
253        {
254            return ((*(*list.begin()).first) + " ");
255        }
256        else
257        {
258            std::string output = "";
259            for (unsigned int i = 0; true; i++)
260            {
261                char temp = 0;
262                for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
263                {
264                    if ((*(*it).first).size() > i)
265                    {
266                        if (it == list.begin())
267                        {
268                            temp = (*(*it).first)[i];
269                        }
270                        else
271                        {
272                            if (temp != (*(*it).first)[i])
273                                return output;
274                        }
275                    }
276                    else
277                    {
278                        return output;
279                    }
280                }
281                output += temp;
282            }
283            return output;
284        }
285    }
286
287    std::string CommandEvaluation::dump(const std::list<std::pair<const std::string*, const std::string*> >& list)
288    {
289        std::string output = "";
290        for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
291        {
292            if (it != list.begin())
293                output += " ";
294
295            output += *(*it).second;
296        }
297        return output;
298    }
299
300    std::string CommandEvaluation::dump(const ConsoleCommand* command)
301    {
302        std::string output = "";
303        for (unsigned int i = 0; i < command->getParamCount(); i++)
304        {
305            if (i != 0)
306                output += " ";
307
308            if (command->defaultValueSet(i))
309                output += "[";
310            else
311                output += "{";
312
313            output += command->getTypenameParam(i);
314
315            if (command->defaultValueSet(i))
316                output += "=" + command->getDefaultValue(i).toString() + "]";
317            else
318                output += "}";
319        }
320        return output;
321    }
322}
Note: See TracBrowser for help on using the repository browser.