00001
00002 // Copyright (C) 2000--2001 Petter Urkedal (petter.urkedal@matfys.lth.se)
00003
00004 // This file is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008
00009 // This file is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012 // GNU General Public License for more details.
00013
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00017
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction. Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License. This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027
00028 // $Id: opstream.h,v 1.1 2002/05/30 18:01:37 petter_urkedal Exp $
00029
00030
00031 #include <ios>
00032 #include <string>
00033 #include <iosfwd>
00034 #include <streambuf>
00035
00036 namespace more {
00037 namespace io {
00038
00039 /** \class basic_opstreambuf opstream.h more/io/opstream.h
00040 ** An IO stream buffer of an output pipe. */
00041 template<typename Char, typename Traits>
00042 struct basic_opstreambuf
00043 : std::basic_streambuf<Char, Traits>
00044 {
00045 typedef Char char_type;
00046 typedef Traits traits_type;
00047 typedef typename Traits::int_type int_type;
00048 typedef typename Traits::pos_type pos_type;
00049 typedef typename Traits::off_type off_type;
00050
00051 basic_opstreambuf(char const*);
00052 virtual ~basic_opstreambuf();
00053 bool is_open() const { return m_fp; }
00054
00055 protected:
00056 virtual int sync();
00057 virtual int_type overflow(int_type = traits_type::eof());
00058
00059 private:
00060 static const int s_nbuf = 256;
00061
00062 void *m_fp;
00063 Char *m_buf;
00064 };
00065
00066 /** \class basic_opstream opstream.h more/io/opstream.h
00067 ** A stream buffer of an output pipe. */
00068 template< typename Char, typename Traits = std::char_traits<Char> >
00069 struct basic_opstream
00070 : std::basic_ostream<Char, Traits>
00071 {
00072 typedef basic_opstreambuf<Char, Traits> streambuf_type;
00073
00074 basic_opstream(char const* cmd)
00075 : std::basic_ostream<Char, Traits>(m_buf=new streambuf_type(cmd))
00076 {
00077 if (!is_open()) setstate(std::ios_base::failbit);
00078 }
00079
00080 ~basic_opstream() { delete m_buf; }
00081
00082 bool is_open() const { return m_buf->is_open(); }
00083
00084 private:
00085 streambuf_type* m_buf;
00086 };
00087
00088 typedef basic_opstream<char> opstream;
00089
00090 }
00091 }