Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc @ 7216

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

removed old console command implementation (doesn't compile)

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