/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "shell.h" #include "text_engine.h" #include "list.h" #include #include using namespace std; /** * standard constructor */ Shell::Shell () { this->setClassID(CL_SHELL, "Shell"); this->setName("Shell"); this->bufferText = new tList; this->buffer = new tList; //this->bufferSize = 0; //this->bufferDisplaySize = 0; this->setBufferSize(100); this->setBufferDisplaySize(1); // this->inputLineText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 0, 255, 0); // this->inputLineText->setText(NULL); //this->addBufferLineStatic("asjflksjdvklasmv %s", "doom"); //TextEngine::getInstance()->debug(); //exit(-1); } Shell* Shell::singletonRef = NULL; /** * standard deconstructor */ Shell::~Shell () { // delete what has to be deleted here Shell::singletonRef = NULL; } /** * sets The count of Lines to display in the buffer. * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. */ void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) { this->bufferDisplaySize = bufferDisplaySize; while (bufferDisplaySize < this->bufferText->getSize()) { delete this->bufferText->lastElement(); this->bufferText->removeLast(); } while(bufferDisplaySize > this->bufferText->getSize()) { Text* newText = TextEngine::getInstance()->createText("fonts/earth.ttf", 10, TEXT_DYNAMIC, 0, 255, 0); newText->setAlignment(TEXT_ALIGN_LEFT); newText->setPosition2D(5, 500); newText->setText(""); this->bufferText->add(newText); } } /** * deletes all the Buffers */ void Shell::flushBuffers() { // remove all chars from the BufferTexts. tIterator* textIterator = this->bufferText->getIterator(); Text* textElem = textIterator->nextElement(); while (textElem != NULL) { textElem->setText(NULL); textElem = textIterator->nextElement(); } delete textIterator; // delete all the Chars in the Buffers tIterator* charIterator = this->buffer->getIterator(); char* charElem = charIterator->nextElement(); while (charElem != NULL) { delete charElem; charElem = charIterator->nextElement(); } delete charIterator; } /** * adds a new Line to the List of Buffers * @param line the Line as in the first argument in printf * @param args the arguments as a va_list * * @todo optimize */ bool Shell::addBufferLineStatic(const char* line, ...) { va_list arguments; va_start(arguments, line); if (Shell::singletonRef == NULL) vprintf(line, arguments); else Shell::singletonRef->addBufferLine(line, arguments); return true; } void Shell::addBufferLine(const char* line, va_list arguments) { vsprintf(this->bufferArray, line, arguments); char* newLine = new char[strlen(this->bufferArray)+1]; strcpy(newLine, this->bufferArray); // this->buffer->add(newLine); // // if (this->buffer->getSize() > this->bufferSize) // { // delete this->buffer->firstElement(); // this->buffer->remove(this->buffer->firstElement()); // } this->bufferText->firstElement()->setText(newLine); // this->inputLineText->setText(line); } /** * moves the buffer around lineCount lines upwards (negative values move down) * @param lineCount the Count of lines to move upwards * * @todo */ void Shell::moveBuffer(int lineCount) { } /** * @param lineNumber the n-th line from the bottom * @returns the Buffer at Line lineNumber */ const char* Shell::getBufferLine(unsigned int lineNumber) { tIterator* charIterator = this->buffer->getIterator(); char* charElem = charIterator->nextElement(); int i = 1; while (charElem != NULL) { if (i++ < lineNumber) { delete charIterator; return charElem; } charElem = charIterator->nextElement(); } delete charIterator; } /** * deletes the InputLine */ void Shell::flushInputLine() { if (likely(this->inputLine != NULL)) { delete [] this->inputLine; } this->inputLine = new char[1]; *this->inputLine = '\0'; } /** * adds one character to the inputLine * @param character the character to add to the inputLine */ void Shell::addCharacter(char character) { char* addCharLine = new char[strlen(inputLine)+2]; sprintf(addCharLine, "%s%c", this->inputLine, character); delete this->inputLine; this->inputLine = addCharLine; } /** * adds multiple Characters to thr inputLine * @param characters a '\0' terminated char-array to add to the InputLine */ void Shell::addCharacters(const char* characters) { char* addCharLine = new char[strlen(inputLine)+strlen(characters)+1]; sprintf(addCharLine, "%s%s", this->inputLine, characters); delete this->inputLine; this->inputLine = addCharLine; } /** * removes characterCount characters from the InputLine * @param characterCount the count of Characters to remove from the input Line */ void Shell::removeCharacters(unsigned int characterCount) { if (characterCount > strlen(this->inputLine)) characterCount = strlen(this->inputLine); char* removeCharLine = new char[strlen(inputLine)-characterCount+1]; strncpy(removeCharLine, this->inputLine, strlen(inputLine)-characterCount); delete this->inputLine; this->inputLine = removeCharLine; } /** * listens for some event * @param event the Event happened */ void Shell::process(const Event &event) { // if (event.type) } /** * ticks the Shell for dt Seconds * @param dt the elapsed time since the last tick(); */ //void Shell::tick(float dt) //{ //} /** * displays the Shell */ void Shell::draw() const { } /** * autocompletes the Shell's inputLine * @returns true, if a result was found, false otherwise */ bool Shell::autoComplete() { } /** * displays some nice output from the Shell */ void Shell::debug() const { }