update pet for support for recent clangs
[barvinok.git] / fdstream.h
blob046f2eb30e64270de7cd59a66120c5508183d863
1 /* Copyright (C) 2004 and 2005 Chris Vine
4 The following code declares classes to read from and write to
5 Unix file descriptors.
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License
9 as published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library (see the file LGPL.TXT which came
19 with this source code package); if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.
24 #ifndef FDSTREAM_H
25 #define FDSTREAM_H
27 #include <istream>
28 #include <ostream>
29 #include <streambuf>
30 #include <cstdio>
33 struct fdstream_error {
34 bool error;
35 enum {dup, open, flush, eof} code;
39 class fdoutbuf: public std::streambuf {
41 fdstream_error error_condition;
42 std::FILE* stream_p;
44 void reset();
45 protected:
46 virtual int sync();
47 virtual int_type overflow(int_type);
48 virtual std::streamsize xsputn(const char*, std::streamsize);
49 public:
50 int attach_fd(int fd, bool manage = true);
51 void close_filestream();
52 int get_fd() const;
53 fdstream_error get_error() const {return error_condition;}
54 fdoutbuf(int fd, bool manage = true);
55 fdoutbuf();
56 virtual ~fdoutbuf();
59 class fdostream: public std::ostream {
60 fdoutbuf buf;
61 public:
62 int attach(int fd, bool manage = true) {return buf.attach_fd(fd, manage);}
63 void close() {buf.close_filestream();}
64 int filedesc() const {return buf.get_fd();}
65 fdstream_error get_error() const {return buf.get_error();}
66 fdostream(int fd, bool manage = true);
67 fdostream();
70 class fdinbuf: public std::streambuf {
72 fdstream_error error_condition;
73 std::FILE* stream_p;
75 // putback_buffer does not do any buffering: it reserves one character
76 // for putback and one character for a peek() and/or for bumping
77 // with sbumpc/uflow() - buffering is done by the underlying C stream
78 char_type putback_buffer[2];
80 void reset();
81 protected:
82 virtual int_type underflow();
83 virtual std::streamsize xsgetn(char*, std::streamsize);
84 public:
85 int attach_fd(int fd, bool manage = true);
86 void close_filestream();
87 int get_fd() const;
88 fdstream_error get_error() const {return error_condition;}
89 fdinbuf(int fd, bool manage = true);
90 fdinbuf();
91 virtual ~fdinbuf();
94 class fdistream: public std::istream {
95 fdinbuf buf;
96 public:
97 int attach(int fd, bool manage = true) {return buf.attach_fd(fd, manage);}
98 void close() {buf.close_filestream();}
99 int filedesc() const {return buf.get_fd();}
100 fdstream_error get_error() const {return buf.get_error();}
101 fdistream(int fd, bool manage = true);
102 fdistream();
105 #endif /*FDSTREAM_H*/