Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

re-implemented parameter evaluation in CommandEvaluation and simplified parse() and evaluateParams() in Executor.

  • Property svn:eol-style set to native
File size: 13.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
31#include "util/StringUtils.h"
32#include "CommandExecutor.h"
33#include "ConsoleCommand.h"
34
35namespace orxonox
36{
37    CommandEvaluation::CommandEvaluation()
38    {
39        this->initialize("");
40    }
41
42    void CommandEvaluation::initialize(const std::string& command)
43    {
44        this->execCommand_ = 0;
45        this->hintCommand_ = 0;
46        this->string_ = command;
47        this->execArgumentsOffset_ = 0;
48        this->hintArgumentsOffset_ = 0;
49        this->bPossibleArgumentsRetrieved_ = false;
50        this->possibleArguments_.clear();
51        this->bEvaluatedParams_ = false;
52        this->bTriedToEvaluatedParams_ = false;
53        this->numberOfEvaluatedParams_ = 0;
54
55        this->tokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0');
56    }
57
58    unsigned int CommandEvaluation::getNumberOfArguments() const
59    {
60        unsigned int count = this->tokens_.size();
61        if (count > 0 && this->string_[this->string_.size() - 1] != ' ')
62            return count;
63        else
64            return count + 1;
65    }
66
67    const std::string& CommandEvaluation::getLastArgument() const
68    {
69        if (this->tokens_.size() > 0 && this->string_[this->string_.size() - 1] != ' ')
70            return this->tokens_.back();
71        else
72            return BLANKSTRING;
73    }
74
75    const std::string& CommandEvaluation::getToken(unsigned int i) const
76    {
77        if (i < this->tokens_.size())
78            return this->tokens_[i];
79        else
80            return BLANKSTRING;
81    }
82
83    int CommandEvaluation::execute()
84    {
85        int error;
86        this->query(&error);
87        return error;
88    }
89
90    MultiType CommandEvaluation::query(int* error)
91    {
92        if (error)
93        {
94            *error = CommandExecutor::Success;
95
96            if (!this->execCommand_)
97                *error = CommandExecutor::Error;
98            else if (!this->execCommand_->isActive())
99                *error = CommandExecutor::Deactivated;
100            else if (!this->execCommand_->hasAccess())
101                *error = CommandExecutor::Denied;
102
103            if (*error != CommandExecutor::Success)
104                return MT_Type::Null;
105        }
106
107        if (this->execCommand_ && this->execCommand_->isActive() && this->execCommand_->hasAccess())
108        {
109            if (!this->bTriedToEvaluatedParams_)
110                this->evaluateParams(false);
111
112            if (this->bEvaluatedParams_)
113            {
114                COUT(0) << "call evaluated" << std::endl;
115                COUT(6) << "CE_execute (evaluation): " << this->execCommand_->getName() << " with " << this->numberOfEvaluatedParams_ << " params: " << this->param_[0] << ' ' << this->param_[1] << ' ' << this->param_[2] << ' ' << this->param_[3] << ' ' << this->param_[4] << std::endl;
116                switch (this->numberOfEvaluatedParams_)
117                {
118                    case 0:  return (*this->execCommand_->getExecutor())();
119                    case 1:  return (*this->execCommand_->getExecutor())(this->param_[0]);
120                    case 2:  return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1]);
121                    case 3:  return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2]);
122                    case 4:  return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2], this->param_[3]);
123                    case 5:
124                    default: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2], this->param_[3], this->param_[4]);
125                }
126            }
127            else
128            {
129                COUT(0) << "call parsed" << std::endl;
130                COUT(5) << "CE_execute: " << this->string_ << "\n";
131                return this->execCommand_->getExecutor()->parse(this->tokens_.subSet(this->execArgumentsOffset_), error, " ");
132            }
133        }
134        else
135            return MT_Type::Null;
136    }
137
138    int CommandEvaluation::evaluateParams(bool bPrintError)
139    {
140COUT(0) << "evaluate params" << std::endl;
141        this->bTriedToEvaluatedParams_ = true;
142
143        if (!this->execCommand_)
144        {
145            if (bPrintError)
146                COUT(1) << "Error: Can't evaluate params, no console command assigned." << std::endl;
147            return CommandExecutor::Error;
148        }
149
150        int error;
151        this->numberOfEvaluatedParams_ = this->execCommand_->getExecutor()->evaluateParams(this->tokens_.subSet(this->execArgumentsOffset_), this->param_, &error, " ");
152        if (!error)
153            this->bEvaluatedParams_ = true;
154        else if (bPrintError)
155            COUT(1) << "Error: Can't evaluate params, not enough arguments given." << std::endl;
156
157        return error;
158    }
159
160    void CommandEvaluation::setEvaluatedParameter(unsigned int index, const MultiType& param)
161    {
162        if (index < MAX_FUNCTOR_ARGUMENTS)
163            this->param_[index] = param;
164    }
165
166    MultiType CommandEvaluation::getEvaluatedParameter(unsigned int index) const
167    {
168        if (index < MAX_FUNCTOR_ARGUMENTS)
169            return this->param_[index];
170
171        return MT_Type::Null;
172    }
173
174    std::string CommandEvaluation::complete()
175    {
176        if (!this->hintCommand_ || !this->hintCommand_->isActive())
177            return this->string_;
178
179        if (!this->bPossibleArgumentsRetrieved_)
180            this->retrievePossibleArguments();
181
182        if (this->possibleArguments_.empty())
183        {
184            return this->string_;
185        }
186        else
187        {
188            std::string output = this->string_.substr(0, this->string_.find_last_of(' ') + 1);
189//            for (unsigned int i = 0; i < this->getNumberOfArguments() - 1; ++i)
190//                output += this->getToken(i) + ' ';
191
192            output += CommandEvaluation::getCommonBegin(this->possibleArguments_);
193            return output;
194        }
195    }
196
197    std::string CommandEvaluation::hint()
198    {
199        if (!this->hintCommand_ || !this->hintCommand_->isActive())
200            return "";
201
202        if (!this->bPossibleArgumentsRetrieved_)
203            this->retrievePossibleArguments();
204
205        if (!this->possibleArguments_.empty())
206            return CommandEvaluation::dump(this->possibleArguments_);
207
208        if (this->isValid())
209        {
210            return CommandEvaluation::dump(this->hintCommand_);
211        }
212        else
213        {
214            if (this->getNumberOfArguments() > 2)
215            {
216                return std::string("Error: There is no command with name \"") + this->getToken(0) + " " + this->getToken(1) + "\".";
217            }
218            else
219            {
220                std::string groupLC = getLowercase(this->getToken(0));
221                std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = _ConsoleCommand::getCommands().begin();
222                for ( ; it_group != _ConsoleCommand::getCommands().end(); ++it_group)
223                    if (getLowercase(it_group->first) == groupLC)
224                        return std::string("Error: There is no command in group \"") + this->getToken(0) + "\" starting with \"" + this->getToken(1) + "\".";
225
226                return std::string("Error: There is no command starting with \"") + this->getToken(0) + "\".";
227            }
228        }
229    }
230
231    void CommandEvaluation::retrievePossibleArguments()
232    {
233        this->bPossibleArgumentsRetrieved_ = true;
234        unsigned int argumentID = std::min(this->getNumberOfArguments() - this->hintArgumentsOffset_, this->hintCommand_->getExecutor()->getParamCount());
235        ArgumentCompleter* ac = this->hintCommand_->getArgumentCompleter(argumentID - 1);
236
237COUT(0) << "hint: args: " << this->getNumberOfArguments() << ", aID: " << argumentID << ", offset: " << this->hintArgumentsOffset_ << ", ac: " << ac << std::endl;
238        if (ac)
239        {
240            MultiType param[MAX_FUNCTOR_ARGUMENTS];
241
242            for (size_t i = 0; i < argumentID; ++i)
243            {
244                param[i] = this->getToken(this->getNumberOfArguments() - i - 1);
245COUT(0) << i << ": " << (this->getNumberOfArguments() - i - 1) << " -> " << this->getToken(this->getNumberOfArguments() - i - 1) << " / " << param[i] << std::endl;
246            }
247
248COUT(0) << "hint: 1: " << param[0] << ", 2: " << param[1] << ", 3: " << param[2] << ", 4: " << param[3] << ", 5: " << param[4] << std::endl;
249            this->possibleArguments_ = (*ac)(param[0], param[1], param[2], param[3], param[4]);
250
251            CommandEvaluation::strip(this->possibleArguments_, param[0]);
252        }
253    }
254
255    /* static */ void CommandEvaluation::strip(ArgumentCompletionList& list, const std::string& fragment)
256    {
257        std::string fragmentLC = getLowercase(fragment);
258
259        for (ArgumentCompletionList::iterator it = list.begin(); it != list.end(); )
260        {
261            const std::string& entry = it->getComparable();
262
263            if (entry.size() < fragmentLC.size())
264            {
265                list.erase(it++);
266            }
267            else
268            {
269                bool bErase = false;
270                for (size_t i = 0; i < fragmentLC.size(); ++i)
271                {
272                    if (fragmentLC[i] != entry[i])
273                    {
274                        bErase = true;
275                        break;
276                    }
277                }
278
279                if (bErase)
280                    list.erase(it++);
281                else
282                    ++it;
283            }
284        }
285    }
286
287    /* static */ std::string CommandEvaluation::dump(const ArgumentCompletionList& list)
288    {
289        std::string output;
290        for (ArgumentCompletionList::const_iterator it = list.begin(); it != list.end(); ++it)
291        {
292            if (it != list.begin())
293                output += ' ';
294
295            output += it->getDisplay();
296        }
297        return output;
298    }
299
300    /* static */ std::string CommandEvaluation::dump(const _ConsoleCommand* command)
301    {
302        std::string output = command->getName();
303        if (command->getExecutor()->getParamCount() > 0)
304            output += ": ";
305
306        for (unsigned int i = 0; i < command->getExecutor()->getParamCount(); i++)
307        {
308            if (i != 0)
309                output += ' ';
310
311            if (command->getExecutor()->defaultValueSet(i))
312                output += '[';
313            else
314                output += '{';
315
316            output += command->getExecutor()->getTypenameParam(i);
317
318            if (command->getExecutor()->defaultValueSet(i))
319                output += '=' + command->getExecutor()->getDefaultValue(i).getString() + ']';
320            else
321                output += '}';
322        }
323        return output;
324    }
325
326    /* static */ std::string CommandEvaluation::getCommonBegin(const ArgumentCompletionList& list)
327    {
328        if (list.size() == 0)
329        {
330            return "";
331        }
332        else if (list.size() == 1)
333        {
334            if (list.begin()->hasDisplay())
335                return (list.begin()->getString());
336            else
337                return (list.begin()->getString() + ' ');
338        }
339        else
340        {
341            std::string output;
342            for (unsigned int i = 0; true; i++)
343            {
344                char tempComparable = 0;
345                char temp = 0;
346                for (ArgumentCompletionList::const_iterator it = list.begin(); it != list.end(); ++it)
347                {
348                    const std::string& argumentComparable = it->getComparable();
349                    const std::string& argument = it->getString();
350                    if (argument.size() > i)
351                    {
352                        if (it == list.begin())
353                        {
354                            tempComparable = argumentComparable[i];
355                            temp = argument[i];
356                        }
357                        else
358                        {
359                            if (tempComparable != argumentComparable[i])
360                                return output;
361                            else if (temp != argument[i])
362                                temp = tempComparable;
363                        }
364                    }
365                    else
366                    {
367                        return output;
368                    }
369                }
370                output += temp;
371            }
372            return output;
373        }
374    }
375}
Note: See TracBrowser for help on using the repository browser.