Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_completion.cc @ 7408

Last change on this file since 7408 was 7408, checked in by bensch, 20 years ago

orxonox/trunk: exist get functions

File size: 9.8 KB
RevLine 
[4744]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[5254]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[7374]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
[1853]17
[5170]18#include "shell_completion.h"
[5639]19#include "shell_command_class.h"
[7388]20#include "shell_completion_plugin.h"
[1853]21
[5194]22#include "shell_command.h"
[5181]23
[5183]24#include "substring.h"
[5178]25#include "class_list.h"
26#include "debug.h"
27
[7374]28namespace OrxShell
29{
[1853]30
[7374]31  /**
32   * @brief standard constructor
33   */
34  ShellCompletion::ShellCompletion()
35  { }
[1853]36
37
[7374]38  /**
39   * @brief standard deconstructor
40   */
41  ShellCompletion::~ShellCompletion ()
42  { }
[5178]43
44
45
[7374]46  /**
47   * @brief autocompletes the Shell's inputLine
[7406]48   * @param input the input to complete, will most possibly be changed.
[7374]49   * @returns true, if a result was found, false otherwise
50   */
51  bool ShellCompletion::autoComplete(std::string& input)
52  {
[7406]53    long classID = CL_NULL;                          //< the classID retrieved from the Class.
54    const std::list<BaseObject*>* objectList = NULL; //< the list of Objects stored in classID's ClassList
55    bool emptyComplete = false;                      //< if the completion input is empty string. e.g ""
56    long completeType = NullCompletion;              //< the Type we'd like to complete.
57    std::string completeString = "";                 //< the string to complete.
[7408]58    unsigned int parameterBegin = 0;                 //< The SubString-entry, the Parameters begin.
[5183]59
[5190]60
[7374]61    PRINTF(5)("AutoComplete on input\n");
62    this->clearCompletionList();
[5185]63
[7374]64    // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object
[7403]65    if (input.empty() || input[input.size()-1] == ' ')
[7374]66      emptyComplete = true;
[5190]67
[7374]68    // CREATE INPUTS
69    SubString inputSplits(input, SubString::WhiteSpacesWithComma);
[5184]70
[7374]71    // What String will be completed
[7403]72    if (!emptyComplete && inputSplits.size() >= 1)
[7374]73      completeString = inputSplits.getString(inputSplits.size()-1);
[5193]74
[7403]75    // CLASS/ALIAS COMPLETION (on first argument)
76    if (inputSplits.size() == 0 || (!emptyComplete && inputSplits.size() == 1))
[7374]77    {
78      completeType |= ClassCompletion;
79      completeType |= AliasCompletion;
80    }
81
82    // OBJECT/FUNCTION COMPLETIONS
[7403]83    else if ((emptyComplete && inputSplits.size() == 1) ||
84              (!emptyComplete && inputSplits.size() == 2))
[7374]85    {
86      classID = ClassList::StringToID(inputSplits.getString(0));
87      objectList = ClassList::getList((ClassID)classID);
88      if (classID != CL_NULL)
89        completeType |= ObjectCompletion;
90      completeType |= FunctionCompletion;
91    }
[7404]92    // Complete the last Function
[7403]93    else if ((emptyComplete && inputSplits.size() == 2 ) ||
94             (!emptyComplete && inputSplits.size() == 3))
[7374]95    {
[7403]96      if (ClassList::exists(inputSplits[0], inputSplits[1]))
[7374]97        completeType |= FunctionCompletion;
98    }
99
[7408]100    // Looking for ParameterCompletions.
101
[7374]102    if (completeType & ClassCompletion)
103      this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS);
104    if (completeType & ObjectCompletion)
105      this->objectComplete(completeString, classID);
106    if (completeType & FunctionCompletion)
[7393]107      this->commandComplete(completeString, inputSplits.getString(0));
[7374]108    if (completeType & AliasCompletion)
109      this->aliasComplete(completeString);
110
[7408]111    if (completeType == NullCompletion)
112    {
113      printf("Completing a Parameter\n");
114//      ShellCommand::
115    }
[7374]116
[7408]117
[7374]118    this->generalComplete(input, completeString);
119    return true;
[5184]120  }
[7374]121
122  /**
123   * @brief autocompletes an ObjectName
124   * @param objectBegin the beginning string of a Object
125   * @param classID the ID of the Class to search for.
126   * @return true on success, false otherwise
127   */
128  bool ShellCompletion::objectComplete(const std::string& objectBegin, long classID)
[5184]129  {
[7374]130    const std::list<BaseObject*>* boList = ClassList::getList((ClassID)classID);
131    if (boList != NULL)
132    {
133      CompletionType type = ObjectCompletion;
134      if (classID == CL_SHELL_COMMAND_CLASS)
135        type = ClassCompletion;
136      if (!this->addToCompleteList(*boList, objectBegin, type))
137        return false;
138    }
139    else
140      return false;
141    return true;
[5184]142  }
[7374]143
144  /**
[7393]145   * @brief completes a Command
146   * @param commandBegin the beginning of the function String
[7374]147   * @param classID the class' ID to complete the function of
148   */
[7393]149  bool ShellCompletion::commandComplete(const std::string& commandBegin, const std::string& className)
[5194]150  {
[7374]151    std::list<std::string> fktList;
[7386]152    ShellCommandClass::getCommandListOfClass(className, fktList);
[7374]153    //printf("%s\n", boList->firstElement()->getName());
[7393]154    if (!this->addToCompleteList(fktList, commandBegin, FunctionCompletion))
[5194]155      return false;
[7374]156    return true;
[5194]157  }
[5184]158
[7374]159  /**
160   * @brief completes an Alias
161   * @param aliasBegin the beginning of the Alias-String to complete
162   * @returns true on succes, false if something went wrong
163   */
164  bool ShellCompletion::aliasComplete(const std::string& aliasBegin)
[5178]165  {
[7374]166    std::list<std::string> aliasList;
[7389]167    ShellCommandAlias::getCommandListOfAlias(aliasList);
[7374]168    //printf("%s\n", boList->firstElement()->getName());
169    if (!this->addToCompleteList(aliasList, aliasBegin, AliasCompletion))
[5178]170      return false;
[7374]171    return true;
[5178]172  }
173
[7374]174
175  /**
176   * @brief completes the inputline on grounds of an inputList
177   * @param input the Input to complete.
178   * @param begin the String to search in the inputList, and to extend with it.
179   * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::"
180   * @param addBack what should be added at the end of the completion
181   * @param addFront what should be added to the front of one finished completion
182   * @return true if ok, false otherwise
183   */
184  bool ShellCompletion::generalComplete(std::string& input,
185                                        const std::string& begin, const std::string& displayAs,
186                                        const std::string& addBack, const std::string& addFront)
[5178]187  {
[7374]188    if (completionList.size() == 0)
[5178]189      return false;
190
[7374]191    CompletionElement addElem = completionList.front();
192    const std::string& addString = addElem.name;
193    unsigned int addLength = 0;
194    unsigned int inputLenght = begin.size();
[5178]195
[7374]196    // Determin the longest Match
197    addLength = addString.size();
[5195]198
[7374]199    CompletionType changeType = NullCompletion;
200    std::vector<CompletionElement>::iterator charIT;
201    for (charIT = completionList.begin(); charIT != completionList.end(); charIT++)
202    {
203      if ((*charIT).type != changeType)
204      {
205        if (changeType != NullCompletion)
206          PRINT(0)("\n");
207        PRINT(0)("%s: ", ShellCompletion::typeToString((*charIT).type).c_str());
208        changeType = (*charIT).type;
209      }
210      PRINTF(0)("%s ", (*charIT).name.c_str());
211      for (unsigned int i = inputLenght; i < addLength; i++)
212        if (addString[i] != (*charIT).name[i])
213        {
214          addLength = i;
215          //       break;
216        }
217    }
218    PRINT(0)("\n");
[5195]219
[7374]220    if (addLength >= inputLenght)
221    {
222      std::string adder = addString;
223      adder.resize(addLength);
[5178]224
[7374]225      input.resize(input.size()-inputLenght);
226      input += adder;
[5178]227
[7374]228      if (completionList.size() == 1)
229      {
230        if ( addBack != "")
231          input += addBack;
232        input += ' ';
233      }
234    }
235    return true;
236  }
[5779]237
[7374]238  /**
239   * @brief searches for classes, which beginn with completionBegin
240   * @param inputList the List to parse through
241   * @param completionBegin the beginning string
242   * !! The strings MUST NOT be deleted !!
243   */
244  bool ShellCompletion::addToCompleteList(const std::list<std::string>& inputList, const std::string& completionBegin, CompletionType type)
[5178]245  {
[7374]246    unsigned int searchLength = completionBegin.size();
247
248    std::list<std::string>::const_iterator string;
249    for (string = inputList.begin(); string != inputList.end(); string++)
[5245]250    {
[7374]251      if ((*string).size() >= searchLength &&
252          !nocaseCmp(*string, completionBegin, searchLength))
[5185]253      {
[7374]254        CompletionElement newElem;
255        newElem.name = (*string);
256        newElem.type = type;
257        this->completionList.push_back(newElem);
[5185]258      }
[7374]259    }
260    return true;
[5178]261  }
262
[7374]263  /**
264   * @brief searches for classes, which beginn with completionBegin
265   * @param inputList the List to parse through
266   * @param completionBegin the beginning string
267   * !! The strings MUST NOT be deleted !!
268   */
269  bool ShellCompletion::addToCompleteList(const std::list<BaseObject*>& inputList, const std::string& completionBegin, CompletionType type)
[5178]270  {
[7374]271    unsigned int searchLength = completionBegin.size();
[5178]272
[7374]273    std::list<BaseObject*>::const_iterator bo;
274    for(bo = inputList.begin(); bo != inputList.end(); bo++)
[5182]275    {
[7374]276      if ((*bo)->getName() != NULL &&
277          strlen((*bo)->getName()) >= searchLength &&
278          !nocaseCmp((*bo)->getName(), completionBegin, searchLength))
279      {
280        CompletionElement newElem;
281        newElem.name = (*bo)->getName();
282        newElem.type = type;
283        this->completionList.push_back(newElem);
284      }
[5182]285    }
[7374]286
287    return true;
[5178]288  }
289
[7374]290  /**
291   * @brief deletes the Completion List.
292   *
293   * This is done at the beginning of each completion-run
294   */
295  void ShellCompletion::clearCompletionList()
[5178]296  {
[7374]297    this->completionList.clear();
[5178]298  }
299
[7374]300  const std::string& ShellCompletion::typeToString(CompletionType type)
[5178]301  {
[7374]302    switch (type)
[5178]303    {
[7374]304      default:// SHELLC_NONE
305        return typeNames[0];
306      case  ClassCompletion:
307        return typeNames[1];
308      case ObjectCompletion:
309        return typeNames[2];
310      case FunctionCompletion:
311        return typeNames[3];
312      case AliasCompletion:
313        return typeNames[4];
[5178]314    }
315  }
316
[5187]317
[7374]318  const std::string ShellCompletion::typeNames[] =
319    {
320      "error",
321      "class",
322      "object",
323      "function",
324      "alias"
325    };
[5245]326
327}
Note: See TracBrowser for help on using the repository browser.