[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: |
---|
[5246] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 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>; |
---|
[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 | |
---|
[5246] | 56 | this->flush(); |
---|
[5174] | 57 | delete buffer; |
---|
| 58 | |
---|
| 59 | ShellBuffer::singletonRef = NULL; |
---|
[3543] | 60 | } |
---|
[5174] | 61 | |
---|
| 62 | /** |
---|
[5206] | 63 | * registers the Shell to the Buffer |
---|
| 64 | * @param shell the Shell to register. |
---|
| 65 | */ |
---|
| 66 | void ShellBuffer::registerShell(Shell* shell) |
---|
| 67 | { |
---|
| 68 | if (this->shell == NULL) |
---|
| 69 | this->shell = shell; |
---|
| 70 | else |
---|
| 71 | PRINTF(1)("already registered a Shell to the ShellBuffer\n"); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * unregisters the Shell from the Buffer |
---|
| 76 | * @param shell the Shell to unregister. |
---|
| 77 | */ |
---|
| 78 | void ShellBuffer::unregisterShell(Shell* shell) |
---|
| 79 | { |
---|
| 80 | if (this->shell == shell) |
---|
| 81 | this->shell = NULL; |
---|
| 82 | else |
---|
| 83 | PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n"); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
[5174] | 87 | * deletes all the Buffers |
---|
| 88 | */ |
---|
[5246] | 89 | void ShellBuffer::flush() |
---|
[5174] | 90 | { |
---|
| 91 | // delete all the Chars in the Buffers |
---|
[5246] | 92 | tIterator<char>* bufferIterator = this->buffer->getIterator(); |
---|
[5175] | 93 | char* charElem = bufferIterator->firstElement(); |
---|
[5174] | 94 | while (charElem != NULL) |
---|
| 95 | { |
---|
[5210] | 96 | delete[] charElem; |
---|
[5175] | 97 | charElem = bufferIterator->nextElement(); |
---|
[5174] | 98 | } |
---|
[5246] | 99 | delete bufferIterator; |
---|
[5174] | 100 | delete this->buffer; |
---|
| 101 | this->buffer = new tList<char>; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * adds a new Line to the List of Buffers |
---|
| 106 | * @param line the Line as in the first argument in printf |
---|
| 107 | */ |
---|
| 108 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
| 109 | { |
---|
| 110 | va_list arguments; |
---|
| 111 | va_start(arguments, line); |
---|
| 112 | |
---|
| 113 | #if DEBUG < 3 |
---|
| 114 | if (ShellBuffer::singletonRef == NULL) |
---|
| 115 | #endif |
---|
| 116 | |
---|
[5183] | 117 | vprintf(line, arguments); |
---|
[5174] | 118 | #if DEBUG < 3 |
---|
| 119 | else |
---|
| 120 | #else |
---|
| 121 | if (ShellBuffer::singletonRef != NULL) |
---|
| 122 | #endif |
---|
| 123 | ShellBuffer::singletonRef->addBufferLine(line, arguments); |
---|
| 124 | return true; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * add a Line to the List of Buffers |
---|
| 129 | * @param line |
---|
| 130 | * @param arguments |
---|
| 131 | * |
---|
| 132 | * This function Adds one line to the buffer. |
---|
| 133 | * and displays the line as the First Line of the display-buffer |
---|
| 134 | */ |
---|
| 135 | void ShellBuffer::addBufferLine(const char* line, va_list arguments) |
---|
| 136 | { |
---|
| 137 | vsprintf(this->bufferArray, line, arguments); |
---|
| 138 | |
---|
| 139 | char* inputEnd; |
---|
| 140 | char* newLineBegin; |
---|
| 141 | char* newLineEnd; |
---|
| 142 | |
---|
| 143 | // check if we have something left in the buffers |
---|
| 144 | if (unlikely(this->keepBuffer)) |
---|
| 145 | { |
---|
| 146 | strcat(this->keepBufferArray, this->bufferArray); |
---|
| 147 | inputEnd = this->keepBufferArray + strlen(this->keepBufferArray); |
---|
| 148 | newLineBegin = this->keepBufferArray; |
---|
| 149 | this->keepBuffer = false; |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
---|
| 154 | newLineBegin = this->bufferArray; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | // adding all the new Lines |
---|
[5212] | 158 | while (newLineBegin < inputEnd ) |
---|
[5174] | 159 | { |
---|
| 160 | newLineEnd = strchr(newLineBegin, '\n'); |
---|
| 161 | if (newLineEnd != NULL && *newLineEnd == '\n') |
---|
| 162 | *newLineEnd = '\0'; |
---|
| 163 | else |
---|
| 164 | { |
---|
| 165 | // newLineEnd = newLineBegin + strlen(newLineBegin); |
---|
[5215] | 166 | unsigned int len = strlen(newLineBegin); |
---|
| 167 | strncpy(this->keepBufferArray, newLineBegin, len); |
---|
| 168 | this->keepBufferArray[len] = '\0'; |
---|
[5174] | 169 | this->keepBuffer = true; |
---|
| 170 | break; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | char* addLine = new char[strlen(newLineBegin)+1]; |
---|
| 174 | strcpy(addLine, newLineBegin); |
---|
| 175 | |
---|
[5176] | 176 | this->lineCount++; |
---|
[5174] | 177 | this->buffer->add(addLine); |
---|
[5206] | 178 | if (likely (this->shell != NULL) && unlikely (this->shell->isActive())) |
---|
| 179 | this->shell->printToDisplayBuffer(addLine); |
---|
[5174] | 180 | |
---|
| 181 | if (this->buffer->getSize() > this->bufferSize) |
---|
| 182 | { |
---|
[5210] | 183 | delete[] this->buffer->firstElement(); |
---|
[5174] | 184 | this->buffer->remove(this->buffer->firstElement()); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | newLineBegin = newLineEnd+1; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
[5177] | 192 | * displays some nice output from the Shell |
---|
| 193 | */ |
---|
| 194 | void ShellBuffer::debug() const |
---|
| 195 | { |
---|
| 196 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
| 197 | |
---|
[5246] | 198 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
| 199 | char* tmpChar = charIterator->firstElement(); |
---|
[5177] | 200 | while(tmpChar != NULL) |
---|
| 201 | { |
---|
| 202 | printf(tmpChar); |
---|
[5246] | 203 | tmpChar = charIterator->nextElement(); |
---|
[5177] | 204 | } |
---|
[5246] | 205 | delete charIterator; |
---|
[5177] | 206 | } |
---|