Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

CommandExecutor seems to work very well right now. yet to come: autocompletion lists

File size: 28.3 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() - 1;
358                    if (CommandExecutor::getEvaluation().functionclass_)
359                        argumentNumber -= 1;
360
361                    CommandExecutor::createListOfPossibleArguments(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber);
362                    CommandExecutor::getEvaluation().state_ = CS_Params;
363
364                    if (CommandExecutor::getEvaluation().bCommandChanged_)
365                    {
366                        // Don't do more than one change
367                        return;
368                    }
369                }
370            }
371            case CS_Params:
372std::cout << "3\n";
373            {
374                if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 1)
375                {
376std::cout << "3_1\n";
377                    // There is exactly one possible argument
378                    CommandExecutor::getEvaluation().argument_ = *(*CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()).second;
379                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
380                    return;
381                }
382                else if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 0)
383                {
384std::cout << "3_2\n";
385                    // The user tries something new - we let him do
386                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
387                    CommandExecutor::getEvaluation().argument_ = CommandExecutor::getLastArgument();
388                    return;
389                }
390                else
391                {
392std::cout << "3_3\n";
393                    // There are several possibilities
394                    unsigned int argumentNumber = CommandExecutor::argumentsGiven() - 1;
395                    if (CommandExecutor::getEvaluation().functionclass_)
396                        argumentNumber -= 1;
397
398                    CommandExecutor::getEvaluation().argument_ = CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleArguments_);
399                    CommandExecutor::getEvaluation().possibleArgument_ = CommandExecutor::getPossibleArgument(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber);
400                    CommandExecutor::getEvaluation().state_ = CS_ParamPreparation;
401                    return;
402                }
403            }
404            case CS_Finished:
405std::cout << "4\n";
406            {
407                // Nothing more to do
408                break;
409            }
410            case CS_Error:
411            {
412                // Bad, very bad
413                break;
414            }
415        }
416    }
417
418    unsigned int CommandExecutor::argumentsFinished()
419    {
420        unsigned int argumentsGiven = CommandExecutor::argumentsGiven();
421        if (argumentsGiven > 0)
422            return argumentsGiven - 1;
423        else
424            return 0;
425    }
426
427    unsigned int CommandExecutor::argumentsGiven()
428    {
429        if (CommandExecutor::getEvaluation().command_.size() > 0 && CommandExecutor::getEvaluation().command_[CommandExecutor::getEvaluation().command_.size() - 1] == ' ')
430            return CommandExecutor::getEvaluation().commandTokens_.size() + 1;
431        else
432            return CommandExecutor::getEvaluation().commandTokens_.size();
433    }
434
435    bool CommandExecutor::enoughArgumentsGiven(ConsoleCommand* command)
436    {
437        if (CommandExecutor::getEvaluation().functionclass_)
438            return (CommandExecutor::argumentsGiven() > (2 + command->getParamCount()));
439        else
440            return (CommandExecutor::argumentsGiven() > (1 + command->getParamCount()));
441    }
442
443    std::string CommandExecutor::getArgument(unsigned int index)
444    {
445        if (index < (CommandExecutor::getEvaluation().commandTokens_.size()))
446            return CommandExecutor::getEvaluation().commandTokens_[index];
447        else
448            return "";
449    }
450
451    std::string CommandExecutor::getLastArgument()
452    {
453        return CommandExecutor::getArgument(CommandExecutor::argumentsGiven() - 1);
454    }
455
456    void CommandExecutor::createListOfPossibleIdentifiers(const std::string& fragment)
457    {
458        CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.clear();
459        std::string lowercase = getLowercase(fragment);
460        for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMapBegin(); it != Identifier::getLowercaseIdentifierMapEnd(); ++it)
461            if ((*it).second->hasConsoleCommands())
462                if ((*it).first.find(lowercase) == 0 || fragment == "")
463                    CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
464
465        CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.sort(CommandExecutor::compareStringsInList);
466    }
467
468    void CommandExecutor::createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier)
469    {
470        CommandExecutor::getEvaluation().listOfPossibleFunctions_.clear();
471        std::string lowercase = getLowercase(fragment);
472        if (!identifier)
473        {
474            for (std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMapBegin(); it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd(); ++it)
475                if ((*it).first.find(lowercase) == 0 || fragment == "")
476                    CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
477        }
478        else
479        {
480            for (std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMapBegin(); it != identifier->getLowercaseConsoleCommandMapEnd(); ++it)
481                if ((*it).first.find(lowercase) == 0 || fragment == "")
482                    CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
483        }
484
485        CommandExecutor::getEvaluation().listOfPossibleFunctions_.sort(CommandExecutor::compareStringsInList);
486    }
487
488    void CommandExecutor::createListOfPossibleArguments(const std::string& fragment, ConsoleCommand* command, unsigned int param)
489    {
490        CommandExecutor::getEvaluation().listOfPossibleArguments_.clear();
491        std::string lowercase = getLowercase(fragment);
492        for (std::list<std::pair<std::string, std::string> >::const_iterator it = command->getArgumentCompletionListBegin(param); it != command->getArgumentCompletionListEnd(param); ++it)
493            if ((*it).first.find(lowercase) == 0 || fragment == "")
494                CommandExecutor::getEvaluation().listOfPossibleArguments_.push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second));
495
496        CommandExecutor::getEvaluation().listOfPossibleArguments_.sort(CommandExecutor::compareStringsInList);
497    }
498
499    Identifier* CommandExecutor::getPossibleIdentifier(const std::string& name)
500    {
501        std::string lowercase = getLowercase(name);
502        std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMap().find(lowercase);
503        if ((it != Identifier::getLowercaseIdentifierMapEnd()) && (*it).second->hasConsoleCommands())
504            return (*it).second;
505
506        return 0;
507    }
508
509    ConsoleCommand* CommandExecutor::getPossibleCommand(const std::string& name, Identifier* identifier)
510    {
511        std::string lowercase = getLowercase(name);
512        if (!identifier)
513        {
514            std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMap().find(lowercase);
515            if (it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd())
516                return (*it).second;
517        }
518        else
519        {
520            std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMap().find(lowercase);
521            if (it != identifier->getLowercaseConsoleCommandMapEnd())
522                return (*it).second;
523        }
524        return 0;
525    }
526
527    std::string CommandExecutor::getPossibleArgument(const std::string& name, ConsoleCommand* command, unsigned int param)
528    {
529        std::string lowercase = getLowercase(name);
530        for (std::list<std::pair<std::string, std::string> >::const_iterator it = command->getArgumentCompletionListBegin(param); it != command->getArgumentCompletionListEnd(param); ++it)
531            if ((*it).first == lowercase)
532                return (*it).second;
533
534        return 0;
535    }
536
537    std::string CommandExecutor::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list)
538    {
539        if (list.size() == 0)
540        {
541            return "";
542        }
543        else if (list.size() == 1)
544        {
545            return ((*(*list.begin()).first) + " ");
546        }
547        else
548        {
549            std::string output = "";
550            for (unsigned int i = 0; true; i++)
551            {
552                char temp = 0;
553                for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it)
554                {
555                    if ((*(*it).first).size() > i)
556                    {
557                        if (it == list.begin())
558                        {
559                            temp = (*(*it).first)[i];
560                        }
561                        else
562                        {
563                            if (temp != (*(*it).first)[i])
564                                return output;
565                        }
566                    }
567                    else
568                    {
569                        return output;
570                    }
571                }
572                output += temp;
573            }
574            return output;
575        }
576    }
577
578    bool CommandExecutor::compareStringsInList(const std::pair<const std::string*, const std::string*>& first, const std::pair<const std::string*, const std::string*>& second)
579    {
580        return ((*first.first) < (*second.first));
581    }
582}
Note: See TracBrowser for help on using the repository browser.