Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/command/CommandEvaluation.cc @ 7221

Last change on this file since 7221 was 7221, checked in by landauf, 14 years ago

adapted CommandExecutor and CommandEvaluation to make it compile again, but it doesn't run yet. ready for refactoring.

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