[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; |
---|
[1853] | 34 | |
---|
[7375] | 35 | this->lineCount = 0; |
---|
| 36 | this->bufferArray[0] = '\0'; |
---|
[1856] | 37 | |
---|
[7762] | 38 | this->setMaxBufferSize(100); |
---|
[7375] | 39 | } |
---|
[5175] | 40 | |
---|
[7375] | 41 | ShellBuffer* ShellBuffer::singletonRef = NULL; |
---|
[7737] | 42 | std::list<std::string> ShellBuffer::buffer; |
---|
[8145] | 43 | char ShellBuffer::bufferArray[SHELL_BUFFER_SIZE]; |
---|
[4320] | 44 | |
---|
[8145] | 45 | |
---|
[7375] | 46 | /** |
---|
| 47 | * @brief standard deconstructor |
---|
| 48 | */ |
---|
| 49 | ShellBuffer::~ShellBuffer () |
---|
| 50 | { |
---|
| 51 | ShellBuffer::singletonRef = NULL; |
---|
| 52 | } |
---|
[5174] | 53 | |
---|
[7375] | 54 | /** |
---|
| 55 | * @brief deletes all the Buffers |
---|
| 56 | */ |
---|
| 57 | void ShellBuffer::flush() |
---|
| 58 | { |
---|
| 59 | this->buffer.clear(); |
---|
| 60 | } |
---|
[5206] | 61 | |
---|
[7375] | 62 | /** |
---|
| 63 | * @brief adds a new Line to the List of Buffers |
---|
| 64 | * @param line the Line as in the first argument in printf |
---|
| 65 | */ |
---|
[8145] | 66 | void ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
[7375] | 67 | { |
---|
[8145] | 68 | static OrxThread::Mutex ShellBuffer__bufferMutex; |
---|
| 69 | |
---|
| 70 | OrxThread::MutexLock bufferLock(&ShellBuffer__bufferMutex); |
---|
| 71 | |
---|
[7375] | 72 | va_list arguments; |
---|
| 73 | va_start(arguments, line); |
---|
[8145] | 74 | vsnprintf(ShellBuffer::bufferArray, SHELL_BUFFER_SIZE, line, arguments); |
---|
| 75 | va_end(arguments); |
---|
[5206] | 76 | |
---|
[7729] | 77 | #if DEBUG_LEVEL < 3 |
---|
[7375] | 78 | if (ShellBuffer::singletonRef == NULL) |
---|
[5174] | 79 | #endif |
---|
[8145] | 80 | printf(ShellBuffer::bufferArray); |
---|
[7729] | 81 | #if DEBUG_LEVEL < 3 |
---|
[7375] | 82 | else |
---|
[5174] | 83 | #else |
---|
[7375] | 84 | if (ShellBuffer::singletonRef != NULL) |
---|
[5174] | 85 | #endif |
---|
[8145] | 86 | ShellBuffer::singletonRef->addBufferLine(ShellBuffer::bufferArray); |
---|
[7375] | 87 | } |
---|
[5174] | 88 | |
---|
[7375] | 89 | /** |
---|
| 90 | * @brief add a Line to the List of Buffers |
---|
| 91 | * @param line |
---|
| 92 | * @param arguments |
---|
| 93 | * |
---|
| 94 | * This function Adds one line to the buffer. |
---|
| 95 | * and displays the line as the First Line of the display-buffer |
---|
| 96 | */ |
---|
[8145] | 97 | void ShellBuffer::addBufferLine(const char* line) |
---|
[7375] | 98 | { |
---|
[8145] | 99 | std::string inputBuffer = this->keepBuffer + line; |
---|
[5290] | 100 | |
---|
[7761] | 101 | int lineBegin = 0; |
---|
| 102 | int lineEnd = 0; |
---|
[7375] | 103 | // adding all the new Lines |
---|
[7761] | 104 | while (lineEnd < inputBuffer.size()) |
---|
[7375] | 105 | { |
---|
[7761] | 106 | lineBegin = lineEnd; |
---|
| 107 | lineEnd = inputBuffer.find('\n', (lineBegin == 0) ? 0: ++lineBegin); |
---|
| 108 | if (likely(lineEnd != std::string::npos )) |
---|
[7375] | 109 | { |
---|
[7761] | 110 | this->lineCount++; |
---|
| 111 | this->buffer.push_front(inputBuffer.substr(lineBegin, lineEnd - lineBegin)); |
---|
| 112 | } |
---|
| 113 | else // No end of Line reached. |
---|
| 114 | { |
---|
| 115 | this->keepBuffer = inputBuffer.substr(lineBegin, inputBuffer.size() - lineBegin); |
---|
[7375] | 116 | break; |
---|
| 117 | } |
---|
[5174] | 118 | |
---|
[7764] | 119 | if (inputBuffer[lineBegin] == '\n') |
---|
| 120 | lineBegin++, lineEnd++; |
---|
| 121 | |
---|
[7762] | 122 | if (this->buffer.size() > this->maxBufferSize) |
---|
[7744] | 123 | this->buffer.pop_back(); |
---|
[7375] | 124 | } |
---|
[5174] | 125 | } |
---|
| 126 | |
---|
[7375] | 127 | /** |
---|
[7729] | 128 | * @brief displays some nice output from the Shell |
---|
[7375] | 129 | */ |
---|
| 130 | void ShellBuffer::debug() const |
---|
| 131 | { |
---|
| 132 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
[5177] | 133 | |
---|
[7375] | 134 | std::list<std::string>::const_iterator bufferLine; |
---|
[7744] | 135 | for (bufferLine = --this->buffer.end(); bufferLine != this->buffer.begin(); --bufferLine) |
---|
[7375] | 136 | printf((*bufferLine).c_str()); |
---|
| 137 | } |
---|
| 138 | |
---|
[5177] | 139 | } |
---|