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