Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1434 was 1434, checked in by landauf, 16 years ago

autocompletion is almost done

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