/* Copyright (c) 1997-2005
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program 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: http://www.gnu.org/licenses/gpl.txt.

   This program 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.
*/

#ifndef _POLYMAKE_PIPESTREAM_H
#define _POLYMAKE_PIPESTREAM_H "$Project: polymake $$Id: pipestream.h,v 1.9 2005/10/06 21:02:03 gawrilow Exp $"

#if defined(__GNUC__) && __GNUC__==3 && __GNUC_MINOR__<3 && !defined(__APPLE__) && !defined(__COMO__)
#pragma interface
#endif

#include <socketstream.h>
#include <sys/types.h>

namespace std {

class background_process {
public:
   background_process(const char* progname, const char* const command[])
   {
      start(progname, command, 0, 0, 0);
   }

   background_process(const char* const command[])
   {
      start(command[0], command, 0, 0, 0);
   }

   background_process(const char* progname, const char* const command[], istream *Stdin, ostream *Stdout, ostream *Stderr)
   {
      start(progname, command, Stdin, Stdout, Stderr);
   }

   background_process(const char* const command[], istream *Stdin, ostream *Stdout, ostream *Stderr)
   {
      start(command[0], command, Stdin, Stdout, Stderr);
   }

   ~background_process();
protected:
   pid_t _pid;
   void start(const char* progname, const char* const command[], istream *Stdin, ostream *Stdout, ostream *Stderr);
};

class pipestream_base {
public:
   pipestream_base(int fd_arg)
      : _pid(0), _fd(fd_arg) { }

   pipestream_base(const char* progname, const char* const command[])
   {
      open(progname, command);
   }

   pipestream_base(const char* const command[])
   {
      open(command[0], command);
   }

   ~pipestream_base();
protected:
   pid_t _pid;
   int _fd;

   void open(const char* progname, const char* const command[]);
};

class pipestream
   : protected pipestream_base, public procstream {
public:
   pipestream(const char* progname, const char* const command[])
      : pipestream_base(progname,command),
	procstream(new socketbuf(_fd)) { }

   pipestream(const char* const command[])
      : pipestream_base(command),
	procstream(new socketbuf(_fd)) { }

   pipestream(int fd_arg)
      : pipestream_base(fd_arg),
	procstream(new socketbuf(_fd)) { }
};

} // end namespace std

#endif // _POLYMAKE_PIPESTREAM_H

// Local Variables:
// mode:C++
// End:
