/* This file is part of cardwords
   (c) 1998 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   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 of the License, or
   (at your option) any later version.

   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.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_pipe.hh

#ifndef CARDWORDS_PIPE_HH
#define CARDWORDS_PIPE_HH

class CardWords_Pipe {
public:
  // construct the pipe:
  CardWords_Pipe(unsigned int tries = 1);
  
  int
  get_talkFD(void)const;
  int
  get_listenFD(void)const;

  // test if everything went ok so far:
  bool is_good(void) const;
  bool is_bad(void) const;

  // get the error number of the last error:
  int
  get_errno (void);

  // ignore an error:
  void set_good(void);

  // close one end of the line:
  int close_talk(void);
  int close_listen(void);

  // the destructor closes open FD's:
  ~CardWords_Pipe();

private:
  int talkFD, listenFD;
  bool talkOpen, listenOpen;
  bool good;
  int my_errno;
};

inline
int
CardWords_Pipe::get_talkFD(void) const
{
  if (is_bad() || !talkOpen) {
    return -1;
  }
  return talkFD;
}

inline
int
CardWords_Pipe::get_listenFD(void) const
{
  if (listenOpen && good) {
    return listenFD;
  }
  return -1;
}

// test if everything went ok so far:
inline bool
CardWords_Pipe::is_good(void) const
{
  return good;
}
inline  bool
CardWords_Pipe::is_bad(void) const
{
  return !good;
}

// get the error number of the last error:
inline  int
CardWords_Pipe::get_errno (void)
{
  if (good) {
    return 0;
  }
  return my_errno;
}

// ignore an error:
inline void CardWords_Pipe::set_good(void)
{
  good = true;
}


#endif

