Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7371 was 7371, checked in by bensch, 18 years ago

orxonox/trunk: better completion-algos

File size: 9.5 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
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5170]18#include "shell_completion.h"
[5639]19#include "shell_command_class.h"
[1853]20
[5181]21#include "shell_input.h"
[5194]22#include "shell_command.h"
[5181]23
[5183]24#include "substring.h"
[5178]25#include "base_object.h"
26#include "class_list.h"
27#include "debug.h"
28
29#include "stdlibincl.h"
30
[1856]31using namespace std;
[1853]32
[3245]33/**
[7343]34 * @brief standard constructor
[5406]35 */
[7343]36ShellCompletion::ShellCompletion()
37{ }
[1853]38
39
[3245]40/**
[7343]41 * @brief standard deconstructor
[5406]42 */
[5170]43ShellCompletion::~ShellCompletion ()
[7343]44{ }
[5178]45
46
47
48/**
[7343]49 * @brief autocompletes the Shell's inputLine
[7344]50 * @param input the input to complete.
[5178]51 * @returns true, if a result was found, false otherwise
52 */
[7343]53bool ShellCompletion::autoComplete(std::string& input)
[5178]54{
[5779]55  const char* completionLine;           //< the inputLine we complete.
[5183]56
[5779]57  long classID;                         //< the classID retrieved from the Class.
[5885]58  const std::list<BaseObject*>* objectList;   //< the list of Objects stored in classID
[5779]59  bool emptyComplete = false;           //< if the completion input is empty string. e.g ""
60  long completeType = SHELLC_NONE;      //< the Type we'd like to complete.
[7344]61  std::string completeString;           //< the string to complete.
[5183]62
[5190]63
[5191]64  PRINTF(5)("AutoComplete on input\n");
[7371]65  this->clearCompletionList();
[5185]66
[5190]67  // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object
[7343]68  if (input[input.size()-1] == ' ')
[5190]69    emptyComplete = true;
70
[5193]71  // CREATE INPUTS
[7344]72  SubString inputSplits(input, SubString::WhiteSpacesWithComma);
[5184]73
[5193]74  // What String will be completed
75  if (emptyComplete == true)
76    completeString = "";
77  else
[7344]78    completeString = inputSplits.getString(inputSplits.size()-1);
[5193]79
[5185]80  // CLASS COMPLETION
[7319]81  if (inputSplits.size() == 0)
[5184]82  {
[5193]83    completeType |= SHELLC_CLASS;
[5195]84    completeType |= SHELLC_ALIAS;
[5184]85  }
[7319]86  else if (inputSplits.size() == 1 && emptyComplete == false)
[5184]87  {
[5193]88    completeType |= SHELLC_CLASS;
[5195]89    completeType |= SHELLC_ALIAS;
[5184]90  }
91
[5191]92  // OBJECT/FUNCTION COMPLETIONS
[7319]93  else if ((inputSplits.size() == 1 && emptyComplete == true) ||
94            (inputSplits.size() == 2 && emptyComplete == false))
[5184]95  {
[7344]96    classID = ClassList::StringToID(inputSplits.getString(0));
[5791]97    objectList = ClassList::getList((ClassID)classID);
[5330]98    if (classID != CL_NULL)
[5193]99      completeType |= SHELLC_OBJECT;
[5330]100    //if (objectList != NULL && objectList->getSize() == 1)
101      completeType |= SHELLC_FUNCTION;
[5184]102  }
[7319]103  else if ((inputSplits.size() == 2 && emptyComplete == true) ||
104            (inputSplits.size() == 3 && emptyComplete == false))
[5194]105  {
[7344]106    classID = ClassList::StringToID(inputSplits.getString(0));
[5194]107    if (classID == CL_NULL)
108      return false;
109    else
110     completeType |= SHELLC_FUNCTION;
111  }
[5184]112
[5193]113  if (completeType & SHELLC_CLASS)
114    this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS);
115  if (completeType & SHELLC_OBJECT)
116    this->objectComplete(completeString, classID);
[5194]117  if (completeType & SHELLC_FUNCTION)
[7344]118    this->functionComplete(completeString, inputSplits.getString(0));
[5195]119  if (completeType & SHELLC_ALIAS)
120    this->aliasComplete(completeString);
[5193]121
[5194]122
[7343]123  this->generalComplete(input, completeString);
[5194]124  return true;
[5178]125}
126
127/**
[7343]128 * @brief autocompletes a className
[5178]129 * @param classBegin the Beginning of a String to autoComplete
130 * @return true on success, false otherwise
131 */
[7225]132bool ShellCompletion::classComplete(const std::string& classBegin)
[5178]133{
[7221]134  const std::list<std::string>* clList = ClassList::getClassNames();
[5178]135  if (clList != NULL)
136  {
[7371]137    if (!this->addToCompleteList(*clList, classBegin, SHELLC_CLASS))
[5178]138      return false;
139  }
140  else
141    return false;
142  return true;
143}
144
145/**
[7343]146 * @brief autocompletes an ObjectName
[5178]147 * @param objectBegin the beginning string of a Object
148 * @param classID the ID of the Class to search for.
149 * @return true on success, false otherwise
150 */
[7225]151bool ShellCompletion::objectComplete(const std::string& objectBegin, long classID)
[5178]152{
[5791]153  const std::list<BaseObject*>* boList = ClassList::getList((ClassID)classID);
[5178]154  if (boList != NULL)
155  {
[5245]156    SHELLC_TYPE type = SHELLC_OBJECT;
157    if (classID == CL_SHELL_COMMAND_CLASS)
158      type = SHELLC_CLASS;
[7371]159    if (!this->addToCompleteList(*boList, objectBegin, type))
[5178]160      return false;
161  }
162  else
163    return false;
164  return true;
165}
166
167/**
[7343]168 * @brief completes a Function
[5178]169 * @param functionBegin the beginning of the function String
[5197]170 * @param classID the class' ID to complete the function of
[5178]171 */
[7225]172bool ShellCompletion::functionComplete(const std::string& functionBegin, const std::string& className)
[5178]173{
[7221]174  std::list<std::string> fktList;
[5330]175  ShellCommandClass::getCommandListOfClass(className, &fktList);
[5194]176  //printf("%s\n", boList->firstElement()->getName());
[7371]177  if (!this->addToCompleteList(fktList, functionBegin, SHELLC_FUNCTION))
[5194]178    return false;
179  return true;
[5178]180}
181
[5197]182/**
[7343]183 * @brief completes an Alias
[5197]184 * @param aliasBegin the beginning of the Alias-String to complete
185 * @returns true on succes, false if something went wrong
186 */
[7225]187bool ShellCompletion::aliasComplete(const std::string& aliasBegin)
[5195]188{
[7221]189  std::list<std::string> aliasList;
[5195]190  ShellCommandClass::getCommandListOfAlias(&aliasList);
191  //printf("%s\n", boList->firstElement()->getName());
[7371]192  if (!this->addToCompleteList(aliasList, aliasBegin, SHELLC_ALIAS))
[5195]193    return false;
194  return true;
195}
196
197
[5178]198/**
[7343]199 * @brief completes the inputline on grounds of an inputList
[7344]200 * @param input the Input to complete.
[5178]201 * @param begin the String to search in the inputList, and to extend with it.
202 * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::"
203 * @param addBack what should be added at the end of the completion
204 * @param addFront what should be added to the front of one finished completion
205 * @return true if ok, false otherwise
206 */
[7343]207bool ShellCompletion::generalComplete(std::string& input,
208                                      const std::string& begin, const std::string& displayAs,
209                                      const std::string& addBack, const std::string& addFront)
[5178]210{
[5780]211  if (completionList.size() == 0)
[5178]212    return false;
213
[5780]214  ShellC_Element addElem = completionList.front();
[7225]215  const std::string& addString = addElem.name;
[5178]216  unsigned int addLength = 0;
[7225]217  unsigned int inputLenght = begin.size();
[5178]218
[5187]219  // Determin the longest Match
[7225]220  addLength = addString.size();
[5779]221
[5245]222  SHELLC_TYPE changeType = SHELLC_NONE;
[5780]223  list<ShellC_Element>::iterator charIT;
224  for (charIT = completionList.begin(); charIT != completionList.end(); charIT++)
[5178]225  {
[5780]226    if ((*charIT).type != changeType)
[5245]227    {
228      if (changeType != SHELLC_NONE)
229        PRINT(0)("\n");
[5780]230      PRINT(0)("%s: ", ShellCompletion::typeToString((*charIT).type));
231      changeType = (*charIT).type;
[5245]232    }
[7225]233    PRINTF(0)("%s ", (*charIT).name.c_str());
[5178]234    for (unsigned int i = inputLenght; i < addLength; i++)
[5780]235      if (addString[i] != (*charIT).name[i])
[5185]236      {
237       addLength = i;
[5245]238//       break;
[5185]239      }
[5178]240  }
[5245]241  PRINT(0)("\n");
[5178]242
243  if (addLength >= inputLenght)
244  {
[7225]245    std::string adder = addString;
246    adder.resize(addLength);
[5178]247
[7343]248    input.resize(input.size()-inputLenght);
249    input += adder;
250
251    if (completionList.size() == 1)
[5182]252    {
[7343]253      if ( addBack != "")
254       input += addBack;
255      input += ' ';
[5182]256    }
[5178]257  }
258  return true;
259}
260
261/**
[5406]262 * @brief searches for classes, which beginn with completionBegin
[5178]263 * @param inputList the List to parse through
[5187]264 * @param completionBegin the beginning string
[5178]265 * !! The strings MUST NOT be deleted !!
266 */
[7371]267bool ShellCompletion::addToCompleteList(const std::list<std::string>& inputList, const std::string& completionBegin, SHELLC_TYPE type)
[5178]268{
[7225]269  unsigned int searchLength = completionBegin.size();
[5178]270
[7371]271  std::list<std::string>::const_iterator string;
272  for (string = inputList.begin(); string != inputList.end(); string++)
[5178]273  {
[7221]274    if ((*string).size() >= searchLength &&
[7371]275          !nocaseCmp(*string, completionBegin, searchLength))
[5178]276    {
[7371]277      printf ("%s\n", (*string).c_str());
[5780]278      ShellC_Element newElem;
[7371]279      newElem.name = (*string);
[5780]280      newElem.type = type;
281      this->completionList.push_back(newElem);
[5178]282    }
283  }
[5192]284  return true;
[5178]285}
286
287/**
[7343]288 * @brief searches for classes, which beginn with completionBegin
[5178]289 * @param inputList the List to parse through
[5187]290 * @param completionBegin the beginning string
[5178]291 * !! The strings MUST NOT be deleted !!
292 */
[7371]293bool ShellCompletion::addToCompleteList(const std::list<BaseObject*>& inputList, const std::string& completionBegin, SHELLC_TYPE type)
[5178]294{
[7225]295  unsigned int searchLength = completionBegin.size();
[5178]296
[7371]297  std::list<BaseObject*>::const_iterator bo;
298  for(bo = inputList.begin(); bo != inputList.end(); bo++)
[5178]299  {
[5779]300    if ((*bo)->getName() != NULL &&
301        strlen((*bo)->getName()) >= searchLength &&
[7371]302          !nocaseCmp((*bo)->getName(), completionBegin, searchLength))
[5178]303    {
[5780]304      ShellC_Element newElem;
305      newElem.name = (*bo)->getName();
306      newElem.type = type;
307      this->completionList.push_back(newElem);
[5178]308    }
309  }
310
[5192]311  return true;
[5178]312}
[5187]313
[5197]314/**
[7343]315 * @brief deletes the Completion List.
[5197]316 *
317 * This is done at the beginning of each completion-run
318 */
[7371]319void ShellCompletion::clearCompletionList()
[5187]320{
[5783]321  this->completionList.clear();
[5187]322}
[5245]323
324const char* ShellCompletion::typeToString(SHELLC_TYPE type)
325{
326  switch (type)
327  {
328    default:// SHELLC_NONE
329      return "error";
330    case  SHELLC_CLASS:
331      return "class";
332    case SHELLC_OBJECT:
333      return "object";
334    case SHELLC_FUNCTION:
335      return "function";
336    case SHELLC_ALIAS:
337      return "alias";
338  }
339}
Note: See TracBrowser for help on using the repository browser.