stringstream.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_STREAM_STRINGSTREAM_H)
00023 #define INCLUDED_STREAM_STRINGSTREAM_H
00024 
00025 #include "itextstream.h"
00026 #include "string/string.h"
00027 #include <vector>
00028 
00032 class StringBuffer
00033 {
00034         std::vector<char> m_string;
00035     public:
00036         StringBuffer ()
00037         {
00038             m_string.push_back('\0');
00039         }
00040         explicit StringBuffer (std::size_t capacity)
00041         {
00042             m_string.reserve(capacity);
00043             m_string.push_back('\0');
00044         }
00045         explicit StringBuffer (const char* string) :
00046             m_string(string, string + string_length(string) + 1)
00047         {
00048         }
00049 
00050         typedef std::vector<char>::iterator iterator;
00051         typedef std::vector<char>::const_iterator const_iterator;
00052 
00053         iterator begin ()
00054         {
00055             return m_string.begin();
00056         }
00057         const_iterator begin () const
00058         {
00059             return m_string.begin();
00060         }
00061         iterator end ()
00062         {
00063             return m_string.end() - 1;
00064         }
00065         const_iterator end () const
00066         {
00067             return m_string.end() - 1;
00068         }
00069 
00070         void push_back (char c)
00071         {
00072             m_string.insert(end(), c);
00073         }
00074         void pop_back ()
00075         {
00076             m_string.erase(end() - 1);
00077         }
00078         void push_range (const char* first, const char* last)
00079         {
00080             m_string.insert(end(), first, last);
00081         }
00082         void push_string (const char* string)
00083         {
00084             push_range(string, string + string_length(string));
00085         }
00086         char* c_str ()
00087         {
00088             return &(*m_string.begin());
00089         }
00090         const char* c_str () const
00091         {
00092             return &(*m_string.begin());
00093         }
00094 
00095         char& back ()
00096         {
00097             return *(end() - 1);
00098         }
00099         const char& back () const
00100         {
00101             return *(end() - 1);
00102         }
00103         bool empty () const
00104         {
00105             return m_string.size() == 1;
00106         }
00107         void clear ()
00108         {
00109             m_string.clear();
00110             m_string.push_back('\0');
00111         }
00112 };
00113 
00116 class StringOutputStream: public TextOutputStream
00117 {
00118         StringBuffer m_string;
00119     public:
00120         typedef StringBuffer::iterator iterator;
00121         typedef StringBuffer::const_iterator const_iterator;
00122 
00123         StringOutputStream ()
00124         {
00125         }
00126         StringOutputStream (std::size_t capacity) :
00127             m_string(capacity)
00128         {
00129         }
00130         ~StringOutputStream ()
00131         {
00132         }
00133 
00134         std::size_t write (const char* buffer, std::size_t length)
00135         {
00136             m_string.push_range(buffer, buffer + length);
00137             return length;
00138         }
00139 
00140         iterator begin ()
00141         {
00142             return m_string.begin();
00143         }
00144         const_iterator begin () const
00145         {
00146             return m_string.begin();
00147         }
00148         iterator end ()
00149         {
00150             return m_string.end();
00151         }
00152         const_iterator end () const
00153         {
00154             return m_string.end();
00155         }
00156 
00157         bool empty () const
00158         {
00159             return m_string.empty();
00160         }
00161         char* c_str ()
00162         {
00163             return m_string.c_str();
00164         }
00165         const char* c_str () const
00166         {
00167             return m_string.c_str();
00168         }
00169         void clear ()
00170         {
00171             m_string.clear();
00172         }
00173 };
00174 
00175 template<typename T>
00176 inline StringOutputStream& operator<< (StringOutputStream& ostream, const T& t)
00177 {
00178     return ostream_write(ostream, t);
00179 }
00180 
00181 #endif

Generated by  doxygen 1.6.2