VICI  0.11.815
Visual Chart Interpreter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
fdstream.h
Go to the documentation of this file.
1 // fdstream.h
2 //
3 // Input and output streams which use UNIX file descriptors.
4 //
5 // This code is adapted from the examples in the book:
6 // The C++ Stanbdard Library A Tutorial and Reference
7 // by Nicolai M. Josuttis
8 /*
9  *
10  * Copyright 2007 - 2016 Brenton Ross
11  *
12  * This code is part of VICI.
13  *
14  * VICI is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * VICI is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with VICI. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
32 #ifndef CFI_FDSTREAM_H
33 #define CFI_FDSTREAM_H
34 
35 
36 #include <iostream>
37 #include <string>
38 
39 namespace VICI
40 {
41 
42 namespace cfi
43 {
44 //
45 // Forward declarations.
46 //
47 class fdostream;
48 class fdistream;
49 
51 
59 class FD
60 {
61 private:
62  int fd;
63  bool locked;
64 public:
66 
71  FD( const std::string & name, int mode, bool lock );
72 
74 
77  FD( int x ) : fd(x), locked(false) {}
78 
80  ~FD();
81 
83  operator int () { return fd; }
84 
86 
90  static void report( std::ostream &s, int fd);
91 };
92 
93 
94 
95 // -----------------------------------------------------------------
96 // f d o b u f
97 // -----------------------------------------------------------------
98 
100 
107 class fdoutbuf : public std::streambuf
108 {
109 protected:
112 
114  int mFd;
115 
118 
120  static const int BUFFER_SIZE = 8192;
121 
124 
125 
126 public:
128 
133  fdoutbuf( int fd, bool isCloseNeeded, fdostream & owner );
134 
136 
140  void close();
141 
143  virtual ~fdoutbuf();
144 
145 
146 protected:
148  int flushBuffer();
149 
151 
154  virtual int overflow( int c );
155 
157  virtual int sync();
158 };
159 
160 
161 // -----------------------------------------------------------------
162 // f d o s t r e a m
163 // -----------------------------------------------------------------
164 
166 
171 class fdostream : public std::ostream
172 {
173 public:
174  static bool CLOSE_NEEDED;
175  static bool CLOSE_NOT_NEEDED;
176 
177  friend class fdoutbuf;
178 
179 protected:
182 
183 public:
185 
193  fdostream( int fd, bool isCloseNeeded = false );
194 
196  void close();
197 };
198 
199 
200 // -----------------------------------------------------------------
201 // f d i n b u f
202 // -----------------------------------------------------------------
203 
205 
213 class fdinbuf : public std::streambuf
214 {
215 protected:
218 
220  int mFd;
221 
224 
225  static const int BUFFER_SIZE = 8192;
226  static const int PUSHBACK_SIZE = 4;
227 
231 
232 
233 public:
234  //
235  // Constructor which accepts the file descriptor to read from,
236  // a flag indicating whether the file descriptor is closed on
237  // destruction, and our owning input stream.
238  //
239 
241 
249  fdinbuf( int fd, bool isCloseNeeded, fdistream & owner );
250 
252  void close();
253 
255  virtual ~fdinbuf();
256 
257 
258 protected:
260  virtual int underflow();
261 };
262 
263 
264 // -----------------------------------------------------------------
265 // f d i s t r e a m
266 // -----------------------------------------------------------------
267 
269 
274 class fdistream : public std::istream
275 {
276 public:
277  static bool CLOSE_NEEDED;
278  static bool CLOSE_NOT_NEEDED;
279 
280  friend class fdinbuf;
281 
282 protected:
285 
286 public:
288 
294  fdistream( int fd, bool isCloseNeeded = false );
295 
296  //
297  // Close the input file descriptor if necessary.
298  // This will be done whether or not the close needed flag is set.
299  //
300 
302  void close();
303 };
304 
305 } // namespace cfi
306 } // namespace VICI
307 
308 #endif
FD(const std::string &name, int mode, bool lock)
constructor which opens the file
Definition: fd.cpp:35
~FD()
destructor
Definition: fd.cpp:62
int flushBuffer()
Flush the characters in the buffer.
Definition: fdostream.cpp:77
virtual ~fdinbuf()
destructor.
Definition: fdistream.cpp:59
A file descriptor input stream.
Definition: fdstream.h:274
static const int PUSHBACK_SIZE
maximum number of push back characters supported
Definition: fdstream.h:226
bool mIsCloseNeeded
Whether the file descriptor needs to be closed on destruction.
Definition: fdstream.h:223
An output stream buffer.
Definition: fdstream.h:107
fdostream(int fd, bool isCloseNeeded=false)
constructor
Definition: fdostream.cpp:182
virtual int overflow(int c)
Write the indicated character and all previous characters.
Definition: fdostream.cpp:104
fdostream & mOwner
Our owning output stream.
Definition: fdstream.h:111
static bool CLOSE_NEEDED
Flag to indicate file descriptor should be closed.
Definition: fdstream.h:174
static bool CLOSE_NOT_NEEDED
Flag to indicate file descriptor should not be closed.
Definition: fdstream.h:278
static const int BUFFER_SIZE
Size of buffer.
Definition: fdstream.h:225
A file descriptor output stream.
Definition: fdstream.h:171
void close()
Close the output file descriptor.
Definition: fdostream.cpp:139
fdinbuf mBuf
Our input file descriptor stream buffer.
Definition: fdstream.h:284
bool mIsCloseNeeded
Whether the file descriptor needs to be closed on destruction.
Definition: fdstream.h:117
fdistream & mOwner
Our owning input stream.
Definition: fdstream.h:217
Wrap file descriptors in a class to ensure closed.
Definition: fdstream.h:59
int mFd
The input file descriptor.
Definition: fdstream.h:220
virtual int sync()
Flush the data in the buffer.
Definition: fdostream.cpp:128
FD(int x)
constructor for existing file descriptor
Definition: fdstream.h:77
void close()
close the file descriptor
Definition: fdistream.cpp:190
fdoutbuf mBuf
Our output file descriptor stream buffer.
Definition: fdstream.h:181
char mBuffer[BUFFER_SIZE]
Output buffer.
Definition: fdstream.h:123
fdoutbuf(int fd, bool isCloseNeeded, fdostream &owner)
constructor
Definition: fdostream.cpp:39
static void report(std::ostream &s, int fd)
Report the details or status of a file descriptor.
Definition: fd.cpp:71
void close()
close the file descriptor
Definition: fdostream.cpp:191
fdistream(int fd, bool isCloseNeeded=false)
constructor
Definition: fdistream.cpp:181
An input stream buffer.
Definition: fdstream.h:213
int mFd
The output file descriptor.
Definition: fdstream.h:114
virtual int underflow()
read characters from the buffer.
Definition: fdistream.cpp:75
void close()
Close the input file descriptor.
Definition: fdistream.cpp:144
virtual ~fdoutbuf()
destructor.
Definition: fdostream.cpp:54
static bool CLOSE_NEEDED
Flag to indicate file descriptor should be closed.
Definition: fdstream.h:277
char mBuffer[BUFFER_SIZE+PUSHBACK_SIZE]
Definition: fdstream.h:230
static const int BUFFER_SIZE
Output buffer size.
Definition: fdstream.h:120
static bool CLOSE_NOT_NEEDED
Flag to indicate file descriptor should not be closed.
Definition: fdstream.h:175
fdinbuf(int fd, bool isCloseNeeded, fdistream &owner)
constructor
Definition: fdistream.cpp:41