[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->bufferArray[0] = '\0'; |
---|
[1856] | 38 | |
---|
[7375] | 39 | this->setBufferSize(100); |
---|
| 40 | } |
---|
[5175] | 41 | |
---|
[7375] | 42 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
---|
[7737] | 43 | std::list<std::string> ShellBuffer::buffer; |
---|
[4320] | 44 | |
---|
[7375] | 45 | /** |
---|
| 46 | * @brief standard deconstructor |
---|
| 47 | */ |
---|
| 48 | ShellBuffer::~ShellBuffer () |
---|
| 49 | { |
---|
| 50 | if (this->shell != NULL) |
---|
| 51 | delete this->shell; |
---|
[1853] | 52 | |
---|
[7375] | 53 | ShellBuffer::singletonRef = NULL; |
---|
| 54 | } |
---|
[5174] | 55 | |
---|
[7375] | 56 | /** |
---|
| 57 | * @brief registers the Shell to the Buffer |
---|
| 58 | * @param shell the Shell to register. |
---|
| 59 | */ |
---|
| 60 | void ShellBuffer::registerShell(Shell* shell) |
---|
| 61 | { |
---|
| 62 | if (this->shell == NULL) |
---|
| 63 | this->shell = shell; |
---|
| 64 | else |
---|
| 65 | PRINTF(1)("already registered a Shell to the ShellBuffer\n"); |
---|
| 66 | } |
---|
[7314] | 67 | |
---|
[7375] | 68 | /** |
---|
| 69 | * @brief unregisters the Shell from the Buffer |
---|
| 70 | * @param shell the Shell to unregister. |
---|
| 71 | */ |
---|
| 72 | void ShellBuffer::unregisterShell(Shell* shell) |
---|
| 73 | { |
---|
| 74 | if (this->shell == shell) |
---|
| 75 | this->shell = NULL; |
---|
| 76 | else |
---|
| 77 | PRINTF(1)("cannot unregister shell, because it is not registered to the ShellBuffer\n"); |
---|
| 78 | } |
---|
[5174] | 79 | |
---|
[7375] | 80 | /** |
---|
| 81 | * @brief deletes all the Buffers |
---|
| 82 | */ |
---|
| 83 | void ShellBuffer::flush() |
---|
| 84 | { |
---|
| 85 | this->buffer.clear(); |
---|
| 86 | } |
---|
[5206] | 87 | |
---|
[7375] | 88 | /** |
---|
| 89 | * @brief adds a new Line to the List of Buffers |
---|
| 90 | * @param line the Line as in the first argument in printf |
---|
| 91 | */ |
---|
| 92 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
| 93 | { |
---|
| 94 | va_list arguments; |
---|
| 95 | va_start(arguments, line); |
---|
[5206] | 96 | |
---|
[7737] | 97 | static OrxThread::Mutex ShellBuffer__bufferMutex; |
---|
[5174] | 98 | |
---|
[7737] | 99 | OrxThread::MutexLock bufferLock(&ShellBuffer__bufferMutex); |
---|
[7729] | 100 | #if DEBUG_LEVEL < 3 |
---|
[7375] | 101 | if (ShellBuffer::singletonRef == NULL) |
---|
[5174] | 102 | #endif |
---|
[7375] | 103 | vprintf(line, arguments); |
---|
[7729] | 104 | #if DEBUG_LEVEL < 3 |
---|
[7375] | 105 | else |
---|
[5174] | 106 | #else |
---|
[7375] | 107 | if (ShellBuffer::singletonRef != NULL) |
---|
[5174] | 108 | #endif |
---|
[7375] | 109 | ShellBuffer::singletonRef->addBufferLine(line, arguments); |
---|
| 110 | return true; |
---|
| 111 | } |
---|
[5174] | 112 | |
---|
[7375] | 113 | /** |
---|
| 114 | * @brief add a Line to the List of Buffers |
---|
| 115 | * @param line |
---|
| 116 | * @param arguments |
---|
| 117 | * |
---|
| 118 | * This function Adds one line to the buffer. |
---|
| 119 | * and displays the line as the First Line of the display-buffer |
---|
| 120 | */ |
---|
| 121 | void ShellBuffer::addBufferLine(const char* line, va_list arguments) |
---|
| 122 | { |
---|
| 123 | // copy the output to the bufferArray |
---|
| 124 | vsprintf(this->bufferArray, line, arguments); |
---|
[5290] | 125 | |
---|
[7761] | 126 | std::string inputBuffer = this->keepBuffer + this->bufferArray; |
---|
[5174] | 127 | |
---|
[7761] | 128 | int lineBegin = 0; |
---|
| 129 | int lineEnd = 0; |
---|
[7375] | 130 | // adding all the new Lines |
---|
[7761] | 131 | while (lineEnd < inputBuffer.size()) |
---|
[7375] | 132 | { |
---|
[7761] | 133 | lineBegin = lineEnd; |
---|
| 134 | lineEnd = inputBuffer.find('\n', (lineBegin == 0) ? 0: ++lineBegin); |
---|
| 135 | if (likely(lineEnd != std::string::npos )) |
---|
[7375] | 136 | { |
---|
[7761] | 137 | this->lineCount++; |
---|
| 138 | |
---|
| 139 | printf("Test:: %s \n", inputBuffer.substr(lineBegin, lineEnd - lineBegin).c_str()); |
---|
| 140 | this->buffer.push_front(inputBuffer.substr(lineBegin, lineEnd - lineBegin)); |
---|
| 141 | if (likely (this->shell != NULL) && unlikely (this->shell->isActive())) |
---|
| 142 | this->shell->printToDisplayBuffer(this->buffer.front()); |
---|
| 143 | } |
---|
| 144 | else // No end of Line reached. |
---|
| 145 | { |
---|
| 146 | this->keepBuffer = inputBuffer.substr(lineBegin, inputBuffer.size() - lineBegin); |
---|
[7375] | 147 | break; |
---|
| 148 | } |
---|
[5174] | 149 | |
---|
| 150 | |
---|
[7761] | 151 | |
---|
[7375] | 152 | if (this->buffer.size() > this->bufferSize) |
---|
[7744] | 153 | this->buffer.pop_back(); |
---|
[7375] | 154 | } |
---|
[5174] | 155 | } |
---|
| 156 | |
---|
[7375] | 157 | /** |
---|
[7729] | 158 | * @brief displays some nice output from the Shell |
---|
[7375] | 159 | */ |
---|
| 160 | void ShellBuffer::debug() const |
---|
| 161 | { |
---|
| 162 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
[5177] | 163 | |
---|
[7375] | 164 | std::list<std::string>::const_iterator bufferLine; |
---|
[7744] | 165 | for (bufferLine = --this->buffer.end(); bufferLine != this->buffer.begin(); --bufferLine) |
---|
[7375] | 166 | printf((*bufferLine).c_str()); |
---|
| 167 | } |
---|
| 168 | |
---|
[5177] | 169 | } |
---|