[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 | |
---|
[7374] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL |
---|
[1853] | 17 | |
---|
[5173] | 18 | #include "shell_buffer.h" |
---|
[7737] | 19 | |
---|
| 20 | #include <stdarg.h> |
---|
| 21 | |
---|
[5174] | 22 | #include "debug.h" |
---|
[5175] | 23 | #include "shell.h" |
---|
[7737] | 24 | #include "lib/util/threading.h" |
---|
[1853] | 25 | |
---|
[7374] | 26 | namespace OrxShell |
---|
| 27 | { |
---|
[7375] | 28 | /** |
---|
| 29 | * @brief standard constructor |
---|
| 30 | */ |
---|
| 31 | ShellBuffer::ShellBuffer () |
---|
| 32 | { |
---|
| 33 | ShellBuffer::singletonRef = this; |
---|
| 34 | this->shell = NULL; |
---|
[1853] | 35 | |
---|
[7375] | 36 | this->lineCount = 0; |
---|
| 37 | this->keepBufferArray[0] = '\0'; |
---|
| 38 | this->bufferArray[0] = '\0'; |
---|
| 39 | this->keepBuffer = false; |
---|
[1856] | 40 | |
---|
[7375] | 41 | this->setBufferSize(100); |
---|
| 42 | } |
---|
[5175] | 43 | |
---|
[7375] | 44 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
---|
[7737] | 45 | std::list<std::string> ShellBuffer::buffer; |
---|
[4320] | 46 | |
---|
[7375] | 47 | /** |
---|
| 48 | * @brief standard deconstructor |
---|
| 49 | */ |
---|
| 50 | ShellBuffer::~ShellBuffer () |
---|
| 51 | { |
---|
| 52 | if (this->shell != NULL) |
---|
| 53 | delete this->shell; |
---|
[1853] | 54 | |
---|
[7375] | 55 | ShellBuffer::singletonRef = NULL; |
---|
| 56 | } |
---|
[5174] | 57 | |
---|
[7375] | 58 | /** |
---|
| 59 | * @brief registers the Shell to the Buffer |
---|
| 60 | * @param shell the Shell to register. |
---|
| 61 | */ |
---|
| 62 | void ShellBuffer::registerShell(Shell* shell) |
---|
| 63 | { |
---|
| 64 | if (this->shell == NULL) |
---|
| 65 | this->shell = shell; |
---|
| 66 | else |
---|
| 67 | PRINTF(1)("already registered a Shell to the ShellBuffer\n"); |
---|
| 68 | } |
---|
[7314] | 69 | |
---|
[7375] | 70 | /** |
---|
| 71 | * @brief unregisters the Shell from the Buffer |
---|
| 72 | * @param shell the Shell to unregister. |
---|
| 73 | */ |
---|
| 74 | void ShellBuffer::unregisterShell(Shell* shell) |
---|
| 75 | { |
---|
| 76 | if (this->shell == shell) |
---|
| 77 | this->shell = NULL; |
---|
| 78 | else |
---|
| 79 | PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n"); |
---|
| 80 | } |
---|
[5174] | 81 | |
---|
[7375] | 82 | /** |
---|
| 83 | * @brief deletes all the Buffers |
---|
| 84 | */ |
---|
| 85 | void ShellBuffer::flush() |
---|
| 86 | { |
---|
| 87 | this->buffer.clear(); |
---|
| 88 | } |
---|
[5206] | 89 | |
---|
[7375] | 90 | /** |
---|
| 91 | * @brief adds a new Line to the List of Buffers |
---|
| 92 | * @param line the Line as in the first argument in printf |
---|
| 93 | */ |
---|
| 94 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
| 95 | { |
---|
| 96 | va_list arguments; |
---|
| 97 | va_start(arguments, line); |
---|
[5206] | 98 | |
---|
[7737] | 99 | static OrxThread::Mutex ShellBuffer__bufferMutex; |
---|
[5174] | 100 | |
---|
[7737] | 101 | OrxThread::MutexLock bufferLock(&ShellBuffer__bufferMutex); |
---|
[7729] | 102 | #if DEBUG_LEVEL < 3 |
---|
[7375] | 103 | if (ShellBuffer::singletonRef == NULL) |
---|
[5174] | 104 | #endif |
---|
[7375] | 105 | vprintf(line, arguments); |
---|
[7729] | 106 | #if DEBUG_LEVEL < 3 |
---|
[7375] | 107 | else |
---|
[5174] | 108 | #else |
---|
[7375] | 109 | if (ShellBuffer::singletonRef != NULL) |
---|
[5174] | 110 | #endif |
---|
[7375] | 111 | ShellBuffer::singletonRef->addBufferLine(line, arguments); |
---|
| 112 | return true; |
---|
| 113 | } |
---|
[5174] | 114 | |
---|
[7375] | 115 | /** |
---|
| 116 | * @brief add a Line to the List of Buffers |
---|
| 117 | * @param line |
---|
| 118 | * @param arguments |
---|
| 119 | * |
---|
| 120 | * This function Adds one line to the buffer. |
---|
| 121 | * and displays the line as the First Line of the display-buffer |
---|
| 122 | */ |
---|
| 123 | void ShellBuffer::addBufferLine(const char* line, va_list arguments) |
---|
| 124 | { |
---|
| 125 | char* inputEnd; |
---|
| 126 | char* newLineBegin; |
---|
| 127 | char* newLineEnd; |
---|
[5174] | 128 | |
---|
[7375] | 129 | // copy the output to the bufferArray |
---|
| 130 | vsprintf(this->bufferArray, line, arguments); |
---|
[5290] | 131 | |
---|
[7375] | 132 | // check if we have something left in the buffers |
---|
| 133 | // if so, append the new String to this one. |
---|
| 134 | if (unlikely(this->keepBuffer)) |
---|
| 135 | { |
---|
| 136 | strcat(this->keepBufferArray, this->bufferArray); |
---|
| 137 | inputEnd = this->keepBufferArray + strlen(this->bufferArray); |
---|
| 138 | newLineBegin = this->keepBufferArray; |
---|
| 139 | this->keepBuffer = false; |
---|
| 140 | } |
---|
[5174] | 141 | else |
---|
| 142 | { |
---|
[7375] | 143 | inputEnd = this->bufferArray + strlen(this->bufferArray); |
---|
| 144 | newLineBegin = this->bufferArray; |
---|
[5174] | 145 | } |
---|
| 146 | |
---|
[7375] | 147 | // adding all the new Lines |
---|
| 148 | while (newLineBegin < inputEnd) |
---|
| 149 | { |
---|
| 150 | newLineEnd = strchr(newLineBegin, '\n'); |
---|
| 151 | if (likely(newLineEnd != NULL && *newLineEnd == '\n')) |
---|
| 152 | *newLineEnd = '\0'; |
---|
| 153 | else |
---|
| 154 | { |
---|
| 155 | unsigned int len = strlen(newLineBegin); |
---|
| 156 | char* copyBuffer = new char[len+1]; |
---|
| 157 | strcpy(copyBuffer, newLineBegin); |
---|
| 158 | strncpy(this->keepBufferArray, copyBuffer, len); |
---|
| 159 | delete[] copyBuffer; |
---|
| 160 | this->keepBufferArray[len] = '\0'; |
---|
| 161 | this->keepBuffer = true; |
---|
| 162 | break; |
---|
| 163 | } |
---|
[5174] | 164 | |
---|
[7375] | 165 | this->lineCount++; |
---|
[7744] | 166 | this->buffer.push_front(newLineBegin); |
---|
[7375] | 167 | if (likely (this->shell != NULL) && unlikely (this->shell->isActive())) |
---|
| 168 | this->shell->printToDisplayBuffer(newLineBegin); |
---|
[5174] | 169 | |
---|
[7375] | 170 | if (this->buffer.size() > this->bufferSize) |
---|
[7744] | 171 | this->buffer.pop_back(); |
---|
[7375] | 172 | |
---|
| 173 | newLineBegin = newLineEnd+1; |
---|
| 174 | } |
---|
[5174] | 175 | } |
---|
| 176 | |
---|
[7375] | 177 | /** |
---|
[7729] | 178 | * @brief displays some nice output from the Shell |
---|
[7375] | 179 | */ |
---|
| 180 | void ShellBuffer::debug() const |
---|
| 181 | { |
---|
| 182 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
[5177] | 183 | |
---|
[7375] | 184 | std::list<std::string>::const_iterator bufferLine; |
---|
[7744] | 185 | for (bufferLine = --this->buffer.end(); bufferLine != this->buffer.begin(); --bufferLine) |
---|
[7375] | 186 | printf((*bufferLine).c_str()); |
---|
| 187 | } |
---|
| 188 | |
---|
[5177] | 189 | } |
---|