cartobase  5.0.5
fdstream.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
35 
36 /* The following code declares classes to read from and write to
37  * file descriptore or file handles.
38  *
39  * See
40  * http://www.josuttis.com/cppcode
41  * for details and the latest version.
42  *
43  * - open:
44  * - integrating BUFSIZ on some systems?
45  * - optimized reading of multiple characters
46  * - stream for reading AND writing
47  * - i18n
48  *
49  * (C) Copyright Nicolai M. Josuttis 2001.
50  * Permission to copy, use, modify, sell and distribute this software
51  * is granted provided this copyright notice appears in all copies.
52  * This software is provided "as is" without express or implied
53  * warranty, and with no claim as to its suitability for any purpose.
54  *
55  * Version: Jul 28, 2002
56  * History:
57  * Jul 28, 2002: bugfix memcpy() => memmove()
58  * fdinbuf::underflow(): cast for return statements
59  * Aug 05, 2001: first public version
60  */
61 #ifndef BOOST_FDSTREAM_HPP
62 #define BOOST_FDSTREAM_HPP
63 
64 #if !defined( __GNUC__ ) || __GNUC__-0 >= 3
65 #include <istream>
66 #include <ostream>
67 #include <streambuf>
68 #else
69 #include <iostream>
70 #include <streambuf.h>
71 #include <string>
72 #endif
73 
74 // BEGIN namespace BOOST
75 namespace boost {
76 
77 
78 /************************************************************
79  * fdostream
80  * - a stream that writes on a file descriptor
81  ************************************************************/
82 
83 
84 class fdoutbuf : public std::streambuf {
85  protected:
86  int fd; // file descriptor
87 #if defined( __GNUC__ ) && __GNUC__-0 < 3
88  typedef int int_type;
89 #endif
90 #ifdef _WIN32
91  // on windows we can't write() on a socket fd (!)
92  bool _sock;
93 #endif
94  public:
95  // constructor
96  fdoutbuf (int _fd );
97 
98  protected:
99  // write one character
100  virtual int_type overflow (int_type c);
101  // write multiple characters
102  virtual
103  std::streamsize xsputn (const char* s,
104  std::streamsize num);
105 };
106 
107 class fdostream : public std::ostream {
108  protected:
110  public:
111  fdostream (int fd) : std::ostream(0), buf(fd) {
112  rdbuf(&buf);
113  }
114 };
115 
116 
117 /************************************************************
118  * fdistream
119  * - a stream that reads on a file descriptor
120  ************************************************************/
121 
122 class fdinbuf : public std::streambuf {
123  protected:
124  int fd; // file descriptor
125 #ifdef _WIN32
126  // on windows we can't read() on a socket fd (!)
127  bool _sock;
128 #endif
129  protected:
130  /* data buffer:
131  * - at most, pbSize characters in putback area plus
132  * - at most, bufSize characters in ordinary read buffer
133  */
134  static const int pbSize = 4; // size of putback area
135  static const int bufSize = 1024; // size of the data buffer
136  char buffer[bufSize+pbSize]; // data buffer
137 #if defined( __GNUC__ ) && __GNUC__-0 < 3
138  typedef int int_type;
139  typedef string::traits_type traits_type;
140 #endif
141 
142  public:
143  /* constructor
144  * - initialize file descriptor
145  * - initialize empty data buffer
146  * - no putback area
147  * => force underflow()
148  */
149  fdinbuf (int _fd);
150 
151  protected:
152  // insert new characters into the buffer
153  virtual int_type underflow ();
154 };
155 
156 class fdistream : public std::istream {
157  protected:
159  public:
160  fdistream (int fd) : std::istream(0), buf(fd) {
161  rdbuf(&buf);
162  }
163 };
164 
165 
166 } // END namespace boost
167 
168 #endif /*BOOST_FDSTREAM_HPP*/
Definition: fdstream.h:75
STL namespace.
virtual std::streamsize xsputn(const char *s, std::streamsize num)
fdoutbuf buf
Definition: fdstream.h:109
virtual int_type overflow(int_type c)
fdistream(int fd)
Definition: fdstream.h:160
fdoutbuf(int _fd)
fdostream(int fd)
Definition: fdstream.h:111