Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: half-faulty object Completion…. fixing

File size: 9.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: ...
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell_completion.h"
19
20#include "shell_input.h"
21
22#include "substring.h"
23#include "base_object.h"
24#include "class_list.h"
25#include "list.h"
26#include "debug.h"
27
28#include "stdlibincl.h"
29
30using namespace std;
31
32/**
33 * standard constructor
34 * @todo this constructor is not jet implemented - do it
35*/
36ShellCompletion::ShellCompletion(ShellInput* input)
37{
38  this->completionList = NULL;
39  this->input = input;
40}
41
42
43/**
44 * standard deconstructor
45*/
46ShellCompletion::~ShellCompletion ()
47{
48  // delete what has to be deleted here
49  if (this->completionList)
50  {
51    delete this->completionList;
52  }
53}
54
55
56
57/**
58 * autocompletes the Shell's inputLine
59 * @returns true, if a result was found, false otherwise
60 *
61 * @todo implement it!!
62 */
63bool ShellCompletion::autoComplete(ShellInput* input)
64{
65  const char* completionLine;
66
67  long classID;         //< the classID retrieved from the Class.
68  char* classBegin;     //< the beginn of the slass string
69  char* classEnd;       //< the end of the class string
70  char* objectBegin;    //< the begin of the object string
71  char* objectEnd;      //< the end of the object string
72  char* functionBegin;  //< the begin of the function string
73  char* functionEnd;    //< the end of the function string
74
75  PRINTF(4)("AutoComplete on input\n");
76
77  if (input != NULL)
78    this->input = input;
79  if (this->input == NULL)
80  {
81    PRINTF(2)("No ShellInput supplied\n");
82    return false;
83  }
84  if (this->input->getInput() == NULL)
85    return this->classComplete("");
86
87
88  completionLine = this->input->getInput() + strspn(this->input->getInput(), " \t\n");
89
90  SubString inputSplits(completionLine, true);
91
92
93  // CLASS COMPLETION
94  if (inputSplits.getCount() == 0)
95  {
96    PRINTF(5)("Listing all Classes\n");
97    this->classComplete("");
98    return false;
99  }
100  else if (inputSplits.getCount() == 1 && strlen(inputSplits.getString(0)) == strlen(completionLine))
101  {
102    printf("trying to complete a Class with %d\n", inputSplits.getString(0));
103    this->classComplete(inputSplits.getString(0));
104  }
105
106  // OBJECT COMPLETIONS
107  else if ( inputSplits.getCount() <= 2 && strlen(inputSplits.getString(0)) < strlen(completionLine))
108  {
109    classID = ClassList::StringToID(inputSplits.getString(0));
110    if (classID == CL_NULL)
111      return false;
112    if (inputSplits.getCount()==2)
113      this->objectComplete(inputSplits.getString(1), classID);
114    else
115      this->objectComplete("", classID);
116  }
117
118/*  completionLine = new char[strlen(this->input->getText())+1];
119  strcpy(completionLine, this->input->getText());
120
121  char* commandBegin = strrchr(completionLine, ' ');
122  if (commandBegin == NULL)
123    commandBegin = completionLine;
124  else
125  {
126    if(commandBegin >= completionLine + strlen(completionLine))
127      commandBegin = completionLine + strlen(completionLine);
128    else
129      commandBegin++;
130  }
131
132  char* objectStart;
133  if (objectStart = strstr(commandBegin, "::"))
134  {
135    char* classIdentity = new char[objectStart - commandBegin +1];
136    strncpy(classIdentity, commandBegin, objectStart - commandBegin);
137    classIdentity[objectStart - commandBegin] = '\0';
138    this->objectComplete(objectStart+2, ClassList::StringToID(classIdentity));
139    delete[] classIdentity;
140  }
141  else
142    this->classComplete(commandBegin);
143
144  delete[] completionLine;*/
145}
146
147/**
148 * autocompletes a className
149 * @param classBegin the Beginning of a String to autoComplete
150 * @return true on success, false otherwise
151 */
152bool ShellCompletion::classComplete(const char* classBegin)
153{
154  if (unlikely(classBegin == NULL))
155    return false;
156  const tList<const char>* clList = ClassList::getClassList();
157  if (clList != NULL)
158  {
159    const tList<const char>* classList = this->createCompleteList(clList, classBegin);
160    if (classList != NULL)
161      this->generalComplete(classList, classBegin, "CL: %s ");
162    else
163      return false;
164  }
165  else
166    return false;
167  return true;
168}
169
170/**
171 * autocompletes an ObjectName
172 * @param objectBegin the beginning string of a Object
173 * @param classID the ID of the Class to search for.
174 * @return true on success, false otherwise
175 */
176bool ShellCompletion::objectComplete(const char* objectBegin, long classID)
177{
178  printf("%s\n", objectBegin);
179
180  if (unlikely(objectBegin == NULL))
181    return false;
182  tList<BaseObject>* boList = ClassList::getList(classID);
183  if (boList != NULL)
184  {
185    printf("\n", boList->firstElement()->getName());
186    const tList<const char>* objectList = this->createCompleteList(boList, objectBegin);
187    if (objectList != NULL)
188      this->generalComplete(objectList, objectBegin, "%s", " ");
189    else
190      return false;
191  }
192  else
193    return false;
194  return true;
195}
196
197/**
198 * completes a Function
199 * @param functionBegin the beginning of the function String
200 */
201bool ShellCompletion::functionComplete(const char* functionBegin)
202{
203}
204
205/**
206 * completes the inputline on grounds of an inputList
207 * @param stringList the List to parse through
208 * @param begin the String to search in the inputList, and to extend with it.
209 * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::"
210 * @param addBack what should be added at the end of the completion
211 * @param addFront what should be added to the front of one finished completion
212 * @return true if ok, false otherwise
213 */
214bool ShellCompletion::generalComplete(const tList<const char>* stringList, const char* begin, const char* displayAs, const char* addBack, const char* addFront)
215{
216  if (stringList == NULL || this->input == NULL )
217    return false;
218  if (stringList->getSize() == 0)
219    return false;
220
221  const char* addString = stringList->firstElement();
222  unsigned int addLength = 0;
223  unsigned int inputLenght = strlen(begin);
224
225  if (addString != NULL)
226    addLength = strlen(addString);
227  tIterator<const char>* charIterator = stringList->getIterator();
228  const char* charElem = charIterator->firstElement();
229  while (charElem != NULL)
230  {
231    PRINTF(0)(displayAs, charElem);
232    for (unsigned int i = inputLenght; i < addLength; i++)
233      if (addString[i] != charElem[i])
234      {
235       addLength = i;
236       break;
237      }
238    charElem = charIterator->nextElement();
239  }
240  delete charIterator;
241
242  if (addLength >= inputLenght)
243  {
244    char* adder = new char[addLength+1];
245    strncpy(adder, addString, addLength);
246    adder[addLength] = '\0';
247
248    if (this->input)
249    {
250     this->input->removeCharacters(inputLenght);
251     this->input->addCharacters(adder);
252
253      if (stringList->getSize() == 1)
254      {
255        if ( addBack != NULL )
256         this->input->addCharacters(addBack);
257        this->input->addCharacter(' ');
258      }
259     delete[] adder;
260    }
261  }
262  return true;
263}
264
265/**
266 * searches for classes, which beginn with classNameBegin
267 * @param inputList the List to parse through
268 * @param classNameBegin the beginning string
269 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,
270 * !! The strings MUST NOT be deleted !!
271 */
272const tList<const char>* ShellCompletion::createCompleteList(const tList<const char>* inputList, const char* classNameBegin)
273{
274  if (inputList == NULL || classNameBegin == NULL)
275    return NULL;
276  unsigned int searchLength = strlen(classNameBegin);
277  if (this->completionList != NULL)
278    delete this->completionList;
279  this->completionList = new tList<const char>;
280
281//  tList<const char>* classList = ClassList::getClassList();
282
283  tIterator<const char>* iterator = inputList->getIterator();
284  const char* enumString = iterator->firstElement();
285  while (enumString != NULL)
286  {
287    if (strlen(enumString) >= searchLength &&
288        !strncasecmp(enumString, classNameBegin, searchLength))
289    {
290      printf("%s\n", enumString);
291      this->completionList->add(enumString);
292    }
293    enumString = iterator->nextElement();
294  }
295  delete iterator;
296
297  return this->completionList;
298}
299
300/**
301 * searches for classes, which beginn with classNameBegin
302 * @param inputList the List to parse through
303 * @param classNameBegin the beginning string
304 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,
305 * !! The strings MUST NOT be deleted !!
306 */
307const tList<const char>* ShellCompletion::createCompleteList(const tList<BaseObject>* inputList, const char* classNameBegin)
308{
309  if (inputList == NULL || classNameBegin == NULL)
310    return NULL;
311  unsigned int searchLength = strlen(classNameBegin);
312  if (this->completionList != NULL)
313    delete this->completionList;
314  this->completionList = new tList<const char>;
315
316  tIterator<BaseObject>* iterator = inputList->getIterator();
317  BaseObject* enumBO = iterator->firstElement();
318  while (enumBO != NULL)
319  {
320    if (enumBO->getName() != NULL &&
321        strlen(enumBO->getName()) >= searchLength &&
322        !strncasecmp(enumBO->getName(), classNameBegin, searchLength))
323    {
324      this->completionList->add(enumBO->getName());
325    }
326    enumBO = iterator->nextElement();
327  }
328  delete iterator;
329
330  return this->completionList;
331}
Note: See TracBrowser for help on using the repository browser.