2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_STREAM_STRINGSTREAM_H )
23 #define INCLUDED_STREAM_STRINGSTREAM_H
25 #include "itextstream.h"
26 #include "string/string.h"
30 /// \brief A wrapper around a STL vector of char.
31 /// Maintains a null-terminated array of char.
32 /// Provides a limited STL-style interface to push and pop characters at the end of the string.
35 std::vector<char> m_string;
38 m_string.push_back( '\0' );
40 explicit StringBuffer( std::size_t capacity ){
41 m_string.reserve( capacity );
42 m_string.push_back( '\0' );
44 explicit StringBuffer( const char* string ) : m_string( string, string + string_length( string ) + 1 ){
47 typedef std::vector<char>::iterator iterator;
48 typedef std::vector<char>::const_iterator const_iterator;
51 return m_string.begin();
53 const_iterator begin() const {
54 return m_string.begin();
57 return m_string.end() - 1;
59 const_iterator end() const {
60 return m_string.end() - 1;
63 void push_back( char c ){
64 m_string.insert( end(), c );
67 m_string.erase( end() - 1 );
69 void push_range( const char* first, const char* last ){
70 m_string.insert( end(), first, last );
72 void push_string( const char* string ){
73 push_range( string, string + string_length( string ) );
76 return &( *m_string.begin() );
78 const char* c_str() const {
79 return &( *m_string.begin() );
83 return *( end() - 1 );
85 const char& back() const {
86 return *( end() - 1 );
89 return m_string.size() == 1;
93 m_string.push_back( '\0' );
97 /// \brief A TextOutputStream which writes to a StringBuffer.
98 /// Similar to std::stringstream.
99 class StringOutputStream : public TextOutputStream
101 StringBuffer m_string;
103 typedef StringBuffer::iterator iterator;
104 typedef StringBuffer::const_iterator const_iterator;
106 StringOutputStream(){
108 explicit StringOutputStream( std::size_t capacity ) : m_string( capacity ){
110 std::size_t write( const char* buffer, std::size_t length ){
111 m_string.push_range( buffer, buffer + length );
116 return m_string.begin();
118 const_iterator begin() const {
119 return m_string.begin();
122 return m_string.end();
124 const_iterator end() const {
125 return m_string.end();
129 return m_string.empty();
132 return m_string.c_str();
134 const char* c_str() const {
135 return m_string.c_str();
143 inline StringOutputStream& operator<<( StringOutputStream& ostream, const T& t ){
144 return ostream_write( ostream, t );