Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7264 was 7225, checked in by bensch, 19 years ago

orxonox/trunk: more std::string

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