[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; |
---|
| 35 | |
---|
| 36 | this->lineCount = 0; |
---|
[5174] | 37 | this->keepBufferArray[0] = '\0'; |
---|
| 38 | this->keepBuffer = false; |
---|
| 39 | this->buffer = new tList<char>; |
---|
[5175] | 40 | this->bufferIterator = this->buffer->getIterator(); |
---|
[4320] | 41 | |
---|
[5174] | 42 | this->setBufferSize(10); |
---|
[3365] | 43 | } |
---|
[1853] | 44 | |
---|
[5174] | 45 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
---|
[1853] | 46 | |
---|
[3245] | 47 | /** |
---|
[4838] | 48 | * standard deconstructor |
---|
[3245] | 49 | */ |
---|
[5173] | 50 | ShellBuffer::~ShellBuffer () |
---|
[3543] | 51 | { |
---|
[5175] | 52 | if (Shell::isInstanciated()) |
---|
| 53 | delete Shell::getInstance(); |
---|
[5174] | 54 | |
---|
[5175] | 55 | this->flushBuffers(); |
---|
| 56 | delete bufferIterator; |
---|
[5174] | 57 | delete buffer; |
---|
| 58 | |
---|
| 59 | ShellBuffer::singletonRef = NULL; |
---|
[3543] | 60 | } |
---|
[5174] | 61 | |
---|
| 62 | /** |
---|
| 63 | * deletes all the Buffers |
---|
| 64 | */ |
---|
| 65 | void ShellBuffer::flushBuffers() |
---|
| 66 | { |
---|
| 67 | // delete all the Chars in the Buffers |
---|
[5175] | 68 | char* charElem = bufferIterator->firstElement(); |
---|
[5174] | 69 | while (charElem != NULL) |
---|
| 70 | { |
---|
| 71 | delete charElem; |
---|
[5175] | 72 | charElem = bufferIterator->nextElement(); |
---|
[5174] | 73 | } |
---|
[5175] | 74 | delete this->bufferIterator; |
---|
[5174] | 75 | delete this->buffer; |
---|
| 76 | this->buffer = new tList<char>; |
---|
[5175] | 77 | this->bufferIterator = this->buffer->getIterator(); |
---|
[5174] | 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * adds a new Line to the List of Buffers |
---|
| 82 | * @param line the Line as in the first argument in printf |
---|
| 83 | */ |
---|
| 84 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
| 85 | { |
---|
| 86 | va_list arguments; |
---|
| 87 | va_start(arguments, line); |
---|
| 88 | |
---|
| 89 | #if DEBUG < 3 |
---|
| 90 | if (ShellBuffer::singletonRef == NULL) |
---|
| 91 | #endif |
---|
| 92 | |
---|
| 93 | // vprintf(line, arguments); |
---|
| 94 | #if DEBUG < 3 |
---|
| 95 | else |
---|
| 96 | #else |
---|
| 97 | if (ShellBuffer::singletonRef != NULL) |
---|
| 98 | #endif |
---|
| 99 | ShellBuffer::singletonRef->addBufferLine(line, arguments); |
---|
| 100 | return true; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * add a Line to the List of Buffers |
---|
| 105 | * @param line |
---|
| 106 | * @param arguments |
---|
| 107 | * |
---|
| 108 | * This function Adds one line to the buffer. |
---|
| 109 | * and displays the line as the First Line of the display-buffer |
---|
| 110 | */ |
---|
| 111 | void ShellBuffer::addBufferLine(const char* line, va_list arguments) |
---|
| 112 | { |
---|
| 113 | vsprintf(this->bufferArray, line, arguments); |
---|
| 114 | |
---|
| 115 | char* inputEnd; |
---|
| 116 | char* newLineBegin; |
---|
| 117 | char* newLineEnd; |
---|
| 118 | |
---|
| 119 | // check if we have something left in the buffers |
---|
| 120 | if (unlikely(this->keepBuffer)) |
---|
| 121 | { |
---|
| 122 | strcat(this->keepBufferArray, this->bufferArray); |
---|
| 123 | inputEnd = this->keepBufferArray + strlen(this->keepBufferArray); |
---|
| 124 | newLineBegin = this->keepBufferArray; |
---|
| 125 | this->keepBuffer = false; |
---|
| 126 | } |
---|
| 127 | else |
---|
| 128 | { |
---|
| 129 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
---|
| 130 | newLineBegin = this->bufferArray; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | // adding all the new Lines |
---|
| 134 | while (newLineBegin < inputEnd) |
---|
| 135 | { |
---|
| 136 | newLineEnd = strchr(newLineBegin, '\n'); |
---|
| 137 | if (newLineEnd != NULL && *newLineEnd == '\n') |
---|
| 138 | *newLineEnd = '\0'; |
---|
| 139 | else |
---|
| 140 | { |
---|
| 141 | // newLineEnd = newLineBegin + strlen(newLineBegin); |
---|
| 142 | strcpy(this->keepBufferArray, newLineBegin); |
---|
| 143 | this->keepBuffer = true; |
---|
| 144 | break; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | char* addLine = new char[strlen(newLineBegin)+1]; |
---|
| 148 | strcpy(addLine, newLineBegin); |
---|
| 149 | |
---|
[5176] | 150 | this->lineCount++; |
---|
[5174] | 151 | this->buffer->add(addLine); |
---|
[5176] | 152 | if (Shell::isInstanciated() && Shell::getInstance()->isActive()) |
---|
| 153 | Shell::getInstance()->printToDisplayBuffer(addLine); |
---|
[5174] | 154 | |
---|
| 155 | if (this->buffer->getSize() > this->bufferSize) |
---|
| 156 | { |
---|
| 157 | delete this->buffer->firstElement(); |
---|
| 158 | this->buffer->remove(this->buffer->firstElement()); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | newLineBegin = newLineEnd+1; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | * moves the buffer around lineCount lines upwards (negative values move down) |
---|
| 167 | * @param lineCount the Count of lines to move upwards |
---|
| 168 | * |
---|
| 169 | * @todo |
---|
| 170 | */ |
---|
| 171 | void ShellBuffer::moveBuffer(unsigned int lineCount) |
---|
| 172 | { |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * @param lineNumber the n-th line from the bottom |
---|
| 177 | * @returns the Buffer at Line lineNumber |
---|
| 178 | */ |
---|
| 179 | const char* ShellBuffer::getBufferLine(unsigned int lineNumber) |
---|
| 180 | { |
---|
| 181 | tIterator<char>* charIterator = this->buffer->getIterator(); |
---|
| 182 | char* charElem = charIterator->firstElement(); |
---|
| 183 | |
---|
| 184 | int i = 1; |
---|
| 185 | while (charElem != NULL) |
---|
| 186 | { |
---|
| 187 | if (i++ < lineNumber) |
---|
| 188 | { |
---|
| 189 | delete charIterator; |
---|
| 190 | return charElem; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | charElem = charIterator->nextElement(); |
---|
| 194 | } |
---|
| 195 | delete charIterator; |
---|
| 196 | } |
---|