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
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#include "util/String.h"
33
34namespace orxonox
35{
36    CommandEvaluation::CommandEvaluation()
37    {
38        this->initialize("");
39        this->state_ = CS_Uninitialized;
40    }
41
42    void CommandEvaluation::initialize(const std::string& command)
43    {
44        this->bNewCommand_ = true;
45        this->bCommandChanged_ = false;
46        this->originalCommand_ = command;
47        this->command_ = command;
48        this->commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
49
50        this->additionalParameter_ = "";
51
52        this->bEvaluatedParams_ = false;
53
54        this->listOfPossibleIdentifiers_.clear();
55        this->listOfPossibleFunctions_.clear();
56        this->listOfPossibleArguments_.clear();
57
58        this->functionclass_ = 0;
59        this->function_ = 0;
60        this->possibleArgument_ = "";
61        this->argument_ = "";
62
63        this->errorMessage_ = "";
64        this->state_ = CS_Empty;
65    }
66
67    bool CommandEvaluation::isValid() const
68    {
69        return (this->function_);
70    }
71
72    bool CommandEvaluation::execute() const
73    {
74        if (!this->isValid())
75            return false;
76
77        if (this->bEvaluatedParams_ && this->function_)
78        {
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;
82        }
83
84        if (!this->bCommandChanged_ || removeTrailingWhitespaces(this->command_) == removeTrailingWhitespaces(this->originalCommand_))
85        {
86            COUT(4) << "CE_execute: " << this->command_ << "\n";
87
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;
96    }
97
98    std::string CommandEvaluation::complete()
99    {
100        if (!this->bNewCommand_)
101        {
102std::cout << "not new" << std::endl;
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_)
111                    {
112                        if (this->function_->getParamCount() == 0)
113                            return (this->command_ = this->function_->getName());
114                        else
115                            return (this->command_ = this->function_->getName() + " ");
116                    }
117                    else if (this->functionclass_)
118                        return (this->command_ = this->functionclass_->getName() + " ");
119                    break;
120                case CS_Function:
121                    if (this->function_)
122                    {
123                        if (this->function_->getParamCount() == 0)
124                            return (this->command_ = this->functionclass_->getName() + " " + this->function_->getName());
125                        else
126                            return (this->command_ = this->functionclass_->getName() + " " + this->function_->getName() + " ");
127                    }
128                    break;
129                case CS_ParamPreparation:
130                case CS_Params:
131                {
132                    if (this->argument_ == "" && this->possibleArgument_ == "")
133                        break;
134
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_;
143                        if (this->function_->getParamCount() > (maxIndex + 1 - this->getStartindex()))
144                            whitespace = " ";
145                    }
146
147                    return (this->command_ = this->commandTokens_.subSet(0, maxIndex).join() + " " + this->argument_ + whitespace);
148                    break;
149                }
150                case CS_Finished:
151                    break;
152                case CS_Error:
153                    break;
154            }
155        }
156        this->bNewCommand_ = false;
157        return this->command_;
158    }
159
160    std::string CommandEvaluation::hint() const
161    {
162        switch (this->state_)
163        {
164            case CS_Uninitialized:
165std::cout << "hint: CS_Uninitialized" << std::endl;
166                break;
167            case CS_Empty:
168std::cout << "hint: CS_Empty" << std::endl;
169            case CS_ShortcutOrIdentifier:
170std::cout << "hint: CS_ShortcutOrIdentifier" << std::endl;
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_));
177                break;
178            case CS_Function:
179std::cout << "hint: CS_Function" << std::endl;
180                return CommandEvaluation::dump(this->listOfPossibleFunctions_);
181                break;
182            case CS_ParamPreparation:
183std::cout << "hint: CS_ParamPreparation" << std::endl;
184            case CS_Params:
185std::cout << "hint: CS_Params" << std::endl;
186                if (this->listOfPossibleArguments_.size() > 0)
187                    return CommandEvaluation::dump(this->listOfPossibleArguments_);
188                else
189                    return CommandEvaluation::dump(this->function_);
190            case CS_Finished:
191std::cout << "hint: CS_Finished" << std::endl;
192                if (this->function_)
193                    return CommandEvaluation::dump(this->function_);
194                break;
195            case CS_Error:
196std::cout << "hint: CS_Error" << std::endl;
197                return this->errorMessage_;
198                break;
199        }
200
201        return "";
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
211        if (!this->isValid())
212            return;
213
214        unsigned int startindex = this->getStartindex();
215
216        if (this->commandTokens_.size() <= startindex)
217        {
218            if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " "))
219                this->bEvaluatedParams_ = true;
220        }
221        else if (this->commandTokens_.size() > startindex)
222        {
223            if (this->function_->evaluate(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " "))
224                this->bEvaluatedParams_ = true;
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    {
244        if (this->function_)
245            return this->function_->hasReturnvalue();
246
247        return MT_null;
248    }
249
250    MultiTypeMath CommandEvaluation::getReturnvalue() const
251    {
252        if (this->function_)
253            return this->function_->getReturnvalue();
254
255        return MultiTypeMath();
256    }
257
258
259    unsigned int CommandEvaluation::getStartindex() const
260    {
261        if (this->functionclass_ && this->function_)
262            return 2;
263        else if (this->function_)
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;
280    }
281
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
295    std::string CommandEvaluation::dump(const ConsoleCommand* command)
296    {
297        std::string output = command->getName();
298        if (command->getParamCount() > 0)
299            output += ": ";
300
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    }
320}
Note: See TracBrowser for help on using the repository browser.