Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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