Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
fdstream.hpp
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * \brief
26 * ofdstream takes a primary file descriptor (fd) and fallback file descriptor by constructor to use for output
27 * if the primary fd is not valid for any reason, ofdstream will try to use fallback fd, if even fallback fd is not valid, ofdstream sets its state to ios::fail
28 * if the primary fd (or fallback fd) is valid , it is used for successive buffered output
29 *
30 * supplied file descriptor has to be already open in order for ofdstream to work
31 * ofdstream does not close the file descriptor and does not change internal flags of the file descriptor during the lifetime of ofdstream and during destruction of ofdstream
32 *
33 * typical use is to use as primary fd some arbitrary fd (5) and for fallback fd some standard fd (2) if we wish to see the output
34 */
35
36#pragma once
37
38#include <unistd.h>
39#include <fcntl.h>
40#include <ostream>
41#include <istream>
42#include <array>
43#include <ext/ostream>
44
45namespace ext {
46
51extern const int CERR_FD;
52
57extern const int CMEASURE_FD;
58
63extern const int CLOG_FD;
64
69extern const int FAIL_FD;
70
76class fdstreambuf final : public std::streambuf {
81 static const size_t buff_sz = 512;
82
87 int fd;
88
93 std::array < char_type, buff_sz > buff;
94
99 bool flush ( );
100
101protected:
110 int_type pbackfail ( int_type ) override;
111
120 int_type underflow ( ) override;
121
130 int_type overflow ( int_type ) override;
131
140 int sync ( ) override;
141
142public:
149 explicit fdstreambuf ( int fileDescriptor );
150
155 ~fdstreambuf ( ) override;
156
161 fdstreambuf ( const fdstreambuf & ) = delete;
162
167 fdstreambuf & operator =( const fdstreambuf & ) = delete;
168};
169
181 int fd;
182
187 bool redirected;
188
189public:
194 explicit fdaccessor ( int, int );
195
202 int get_fd ( ) const;
203
210 bool is_redirected ( ) const;
211};
212
217class ofdstream : public ext::ostream {
222 fdaccessor fda;
223
228 fdstreambuf fdbuf;
229
230public:
238 explicit ofdstream ( int fd, int fallback_fd = FAIL_FD);
239
244 ~ofdstream ( ) override;
245
252 bool is_redirected ( ) const;
253};
254
259class ifdstream : public std::istream {
263 fdaccessor fda;
264
269 fdstreambuf fdbuf;
270
271public:
279 explicit ifdstream ( int, int = FAIL_FD);
280
285 ~ifdstream ( ) override;
286
293 bool is_redirected ( ) const;
294};
295
296} /* namespace ext */
297
A class determining the used file descriptor from prefered (if available) and fallback file descripto...
Definition: fdstream.hpp:176
int get_fd() const
Getter of the chosen file descriptor, either the prefered, or fallback one.
Definition: fdstream.cpp:97
bool is_redirected() const
Getter of the redirected flag informing whether prefered or fallback file descriptor is used.
Definition: fdstream.cpp:101
fdaccessor(int, int)
Constrcutor of the class determining the resulting used descriptor.
Definition: fdstream.cpp:84
Class representing buffered stream designed to work on defined file descriptor instead of usual file ...
Definition: fdstream.hpp:76
int_type pbackfail(int_type) override
Put character back in the case of backup underflow.
Definition: fdstream.cpp:62
fdstreambuf(const fdstreambuf &)=delete
Do not allow to copy construct the fdsreambuf.
int_type underflow() override
Get character on underflow.
Definition: fdstream.cpp:39
int sync() override
Synchronize stream buffer.
Definition: fdstream.cpp:80
~fdstreambuf() override
The destructor of the fdstreambuf.
Definition: fdstream.cpp:25
fdstreambuf(int fileDescriptor)
Constructor of the fdstreambuf.
Definition: fdstream.cpp:20
int_type overflow(int_type) override
Put character on overflow.
Definition: fdstream.cpp:68
fdstreambuf & operator=(const fdstreambuf &)=delete
Do not allow to copy assign the fdsreambuf.
A class implementing an input stream interface, with destination specified by file descriptor.
Definition: fdstream.hpp:259
bool is_redirected() const
Getter of the redirected flag informing whether prefered or fallback file descriptor is used.
Definition: fdstream.cpp:123
~ifdstream() override
A desctructor of the input file descriptor.
ifdstream(int, int=FAIL_FD)
A constructor of the input file descriptor stream with specified prefered and fallback file descripto...
Definition: fdstream.cpp:116
A class implementing an output stream interface, with destination specified by file descriptor.
Definition: fdstream.hpp:217
~ofdstream() override
A desctructor of the output file descriptor.
ofdstream(int fd, int fallback_fd=FAIL_FD)
A constructor of the output file descriptor stream with specified prefered and fallback file descript...
Definition: fdstream.cpp:105
bool is_redirected() const
Getter of the redirected flag informing whether prefered or fallback file descriptor is used.
Definition: fdstream.cpp:112
Definition: ostream.h:14
Definition: sigHandler.cpp:20
const int CLOG_FD
The number of logging stream descriptor. Expected to be 4.
Definition: fdstream.cpp:16
const int FAIL_FD
The number of descriptor when binding to the predefined one fails.
Definition: fdstream.cpp:18
const int CMEASURE_FD
The number of measurement stream descriptor. Expected to be 5.
Definition: fdstream.cpp:13
const int CERR_FD
The number of standard error stream descriptor. Expected to be 2.
Definition: fdstream.cpp:10