Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1417 was 1416, checked in by landauf, 16 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
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
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
60        this->errorMessage_ = "";
61        this->state_ = CS_Empty;
62    }
63
64    bool CommandEvaluation::isValid() const
65    {
66        return (this->function_);
67    }
68
69    bool CommandEvaluation::execute() const
70    {
71        if (!this->isValid())
72            return false;
73
74        if (this->bEvaluatedParams_ && this->function_)
75        {
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;
79        }
80
81        COUT(4) << "CE_execute: " << this->command_ << "\n";
82
83        unsigned int startindex = this->getStartindex();
84        if (this->commandTokens_.size() > startindex)
85            return this->function_->parse(removeSlashes(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter()));
86        else
87            return this->function_->parse(removeSlashes(this->additionalParameter_));
88    }
89
90    std::string CommandEvaluation::complete() const
91    {
92        switch (this->state_)
93        {
94            case CS_Uninitialized:
95std::cout << "complete: state: CS_Uninitialized" << std::endl;
96            case CS_Empty:
97std::cout << "complete: state: CS_Empty" << std::endl;
98            case CS_ShortcutOrIdentifier:
99std::cout << "complete: state: CS_ShortcutOrIdentifier" << std::endl;
100                {
101                    std::list<std::pair<const std::string*, const std::string*> > temp;
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)
105                    {
106std::cout << "complete: temp > 0" << std::endl;
107                        return (CommandEvaluation::getCommonBegin(temp));
108                    }
109                }
110                break;
111            case CS_Shortcut_Params:
112std::cout << "complete: state: CS_Shortcut_Params" << std::endl;
113                if (this->function_)
114                {
115std::cout << "complete: function != 0" << std::endl;
116                    if (this->commandTokens_.size() > 1)
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                    }
123                    else
124                        return (this->function_->getName() + " ");
125                }
126                break;
127            case CS_Shortcut_Finished:
128std::cout << "complete: state: CS_Shortcut_Finished" << std::endl;
129                if (this->function_)
130                {
131std::cout << "complete: function != 0" << std::endl;
132                    if (this->commandTokens_.size() > 1)
133                        return (this->function_->getName() + " " + this->commandTokens_.subSet(1, this->commandTokens_.size()).join());
134                    else
135                        return (this->function_->getName());
136                }
137                break;
138            case CS_Function:
139std::cout << "complete: state: CS_Function" << std::endl;
140                if (this->functionclass_)
141                {
142std::cout << "complete: functionclass != 0" << std::endl;
143                    return (this->functionclass_->getName() + " " + CommandEvaluation::getCommonBegin(this->listOfPossibleFunctions_));
144                }
145                break;
146            case CS_Function_Params:
147std::cout << "complete: state: CS_Function_Params" << std::endl;
148                if (this->functionclass_ && this->function_)
149                {
150std::cout << "complete: function und functionclass != 0" << std::endl;
151                    if (this->commandTokens_.size() > 2)
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                    }
158                    else
159                        return (this->functionclass_->getName() + " " + this->function_->getName() + " ");
160                }
161                break;
162            case CS_Function_Finished:
163std::cout << "complete: state: CS_Function_Finished" << std::endl;
164                if (this->functionclass_ && this->function_)
165                {
166std::cout << "complete: function und functionclass != 0" << std::endl;
167                    if (this->commandTokens_.size() > 2)
168                        return (this->functionclass_->getName() + " " + this->function_->getName() + " " + this->commandTokens_.subSet(2, this->commandTokens_.size()).join());
169                    else
170                        return (this->functionclass_->getName() + " " + this->function_->getName());
171                }
172               break;
173            case CS_Error:
174std::cout << "complete: state: CS_Error" << std::endl;
175                break;
176        }
177
178        return this->originalCommand_;
179    }
180
181    std::string CommandEvaluation::hint() const
182    {
183        switch (this->state_)
184        {
185            case CS_Uninitialized:
186                break;
187            case CS_Empty:
188            case CS_ShortcutOrIdentifier:
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_));
195                break;
196            case CS_Function:
197                return CommandEvaluation::dump(this->listOfPossibleFunctions_);
198                break;
199            case CS_Shortcut_Params:
200            case CS_Function_Params:
201                if (this->listOfPossibleArguments_.size() > 0)
202                    return CommandEvaluation::dump(this->listOfPossibleArguments_);
203                else
204                    return CommandEvaluation::dump(this->function_);
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;
213        }
214
215        return "";
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
225        if (!this->isValid())
226            return;
227
228        unsigned int startindex = this->getStartindex();
229
230        if (this->commandTokens_.size() <= startindex)
231        {
232            if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " "))
233                this->bEvaluatedParams_ = true;
234        }
235        else if (this->commandTokens_.size() > startindex)
236        {
237            if (this->function_->evaluate(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " "))
238                this->bEvaluatedParams_ = true;
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    {
258        if (this->function_)
259            return this->function_->hasReturnvalue();
260
261        return MT_null;
262    }
263
264    MultiTypeMath CommandEvaluation::getReturnvalue() const
265    {
266        if (this->function_)
267            return this->function_->getReturnvalue();
268
269        return MultiTypeMath();
270    }
271
272
273    unsigned int CommandEvaluation::getStartindex() const
274    {
275        if (this->state_ == CS_Shortcut_Params || this->state_ == CS_Shortcut_Finished)
276            return 1;
277        else if (this->state_ == CS_Function || this->state_ == CS_Function_Params || this->state_ == CS_Function_Finished)
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)
286        {
287            return "";
288        }
289        else if (list.size() == 1)
290        {
291            return ((*(*list.begin()).first) + " ");
292        }
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    }
323
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;
335    }
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    }
359}
Note: See TracBrowser for help on using the repository browser.