Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/CommandExecutor.cc @ 1437

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

removed debug output and fixed a small stupid bug ;)

File size: 30.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 "CommandExecutor.h"
30#include "ConsoleCommand.h"
31#include "util/String.h"
32#include "util/Convert.h"
33#include "Identifier.h"
34#include "Language.h"
35#include "Debug.h"
36#include "TclBind.h"
37
38namespace orxonox
39{
40    CommandExecutor& CommandExecutor::getInstance()
41    {
42        static CommandExecutor instance;
43        return instance;
44    }
45
46    CommandEvaluation& CommandExecutor::getEvaluation()
47    {
48        return CommandExecutor::getInstance().evaluation_;
49    }
50
51    const CommandEvaluation& CommandExecutor::getLastEvaluation()
52    {
53        return CommandExecutor::getInstance().evaluation_;
54    }
55
56    ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command)
57    {
58        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(command->getName());
59        if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end())
60        {
61            COUT(2) << "Warning: Overwriting console-command shortcut with name " << command->getName() << "." << std::endl;
62        }
63
64
65        CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command;
66        CommandExecutor::getInstance().consoleCommandShortcuts_LC_[getLowercase(command->getName())] = command;
67        return (*command);
68    }
69
70    /**
71        @brief Returns the executor of a console command shortcut with given name.
72        @brief name The name of the requested console command shortcut
73        @return The executor of the requested console command shortcut
74    */
75    ConsoleCommand* CommandExecutor::getConsoleCommandShortcut(const std::string& name)
76    {
77        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(name);
78        if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end())
79            return (*it).second;
80        else
81            return 0;
82    }
83
84    /**
85        @brief Returns the executor of a console command shortcut with given name in lowercase.
86        @brief name The name of the requested console command shortcut in lowercase
87        @return The executor of the requested console command shortcut
88    */
89    ConsoleCommand* CommandExecutor::getLowercaseConsoleCommandShortcut(const std::string& name)
90    {
91        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_LC_.find(name);
92        if (it != CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end())
93            return (*it).second;
94        else
95            return 0;
96    }
97
98    bool CommandExecutor::execute(const std::string& command, bool useTcl)
99    {
100        if (useTcl)
101            return TclBind::eval(command);
102
103        CommandExecutor::parseIfNeeded(command);
104        return CommandExecutor::getEvaluation().execute();
105    }
106
107    std::string CommandExecutor::complete(const std::string& command)
108    {
109        CommandExecutor::parseIfNeeded(command);
110        return CommandExecutor::getEvaluation().complete();
111    }
112
113    std::string CommandExecutor::hint(const std::string& command)
114    {
115        CommandExecutor::parseIfNeeded(command);
116        return CommandExecutor::getEvaluation().hint();
117    }
118
119    CommandEvaluation CommandExecutor::evaluate(const std::string& command)
120    {
121        CommandExecutor::parse(command);
122        CommandExecutor::getEvaluation().evaluateParams();
123        return CommandExecutor::getEvaluation();
124    }
125
126    void CommandExecutor::parseIfNeeded(const std::string& command)
127    {
128        if (CommandExecutor::getEvaluation().state_ == CS_Uninitialized)
129        {
130            CommandExecutor::parse(command);
131        }
132        else if (CommandExecutor::getEvaluation().originalCommand_ != command)
133        {
134            if (CommandExecutor::getEvaluation().command_ == command)
135            {
136                CommandExecutor::parse(command);
137                CommandExecutor::getEvaluation().bNewCommand_ = false;
138            }
139            else
140            {
141                CommandExecutor::parse(command);
142            }
143        }
144    }
145
146    void CommandExecutor::parse(const std::string& command, bool bInitialize)
147    {
148        if (bInitialize)
149            CommandExecutor::getEvaluation().initialize(command);
150
151        CommandExecutor::getEvaluation().commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
152        CommandExecutor::getEvaluation().command_ = command;
153
154        switch (CommandExecutor::getEvaluation().state_)
155        {
156            case CS_Uninitialized:
157            {
158                // Impossible
159                break;
160            }
161            case CS_Empty:
162            {
163                if (CommandExecutor::argumentsGiven() == 0)
164                {
165                    CommandExecutor::createListOfPossibleFunctions("");
166                    CommandExecutor::createListOfPossibleIdentifiers("");
167                    break;
168                }
169                else
170                {
171                    CommandExecutor::getEvaluation().state_ = CS_ShortcutOrIdentifier;
172                    // Move on to next case
173                }
174            }
175            case CS_ShortcutOrIdentifier:
176            {
177                if (CommandExecutor::argumentsGiven() > 1)
178                {
179                    // There's a finished first argument - check if it's a shortcut or a classname
180                    CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0));
181                    CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0));
182
183                    if (CommandExecutor::getEvaluation().function_)
184                    {
185                        // It's a shortcut
186                        CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
187                        CommandExecutor::getEvaluation().functionclass_ = 0;
188                        // Move on to next case
189                    }
190                    else if (CommandExecutor::getEvaluation().functionclass_)
191                    {
192                        // It's a functionname
193                        CommandExecutor::getEvaluation().state_ = CS_Function;
194                        CommandExecutor::getEvaluation().function_ = 0;
195                        // Move on to next case
196                    }
197                    else
198                    {
199                        // The first argument is bad
200                        CommandExecutor::getEvaluation().state_ = CS_Error;
201                        AddLanguageEntry("commandexecutorunknownfirstargument", "is not a shortcut nor a classname");
202                        CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(0) + " " + GetLocalisation("commandexecutorunknownfirstargument") + ".";
203                        return;
204                    }
205                }
206                else
207                {
208                    // There's no finished first argument - search possible shortcuts or classnames
209                    CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(0));
210                    CommandExecutor::createListOfPossibleIdentifiers(CommandExecutor::getArgument(0));
211
212                    unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size();
213                    unsigned int num_identifiers = CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.size();
214
215                    if (num_functions == 1 && num_identifiers == 0)
216                    {
217                        // It's a shortcut
218                        std::string functionname = *(*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).first;
219                        CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname);
220                        if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(0)))
221                        {
222                            // Unfinished shortcut
223                            CommandExecutor::getEvaluation().bCommandChanged_ = true;
224                        }
225                        CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
226                        CommandExecutor::getEvaluation().functionclass_ = 0;
227                        CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().function_->getName();
228                        if (CommandExecutor::getEvaluation().function_->getParamCount() > 0)
229                        {
230                            CommandExecutor::getEvaluation().command_ += " ";
231                            CommandExecutor::getEvaluation().bCommandChanged_ = true;
232                        }
233                        // Move on to next case
234                    }
235                    else if (num_identifiers == 1 && num_functions == 0)
236                    {
237                        // It's a classname
238                        std::string classname = *(*CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin()).first;
239                        CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(classname);
240                        if (getLowercase(classname) != getLowercase(CommandExecutor::getArgument(0)))
241                        {
242                            // Unfinished classname
243                            CommandExecutor::getEvaluation().bCommandChanged_ = true;
244                        }
245                        CommandExecutor::getEvaluation().state_ = CS_Function;
246                        CommandExecutor::getEvaluation().function_ = 0;
247                        CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " ";
248                        // Move on to next case
249                    }
250                    else if (num_identifiers == 0 && num_functions == 0)
251                    {
252                        // No possibilities
253                        CommandExecutor::getEvaluation().state_ = CS_Error;
254                        AddLanguageEntry("commandexecutorunknownfirstargumentstart", "There is no command or classname starting with");
255                        CommandExecutor::getEvaluation().errorMessage_ = "Error: " + GetLocalisation("commandexecutorunknownfirstargumentstart") + " " + CommandExecutor::getArgument(0) + ".";
256                        return;
257                    }
258                    else
259                    {
260                        // There are several possiblilities
261                        std::list<std::pair<const std::string*, const std::string*> > temp;
262                        temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.end());
263                        temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.end());
264                        CommandExecutor::getEvaluation().command_ = CommandExecutor::getCommonBegin(temp);
265                        CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0));
266                        CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0));
267                        CommandExecutor::getEvaluation().bCommandChanged_ = true;
268                        return;
269                    }
270                }
271            }
272            case CS_Function:
273            {
274                if (CommandExecutor::getEvaluation().functionclass_)
275                {
276                    // There is a classname - search for the commandname
277                    if (CommandExecutor::argumentsGiven() > 2)
278                    {
279                        // There is a finished second argument - check if it's a commandname
280                        CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_);
281
282                        if (CommandExecutor::getEvaluation().function_)
283                        {
284                            // It's a function
285                            CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
286                            // Move on to next case
287                        }
288                        else
289                        {
290                            // The second argument is bad
291                            CommandExecutor::getEvaluation().state_ = CS_Error;
292                            AddLanguageEntry("commandexecutorunknownsecondargument", "is not a function of");
293                            CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(1) + " " + GetLocalisation("commandexecutorunknownsecondargument") + " " + CommandExecutor::getEvaluation().functionclass_->getName() + ".";
294                            return;
295                        }
296                    }
297                    else
298                    {
299                        // There is no finished second argument - search for possibilities
300                        CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_);
301                        unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size();
302
303                        if (num_functions == 1)
304                        {
305                            // It's a function
306                            std::string functionname = *(*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).first;
307                            CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname, CommandExecutor::getEvaluation().functionclass_);
308                            if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(1)))
309                            {
310                                // Unfinished function
311                                CommandExecutor::getEvaluation().bCommandChanged_ = true;
312                            }
313                            CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
314                            CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " " + CommandExecutor::getEvaluation().function_->getName();
315                            if (CommandExecutor::getEvaluation().function_->getParamCount() > 0)
316                            {
317                                CommandExecutor::getEvaluation().command_ += " ";
318                                CommandExecutor::getEvaluation().bCommandChanged_ = true;
319                            }
320                            // Move on to next case
321                        }
322                        else if (num_functions == 0)
323                        {
324                            // No possibilities
325                            CommandExecutor::getEvaluation().state_ = CS_Error;
326                            AddLanguageEntry("commandexecutorunknownsecondargumentstart", "has no function starting with");
327                            CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getEvaluation().functionclass_->getName() + " " + GetLocalisation("commandexecutorunknownsecondargumentstart") + " " + CommandExecutor::getArgument(1) + ".";
328                            return;
329                        }
330                        else
331                        {
332                            // There are several possibilities
333                            CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " " + CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleFunctions_);
334                            CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_);
335                            CommandExecutor::getEvaluation().bCommandChanged_ = true;
336                            return;
337                        }
338                    }
339                }
340                else
341                {
342                    // There is no classname - move on to CS_ParamPreparation
343                }
344            }
345            case CS_ParamPreparation:
346            {
347                if (CommandExecutor::getEvaluation().function_->getParamCount() == 0 || CommandExecutor::enoughArgumentsGiven(CommandExecutor::getEvaluation().function_))
348                {
349                    CommandExecutor::getEvaluation().state_ = CS_Finished;
350                    return;
351                }
352                else
353                {
354                    unsigned int argumentNumber = CommandExecutor::argumentsGiven() - 2;
355                    if (CommandExecutor::getEvaluation().functionclass_)
356                        argumentNumber -= 1;
357
358                    CommandExecutor::createListOfPossibleArguments(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber);
359                    CommandExecutor::getEvaluation().state_ = CS_Params;
360
361                    if (CommandExecutor::getEvaluation().bCommandChanged_)
362                    {
363                        // Don't do more than one change
364                        return;
365                    }
366                }
367            }
368            case CS_Params:
369            {
370                if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 1)
371                {
372                    // There is exactly one possible argument
373                    CommandExecutor::getEvaluation().argument_ = (*CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()).second;
374                    CommandExecutor::getEvaluation().possibleArgument_ = (*CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()).second;
375                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
376                    return;
377                }
378                else if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 0)
379                {
380                    // The user tries something new - we let him do
381                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
382                    CommandExecutor::getEvaluation().argument_ = CommandExecutor::getLastArgument();
383                    return;
384                }
385                else
386                {
387                    // There are several possibilities
388                    unsigned int argumentNumber = CommandExecutor::argumentsGiven();
389                    if (argumentNumber > 0)
390                        --argumentNumber;
391                    if (CommandExecutor::getEvaluation().functionclass_ && argumentNumber > 0)
392                        --argumentNumber;
393
394                    CommandExecutor::getEvaluation().argument_ = CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleArguments_);
395                    CommandExecutor::getEvaluation().possibleArgument_ = CommandExecutor::getPossibleArgument(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber);
396                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
397                    return;
398                }
399            }
400            case CS_Finished:
401            {
402                // Nothing more to do
403                break;
404            }
405            case CS_Error:
406            {
407                // Bad, very bad
408                break;
409            }
410        }
411    }
412
413    unsigned int CommandExecutor::argumentsFinished()
414    {
415        unsigned int argumentsGiven = CommandExecutor::argumentsGiven();
416        if (argumentsGiven > 0)
417            return argumentsGiven - 1;
418        else
419            return 0;
420    }
421
422    unsigned int CommandExecutor::argumentsGiven()
423    {
424        if (CommandExecutor::getEvaluation().command_.size() > 0 && CommandExecutor::getEvaluation().command_[CommandExecutor::getEvaluation().command_.size() - 1] == ' ')
425            return CommandExecutor::getEvaluation().commandTokens_.size() + 1;
426        else
427            return CommandExecutor::getEvaluation().commandTokens_.size();
428    }
429
430    bool CommandExecutor::enoughArgumentsGiven(ConsoleCommand* command)
431    {
432        if (CommandExecutor::getEvaluation().functionclass_)
433            return (CommandExecutor::argumentsGiven() > (2 + command->getParamCount()));
434        else
435            return (CommandExecutor::argumentsGiven() > (1 + command->getParamCount()));
436    }
437
438    std::string CommandExecutor::getArgument(unsigned int index)
439    {
440        if (index < (CommandExecutor::getEvaluation().commandTokens_.size()))
441            return CommandExecutor::getEvaluation().commandTokens_[index];
442        else
443            return "";
444    }
445
446    std::string CommandExecutor::getLastArgument()
447    {
448        return CommandExecutor::getArgument(CommandExecutor::argumentsGiven() - 1);
449    }
450
451    void CommandExecutor::createListOfPossibleIdentifiers(const std::string& fragment)
452    {
453        CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.clear();
454        std::string lowercase = getLowercase(fragment);
455        for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMapBegin(); it != Identifier::getLowercaseIdentifierMapEnd(); ++it)
456            if ((*it).second->hasConsoleCommands())
457                if ((*it).first.find(lowercase) == 0 || fragment == "")
458                    CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
459
460        CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.sort(CommandExecutor::compareStringsInList);
461    }
462
463    void CommandExecutor::createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier)
464    {
465        CommandExecutor::getEvaluation().listOfPossibleFunctions_.clear();
466        std::string lowercase = getLowercase(fragment);
467        if (!identifier)
468        {
469            for (std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMapBegin(); it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd(); ++it)
470                if ((*it).first.find(lowercase) == 0 || fragment == "")
471                    CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
472        }
473        else
474        {
475            for (std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMapBegin(); it != identifier->getLowercaseConsoleCommandMapEnd(); ++it)
476                if ((*it).first.find(lowercase) == 0 || fragment == "")
477                    CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
478        }
479
480        CommandExecutor::getEvaluation().listOfPossibleFunctions_.sort(CommandExecutor::compareStringsInList);
481    }
482
483    void CommandExecutor::createListOfPossibleArguments(const std::string& fragment, ConsoleCommand* command, unsigned int param)
484    {
485        CommandExecutor::createArgumentCompletionList(command, param);
486
487        CommandExecutor::getEvaluation().listOfPossibleArguments_.clear();
488        std::string lowercase = getLowercase(fragment);
489        for (std::list<std::pair<std::string, std::string> >::const_iterator it = command->getArgumentCompletionListBegin(); it != command->getArgumentCompletionListEnd(); ++it)
490            if ((*it).first.find(lowercase) == 0 || fragment == "")
491                CommandExecutor::getEvaluation().listOfPossibleArguments_.push_back(std::pair<std::string, std::string>((*it).first, (*it).second));
492
493        CommandExecutor::getEvaluation().listOfPossibleArguments_.sort(CommandExecutor::compareStringsInList2);
494    }
495
496    Identifier* CommandExecutor::getPossibleIdentifier(const std::string& name)
497    {
498        std::string lowercase = getLowercase(name);
499        std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMap().find(lowercase);
500        if ((it != Identifier::getLowercaseIdentifierMapEnd()) && (*it).second->hasConsoleCommands())
501            return (*it).second;
502
503        return 0;
504    }
505
506    ConsoleCommand* CommandExecutor::getPossibleCommand(const std::string& name, Identifier* identifier)
507    {
508        std::string lowercase = getLowercase(name);
509        if (!identifier)
510        {
511            std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMap().find(lowercase);
512            if (it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd())
513                return (*it).second;
514        }
515        else
516        {
517            std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMap().find(lowercase);
518            if (it != identifier->getLowercaseConsoleCommandMapEnd())
519                return (*it).second;
520        }
521        return 0;
522    }
523
524    std::string CommandExecutor::getPossibleArgument(const std::string& name, ConsoleCommand* command, unsigned int param)
525    {
526        CommandExecutor::createArgumentCompletionList(command, param);
527
528        std::string lowercase = getLowercase(name);
529        for (std::list<std::pair<std::string, std::string> >::const_iterator it = command->getArgumentCompletionListBegin(); it != command->getArgumentCompletionListEnd(); ++it)
530        {
531            if ((*it).first == lowercase)
532                return (*it).second;
533        }
534
535        return "";
536    }
537
538    void CommandExecutor::createArgumentCompletionList(ConsoleCommand* command, unsigned int param)
539    {
540        std::string params[5];
541
542        unsigned int index = 0;
543        unsigned int lowestIndex = 1 + (CommandExecutor::getEvaluation().functionclass_ != 0);
544
545        for (unsigned int i = CommandExecutor::argumentsGiven() - 1; i >= lowestIndex; --i)
546        {
547            params[index] = CommandExecutor::getArgument(i);
548            ++index;
549            if (index >= 5)
550                break;
551        }
552
553        command->createArgumentCompletionList(param, params[0], params[1], params[2], params[3], params[4]);
554    }
555
556    std::string CommandExecutor::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list)
557    {
558        if (list.size() == 0)
559        {
560            return "";
561        }
562        else if (list.size() == 1)
563        {
564            return ((*(*list.begin()).first) + " ");
565        }
566        else
567        {
568            std::string output = "";
569            for (unsigned int i = 0; true; i++)
570            {
571                char temp = 0;
572                for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
573                {
574                    if ((*(*it).first).size() > i)
575                    {
576                        if (it == list.begin())
577                        {
578                            temp = (*(*it).first)[i];
579                        }
580                        else
581                        {
582                            if (temp != (*(*it).first)[i])
583                                return output;
584                        }
585                    }
586                    else
587                    {
588                        return output;
589                    }
590                }
591                output += temp;
592            }
593            return output;
594        }
595    }
596
597    std::string CommandExecutor::getCommonBegin(const std::list<std::pair<std::string, std::string> >& list)
598    {
599        if (list.size() == 0)
600        {
601            return "";
602        }
603        else if (list.size() == 1)
604        {
605            return ((*list.begin()).first + " ");
606        }
607        else
608        {
609            std::string output = "";
610            for (unsigned int i = 0; true; i++)
611            {
612                char temp = 0;
613                for (std::list<std::pair<std::string, std::string> >::const_iterator it = list.begin(); it != list.end(); ++it)
614                {
615                    if ((*it).first.size() > i)
616                    {
617                        if (it == list.begin())
618                        {
619                            temp = (*it).first[i];
620                        }
621                        else
622                        {
623                            if (temp != (*it).first[i])
624                                return output;
625                        }
626                    }
627                    else
628                    {
629                        return output;
630                    }
631                }
632                output += temp;
633            }
634            return output;
635        }
636    }
637
638    bool CommandExecutor::compareStringsInList(const std::pair<const std::string*, const std::string*>& first, const std::pair<const std::string*, const std::string*>& second)
639    {
640        return ((*first.first) < (*second.first));
641    }
642
643    bool CommandExecutor::compareStringsInList2(const std::pair<std::string, std::string>& first, const std::pair<std::string, std::string>& second)
644    {
645        return (first.first < second.first);
646    }
647}
Note: See TracBrowser for help on using the repository browser.