Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/std/src/lib/shell/shell_completion.cc @ 7216

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

orxonox/std:: compile and run again, with many more std::strings….

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