00001 // Copyright (C) 2001--2002 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: fstream.h,v 1.1 2002/05/30 18:01:37 petter_urkedal Exp $
00028
00029
00030 #ifndef MORE_IO_FSTREAM_H
00031 #define MORE_IO_FSTREAM_H
00032
00033 #include <ios>
00034 #include <streambuf>
00035
00036 namespace more {
00037 namespace io {
00038
00039 /** \class basic_ifstream fstream.h more/io/fstream.h
00040 **
00041 ** Basically the same as the \c std:: one, but also provides
00042 ** on-the-fly decompression of \c bzip2 or \c gzip compressed
00043 ** files. */
00044 template<typename Char, typename Traits = std::char_traits<Char> >
00045 struct basic_ifstream : std::basic_istream<Char, Traits>
00046 {
00047 typedef Char char_type;
00048 typedef Traits traits_type;
00049 typedef typename traits_type::int_type int_type;
00050 typedef typename traits_type::pos_type pos_type;
00051 typedef typename traits_type::off_type off_type;
00052 private:
00053 typedef std::basic_streambuf<Char, Traits> streambuf_type;
00054
00055 public:
00056 basic_ifstream()
00057 : std::basic_istream<Char, Traits>(0), m_buf(0) {}
00058
00059 basic_ifstream(char const* name,
00060 std::ios_base::openmode mode = std::ios_base::in)
00061 : std::basic_istream<Char, Traits>(0), m_buf(0)
00062 {
00063 open(name, mode);
00064 }
00065
00066 ~basic_ifstream() { close(); }
00067
00068 streambuf_type* rdbuf() { return m_buf; }
00069 bool is_open() { return m_buf; }
00070 streambuf_type* open(char const*, std::ios_base::openmode);
00071 streambuf_type* close() { delete m_buf; m_buf = 0; return 0; }
00072
00073 private:
00074 streambuf_type* m_buf;
00075 bool m_is_pipe;
00076 };
00077
00078 typedef basic_ifstream<char, std::char_traits<char> > ifstream;
00079
00080 }
00081 }
00082
00083 #endif