Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


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 :)

Location:
trunk/src/lib/shell
Files:
5 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
  • trunk/src/lib/shell/shell.h

    r5178 r5179  
    1414// FORWARD DECLARATION
    1515class Text;
     16class ShellInput;
    1617class ShellCommandBase;
    1718template<class T> class tList;
     
    5152    void printToDisplayBuffer(const char* text);
    5253
    53     // InputLine
    54     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 
    6154    void clear();
    6255
     
    7366
    7467  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);
    8568
    8669    // helpers //
     
    9982
    10083    // HANDLING TEXT INPUT
    101     Text*                    inputLineText;          //!< The inputLine of the Shell
    102     char*                    inputLine;              //!< the Char-Array of the Buffer
     84    ShellInput*              shellInput;
     85
    10386    float                    repeatRate;             //!< The Repeat-Delay.
    10487    float                    repeatDelay;            //!< The delay of the first Character of a given Character.
  • trunk/src/lib/shell/shell_command.h

    r5171 r5179  
    2727 * @param class the name of the class to apply this command to (without the "" around the string)
    2828 * @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]
    2936 */
    3037#define SHELL_COMMAND(command, class, function) \
  • trunk/src/lib/shell/shell_input.cc

    r5178 r5179  
    1818#include "shell_input.h"
    1919
     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
    2028using namespace std;
    2129
     
    2735ShellInput::ShellInput ()
    2836{
     37  this->pressedKey = SDLK_FIRST;
    2938
     39  this->inputLine = new char[1];
     40  this->inputLine[0] = '\0';
     41  this->inputHistory = new tList<char>;
    3042}
    3143
     
    3749  // delete what has to be deleted here
    3850}
     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 */
     57void ShellInput::setRepeatDelay(float repeatDelay, float repeatRate)
     58{
     59  this->repeatDelay = repeatDelay;
     60  this->repeatRate = repeatRate;
     61
     62}
     63
     64/**
     65 * deletes the InputLine
     66 */
     67void 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 */
     82void 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 */
     96void 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 */
     110void 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 */
     131bool 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  
    77#define _SHELL_INPUT_H
    88
     9#include "text_engine.h"
     10
    911// FORWARD DECLARATION
    10 class Text;
    1112template<class T> class tList;
    1213
    1314
    1415//! A class for ...
    15 class ShellInput {
     16class ShellInput : public Text {
    1617
    1718 public:
     
    2627  void removeCharacters(unsigned int characterCount = 1);
    2728  void setRepeatDelay(float repeatDelay, float repeatRate);
     29  bool executeCommand();
     30  const char* getInputString() const { return this->inputLine; };
    2831
    2932
    3033 private:
    3134    // HANDLING TEXT INPUT
    32    Text*                    inputLineText;          //!< The inputLine of the Shell
    3335   char*                    inputLine;              //!< the Char-Array of the Buffer
    3436   float                    repeatRate;             //!< The Repeat-Delay.
Note: See TracChangeset for help on using the changeset viewer.