1 /* Copyright (C) 2004 and 2005 Chris Vine
4 The following code declares classes to read from and write to
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
33 struct fdstream_error
{
35 enum {dup
, open
, flush
, eof
} code
;
39 class fdoutbuf
: public std::streambuf
{
41 fdstream_error error_condition
;
47 virtual int_type
overflow(int_type
);
48 virtual std::streamsize
xsputn(const char*, std::streamsize
);
50 int attach_fd(int fd
, bool manage
= true);
51 void close_filestream();
53 fdstream_error
get_error() const {return error_condition
;}
54 fdoutbuf(int fd
, bool manage
= true);
59 class fdostream
: public std::ostream
{
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);
70 class fdinbuf
: public std::streambuf
{
72 fdstream_error error_condition
;
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];
82 virtual int_type
underflow();
83 virtual std::streamsize
xsgetn(char*, std::streamsize
);
85 int attach_fd(int fd
, bool manage
= true);
86 void close_filestream();
88 fdstream_error
get_error() const {return error_condition
;}
89 fdinbuf(int fd
, bool manage
= true);
94 class fdistream
: public std::istream
{
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);
105 #endif /*FDSTREAM_H*/