Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

more/cf/pipestream.h

Go to the documentation of this file.
00001 //  more/pipestream.h
00002 //  Copyright (C) 1998--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: pipestream.h,v 1.1 2002/05/30 18:01:37 petter_urkedal Exp $
00029 
00030 
00031 #ifndef MORE_PIPESTREAM_H
00032 #define MORE_PIPESTREAM_H
00033 
00034 #include <stdexcept>
00035 #include <ios>
00036 #include <iostream>
00037 #include <string>
00038 
00039 #include <unistd.h>
00040 
00041 
00042 namespace more {
00043 
00044 //template <class charT, class traits>
00045 class pipe_streambuf : public std::streambuf {
00046     typedef std::char_traits<char> traits;
00047     typedef traits::int_type int_type;
00048     typedef traits::char_type char_type;
00049     int fd[2];
00050 
00051 public:
00052     pipe_streambuf() {
00053         if(pipe(fd) < 0)
00054             throw std::runtime_error("Could not open pipe.");
00055     }
00056     ~pipe_streambuf() {
00057         if(fd[0] != -1) close(fd[0]);
00058         if(fd[1] != -1) close(fd[1]);
00059     }
00060     void close_input() { close(fd[0]); fd[0] = -1; }
00061     void close_output() { close(fd[1]); fd[1] = -1; }
00062 
00063 protected:
00064 
00065     int_type sync() {
00066         std::streamsize n = pptr() - pbase();
00067         if(n && write(fd[1], pbase(), n*sizeof(char_type)) < 0)
00068             return EOF;
00069         return 0;
00070     }
00071 
00072     int_type underflow() {
00073         char_type buf[2];
00074         if(read(fd[0], buf, sizeof(char_type)) != 1) return traits::eof();
00075         else return traits::to_int_type(buf[0]);
00076     }
00077 
00078     int_type uflow() { return underflow(); }
00079     // to prevent storing in temporary buffer?
00080 
00081     int_type overflow(int_type c = traits::eof()) {
00082         return write(fd[1], &c, sizeof(char_type)) == 1? 1 : traits::eof();
00083     }
00084     std::streamsize xsputn(const char_type* s, std::streamsize n) {
00085         return write(fd[1], s, n*sizeof(char_type));
00086         ///fixme: should we divide returned value by sizeof(char_type)?
00087         //check it. 
00088     }
00089 };
00090 
00091 
00092 class pipe_stream : virtual public pipe_streambuf, public std::iostream {
00093 public:
00094     pipe_stream() : std::iostream(this) {}
00095 };
00096 
00097 }
00098 
00099 #endif

Generated on Sat Sep 7 19:11:10 2002 for more with Doxygen 1.2.13.1. Doxygen 1.2.13.1 is written and copyright 1997-2002 by Dimitri van Heesch.