1 /* The following code declares classes to read from and write to
2 * file descriptore or file handles.
5 * http://www.josuttis.com/cppcode
6 * for details and the latest version.
9 * - integrating BUFSIZ on some systems?
10 * - optimized reading of multiple characters
11 * - stream for reading AND writing
14 * (C) Copyright Nicolai M. Josuttis 2001.
15 * Permission to copy, use, modify, sell and distribute this software
16 * is granted provided this copyright notice appears in all copies.
17 * This software is provided "as is" without express or implied
18 * warranty, and with no claim as to its suitability for any purpose.
20 * Version: Jul 28, 2002
22 * Jul 28, 2002: bugfix memcpy() => memmove()
23 * fdinbuf::underflow(): cast for return statements
24 * Aug 05, 2001: first public version
26 #ifndef BOOST_FDSTREAM_HPP
27 #define BOOST_FDSTREAM_HPP
38 // low-level read and write functions
44 // int write (int fd, const char* buf, int num);
45 // int read (int fd, char* buf, int num);
50 // BEGIN namespace BOOST
54 /************************************************************
56 * - a stream that writes on a file descriptor
57 ************************************************************/
60 class fdoutbuf : public std::streambuf {
62 int fd; // file descriptor
65 fdoutbuf (int _fd) : fd(_fd) {
68 // write one character
69 virtual int_type overflow (int_type c) {
72 if (write (fd, &z, 1) != 1) {
78 // write multiple characters
80 std::streamsize xsputn (const char* s,
81 std::streamsize num) {
82 return write(fd,s,num);
86 class fdostream : public std::ostream {
90 fdostream (int fd) : std::ostream(0), buf(fd) {
96 /************************************************************
98 * - a stream that reads on a file descriptor
99 ************************************************************/
101 class fdinbuf : public std::streambuf {
103 int fd; // file descriptor
106 * - at most, pbSize characters in putback area plus
107 * - at most, bufSize characters in ordinary read buffer
109 static const int pbSize = 4; // size of putback area
110 static const int bufSize = 1024; // size of the data buffer
111 char buffer[bufSize+pbSize]; // data buffer
115 * - initialize file descriptor
116 * - initialize empty data buffer
118 * => force underflow()
120 fdinbuf (int _fd) : fd(_fd) {
121 setg (buffer+pbSize, // beginning of putback area
122 buffer+pbSize, // read position
123 buffer+pbSize); // end position
127 // insert new characters into the buffer
128 virtual int_type underflow () {
133 // is read position before end of buffer?
134 if (gptr() < egptr()) {
135 return traits_type::to_int_type(*gptr());
138 /* process size of putback area
139 * - use number of characters read
140 * - but at most size of putback area
143 numPutback = gptr() - eback();
144 if (numPutback > pbSize) {
148 /* copy up to pbSize characters previously read into
151 memmove (buffer+(pbSize-numPutback), gptr()-numPutback,
154 // read at most bufSize new characters
156 num = read (fd, buffer+pbSize, bufSize);
162 // reset buffer pointers
163 setg (buffer+(pbSize-numPutback), // beginning of putback area
164 buffer+pbSize, // read position
165 buffer+pbSize+num); // end of buffer
167 // return next character
168 return traits_type::to_int_type(*gptr());
172 class fdistream : public std::istream {
176 fdistream (int fd) : std::istream(0), buf(fd) {
182 } // END namespace boost
184 #endif /*BOOST_FDSTREAM_HPP*/