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