Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

adapted CommandExecutor and CommandEvaluation to make it compile again, but it doesn't run yet. ready for refactoring.

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