]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/stream/stringstream.h
Wrap GtkTreePath
[xonotic/netradiant.git] / libs / stream / stringstream.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #if !defined( INCLUDED_STREAM_STRINGSTREAM_H )
23 #define INCLUDED_STREAM_STRINGSTREAM_H
24
25 #include "itextstream.h"
26 #include "string/string.h"
27 #include <vector>
28
29
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
34 class StringBuffer
35 {
36 std::vector<char> m_string;
37 public:
38 StringBuffer(){
39         m_string.push_back( '\0' );
40 }
41 explicit StringBuffer( std::size_t capacity ){
42         m_string.reserve( capacity );
43         m_string.push_back( '\0' );
44 }
45 explicit StringBuffer( const char* string ) : m_string( string, string + string_length( string ) + 1 ){
46 }
47
48 typedef std::vector<char>::iterator iterator;
49 typedef std::vector<char>::const_iterator const_iterator;
50
51 iterator begin(){
52         return m_string.begin();
53 }
54 const_iterator begin() const {
55         return m_string.begin();
56 }
57 iterator end(){
58         return m_string.end() - 1;
59 }
60 const_iterator end() const {
61         return m_string.end() - 1;
62 }
63
64 void push_back( char c ){
65         m_string.insert( end(), c );
66 }
67 void pop_back(){
68         m_string.erase( end() - 1 );
69 }
70 void push_range( const char* first, const char* last ){
71         m_string.insert( end(), first, last );
72 }
73 void push_string( const char* string ){
74         push_range( string, string + string_length( string ) );
75 }
76 char* c_str(){
77         return &( *m_string.begin() );
78 }
79 const char* c_str() const {
80         return &( *m_string.begin() );
81 }
82
83 char& back(){
84         return *( end() - 1 );
85 }
86 const char& back() const {
87         return *( end() - 1 );
88 }
89 bool empty() const {
90         return m_string.size() == 1;
91 }
92 void clear(){
93         m_string.clear();
94         m_string.push_back( '\0' );
95 }
96 };
97
98 /// \brief A TextOutputStream which writes to a StringBuffer.
99 /// Similar to std::stringstream.
100 class StringOutputStream : public TextOutputStream
101 {
102 StringBuffer m_string;
103 public:
104 typedef StringBuffer::iterator iterator;
105 typedef StringBuffer::const_iterator const_iterator;
106
107 StringOutputStream(){
108 }
109 StringOutputStream( std::size_t capacity ) : m_string( capacity ){
110 }
111 std::size_t write( const char* buffer, std::size_t length ){
112         m_string.push_range( buffer, buffer + length );
113         return length;
114 }
115
116 iterator begin(){
117         return m_string.begin();
118 }
119 const_iterator begin() const {
120         return m_string.begin();
121 }
122 iterator end(){
123         return m_string.end();
124 }
125 const_iterator end() const {
126         return m_string.end();
127 }
128
129 bool empty() const {
130         return m_string.empty();
131 }
132 char* c_str(){
133         return m_string.c_str();
134 }
135 const char* c_str() const {
136         return m_string.c_str();
137 }
138 void clear(){
139         m_string.clear();
140 }
141 };
142
143 template<typename T>
144 inline StringOutputStream& operator<<( StringOutputStream& ostream, const T& t ){
145         return ostream_write( ostream, t );
146 }
147
148
149 #endif