Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5179 in orxonox.OLD for trunk/src/lib/shell/shell.cc


Ignore:
Timestamp:
Sep 13, 2005, 11:23:34 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: ShellInput is now almost perfectly extern.
ShellCompletion taken out.
Working again :)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/shell/shell.cc

    r5177 r5179  
    1919#include "shell_command.h"
    2020#include "shell_buffer.h"
     21#include "shell_input.h"
    2122
    2223
     
    5657
    5758  // INPUT LINE
     59  this->shellInput = new ShellInput;
    5860  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);
    6662  //this->commandList = new tList<ShellCommand>;
    6763
     
    8985
    9086  // delete the inputLine
    91   delete this->inputLineText;
    92   delete this->inputLine;
    9387
    9488  Shell::singletonRef = NULL;
     
    155149void Shell::rebuildText()
    156150{
    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);
    165157
    166158  this->setBufferDisplaySize(this->bufferDisplaySize);
     
    240232
    241233/**
    242  * deletes the InputLine
    243  */
    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 inputLine
    257  * @param character the character to add to the inputLine
    258  */
    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 inputLine
    271  * @param characters a \\0 terminated char-array to add to the InputLine
    272  */
    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 InputLine
    285  * @param characterCount the count of Characters to remove from the input Line
    286  */
    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 inputLine
    306  * @return true if the command was commited successfully, false otherwise
    307  */
    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 /**
    324234 * clears the Shell (empties all buffers)
    325235 */
     
    330240}
    331241
    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
    343243
    344244/**
     
    363263      this->debug();
    364264    else if (event.type == SDLK_TAB)
    365       this->autoComplete();
     265      ;//this->autoComplete();
    366266    else if (event.type == SDLK_BACKSPACE)
    367267    {
    368268      this->delayed = this->repeatDelay;
    369269      this->pressedKey = SDLK_BACKSPACE;
    370       this->removeCharacters(1);
     270      this->shellInput->removeCharacters(1);
    371271    }
    372272    else if (event.type == SDLK_RETURN)
    373       this->executeCommand();
     273      this->shellInput->executeCommand();
    374274    /*
    375275    else if (event.type == SDLK_UP)
     
    397297      {
    398298        this->pressedKey = event.type-32;
    399         this->addCharacter(event.type-32);
     299        this->shellInput->addCharacter(event.type-32);
    400300      }
    401301      else
    402302      {
    403303        this->pressedKey = event.type;
    404         this->addCharacter(event.type);
     304        this->shellInput->addCharacter(event.type);
    405305      }
    406306    }
     
    428328    this->delayed = this->repeatRate;
    429329    if (this->pressedKey == SDLK_BACKSPACE)
    430       this->removeCharacters(1);
     330      this->shellInput->removeCharacters(1);
    431331    else if (pressedKey < 127)
    432       this->addCharacter(this->pressedKey);
     332      this->shellInput->addCharacter(this->pressedKey);
    433333  }
    434334}
     
    463363
    464364  glEnd();
    465 }
    466 
    467 
    468 /**
    469  * autocompletes the Shell's inputLine
    470  * @returns true, if a result was found, false otherwise
    471  *
    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   else
    485   {
    486     if(commandBegin >= completionLine + strlen(completionLine))
    487       commandBegin = completionLine + strlen(completionLine);
    488     else
    489       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   else
    502     this->classComplete(commandBegin);
    503 
    504   delete[] completionLine;
    505 }
    506 
    507 /**
    508  * autocompletes a className
    509  * @param classBegin the Beginning of a String to autoComplete
    510  * @return true on success, false otherwise
    511  */
    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     else
    523       return false;
    524   }
    525   else
    526     return false;
    527   return true;
    528 }
    529 
    530 /**
    531  * autocompletes an ObjectName
    532  * @param objectBegin the beginning string of a Object
    533  * @param classID the ID of the Class to search for.
    534  * @return true on success, false otherwise
    535  */
    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     else
    550       return false;
    551   }
    552   else
    553     return false;
    554   return true;
    555 }
    556 
    557 /**
    558  * completes a Function
    559  * @param functionBegin the beginning of the function String
    560  */
    561 bool Shell::functionComplete(const char* functionBegin)
    562 {
    563 }
    564 
    565 /**
    566  * completes the inputline on grounds of an inputList
    567  * @param stringList the List to parse through
    568  * @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 completion
    571  * @param addFront what should be added to the front of one finished completion
    572  * @return true if ok, false otherwise
    573  */
    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 classNameBegin
    616  * @param inputList the List to parse through
    617  * @param classNameBegin the beginning string
    618  * @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 classNameBegin
    650  * @param inputList the List to parse through
    651  * @param classNameBegin the beginning string
    652  * @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;
    679365}
    680366
Note: See TracChangeset for help on using the changeset viewer.