[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 | |
---|
[5173] | 18 | #include "shell_buffer.h" |
---|
[5174] | 19 | #include "debug.h" |
---|
| 20 | #include "list.h" |
---|
[5175] | 21 | #include "shell.h" |
---|
[1853] | 22 | |
---|
[5174] | 23 | #include "stdlibincl.h" |
---|
| 24 | |
---|
[1856] | 25 | using namespace std; |
---|
[1853] | 26 | |
---|
[1856] | 27 | |
---|
[3245] | 28 | /** |
---|
[4838] | 29 | * standard constructor |
---|
| 30 | * @todo this constructor is not jet implemented - do it |
---|
[3245] | 31 | */ |
---|
[5173] | 32 | ShellBuffer::ShellBuffer () |
---|
[3365] | 33 | { |
---|
[5175] | 34 | ShellBuffer::singletonRef = this; |
---|
[5206] | 35 | this->shell = NULL; |
---|
[5175] | 36 | |
---|
| 37 | this->lineCount = 0; |
---|
[5174] | 38 | this->keepBufferArray[0] = '\0'; |
---|
| 39 | this->keepBuffer = false; |
---|
| 40 | this->buffer = new tList<char>; |
---|
[5175] | 41 | this->bufferIterator = this->buffer->getIterator(); |
---|
[4320] | 42 | |
---|
[5177] | 43 | this->setBufferSize(100); |
---|
[3365] | 44 | } |
---|
[1853] | 45 | |
---|
[5174] | 46 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
---|
[1853] | 47 | |
---|
[3245] | 48 | /** |
---|
[4838] | 49 | * standard deconstructor |
---|
[3245] | 50 | */ |
---|
[5173] | 51 | ShellBuffer::~ShellBuffer () |
---|
[3543] | 52 | { |
---|
[5206] | 53 | if (this->shell != NULL) |
---|
| 54 | delete this->shell; |
---|
[5174] | 55 | |
---|
[5175] | 56 | this->flushBuffers(); |
---|
| 57 | delete bufferIterator; |
---|
[5174] | 58 | delete buffer; |
---|
| 59 | |
---|
| 60 | ShellBuffer::singletonRef = NULL; |
---|
[3543] | 61 | } |
---|
[5174] | 62 | |
---|
| 63 | /** |
---|
[5206] | 64 | * registers the Shell to the Buffer |
---|
| 65 | * @param shell the Shell to register. |
---|
| 66 | */ |
---|
| 67 | void ShellBuffer::registerShell(Shell* shell) |
---|
| 68 | { |
---|
| 69 | if (this->shell == NULL) |
---|
| 70 | this->shell = shell; |
---|
| 71 | else |
---|
| 72 | PRINTF(1)("already registered a Shell to the ShellBuffer\n"); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * unregisters the Shell from the Buffer |
---|
| 77 | * @param shell the Shell to unregister. |
---|
| 78 | */ |
---|
| 79 | void ShellBuffer::unregisterShell(Shell* shell) |
---|
| 80 | { |
---|
| 81 | if (this->shell == shell) |
---|
| 82 | this->shell = NULL; |
---|
| 83 | else |
---|
| 84 | PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n"); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
[5174] | 88 | * deletes all the Buffers |
---|
| 89 | */ |
---|
| 90 | void ShellBuffer::flushBuffers() |
---|
| 91 | { |
---|
| 92 | // delete all the Chars in the Buffers |
---|
[5175] | 93 | char* charElem = bufferIterator->firstElement(); |
---|
[5174] | 94 | while (charElem != NULL) |
---|
| 95 | { |
---|
[5210] | 96 | delete[] charElem; |
---|
[5175] | 97 | charElem = bufferIterator->nextElement(); |
---|
[5174] | 98 | } |
---|
[5175] | 99 | delete this->bufferIterator; |
---|
[5174] | 100 | delete this->buffer; |
---|
| 101 | this->buffer = new tList<char>; |
---|
[5175] | 102 | this->bufferIterator = this->buffer->getIterator(); |
---|
[5174] | 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * adds a new Line to the List of Buffers |
---|
| 107 | * @param line the Line as in the first argument in printf |
---|
| 108 | */ |
---|
| 109 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
| 110 | { |
---|
| 111 | va_list arguments; |
---|
| 112 | va_start(arguments, line); |
---|
| 113 | |
---|
| 114 | #if DEBUG < 3 |
---|
| 115 | if (ShellBuffer::singletonRef == NULL) |
---|
| 116 | #endif |
---|
| 117 | |
---|
[5183] | 118 | vprintf(line, arguments); |
---|
[5174] | 119 | #if DEBUG < 3 |
---|
| 120 | else |
---|
| 121 | #else |
---|
| 122 | if (ShellBuffer::singletonRef != NULL) |
---|
| 123 | #endif |
---|
| 124 | ShellBuffer::singletonRef->addBufferLine(line, arguments); |
---|
| 125 | return true; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * add a Line to the List of Buffers |
---|
| 130 | * @param line |
---|
| 131 | * @param arguments |
---|
| 132 | * |
---|
| 133 | * This function Adds one line to the buffer. |
---|
| 134 | * and displays the line as the First Line of the display-buffer |
---|
| 135 | */ |
---|
| 136 | void ShellBuffer::addBufferLine(const char* line, va_list arguments) |
---|
| 137 | { |
---|
| 138 | vsprintf(this->bufferArray, line, arguments); |
---|
| 139 | |
---|
| 140 | char* inputEnd; |
---|
| 141 | char* newLineBegin; |
---|
| 142 | char* newLineEnd; |
---|
| 143 | |
---|
| 144 | // check if we have something left in the buffers |
---|
| 145 | if (unlikely(this->keepBuffer)) |
---|
| 146 | { |
---|
| 147 | strcat(this->keepBufferArray, this->bufferArray); |
---|
| 148 | inputEnd = this->keepBufferArray + strlen(this->keepBufferArray); |
---|
| 149 | newLineBegin = this->keepBufferArray; |
---|
| 150 | this->keepBuffer = false; |
---|
| 151 | } |
---|
| 152 | else |
---|
| 153 | { |
---|
| 154 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
---|
| 155 | newLineBegin = this->bufferArray; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // adding all the new Lines |
---|
[5212] | 159 | while (newLineBegin < inputEnd ) |
---|
[5174] | 160 | { |
---|
| 161 | newLineEnd = strchr(newLineBegin, '\n'); |
---|
| 162 | if (newLineEnd != NULL && *newLineEnd == '\n') |
---|
| 163 | *newLineEnd = '\0'; |
---|
| 164 | else |
---|
| 165 | { |
---|
| 166 | // newLineEnd = newLineBegin + strlen(newLineBegin); |
---|
[5212] | 167 | strcat(this->keepBufferArray, newLineBegin); |
---|
[5174] | 168 | this->keepBuffer = true; |
---|
| 169 | break; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | char* addLine = new char[strlen(newLineBegin)+1]; |
---|
| 173 | strcpy(addLine, newLineBegin); |
---|
| 174 | |
---|
[5176] | 175 | this->lineCount++; |
---|
[5174] | 176 | this->buffer->add(addLine); |
---|
[5206] | 177 | if (likely (this->shell != NULL) && unlikely (this->shell->isActive())) |
---|
| 178 | this->shell->printToDisplayBuffer(addLine); |
---|
[5174] | 179 | |
---|
| 180 | if (this->buffer->getSize() > this->bufferSize) |
---|
| 181 | { |
---|
[5210] | 182 | delete[] this->buffer->firstElement(); |
---|
[5174] | 183 | this->buffer->remove(this->buffer->firstElement()); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | newLineBegin = newLineEnd+1; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * moves the buffer around lineCount lines upwards (negative values move down) |
---|
| 192 | * @param lineCount the Count of lines to move upwards |
---|
| 193 | * |
---|
| 194 | * @todo |
---|
| 195 | */ |
---|
| 196 | void ShellBuffer::moveBuffer(unsigned int lineCount) |
---|
| 197 | { |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * @param lineNumber the n-th line from the bottom |
---|
| 202 | * @returns the Buffer at Line lineNumber |
---|
| 203 | */ |
---|
| 204 | const char* ShellBuffer::getBufferLine(unsigned int lineNumber) |
---|
| 205 | { |
---|
| 206 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
| 207 | char* charElem = charIterator->firstElement(); |
---|
| 208 | |
---|
| 209 | int i = 1; |
---|
| 210 | while (charElem != NULL) |
---|
| 211 | { |
---|
| 212 | if (i++ < lineNumber) |
---|
| 213 | { |
---|
| 214 | delete charIterator; |
---|
| 215 | return charElem; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | charElem = charIterator->nextElement(); |
---|
| 219 | } |
---|
| 220 | delete charIterator; |
---|
| 221 | } |
---|
[5177] | 222 | |
---|
| 223 | /** |
---|
| 224 | * displays some nice output from the Shell |
---|
| 225 | */ |
---|
| 226 | void ShellBuffer::debug() const |
---|
| 227 | { |
---|
| 228 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
| 229 | |
---|
| 230 | char* tmpChar = bufferIterator->firstElement(); |
---|
| 231 | while(tmpChar != NULL) |
---|
| 232 | { |
---|
| 233 | printf(tmpChar); |
---|
| 234 | tmpChar = bufferIterator->nextElement(); |
---|
| 235 | } |
---|
| 236 | } |
---|