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;
39 m_string.push_back('\0');
41 explicit StringBuffer(std::size_t capacity)
43 m_string.reserve(capacity);
44 m_string.push_back('\0');
46 explicit StringBuffer(const char* string) : m_string(string, string + string_length(string) + 1)
50 typedef std::vector<char>::iterator iterator;
51 typedef std::vector<char>::const_iterator const_iterator;
55 return m_string.begin();
57 const_iterator begin() const
59 return m_string.begin();
63 return m_string.end() - 1;
65 const_iterator end() const
67 return m_string.end() - 1;
70 void push_back(char c)
72 m_string.insert(end(), c);
76 m_string.erase(end() - 1);
78 void push_range(const char* first, const char* last)
80 m_string.insert(end(), first, last);
82 void push_string(const char* string)
84 push_range(string, string + string_length(string));
88 return &(*m_string.begin());
90 const char* c_str() const
92 return &(*m_string.begin());
99 const char& back() const
105 return m_string.size() == 1;
110 m_string.push_back('\0');
114 /// \brief A TextOutputStream which writes to a StringBuffer.
115 /// Similar to std::stringstream.
116 class StringOutputStream : public TextOutputStream
118 StringBuffer m_string;
120 typedef StringBuffer::iterator iterator;
121 typedef StringBuffer::const_iterator const_iterator;
126 StringOutputStream(std::size_t capacity) : m_string(capacity)
129 std::size_t write(const char* buffer, std::size_t length)
131 m_string.push_range(buffer, buffer + length);
137 return m_string.begin();
139 const_iterator begin() const
141 return m_string.begin();
145 return m_string.end();
147 const_iterator end() const
149 return m_string.end();
154 return m_string.empty();
158 return m_string.c_str();
160 const char* c_str() const
162 return m_string.c_str();
171 inline StringOutputStream& operator<<(StringOutputStream& ostream, const T& t)
173 return ostream_write(ostream, t);