Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: SubString now also can split by whiteSpaces :>
this cost me almost 2 hours… sometimes i think, that i am unable to think…. :/
other times it just works, like when learning some Information Transfer

File size: 7.9 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  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  if (input != NULL)
76    this->input = input;
77
78  SubString(input->getText(), true).debug();
79
80/*  completionLine = new char[strlen(this->input->getText())+1];
81  strcpy(completionLine, this->input->getText());
82
83  char* commandBegin = strrchr(completionLine, ' ');
84  if (commandBegin == NULL)
85    commandBegin = completionLine;
86  else
87  {
88    if(commandBegin >= completionLine + strlen(completionLine))
89      commandBegin = completionLine + strlen(completionLine);
90    else
91      commandBegin++;
92  }
93
94  char* objectStart;
95  if (objectStart = strstr(commandBegin, "::"))
96  {
97    char* classIdentity = new char[objectStart - commandBegin +1];
98    strncpy(classIdentity, commandBegin, objectStart - commandBegin);
99    classIdentity[objectStart - commandBegin] = '\0';
100    this->objectComplete(objectStart+2, ClassList::StringToID(classIdentity));
101    delete[] classIdentity;
102  }
103  else
104    this->classComplete(commandBegin);
105
106  delete[] completionLine;*/
107}
108
109/**
110 * autocompletes a className
111 * @param classBegin the Beginning of a String to autoComplete
112 * @return true on success, false otherwise
113 */
114bool ShellCompletion::classComplete(const char* classBegin)
115{
116  if (unlikely(classBegin == NULL))
117    return false;
118  const tList<const char>* clList = ClassList::getClassList();
119  if (clList != NULL)
120  {
121    const tList<const char>* classList = this->createCompleteList(clList, classBegin);
122    if (classList != NULL)
123      this->generalComplete(classList, classBegin, "%s::", "::");
124    else
125      return false;
126  }
127  else
128    return false;
129  return true;
130}
131
132/**
133 * autocompletes an ObjectName
134 * @param objectBegin the beginning string of a Object
135 * @param classID the ID of the Class to search for.
136 * @return true on success, false otherwise
137 */
138bool ShellCompletion::objectComplete(const char* objectBegin, long classID)
139{
140  printf("%s\n", objectBegin);
141
142  if (unlikely(objectBegin == NULL))
143    return false;
144  tList<BaseObject>* boList = ClassList::getList(classID);
145  if (boList != NULL)
146  {
147    printf("\n", boList->firstElement()->getName());
148    const tList<const char>* objectList = this->createCompleteList(boList, objectBegin);
149    if (objectList != NULL)
150      this->generalComplete(objectList, objectBegin, "%s");
151    else
152      return false;
153  }
154  else
155    return false;
156  return true;
157}
158
159/**
160 * completes a Function
161 * @param functionBegin the beginning of the function String
162 */
163bool ShellCompletion::functionComplete(const char* functionBegin)
164{
165}
166
167/**
168 * completes the inputline on grounds of an inputList
169 * @param stringList the List to parse through
170 * @param begin the String to search in the inputList, and to extend with it.
171 * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::"
172 * @param addBack what should be added at the end of the completion
173 * @param addFront what should be added to the front of one finished completion
174 * @return true if ok, false otherwise
175 */
176bool ShellCompletion::generalComplete(const tList<const char>* stringList, const char* begin, const char* displayAs, const char* addBack, const char* addFront)
177{
178  if (stringList->getSize() == 0)
179    return false;
180
181  const char* addString = stringList->firstElement();
182  unsigned int addLength = 0;
183  unsigned int inputLenght = strlen(begin);
184
185  if (addString != NULL)
186    addLength = strlen(addString);
187  tIterator<const char>* charIterator = stringList->getIterator();
188  const char* charElem = charIterator->firstElement();
189  while (charElem != NULL)
190  {
191    PRINTF(0)(displayAs, charElem);
192    for (unsigned int i = inputLenght; i < addLength; i++)
193      if (addString[i] != charElem[i])
194    {
195      addLength = i;
196      break;
197    }
198    charElem = charIterator->nextElement();
199  }
200  delete charIterator;
201
202  if (addLength >= inputLenght)
203  {
204    char* adder = new char[addLength+1];
205    strncpy(adder, addString, addLength);
206    adder[addLength] = '\0';
207
208    if (this->input)
209    {
210     this->input->removeCharacters(inputLenght);
211     this->input->addCharacters(adder);
212
213      if (addBack != NULL && stringList->getSize() == 1)
214       this->input->addCharacters("::");
215     delete[] adder;
216    }
217  }
218  return true;
219}
220
221/**
222 * searches for classes, which beginn with classNameBegin
223 * @param inputList the List to parse through
224 * @param classNameBegin the beginning string
225 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,
226 * !! The strings MUST NOT be deleted !!
227 */
228const tList<const char>* ShellCompletion::createCompleteList(const tList<const char>* inputList, const char* classNameBegin)
229{
230  if (inputList == NULL || classNameBegin == NULL)
231    return NULL;
232  unsigned int searchLength = strlen(classNameBegin);
233  if (this->completionList != NULL)
234    delete this->completionList;
235  this->completionList = new tList<const char>;
236
237//  tList<const char>* classList = ClassList::getClassList();
238
239  tIterator<const char>* iterator = inputList->getIterator();
240  const char* enumString = iterator->firstElement();
241  while (enumString != NULL)
242  {
243    if (strlen(enumString)>searchLength+1 &&
244        !strncasecmp(enumString, classNameBegin, searchLength))
245    {
246      this->completionList->add(enumString);
247    }
248    enumString = iterator->nextElement();
249  }
250  delete iterator;
251
252  return this->completionList;
253}
254
255/**
256 * searches for classes, which beginn with classNameBegin
257 * @param inputList the List to parse through
258 * @param classNameBegin the beginning string
259 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,
260 * !! The strings MUST NOT be deleted !!
261 */
262const tList<const char>* ShellCompletion::createCompleteList(const tList<BaseObject>* inputList, const char* classNameBegin)
263{
264  if (inputList == NULL || classNameBegin == NULL)
265    return NULL;
266  unsigned int searchLength = strlen(classNameBegin);
267  if (this->completionList != NULL)
268    delete this->completionList;
269  this->completionList = new tList<const char>;
270
271  tIterator<BaseObject>* iterator = inputList->getIterator();
272  BaseObject* enumBO = iterator->firstElement();
273  while (enumBO != NULL)
274  {
275    if (enumBO->getName() != NULL &&
276        strlen(enumBO->getName())>searchLength+1 &&
277        !strncasecmp(enumBO->getName(), classNameBegin, searchLength))
278    {
279      this->completionList->add(enumBO->getName());
280    }
281    enumBO = iterator->nextElement();
282  }
283  delete iterator;
284
285  return this->completionList;
286}
Note: See TracBrowser for help on using the repository browser.