Changeset 5179 in orxonox.OLD for trunk/src/lib/shell
- Timestamp:
- Sep 13, 2005, 11:23:34 PM (19 years ago)
- Location:
- trunk/src/lib/shell
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/shell/shell.cc
r5177 r5179 19 19 #include "shell_command.h" 20 20 #include "shell_buffer.h" 21 #include "shell_input.h" 21 22 22 23 … … 56 57 57 58 // INPUT LINE 59 this->shellInput = new ShellInput; 58 60 this->delayed = 0; 59 this->setRepeatDelay(.3, .05); 60 this->pressedKey = SDLK_FIRST; 61 62 this->inputLineText = NULL; 63 this->inputLine = new char[1]; 64 this->inputLine[0] = '\0'; 65 this->inputHistory = new tList<char>; 61 this->shellInput->setRepeatDelay(.3, .05); 66 62 //this->commandList = new tList<ShellCommand>; 67 63 … … 89 85 90 86 // delete the inputLine 91 delete this->inputLineText;92 delete this->inputLine;93 87 94 88 Shell::singletonRef = NULL; … … 155 149 void Shell::rebuildText() 156 150 { 157 if (this->inputLineText == NULL) 158 delete this->inputLineText; 159 this->inputLineText = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); 160 this->inputLineText->setColor(1, 0, 0); 161 this->inputLineText->setAlignment(TEXT_ALIGN_LEFT); 162 this->inputLineText->setText(NULL); 163 this->inputLineText->setParent2D(this); 164 this->inputLineText->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); 151 this->shellInput->setFont("fonts/Aniron_Bold.ttf", this->textSize); 152 this->shellInput->setColor(1, 0, 0); 153 this->shellInput->setAlignment(TEXT_ALIGN_LEFT); 154 this->shellInput->setText(NULL); 155 this->shellInput->setParent2D(this); 156 this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); 165 157 166 158 this->setBufferDisplaySize(this->bufferDisplaySize); … … 240 232 241 233 /** 242 * deletes the InputLine243 */244 void Shell::flushInputLine()245 {246 if (likely(this->inputLine != NULL))247 {248 delete[] this->inputLine;249 }250 this->inputLine = new char[1];251 *this->inputLine = '\0';252 this->inputLineText->setText(this->inputLine, true);253 }254 255 /**256 * adds one character to the inputLine257 * @param character the character to add to the inputLine258 */259 void Shell::addCharacter(char character)260 {261 char* addCharLine = new char[strlen(inputLine)+2];262 263 sprintf(addCharLine, "%s%c", this->inputLine, character);264 delete this->inputLine;265 this->inputLine = addCharLine;266 this->inputLineText->setText(inputLine, true);267 }268 269 /**270 * adds multiple Characters to thr inputLine271 * @param characters a \\0 terminated char-array to add to the InputLine272 */273 void Shell::addCharacters(const char* characters)274 {275 char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1];276 277 sprintf(addCharLine, "%s%s", this->inputLine, characters);278 delete this->inputLine;279 this->inputLine = addCharLine;280 this->inputLineText->setText(inputLine, true);281 }282 283 /**284 * removes characterCount characters from the InputLine285 * @param characterCount the count of Characters to remove from the input Line286 */287 void Shell::removeCharacters(unsigned int characterCount)288 {289 if (strlen(this->inputLine) == 0)290 return;291 292 if (characterCount > strlen(this->inputLine))293 characterCount = strlen(this->inputLine);294 295 char* removeCharLine = new char[strlen(inputLine)-characterCount+1];296 297 strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount);298 removeCharLine[strlen(inputLine)-characterCount] = '\0';299 delete this->inputLine;300 this->inputLine = removeCharLine;301 this->inputLineText->setText(inputLine, true);302 }303 304 /**305 * executes the command stored in the inputLine306 * @return true if the command was commited successfully, false otherwise307 */308 bool Shell::executeCommand()309 {310 ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine);311 312 char* newCommand = new char[strlen(this->inputLine)+1];313 strcpy(newCommand, this->inputLine);314 this->inputHistory->add(newCommand);315 316 ShellCommandBase::execute(this->inputLine);317 318 this->flushInputLine();319 320 return false;321 }322 323 /**324 234 * clears the Shell (empties all buffers) 325 235 */ … … 330 240 } 331 241 332 /** 333 * sets the Repeate-delay and rate 334 * @param repeatDelay the Delay it takes, to repeate a key 335 * @param repeatRate the rate to repeate a pressed key 336 */ 337 void Shell::setRepeatDelay(float repeatDelay, float repeatRate) 338 { 339 this->repeatDelay = repeatDelay; 340 this->repeatRate = repeatRate; 341 342 } 242 343 243 344 244 /** … … 363 263 this->debug(); 364 264 else if (event.type == SDLK_TAB) 365 this->autoComplete();265 ;//this->autoComplete(); 366 266 else if (event.type == SDLK_BACKSPACE) 367 267 { 368 268 this->delayed = this->repeatDelay; 369 269 this->pressedKey = SDLK_BACKSPACE; 370 this-> removeCharacters(1);270 this->shellInput->removeCharacters(1); 371 271 } 372 272 else if (event.type == SDLK_RETURN) 373 this-> executeCommand();273 this->shellInput->executeCommand(); 374 274 /* 375 275 else if (event.type == SDLK_UP) … … 397 297 { 398 298 this->pressedKey = event.type-32; 399 this-> addCharacter(event.type-32);299 this->shellInput->addCharacter(event.type-32); 400 300 } 401 301 else 402 302 { 403 303 this->pressedKey = event.type; 404 this-> addCharacter(event.type);304 this->shellInput->addCharacter(event.type); 405 305 } 406 306 } … … 428 328 this->delayed = this->repeatRate; 429 329 if (this->pressedKey == SDLK_BACKSPACE) 430 this-> removeCharacters(1);330 this->shellInput->removeCharacters(1); 431 331 else if (pressedKey < 127) 432 this-> addCharacter(this->pressedKey);332 this->shellInput->addCharacter(this->pressedKey); 433 333 } 434 334 } … … 463 363 464 364 glEnd(); 465 }466 467 468 /**469 * autocompletes the Shell's inputLine470 * @returns true, if a result was found, false otherwise471 *472 * @todo implement it!!473 */474 bool Shell::autoComplete()475 {476 //PRINTF(3)("AutoCompletion not implemented yet\n");477 478 char* completionLine = new char[strlen(inputLine)+1];479 strcpy(completionLine, this->inputLine);480 481 char* commandBegin = strrchr(completionLine, ' ');482 if (commandBegin == NULL)483 commandBegin = completionLine;484 else485 {486 if(commandBegin >= completionLine + strlen(completionLine))487 commandBegin = completionLine + strlen(completionLine);488 else489 commandBegin++;490 }491 492 char* objectStart;493 if (objectStart = strstr(commandBegin, "::"))494 {495 char* classIdentity = new char[objectStart - commandBegin +1];496 strncpy(classIdentity, commandBegin, objectStart - commandBegin);497 classIdentity[objectStart - commandBegin] = '\0';498 this->objectComplete(objectStart+2, ClassList::StringToID(classIdentity));499 delete[] classIdentity;500 }501 else502 this->classComplete(commandBegin);503 504 delete[] completionLine;505 }506 507 /**508 * autocompletes a className509 * @param classBegin the Beginning of a String to autoComplete510 * @return true on success, false otherwise511 */512 bool Shell::classComplete(const char* classBegin)513 {514 if (unlikely(classBegin == NULL))515 return false;516 const tList<const char>* clList = ClassList::getClassList();517 if (clList != NULL)518 {519 const tList<const char>* classList = this->createCompleteList(clList, classBegin);520 if (classList != NULL)521 this->generalComplete(classList, classBegin, "%s::", "::");522 else523 return false;524 }525 else526 return false;527 return true;528 }529 530 /**531 * autocompletes an ObjectName532 * @param objectBegin the beginning string of a Object533 * @param classID the ID of the Class to search for.534 * @return true on success, false otherwise535 */536 bool Shell::objectComplete(const char* objectBegin, long classID)537 {538 printf("%s\n", objectBegin);539 540 if (unlikely(objectBegin == NULL))541 return false;542 tList<BaseObject>* boList = ClassList::getList(classID);543 if (boList != NULL)544 {545 printf("\n", boList->firstElement()->getName());546 const tList<const char>* objectList = this->createCompleteList(boList, objectBegin);547 if (objectList != NULL)548 this->generalComplete(objectList, objectBegin, "%s");549 else550 return false;551 }552 else553 return false;554 return true;555 }556 557 /**558 * completes a Function559 * @param functionBegin the beginning of the function String560 */561 bool Shell::functionComplete(const char* functionBegin)562 {563 }564 565 /**566 * completes the inputline on grounds of an inputList567 * @param stringList the List to parse through568 * @param begin the String to search in the inputList, and to extend with it.569 * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::"570 * @param addBack what should be added at the end of the completion571 * @param addFront what should be added to the front of one finished completion572 * @return true if ok, false otherwise573 */574 bool Shell::generalComplete(const tList<const char>* stringList, const char* begin, const char* displayAs, const char* addBack, const char* addFront)575 {576 if (stringList->getSize() == 0)577 return false;578 579 const char* addString = stringList->firstElement();580 unsigned int addLength = 0;581 unsigned int inputLenght = strlen(begin);582 583 if (addString != NULL)584 addLength = strlen(addString);585 tIterator<const char>* charIterator = stringList->getIterator();586 const char* charElem = charIterator->firstElement();587 while (charElem != NULL)588 {589 PRINTF(0)(displayAs, charElem);590 for (unsigned int i = inputLenght; i < addLength; i++)591 if (addString[i] != charElem[i])592 {593 addLength = i;594 break;595 }596 charElem = charIterator->nextElement();597 }598 delete charIterator;599 600 if (addLength >= inputLenght)601 {602 char* adder = new char[addLength+1];603 strncpy(adder, addString, addLength);604 adder[addLength] = '\0';605 this->removeCharacters(inputLenght);606 this->addCharacters(adder);607 if (addBack != NULL && stringList->getSize() == 1)608 this->addCharacters("::");609 delete[] adder;610 }611 return true;612 }613 614 /**615 * searches for classes, which beginn with classNameBegin616 * @param inputList the List to parse through617 * @param classNameBegin the beginning string618 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,619 * !! The strings MUST NOT be deleted !!620 */621 const tList<const char>* Shell::createCompleteList(const tList<const char>* inputList, const char* classNameBegin)622 {623 if (inputList == NULL || classNameBegin == NULL)624 return NULL;625 unsigned int searchLength = strlen(classNameBegin);626 if (this->completionList != NULL)627 delete this->completionList;628 this->completionList = new tList<const char>;629 630 // tList<const char>* classList = ClassList::getClassList();631 632 tIterator<const char>* iterator = inputList->getIterator();633 const char* enumString = iterator->firstElement();634 while (enumString != NULL)635 {636 if (strlen(enumString)>searchLength+1 &&637 !strncasecmp(enumString, classNameBegin, searchLength))638 {639 this->completionList->add(enumString);640 }641 enumString = iterator->nextElement();642 }643 delete iterator;644 645 return this->completionList;646 }647 648 /**649 * searches for classes, which beginn with classNameBegin650 * @param inputList the List to parse through651 * @param classNameBegin the beginning string652 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,653 * !! The strings MUST NOT be deleted !!654 */655 const tList<const char>* Shell::createCompleteList(const tList<BaseObject>* inputList, const char* classNameBegin)656 {657 if (inputList == NULL || classNameBegin == NULL)658 return NULL;659 unsigned int searchLength = strlen(classNameBegin);660 if (this->completionList != NULL)661 delete this->completionList;662 this->completionList = new tList<const char>;663 664 tIterator<BaseObject>* iterator = inputList->getIterator();665 BaseObject* enumBO = iterator->firstElement();666 while (enumBO != NULL)667 {668 if (enumBO->getName() != NULL &&669 strlen(enumBO->getName())>searchLength+1 &&670 !strncasecmp(enumBO->getName(), classNameBegin, searchLength))671 {672 this->completionList->add(enumBO->getName());673 }674 enumBO = iterator->nextElement();675 }676 delete iterator;677 678 return this->completionList;679 365 } 680 366 -
trunk/src/lib/shell/shell.h
r5178 r5179 14 14 // FORWARD DECLARATION 15 15 class Text; 16 class ShellInput; 16 17 class ShellCommandBase; 17 18 template<class T> class tList; … … 51 52 void printToDisplayBuffer(const char* text); 52 53 53 // InputLine54 void flushInputLine();55 void addCharacter(char character);56 void addCharacters(const char* characters);57 void removeCharacters(unsigned int characterCount = 1);58 void setRepeatDelay(float repeatDelay, float repeatRate);59 bool executeCommand();60 61 54 void clear(); 62 55 … … 73 66 74 67 private: 75 bool autoComplete();76 bool classComplete(const char* classBegin);77 bool objectComplete(const char* objectBegin, long classID);78 bool functionComplete(const char* functionBegin);79 80 bool generalComplete(const tList<const char>* stringList, const char* begin, const char* displayAs = "%s", const char* addBack = NULL, const char* addFront = NULL);81 82 const tList<const char>* createCompleteList(const tList<const char>* inputList, const char* classNameBegin);83 const tList<const char>* createCompleteList(const tList<BaseObject>* inputList, const char* classNameBegin);84 // const tList<const char>* createCompleteList(const tList<ShellCommandBase>* inputList, const char* classNameBegin);85 68 86 69 // helpers // … … 99 82 100 83 // HANDLING TEXT INPUT 101 Text* inputLineText; //!< The inputLine of the Shell102 char* inputLine; //!< the Char-Array of the Buffer 84 ShellInput* shellInput; 85 103 86 float repeatRate; //!< The Repeat-Delay. 104 87 float repeatDelay; //!< The delay of the first Character of a given Character. -
trunk/src/lib/shell/shell_command.h
r5171 r5179 27 27 * @param class the name of the class to apply this command to (without the "" around the string) 28 28 * @param function the function to call 29 * 30 * MEANING: 31 * ShellCommandBase* someUniqueVarName = 32 * ShellCommand<ClassName>::registerCommand("commandNameInShell", "ClassName", &ClassName::FunctionToCall); 33 * 34 * In the Shell you would call this Command using: 35 * $ ClassName [ObjectName] commandNameInShell [parameters] 29 36 */ 30 37 #define SHELL_COMMAND(command, class, function) \ -
trunk/src/lib/shell/shell_input.cc
r5178 r5179 18 18 #include "shell_input.h" 19 19 20 #include "text_engine.h" 21 22 #include "shell_command.h" 23 #include "debug.h" 24 #include "list.h" 25 #include "compiler.h" 26 #include "stdlibincl.h" 27 20 28 using namespace std; 21 29 … … 27 35 ShellInput::ShellInput () 28 36 { 37 this->pressedKey = SDLK_FIRST; 29 38 39 this->inputLine = new char[1]; 40 this->inputLine[0] = '\0'; 41 this->inputHistory = new tList<char>; 30 42 } 31 43 … … 37 49 // delete what has to be deleted here 38 50 } 51 52 /** 53 * sets the Repeate-delay and rate 54 * @param repeatDelay the Delay it takes, to repeate a key 55 * @param repeatRate the rate to repeate a pressed key 56 */ 57 void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate) 58 { 59 this->repeatDelay = repeatDelay; 60 this->repeatRate = repeatRate; 61 62 } 63 64 /** 65 * deletes the InputLine 66 */ 67 void ShellInput::flush() 68 { 69 if (likely(this->inputLine != NULL)) 70 { 71 delete[] this->inputLine; 72 } 73 this->inputLine = new char[1]; 74 *this->inputLine = '\0'; 75 this->setText(this->inputLine, true); 76 } 77 78 /** 79 * adds one character to the inputLine 80 * @param character the character to add to the inputLine 81 */ 82 void ShellInput::addCharacter(char character) 83 { 84 char* addCharLine = new char[strlen(inputLine)+2]; 85 86 sprintf(addCharLine, "%s%c", this->inputLine, character); 87 delete this->inputLine; 88 this->inputLine = addCharLine; 89 this->setText(inputLine, true); 90 } 91 92 /** 93 * adds multiple Characters to thr inputLine 94 * @param characters a \\0 terminated char-array to add to the InputLine 95 */ 96 void ShellInput::addCharacters(const char* characters) 97 { 98 char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1]; 99 100 sprintf(addCharLine, "%s%s", this->inputLine, characters); 101 delete this->inputLine; 102 this->inputLine = addCharLine; 103 this->setText(inputLine, true); 104 } 105 106 /** 107 * removes characterCount characters from the InputLine 108 * @param characterCount the count of Characters to remove from the input Line 109 */ 110 void ShellInput::removeCharacters(unsigned int characterCount) 111 { 112 if (strlen(this->inputLine) == 0) 113 return; 114 115 if (characterCount > strlen(this->inputLine)) 116 characterCount = strlen(this->inputLine); 117 118 char* removeCharLine = new char[strlen(inputLine)-characterCount+1]; 119 120 strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount); 121 removeCharLine[strlen(inputLine)-characterCount] = '\0'; 122 delete this->inputLine; 123 this->inputLine = removeCharLine; 124 this->setText(inputLine, true); 125 } 126 127 /** 128 * executes the command stored in the inputLine 129 * @return true if the command was commited successfully, false otherwise 130 */ 131 bool ShellInput::executeCommand() 132 { 133 ShellBuffer::addBufferLineStatic("Execute Command: %s\n", this->inputLine); 134 135 char* newCommand = new char[strlen(this->inputLine)+1]; 136 strcpy(newCommand, this->inputLine); 137 this->inputHistory->add(newCommand); 138 139 ShellCommandBase::execute(this->inputLine); 140 141 this->flush(); 142 143 return false; 144 } 145 -
trunk/src/lib/shell/shell_input.h
r5178 r5179 7 7 #define _SHELL_INPUT_H 8 8 9 #include "text_engine.h" 10 9 11 // FORWARD DECLARATION 10 class Text;11 12 template<class T> class tList; 12 13 13 14 14 15 //! A class for ... 15 class ShellInput {16 class ShellInput : public Text { 16 17 17 18 public: … … 26 27 void removeCharacters(unsigned int characterCount = 1); 27 28 void setRepeatDelay(float repeatDelay, float repeatRate); 29 bool executeCommand(); 30 const char* getInputString() const { return this->inputLine; }; 28 31 29 32 30 33 private: 31 34 // HANDLING TEXT INPUT 32 Text* inputLineText; //!< The inputLine of the Shell33 35 char* inputLine; //!< the Char-Array of the Buffer 34 36 float repeatRate; //!< The Repeat-Delay.
Note: See TracChangeset
for help on using the changeset viewer.