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