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