Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

CommandExecutor seems to work very well right now. yet to come: autocompletion lists

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