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.
33 /// \todo That vector<char> must become a std::string, and not NUL terminated. Maybe the whole class can be replaced by std::string
36 std::vector<char> m_string;
39 m_string.push_back( '\0' );
41 explicit StringBuffer( std::size_t capacity ){
42 m_string.reserve( capacity );
43 m_string.push_back( '\0' );
45 explicit StringBuffer( const char* string ) : m_string( string, string + string_length( string ) + 1 ){
48 typedef std::vector<char>::iterator iterator;
49 typedef std::vector<char>::const_iterator const_iterator;
52 return m_string.begin();
54 const_iterator begin() const {
55 return m_string.begin();
58 return m_string.end() - 1;
60 const_iterator end() const {
61 return m_string.end() - 1;
64 void push_back( char c ){
65 m_string.insert( end(), c );
68 m_string.erase( end() - 1 );
70 void push_range( const char* first, const char* last ){
71 m_string.insert( end(), first, last );
73 void push_string( const char* string ){
74 push_range( string, string + string_length( string ) );
77 return &( *m_string.begin() );
79 const char* c_str() const {
80 return &( *m_string.begin() );
84 return *( end() - 1 );
86 const char& back() const {
87 return *( end() - 1 );
90 return m_string.size() == 1;
94 m_string.push_back( '\0' );
98 /// \brief A TextOutputStream which writes to a StringBuffer.
99 /// Similar to std::stringstream.
100 class StringOutputStream : public TextOutputStream
102 StringBuffer m_string;
104 typedef StringBuffer::iterator iterator;
105 typedef StringBuffer::const_iterator const_iterator;
107 StringOutputStream(){
109 StringOutputStream( std::size_t capacity ) : m_string( capacity ){
111 std::size_t write( const char* buffer, std::size_t length ){
112 m_string.push_range( buffer, buffer + length );
117 return m_string.begin();
119 const_iterator begin() const {
120 return m_string.begin();
123 return m_string.end();
125 const_iterator end() const {
126 return m_string.end();
130 return m_string.empty();
133 return m_string.c_str();
135 const char* c_str() const {
136 return m_string.c_str();
144 inline StringOutputStream& operator<<( StringOutputStream& ostream, const T& t ){
145 return ostream_write( ostream, t );