/* This is part of libio/iostream, providing -*- C++ -*- input/output. Copyright (C) 2000 Free Software Foundation This file is part of the GNU IO Library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. As a special exception, if you link this library with files compiled with a GNU compiler to produce an executable, this does not cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */ /* Written by Magnus Fromreide (magfr@lysator.liu.se). */ #ifndef __SSTREAM #define __SSTREAM #include #include #include namespace std { class stringstreambuf : public streambuf { std::string buf; ios::open_mode mode; protected: inline virtual int sync(); inline virtual int overflow(int = EOF); inline virtual int underflow(); public: stringstreambuf(int m=ios::in|ios::out) : buf(), mode(static_cast(m)) { } stringstreambuf(const std::string &s, int m=ios::in|ios::out) : buf(s), mode(static_cast(m)) { } _IO_ssize_t pcount() { return buf.size(); } std::string str() { sync(); return buf; }; void str(const std::string& s) { pbump(pbase() - pptr()); gbump(egptr() - gptr()); buf = s; } }; class stringstreambase : virtual public ios { protected: stringstreambuf __my_sb; public: std::string str() const { return dynamic_cast(_strbuf)->str(); } void str(const std::string& s) { return dynamic_cast(_strbuf)->str(s); } stringstreambuf* rdbuf() { return &__my_sb; } protected: stringstreambase(int m) : __my_sb(m) { init (&__my_sb); } stringstreambase(const std::string& s, int m) : __my_sb(s, m) { init (&__my_sb); } }; class istringstream : public stringstreambase, public istream { public: istringstream(std::string& s) : stringstreambase(s, ios::in) { } void str(const std::string& s) { return dynamic_cast(_strbuf)->str(s); } }; class ostringstream : public stringstreambase, public ostream { public: ostringstream(int m=ios::out) : stringstreambase(m) { } ostringstream(const std::string& s, int m=ios::out) : stringstreambase(s, m) { } std::string str() const { return dynamic_cast(_strbuf)->str(); } }; class stringstream : public stringstreambase, public iostream { public: stringstream(int m=ios::in|ios::out) : stringstreambase(m) { } stringstream(const std::string &s, int m=ios::in|ios::out) : stringstreambase(s, m) { } std::string str() const { return dynamic_cast(_strbuf)->str(); } void str(const std::string& s) { return dynamic_cast(_strbuf)->str(s); } _IO_ssize_t pcount() { return dynamic_cast(_strbuf)->pcount(); } }; } inline int std::stringstreambuf::sync() { if((mode & ios::out) == 0) return EOF; std::string::size_type oldSize = buf.size(); std::string::size_type n = pptr() - pbase(); if(n) { buf.append(pbase(), pptr()); if(buf.size() - oldSize != n) return EOF; } return 0; } inline int std::stringstreambuf::overflow(int ch) { if((mode & ios::out) == 0) return EOF; streamsize n = pptr() - pbase(); if(n && sync()) return EOF; if(ch != EOF) { char cbuf[1]; std::string::size_type oldSize = buf.size(); cbuf[0] = ch; buf.append(cbuf, 1); if(buf.size() - oldSize != 1) return EOF; } pbump(-n); return 0; } inline int std::stringstreambuf::underflow() { if((mode & ios::in) == 0) return EOF; std::string::size_type n = egptr() - eback(); std::string::size_type s; if(buf.size() < n) s = buf.copy(egptr() - buf.size(), buf.size()); else s = buf.copy(eback(), n); gbump(egptr() - gptr() - s); return 0; } #endif /*!__STRSTREAM_H*/