Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1406 was 1402, checked in by landauf, 17 years ago

backup-commit

File size: 12.8 KB
RevLine 
[1341]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"
[1351]31#include "Debug.h"
[1341]32
33namespace orxonox
34{
35    CommandEvaluation::CommandEvaluation()
36    {
[1351]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->commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
48
[1341]49        this->additionalParameter_ = "";
50
[1351]51        this->bEvaluatedParams_ = false;
52
[1390]53        this->listOfPossibleIdentifiers_.clear();
[1351]54        this->listOfPossibleFunctions_.clear();
55
[1341]56        this->functionclass_ = 0;
57        this->function_ = 0;
58
59        this->errorMessage_ = "";
[1351]60        this->state_ = CS_Empty;
[1341]61    }
62
[1351]63    bool CommandEvaluation::isValid() const
[1341]64    {
[1390]65        return (this->function_);
[1351]66    }
67
68    bool CommandEvaluation::execute() const
69    {
70        if (!this->isValid())
71            return false;
72
73        if (this->bEvaluatedParams_ && this->function_)
[1341]74        {
[1351]75            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;
76            (*this->function_)(this->param_[0], this->param_[1], this->param_[2], this->param_[3], this->param_[4]);
77            return true;
[1341]78        }
[1351]79
[1402]80        COUT(4) << "CE_execute: " << this->command_ << "\n";
[1351]81
82        unsigned int startindex = this->getStartindex();
[1402]83        if (this->commandTokens_.size() > startindex)
84            return this->function_->parse(removeSlashes(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter()));
[1341]85        else
[1390]86            return this->function_->parse(removeSlashes(this->additionalParameter_));
[1351]87    }
88
89    std::string CommandEvaluation::complete() const
90    {
91        switch (this->state_)
[1341]92        {
[1390]93            case CS_Uninitialized:
[1402]94std::cout << "complete: state: CS_Uninitialized" << std::endl;
[1351]95            case CS_Empty:
[1402]96std::cout << "complete: state: CS_Empty" << std::endl;
[1390]97            case CS_ShortcutOrIdentifier:
[1402]98std::cout << "complete: state: CS_ShortcutOrIdentifier" << std::endl;
[1351]99                {
[1390]100                    std::list<std::pair<const std::string*, const std::string*> > temp;
[1402]101                    temp.insert(temp.end(), this->listOfPossibleFunctions_.begin(), this->listOfPossibleFunctions_.end());
102                    temp.insert(temp.end(), this->listOfPossibleIdentifiers_.begin(), this->listOfPossibleIdentifiers_.end());
103                    if (temp.size() > 0)
[1390]104                    {
[1402]105std::cout << "complete: temp > 0" << std::endl;
106                        return (CommandEvaluation::getCommonBegin(temp));
[1390]107                    }
[1351]108                }
109                break;
110            case CS_Shortcut_Params:
[1402]111std::cout << "complete: state: CS_Shortcut_Params" << std::endl;
[1351]112                if (this->function_)
[1390]113                {
[1402]114std::cout << "complete: function != 0" << std::endl;
[1390]115                    if (this->commandTokens_.size() > 1)
116                        return (this->function_->getName() + " " + this->commandTokens_.subSet(1, this->commandTokens_.size() - 1).join() + " " + CommandEvaluation::getCommonBegin(this->listOfPossibleArguments_));
117                    else
118                        return (this->function_->getName() + " ");
119                }
[1351]120                break;
121            case CS_Shortcut_Finished:
[1402]122std::cout << "complete: state: CS_Shortcut_Finished" << std::endl;
[1351]123                if (this->function_)
124                {
[1402]125std::cout << "complete: function != 0" << std::endl;
[1390]126                    if (this->commandTokens_.size() > 1)
[1402]127                        return (this->function_->getName() + " " + this->commandTokens_.subSet(1, this->commandTokens_.size() - 1).join());
[1390]128                    else
[1351]129                        return (this->function_->getName());
130                }
131                break;
132            case CS_Function:
[1402]133std::cout << "complete: state: CS_Function" << std::endl;
[1351]134                if (this->functionclass_)
[1402]135                {
136std::cout << "complete: functionclass != 0" << std::endl;
[1351]137                    return (this->functionclass_->getName() + " " + CommandEvaluation::getCommonBegin(this->listOfPossibleFunctions_));
[1402]138                }
[1351]139                break;
140            case CS_Function_Params:
[1402]141std::cout << "complete: state: CS_Function_Params" << std::endl;
[1351]142                if (this->functionclass_ && this->function_)
[1390]143                {
[1402]144std::cout << "complete: function und functionclass != 0" << std::endl;
[1390]145                    if (this->commandTokens_.size() > 2)
[1402]146                        return (this->functionclass_->getName() + " " + this->function_->getName() + " " + this->commandTokens_.subSet(2, this->commandTokens_.size()).join() + " " + CommandEvaluation::getCommonBegin(this->listOfPossibleArguments_));
[1390]147                    else
148                        return (this->functionclass_->getName() + " " + this->function_->getName() + " ");
149                }
[1351]150                break;
151            case CS_Function_Finished:
[1402]152std::cout << "complete: state: CS_Function_Finished" << std::endl;
[1351]153                if (this->functionclass_ && this->function_)
154                {
[1402]155std::cout << "complete: function und functionclass != 0" << std::endl;
[1390]156                    if (this->commandTokens_.size() > 2)
[1402]157                        return (this->functionclass_->getName() + " " + this->function_->getName() + " " + this->commandTokens_.subSet(2, this->commandTokens_.size()).join());
[1390]158                    else
159                        return (this->functionclass_->getName() + " " + this->function_->getName());
[1351]160                }
[1390]161               break;
162            case CS_Error:
[1402]163std::cout << "complete: state: CS_Error" << std::endl;
[1351]164                break;
[1341]165        }
[1351]166
167        return this->originalCommand_;
[1341]168    }
169
[1351]170    std::string CommandEvaluation::hint() const
[1341]171    {
[1351]172        switch (this->state_)
[1341]173        {
[1390]174            case CS_Uninitialized:
175                break;
[1351]176            case CS_Empty:
[1390]177            case CS_ShortcutOrIdentifier:
[1402]178                if (this->listOfPossibleFunctions_.size() == 0)
179                    return CommandEvaluation::dump(this->listOfPossibleIdentifiers_);
180                else if (this->listOfPossibleIdentifiers_.size() == 0)
181                    return CommandEvaluation::dump(this->listOfPossibleFunctions_);
182                else
183                    return (CommandEvaluation::dump(this->listOfPossibleFunctions_) + "\n" + CommandEvaluation::dump(this->listOfPossibleIdentifiers_));
[1351]184                break;
185            case CS_Function:
186                return CommandEvaluation::dump(this->listOfPossibleFunctions_);
187                break;
188            case CS_Shortcut_Params:
[1390]189            case CS_Function_Params:
[1402]190                if (this->listOfPossibleArguments_.size() > 0)
191                    return CommandEvaluation::dump(this->listOfPossibleArguments_);
192                else
193                    return CommandEvaluation::dump(this->function_);
[1351]194            case CS_Shortcut_Finished:
195            case CS_Function_Finished:
196                if (this->function_)
197                    return CommandEvaluation::dump(this->function_);
198                break;
199            case CS_Error:
200                return this->errorMessage_;
201                break;
[1341]202        }
[1351]203
204        return "";
[1341]205    }
206
207    void CommandEvaluation::evaluateParams()
208    {
209        this->bEvaluatedParams_ = false;
210
211        for (unsigned int i = 0; i < MAX_FUNCTOR_ARGUMENTS; i++)
212            this->param_[i] = MT_null;
213
[1351]214        if (!this->isValid())
215            return;
216
217        unsigned int startindex = this->getStartindex();
218
[1402]219        if (this->commandTokens_.size() <= startindex)
[1341]220        {
[1351]221            if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " "))
222                this->bEvaluatedParams_ = true;
[1341]223        }
[1402]224        else if (this->commandTokens_.size() > startindex)
[1341]225        {
[1402]226            if (this->function_->evaluate(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " "))
[1351]227                this->bEvaluatedParams_ = true;
[1341]228        }
229    }
230
231    void CommandEvaluation::setEvaluatedParameter(unsigned int index, MultiTypeMath param)
232    {
233        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
234            this->param_[index] = param;
235    }
236
237    MultiTypeMath CommandEvaluation::getEvaluatedParameter(unsigned int index) const
238    {
239        if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS)
240            return this->param_[index];
241
242        return MT_null;
243    }
244
245    bool CommandEvaluation::hasReturnvalue() const
246    {
[1351]247        if (this->function_)
248            return this->function_->hasReturnvalue();
[1341]249
250        return MT_null;
251    }
252
253    MultiTypeMath CommandEvaluation::getReturnvalue() const
254    {
[1351]255        if (this->function_)
256            return this->function_->getReturnvalue();
257
258        return MultiTypeMath();
259    }
260
261
262    unsigned int CommandEvaluation::getStartindex() const
263    {
[1341]264        if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
[1351]265            return 1;
[1390]266        else if (this->state_ == CS_Function || this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
[1351]267            return 2;
268        else
269            return 0;
270    }
271
272    std::string CommandEvaluation::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list)
273    {
274        if (list.size() == 0)
[1341]275        {
[1351]276            return "";
[1341]277        }
[1351]278        else if (list.size() == 1)
[1341]279        {
[1351]280            return ((*(*list.begin()).first) + " ");
[1341]281        }
[1351]282        else
283        {
284            std::string output = "";
285            for (unsigned int i = 0; true; i++)
286            {
287                char temp = 0;
288                for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
289                {
290                    if ((*(*it).first).size() > i)
291                    {
292                        if (it == list.begin())
293                        {
294                            temp = (*(*it).first)[i];
295                        }
296                        else
297                        {
298                            if (temp != (*(*it).first)[i])
299                                return output;
300                        }
301                    }
302                    else
303                    {
304                        return output;
305                    }
306                }
307                output += temp;
308            }
309            return output;
310        }
311    }
[1341]312
[1351]313    std::string CommandEvaluation::dump(const std::list<std::pair<const std::string*, const std::string*> >& list)
314    {
315        std::string output = "";
316        for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
317        {
318            if (it != list.begin())
319                output += " ";
320
321            output += *(*it).second;
322        }
323        return output;
[1341]324    }
[1351]325
326    std::string CommandEvaluation::dump(const ConsoleCommand* command)
327    {
328        std::string output = "";
329        for (unsigned int i = 0; i < command->getParamCount(); i++)
330        {
331            if (i != 0)
332                output += " ";
333
334            if (command->defaultValueSet(i))
335                output += "[";
336            else
337                output += "{";
338
339            output += command->getTypenameParam(i);
340
341            if (command->defaultValueSet(i))
342                output += "=" + command->getDefaultValue(i).toString() + "]";
343            else
344                output += "}";
345        }
346        return output;
347    }
[1341]348}
Note: See TracBrowser for help on using the repository browser.