| [1505] | 1 | /* | 
|---|
|  | 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
|  | 3 | *                    > www.orxonox.net < | 
|---|
|  | 4 | * | 
|---|
|  | 5 | * | 
|---|
|  | 6 | *   License notice: | 
|---|
|  | 7 | * | 
|---|
|  | 8 | *   This program is free software; you can redistribute it and/or | 
|---|
|  | 9 | *   modify it under the terms of the GNU General Public License | 
|---|
|  | 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
|  | 11 | *   of the License, or (at your option) any later version. | 
|---|
|  | 12 | * | 
|---|
|  | 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
|  | 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 16 | *   GNU General Public License for more details. | 
|---|
|  | 17 | * | 
|---|
|  | 18 | *   You should have received a copy of the GNU General Public License | 
|---|
|  | 19 | *   along with this program; if not, write to the Free Software | 
|---|
|  | 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
|  | 21 | * | 
|---|
|  | 22 | *   Author: | 
|---|
|  | 23 | *      Fabian 'x3n' Landau | 
|---|
|  | 24 | *   Co-authors: | 
|---|
|  | 25 | *      ... | 
|---|
|  | 26 | * | 
|---|
|  | 27 | */ | 
|---|
|  | 28 |  | 
|---|
| [1791] | 29 | /** | 
|---|
| [2114] | 30 | @file | 
|---|
| [1791] | 31 | @brief Implementation of the OutputBuffer. | 
|---|
|  | 32 | */ | 
|---|
|  | 33 |  | 
|---|
| [1505] | 34 | #include "OutputBuffer.h" | 
|---|
|  | 35 |  | 
|---|
|  | 36 | namespace orxonox | 
|---|
|  | 37 | { | 
|---|
| [1791] | 38 | const int OUTPUTBUFFER_MAX_LINE_LENGTH = 16384; //! The maximal number of lines that can be stored within the OutputBuffer. | 
|---|
| [1784] | 39 |  | 
|---|
| [1791] | 40 | /** | 
|---|
|  | 41 | @brief Adds a new listener to the list. | 
|---|
|  | 42 | @param listener The new listener | 
|---|
|  | 43 | */ | 
|---|
| [1505] | 44 | void OutputBuffer::registerListener(OutputBufferListener* listener) | 
|---|
|  | 45 | { | 
|---|
|  | 46 | this->listeners_.insert(this->listeners_.end(), listener); | 
|---|
|  | 47 | } | 
|---|
|  | 48 |  | 
|---|
| [1791] | 49 | /** | 
|---|
|  | 50 | @brief Removes a listener from the list. | 
|---|
|  | 51 | @param listener The listener | 
|---|
|  | 52 | */ | 
|---|
| [1505] | 53 | void OutputBuffer::unregisterListener(OutputBufferListener* listener) | 
|---|
|  | 54 | { | 
|---|
|  | 55 | for (std::list<OutputBufferListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) | 
|---|
|  | 56 | { | 
|---|
|  | 57 | if ((*it) == listener) | 
|---|
|  | 58 | this->listeners_.erase(it++); | 
|---|
|  | 59 | else | 
|---|
|  | 60 | ++it; | 
|---|
|  | 61 | } | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
| [1791] | 64 | /** | 
|---|
|  | 65 | @brief Puts a stream manipulator to the stream. | 
|---|
|  | 66 | @param manipulator The manipulator | 
|---|
|  | 67 | */ | 
|---|
| [1505] | 68 | OutputBuffer& OutputBuffer::operator<<(std::ostream& (*manipulator)(std::ostream&)) | 
|---|
|  | 69 | { | 
|---|
|  | 70 | this->stream_ << manipulator; | 
|---|
|  | 71 | this->callListeners(); | 
|---|
|  | 72 | return *this; | 
|---|
|  | 73 | } | 
|---|
|  | 74 |  | 
|---|
| [1791] | 75 | /** | 
|---|
|  | 76 | @brief Puts a stream manipulator to the stream. | 
|---|
|  | 77 | @param manipulator The manipulator | 
|---|
|  | 78 | */ | 
|---|
| [1505] | 79 | OutputBuffer& OutputBuffer::operator<<(std::ios& (*manipulator)(std::ios&)) | 
|---|
|  | 80 | { | 
|---|
|  | 81 | this->stream_ << manipulator; | 
|---|
|  | 82 | this->callListeners(); | 
|---|
|  | 83 | return *this; | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
| [1791] | 86 | /** | 
|---|
|  | 87 | @brief Puts a stream manipulator to the stream. | 
|---|
|  | 88 | @param manipulator The manipulator | 
|---|
|  | 89 | */ | 
|---|
| [1505] | 90 | OutputBuffer& OutputBuffer::operator<<(std::ios_base& (*manipulator)(std::ios_base&)) | 
|---|
|  | 91 | { | 
|---|
|  | 92 | this->stream_ << manipulator; | 
|---|
|  | 93 | this->callListeners(); | 
|---|
|  | 94 | return *this; | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
| [1791] | 97 | /** | 
|---|
|  | 98 | @brief Removes the first line from the stream and assigns it to a given string object. | 
|---|
|  | 99 | @param output The string object to assign the first line | 
|---|
|  | 100 | @return True if there was at least one line in the stream and this line was successfully assigned | 
|---|
|  | 101 |  | 
|---|
|  | 102 | It's important to know the returned line will be removed from the stream. If there are more than one | 
|---|
|  | 103 | listener, they have to cooperate to avoid conflicts. | 
|---|
|  | 104 | */ | 
|---|
| [1505] | 105 | bool OutputBuffer::getLine(std::string* output) | 
|---|
|  | 106 | { | 
|---|
|  | 107 | char line[OUTPUTBUFFER_MAX_LINE_LENGTH]; | 
|---|
|  | 108 |  | 
|---|
|  | 109 | this->stream_.getline(line, OUTPUTBUFFER_MAX_LINE_LENGTH); | 
|---|
|  | 110 | (*output) = std::string(line); | 
|---|
|  | 111 |  | 
|---|
|  | 112 | bool eof = this->stream_.eof(); | 
|---|
|  | 113 | bool fail = this->stream_.fail(); | 
|---|
|  | 114 |  | 
|---|
|  | 115 | if (eof) | 
|---|
|  | 116 | this->stream_.flush(); | 
|---|
|  | 117 |  | 
|---|
|  | 118 | if (eof || fail) | 
|---|
|  | 119 | this->stream_.clear(); | 
|---|
|  | 120 |  | 
|---|
|  | 121 | return (!eof && !fail); | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
| [1791] | 124 | /** | 
|---|
|  | 125 | @brief Calls the outputChanged() function of all registered listeners. | 
|---|
|  | 126 | */ | 
|---|
| [1505] | 127 | void OutputBuffer::callListeners() | 
|---|
|  | 128 | { | 
|---|
|  | 129 | for (std::list<OutputBufferListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) | 
|---|
|  | 130 | (*it)->outputChanged(); | 
|---|
|  | 131 | } | 
|---|
|  | 132 | } | 
|---|