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 "base_object.h" |
---|
21 | #include "class_list.h" |
---|
22 | #include "list.h" |
---|
23 | #include "debug.h" |
---|
24 | |
---|
25 | #include "stdlibincl.h" |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | /** |
---|
30 | * standard constructor |
---|
31 | * @todo this constructor is not jet implemented - do it |
---|
32 | */ |
---|
33 | ShellCompletion::ShellCompletion () |
---|
34 | { |
---|
35 | // this->setClassID(CL_PROTO_ID, "ProtoClass"); |
---|
36 | |
---|
37 | /* If you make a new class, what is most probably the case when you write this file |
---|
38 | don't forget to: |
---|
39 | 1. Add the new file new_class.cc to the ./src/Makefile.am |
---|
40 | 2. Add the class identifier to ./src/class_id.h eg. CL_NEW_CLASS |
---|
41 | |
---|
42 | Advanced Topics: |
---|
43 | - if you want to let your object be managed via the ObjectManager make sure to read |
---|
44 | the object_manager.h header comments. You will use this most certanly only if you |
---|
45 | make many objects of your class, like a weapon bullet. |
---|
46 | */ |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | /** |
---|
51 | * standard deconstructor |
---|
52 | */ |
---|
53 | ShellCompletion::~ShellCompletion () |
---|
54 | { |
---|
55 | // delete what has to be deleted here |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | /** |
---|
61 | * autocompletes the Shell's inputLine |
---|
62 | * @returns true, if a result was found, false otherwise |
---|
63 | * |
---|
64 | * @todo implement it!! |
---|
65 | */ |
---|
66 | bool ShellCompletion::autoComplete(const char* inputLine) |
---|
67 | { |
---|
68 | //PRINTF(3)("AutoCompletion not implemented yet\n"); |
---|
69 | |
---|
70 | char* completionLine = new char[strlen(inputLine)+1]; |
---|
71 | strcpy(completionLine, inputLine); |
---|
72 | |
---|
73 | char* commandBegin = strrchr(completionLine, ' '); |
---|
74 | if (commandBegin == NULL) |
---|
75 | commandBegin = completionLine; |
---|
76 | else |
---|
77 | { |
---|
78 | if(commandBegin >= completionLine + strlen(completionLine)) |
---|
79 | commandBegin = completionLine + strlen(completionLine); |
---|
80 | else |
---|
81 | commandBegin++; |
---|
82 | } |
---|
83 | |
---|
84 | char* objectStart; |
---|
85 | if (objectStart = strstr(commandBegin, "::")) |
---|
86 | { |
---|
87 | char* classIdentity = new char[objectStart - commandBegin +1]; |
---|
88 | strncpy(classIdentity, commandBegin, objectStart - commandBegin); |
---|
89 | classIdentity[objectStart - commandBegin] = '\0'; |
---|
90 | this->objectComplete(objectStart+2, ClassList::StringToID(classIdentity)); |
---|
91 | delete[] classIdentity; |
---|
92 | } |
---|
93 | else |
---|
94 | this->classComplete(commandBegin); |
---|
95 | |
---|
96 | delete[] completionLine; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * autocompletes a className |
---|
101 | * @param classBegin the Beginning of a String to autoComplete |
---|
102 | * @return true on success, false otherwise |
---|
103 | */ |
---|
104 | bool ShellCompletion::classComplete(const char* classBegin) |
---|
105 | { |
---|
106 | if (unlikely(classBegin == NULL)) |
---|
107 | return false; |
---|
108 | const tList<const char>* clList = ClassList::getClassList(); |
---|
109 | if (clList != NULL) |
---|
110 | { |
---|
111 | const tList<const char>* classList = this->createCompleteList(clList, classBegin); |
---|
112 | if (classList != NULL) |
---|
113 | this->generalComplete(classList, classBegin, "%s::", "::"); |
---|
114 | else |
---|
115 | return false; |
---|
116 | } |
---|
117 | else |
---|
118 | return false; |
---|
119 | return true; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * autocompletes an ObjectName |
---|
124 | * @param objectBegin the beginning string of a Object |
---|
125 | * @param classID the ID of the Class to search for. |
---|
126 | * @return true on success, false otherwise |
---|
127 | */ |
---|
128 | bool ShellCompletion::objectComplete(const char* objectBegin, long classID) |
---|
129 | { |
---|
130 | printf("%s\n", objectBegin); |
---|
131 | |
---|
132 | if (unlikely(objectBegin == NULL)) |
---|
133 | return false; |
---|
134 | tList<BaseObject>* boList = ClassList::getList(classID); |
---|
135 | if (boList != NULL) |
---|
136 | { |
---|
137 | printf("\n", boList->firstElement()->getName()); |
---|
138 | const tList<const char>* objectList = this->createCompleteList(boList, objectBegin); |
---|
139 | if (objectList != NULL) |
---|
140 | this->generalComplete(objectList, objectBegin, "%s"); |
---|
141 | else |
---|
142 | return false; |
---|
143 | } |
---|
144 | else |
---|
145 | return false; |
---|
146 | return true; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * completes a Function |
---|
151 | * @param functionBegin the beginning of the function String |
---|
152 | */ |
---|
153 | bool ShellCompletion::functionComplete(const char* functionBegin) |
---|
154 | { |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * completes the inputline on grounds of an inputList |
---|
159 | * @param stringList the List to parse through |
---|
160 | * @param begin the String to search in the inputList, and to extend with it. |
---|
161 | * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::" |
---|
162 | * @param addBack what should be added at the end of the completion |
---|
163 | * @param addFront what should be added to the front of one finished completion |
---|
164 | * @return true if ok, false otherwise |
---|
165 | */ |
---|
166 | bool ShellCompletion::generalComplete(const tList<const char>* stringList, const char* begin, const char* displayAs, const char* addBack, const char* addFront) |
---|
167 | { |
---|
168 | if (stringList->getSize() == 0) |
---|
169 | return false; |
---|
170 | |
---|
171 | const char* addString = stringList->firstElement(); |
---|
172 | unsigned int addLength = 0; |
---|
173 | unsigned int inputLenght = strlen(begin); |
---|
174 | |
---|
175 | if (addString != NULL) |
---|
176 | addLength = strlen(addString); |
---|
177 | tIterator<const char>* charIterator = stringList->getIterator(); |
---|
178 | const char* charElem = charIterator->firstElement(); |
---|
179 | while (charElem != NULL) |
---|
180 | { |
---|
181 | PRINTF(0)(displayAs, charElem); |
---|
182 | for (unsigned int i = inputLenght; i < addLength; i++) |
---|
183 | if (addString[i] != charElem[i]) |
---|
184 | { |
---|
185 | addLength = i; |
---|
186 | break; |
---|
187 | } |
---|
188 | charElem = charIterator->nextElement(); |
---|
189 | } |
---|
190 | delete charIterator; |
---|
191 | |
---|
192 | if (addLength >= inputLenght) |
---|
193 | { |
---|
194 | char* adder = new char[addLength+1]; |
---|
195 | strncpy(adder, addString, addLength); |
---|
196 | adder[addLength] = '\0'; |
---|
197 | |
---|
198 | |
---|
199 | /* this->removeCharacters(inputLenght); |
---|
200 | this->addCharacters(adder); |
---|
201 | if (addBack != NULL && stringList->getSize() == 1) |
---|
202 | this->addCharacters("::"); |
---|
203 | delete[] adder;*/ |
---|
204 | } |
---|
205 | return true; |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * searches for classes, which beginn with classNameBegin |
---|
210 | * @param inputList the List to parse through |
---|
211 | * @param classNameBegin the beginning string |
---|
212 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
---|
213 | * !! The strings MUST NOT be deleted !! |
---|
214 | */ |
---|
215 | const tList<const char>* ShellCompletion::createCompleteList(const tList<const char>* inputList, const char* classNameBegin) |
---|
216 | { |
---|
217 | if (inputList == NULL || classNameBegin == NULL) |
---|
218 | return NULL; |
---|
219 | unsigned int searchLength = strlen(classNameBegin); |
---|
220 | if (this->completionList != NULL) |
---|
221 | delete this->completionList; |
---|
222 | this->completionList = new tList<const char>; |
---|
223 | |
---|
224 | // tList<const char>* classList = ClassList::getClassList(); |
---|
225 | |
---|
226 | tIterator<const char>* iterator = inputList->getIterator(); |
---|
227 | const char* enumString = iterator->firstElement(); |
---|
228 | while (enumString != NULL) |
---|
229 | { |
---|
230 | if (strlen(enumString)>searchLength+1 && |
---|
231 | !strncasecmp(enumString, classNameBegin, searchLength)) |
---|
232 | { |
---|
233 | this->completionList->add(enumString); |
---|
234 | } |
---|
235 | enumString = iterator->nextElement(); |
---|
236 | } |
---|
237 | delete iterator; |
---|
238 | |
---|
239 | return this->completionList; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * searches for classes, which beginn with classNameBegin |
---|
244 | * @param inputList the List to parse through |
---|
245 | * @param classNameBegin the beginning string |
---|
246 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
---|
247 | * !! The strings MUST NOT be deleted !! |
---|
248 | */ |
---|
249 | const tList<const char>* ShellCompletion::createCompleteList(const tList<BaseObject>* inputList, const char* classNameBegin) |
---|
250 | { |
---|
251 | if (inputList == NULL || classNameBegin == NULL) |
---|
252 | return NULL; |
---|
253 | unsigned int searchLength = strlen(classNameBegin); |
---|
254 | if (this->completionList != NULL) |
---|
255 | delete this->completionList; |
---|
256 | this->completionList = new tList<const char>; |
---|
257 | |
---|
258 | tIterator<BaseObject>* iterator = inputList->getIterator(); |
---|
259 | BaseObject* enumBO = iterator->firstElement(); |
---|
260 | while (enumBO != NULL) |
---|
261 | { |
---|
262 | if (enumBO->getName() != NULL && |
---|
263 | strlen(enumBO->getName())>searchLength+1 && |
---|
264 | !strncasecmp(enumBO->getName(), classNameBegin, searchLength)) |
---|
265 | { |
---|
266 | this->completionList->add(enumBO->getName()); |
---|
267 | } |
---|
268 | enumBO = iterator->nextElement(); |
---|
269 | } |
---|
270 | delete iterator; |
---|
271 | |
---|
272 | return this->completionList; |
---|
273 | } |
---|