Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

after some changes in MultiTypeMath and co., the new config and tconfig commands work very well. they substitute set and tset respectively.

File size: 31.0 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    {
148std::cout << "parse (" << bInitialize << "): command: >" << command << "<" << std::endl;
149        if (bInitialize)
150            CommandExecutor::getEvaluation().initialize(command);
151
152        CommandExecutor::getEvaluation().commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0');
153        CommandExecutor::getEvaluation().command_ = command;
154
155        switch (CommandExecutor::getEvaluation().state_)
156        {
157            case CS_Uninitialized:
158            {
159                // Impossible
160                break;
161            }
162            case CS_Empty:
163            {
164                if (CommandExecutor::argumentsGiven() == 0)
165                {
166                    CommandExecutor::createListOfPossibleFunctions("");
167                    CommandExecutor::createListOfPossibleIdentifiers("");
168                    break;
169                }
170                else
171                {
172                    CommandExecutor::getEvaluation().state_ = CS_ShortcutOrIdentifier;
173                    // Move on to next case
174                }
175            }
176            case CS_ShortcutOrIdentifier:
177            {
178                if (CommandExecutor::argumentsGiven() > 1)
179                {
180                    // There's a finished first argument - check if it's a shortcut or a classname
181                    CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0));
182                    CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0));
183
184                    if (CommandExecutor::getEvaluation().function_)
185                    {
186                        // It's a shortcut
187                        CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
188                        CommandExecutor::getEvaluation().functionclass_ = 0;
189                        // Move on to next case
190                    }
191                    else if (CommandExecutor::getEvaluation().functionclass_)
192                    {
193                        // It's a functionname
194                        CommandExecutor::getEvaluation().state_ = CS_Function;
195                        CommandExecutor::getEvaluation().function_ = 0;
196                        // Move on to next case
197                    }
198                    else
199                    {
200                        // The first argument is bad
201                        CommandExecutor::getEvaluation().state_ = CS_Error;
202                        AddLanguageEntry("commandexecutorunknownfirstargument", "is not a shortcut nor a classname");
203                        CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(0) + " " + GetLocalisation("commandexecutorunknownfirstargument") + ".";
204                        return;
205                    }
206                }
207                else
208                {
209                    // There's no finished first argument - search possible shortcuts or classnames
210                    CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(0));
211                    CommandExecutor::createListOfPossibleIdentifiers(CommandExecutor::getArgument(0));
212
213                    unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size();
214                    unsigned int num_identifiers = CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.size();
215
216                    if (num_functions == 1 && num_identifiers == 0)
217                    {
218                        // It's a shortcut
219                        std::string functionname = *(*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).first;
220                        CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname);
221                        if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(0)))
222                        {
223                            // Unfinished shortcut
224                            CommandExecutor::getEvaluation().bCommandChanged_ = true;
225                        }
226                        CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
227                        CommandExecutor::getEvaluation().functionclass_ = 0;
228                        CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().function_->getName();
229                        if (CommandExecutor::getEvaluation().function_->getParamCount() > 0)
230                        {
231                            CommandExecutor::getEvaluation().command_ += " ";
232                            CommandExecutor::getEvaluation().bCommandChanged_ = true;
233                        }
234                        // Move on to next case
235                    }
236                    else if (num_identifiers == 1 && num_functions == 0)
237                    {
238                        // It's a classname
239                        std::string classname = *(*CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin()).first;
240                        CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(classname);
241                        if (getLowercase(classname) != getLowercase(CommandExecutor::getArgument(0)))
242                        {
243                            // Unfinished classname
244                            CommandExecutor::getEvaluation().bCommandChanged_ = true;
245                        }
246                        CommandExecutor::getEvaluation().state_ = CS_Function;
247                        CommandExecutor::getEvaluation().function_ = 0;
248                        CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " ";
249                        // Move on to next case
250                    }
251                    else if (num_identifiers == 0 && num_functions == 0)
252                    {
253                        // No possibilities
254                        CommandExecutor::getEvaluation().state_ = CS_Error;
255                        AddLanguageEntry("commandexecutorunknownfirstargumentstart", "There is no command or classname starting with");
256                        CommandExecutor::getEvaluation().errorMessage_ = "Error: " + GetLocalisation("commandexecutorunknownfirstargumentstart") + " " + CommandExecutor::getArgument(0) + ".";
257                        return;
258                    }
259                    else
260                    {
261                        // There are several possiblilities
262                        std::list<std::pair<const std::string*, const std::string*> > temp;
263                        temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.end());
264                        temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.end());
265                        CommandExecutor::getEvaluation().command_ = CommandExecutor::getCommonBegin(temp);
266                        CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0));
267                        CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0));
268                        CommandExecutor::getEvaluation().bCommandChanged_ = true;
269                        return;
270                    }
271                }
272            }
273            case CS_Function:
274            {
275                if (CommandExecutor::getEvaluation().functionclass_)
276                {
277                    // There is a classname - search for the commandname
278                    if (CommandExecutor::argumentsGiven() > 2)
279                    {
280                        // There is a finished second argument - check if it's a commandname
281                        CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_);
282
283                        if (CommandExecutor::getEvaluation().function_)
284                        {
285                            // It's a function
286                            CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
287                            // Move on to next case
288                        }
289                        else
290                        {
291                            // The second argument is bad
292                            CommandExecutor::getEvaluation().state_ = CS_Error;
293                            AddLanguageEntry("commandexecutorunknownsecondargument", "is not a function of");
294                            CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(1) + " " + GetLocalisation("commandexecutorunknownsecondargument") + " " + CommandExecutor::getEvaluation().functionclass_->getName() + ".";
295                            return;
296                        }
297                    }
298                    else
299                    {
300                        // There is no finished second argument - search for possibilities
301                        CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_);
302                        unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size();
303
304                        if (num_functions == 1)
305                        {
306                            // It's a function
307                            std::string functionname = *(*CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()).first;
308                            CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname, CommandExecutor::getEvaluation().functionclass_);
309                            if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(1)))
310                            {
311                                // Unfinished function
312                                CommandExecutor::getEvaluation().bCommandChanged_ = true;
313                            }
314                            CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
315                            CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " " + CommandExecutor::getEvaluation().function_->getName();
316                            if (CommandExecutor::getEvaluation().function_->getParamCount() > 0)
317                            {
318                                CommandExecutor::getEvaluation().command_ += " ";
319                                CommandExecutor::getEvaluation().bCommandChanged_ = true;
320                            }
321                            // Move on to next case
322                        }
323                        else if (num_functions == 0)
324                        {
325                            // No possibilities
326                            CommandExecutor::getEvaluation().state_ = CS_Error;
327                            AddLanguageEntry("commandexecutorunknownsecondargumentstart", "has no function starting with");
328                            CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getEvaluation().functionclass_->getName() + " " + GetLocalisation("commandexecutorunknownsecondargumentstart") + " " + CommandExecutor::getArgument(1) + ".";
329                            return;
330                        }
331                        else
332                        {
333                            // There are several possibilities
334                            CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + " " + CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleFunctions_);
335                            CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_);
336                            CommandExecutor::getEvaluation().bCommandChanged_ = true;
337                            return;
338                        }
339                    }
340                }
341                else
342                {
343                    // There is no classname - move on to CS_ParamPreparation
344                }
345            }
346std::cout << "1\n";
347            case CS_ParamPreparation:
348std::cout << "2\n";
349            {
350                if (CommandExecutor::getEvaluation().function_->getParamCount() == 0 || CommandExecutor::enoughArgumentsGiven(CommandExecutor::getEvaluation().function_))
351                {
352                    CommandExecutor::getEvaluation().state_ = CS_Finished;
353                    return;
354                }
355                else
356                {
357                    unsigned int argumentNumber = CommandExecutor::argumentsGiven() - 2;
358                    if (CommandExecutor::getEvaluation().functionclass_)
359                        argumentNumber -= 1;
360
361std::cout << "arglist: " << CommandExecutor::getLastArgument() << ", " << CommandExecutor::getEvaluation().function_->getName() << ", " << argumentNumber << std::endl;
362                    CommandExecutor::createListOfPossibleArguments(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber);
363                    CommandExecutor::getEvaluation().state_ = CS_Params;
364
365                    if (CommandExecutor::getEvaluation().bCommandChanged_)
366                    {
367                        // Don't do more than one change
368                        return;
369                    }
370                }
371            }
372            case CS_Params:
373std::cout << "3\n";
374            {
375                if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 1)
376                {
377std::cout << "3_1\n";
378                    // There is exactly one possible argument
379                    CommandExecutor::getEvaluation().argument_ = (*CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()).second;
380                    CommandExecutor::getEvaluation().possibleArgument_ = (*CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()).second;
381                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
382                    return;
383                }
384                else if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 0)
385                {
386std::cout << "3_2\n";
387                    // The user tries something new - we let him do
388                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
389                    CommandExecutor::getEvaluation().argument_ = CommandExecutor::getLastArgument();
390                    return;
391                }
392                else
393                {
394std::cout << "3_3\n";
395                    // There are several possibilities
396                    unsigned int argumentNumber = CommandExecutor::argumentsGiven();
397                    if (argumentNumber > 0)
398                        --argumentNumber;
399std::cout << "3_3_1\n";
400                    if (CommandExecutor::getEvaluation().functionclass_ && argumentNumber > 0)
401                        --argumentNumber;
402
403std::cout << "3_3_2\n";
404                    CommandExecutor::getEvaluation().argument_ = CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleArguments_);
405std::cout << "3_3_3\n";
406                    CommandExecutor::getEvaluation().possibleArgument_ = CommandExecutor::getPossibleArgument(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber);
407std::cout << "3_3_4\n";
408                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
409std::cout << "3_3_5\n";
410                    return;
411                }
412            }
413            case CS_Finished:
414std::cout << "4\n";
415            {
416                // Nothing more to do
417                break;
418            }
419            case CS_Error:
420            {
421                // Bad, very bad
422                break;
423            }
424        }
425    }
426
427    unsigned int CommandExecutor::argumentsFinished()
428    {
429        unsigned int argumentsGiven = CommandExecutor::argumentsGiven();
430        if (argumentsGiven > 0)
431            return argumentsGiven - 1;
432        else
433            return 0;
434    }
435
436    unsigned int CommandExecutor::argumentsGiven()
437    {
438        if (CommandExecutor::getEvaluation().command_.size() > 0 && CommandExecutor::getEvaluation().command_[CommandExecutor::getEvaluation().command_.size() - 1] == ' ')
439            return CommandExecutor::getEvaluation().commandTokens_.size() + 1;
440        else
441            return CommandExecutor::getEvaluation().commandTokens_.size();
442    }
443
444    bool CommandExecutor::enoughArgumentsGiven(ConsoleCommand* command)
445    {
446        if (CommandExecutor::getEvaluation().functionclass_)
447            return (CommandExecutor::argumentsGiven() > (2 + command->getParamCount()));
448        else
449            return (CommandExecutor::argumentsGiven() > (1 + command->getParamCount()));
450    }
451
452    std::string CommandExecutor::getArgument(unsigned int index)
453    {
454        if (index < (CommandExecutor::getEvaluation().commandTokens_.size()))
455            return CommandExecutor::getEvaluation().commandTokens_[index];
456        else
457            return "";
458    }
459
460    std::string CommandExecutor::getLastArgument()
461    {
462        return CommandExecutor::getArgument(CommandExecutor::argumentsGiven() - 1);
463    }
464
465    void CommandExecutor::createListOfPossibleIdentifiers(const std::string& fragment)
466    {
467        CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.clear();
468        std::string lowercase = getLowercase(fragment);
469        for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMapBegin(); it != Identifier::getLowercaseIdentifierMapEnd(); ++it)
470            if ((*it).second->hasConsoleCommands())
471                if ((*it).first.find(lowercase) == 0 || fragment == "")
472                    CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
473
474        CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.sort(CommandExecutor::compareStringsInList);
475    }
476
477    void CommandExecutor::createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier)
478    {
479        CommandExecutor::getEvaluation().listOfPossibleFunctions_.clear();
480        std::string lowercase = getLowercase(fragment);
481        if (!identifier)
482        {
483            for (std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMapBegin(); it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd(); ++it)
484                if ((*it).first.find(lowercase) == 0 || fragment == "")
485                    CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
486        }
487        else
488        {
489            for (std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMapBegin(); it != identifier->getLowercaseConsoleCommandMapEnd(); ++it)
490                if ((*it).first.find(lowercase) == 0 || fragment == "")
491                    CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
492        }
493
494        CommandExecutor::getEvaluation().listOfPossibleFunctions_.sort(CommandExecutor::compareStringsInList);
495    }
496
497    void CommandExecutor::createListOfPossibleArguments(const std::string& fragment, ConsoleCommand* command, unsigned int param)
498    {
499        CommandExecutor::createArgumentCompletionList(command, param);
500
501        CommandExecutor::getEvaluation().listOfPossibleArguments_.clear();
502        std::string lowercase = getLowercase(fragment);
503        for (std::list<std::pair<std::string, std::string> >::const_iterator it = command->getArgumentCompletionListBegin(); it != command->getArgumentCompletionListEnd(); ++it)
504            if ((*it).first.find(lowercase) == 0 || fragment == "")
505                CommandExecutor::getEvaluation().listOfPossibleArguments_.push_back(std::pair<std::string, std::string>((*it).first, (*it).second));
506
507        CommandExecutor::getEvaluation().listOfPossibleArguments_.sort(CommandExecutor::compareStringsInList2);
508    }
509
510    Identifier* CommandExecutor::getPossibleIdentifier(const std::string& name)
511    {
512        std::string lowercase = getLowercase(name);
513        std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMap().find(lowercase);
514        if ((it != Identifier::getLowercaseIdentifierMapEnd()) && (*it).second->hasConsoleCommands())
515            return (*it).second;
516
517        return 0;
518    }
519
520    ConsoleCommand* CommandExecutor::getPossibleCommand(const std::string& name, Identifier* identifier)
521    {
522        std::string lowercase = getLowercase(name);
523        if (!identifier)
524        {
525            std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMap().find(lowercase);
526            if (it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd())
527                return (*it).second;
528        }
529        else
530        {
531            std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMap().find(lowercase);
532            if (it != identifier->getLowercaseConsoleCommandMapEnd())
533                return (*it).second;
534        }
535        return 0;
536    }
537
538    std::string CommandExecutor::getPossibleArgument(const std::string& name, ConsoleCommand* command, unsigned int param)
539    {
540        CommandExecutor::createArgumentCompletionList(command, param);
541
542        std::string lowercase = getLowercase(name);
543        for (std::list<std::pair<std::string, std::string> >::const_iterator it = command->getArgumentCompletionListBegin(); it != command->getArgumentCompletionListEnd(); ++it)
544        {
545            if ((*it).first == lowercase)
546                return (*it).second;
547        }
548
549        return "";
550    }
551
552    void CommandExecutor::createArgumentCompletionList(ConsoleCommand* command, unsigned int param)
553    {
554        std::string params[5];
555
556        unsigned int index = 0;
557        unsigned int lowestIndex = 1 + (CommandExecutor::getEvaluation().functionclass_ != 0);
558
559        for (unsigned int i = CommandExecutor::argumentsGiven() - 2; i >= lowestIndex; --i)
560        {
561            params[index] = CommandExecutor::getArgument(i);
562            ++index;
563            if (index >= 5)
564                break;
565        }
566
567        command->createArgumentCompletionList(param, params[0], params[1], params[2], params[3], params[4]);
568    }
569
570    std::string CommandExecutor::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list)
571    {
572        if (list.size() == 0)
573        {
574            return "";
575        }
576        else if (list.size() == 1)
577        {
578            return ((*(*list.begin()).first) + " ");
579        }
580        else
581        {
582            std::string output = "";
583            for (unsigned int i = 0; true; i++)
584            {
585                char temp = 0;
586                for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
587                {
588                    if ((*(*it).first).size() > i)
589                    {
590                        if (it == list.begin())
591                        {
592                            temp = (*(*it).first)[i];
593                        }
594                        else
595                        {
596                            if (temp != (*(*it).first)[i])
597                                return output;
598                        }
599                    }
600                    else
601                    {
602                        return output;
603                    }
604                }
605                output += temp;
606            }
607            return output;
608        }
609    }
610
611    std::string CommandExecutor::getCommonBegin(const std::list<std::pair<std::string, std::string> >& list)
612    {
613        if (list.size() == 0)
614        {
615            return "";
616        }
617        else if (list.size() == 1)
618        {
619            return ((*list.begin()).first + " ");
620        }
621        else
622        {
623            std::string output = "";
624            for (unsigned int i = 0; true; i++)
625            {
626                char temp = 0;
627                for (std::list<std::pair<std::string, std::string> >::const_iterator it = list.begin(); it != list.end(); ++it)
628                {
629                    if ((*it).first.size() > i)
630                    {
631                        if (it == list.begin())
632                        {
633                            temp = (*it).first[i];
634                        }
635                        else
636                        {
637                            if (temp != (*it).first[i])
638                                return output;
639                        }
640                    }
641                    else
642                    {
643                        return output;
644                    }
645                }
646                output += temp;
647            }
648            return output;
649        }
650    }
651
652    bool CommandExecutor::compareStringsInList(const std::pair<const std::string*, const std::string*>& first, const std::pair<const std::string*, const std::string*>& second)
653    {
654        return ((*first.first) < (*second.first));
655    }
656
657    bool CommandExecutor::compareStringsInList2(const std::pair<std::string, std::string>& first, const std::pair<std::string, std::string>& second)
658    {
659        return (first.first < second.first);
660    }
661}
Note: See TracBrowser for help on using the repository browser.