31 template <
typename T,
int N=1024>
class fbuf ;
53template <
typename T,
int N>
57 using read_fn_t = std::function<ssize_t(T,
char*,std::size_t)> ;
58 using write_fn_t = std::function<ssize_t(T,
const char*,std::size_t)> ;
59 using close_fn_t = std::function<void(T)> ;
61 explicit fbuf( read_fn_t , write_fn_t , close_fn_t ) ;
64 explicit fbuf( T
file , read_fn_t , write_fn_t , close_fn_t ) ;
84 int sync() final override ;
90 fbuf<T,N> & operator=( const
fbuf<T,N> & ) = delete ;
91 fbuf<T,N> & operator=(
fbuf<T,N> && ) = delete ;
94 using traits_type =
std::streambuf::traits_type ;
98 static constexpr
int sync_ok = 0 ;
99 static constexpr
int sync_fail = -1 ;
100 read_fn_t m_read_fn ;
101 write_fn_t m_write_fn ;
102 close_fn_t m_close_fn ;
103 std::vector<
char> m_input ;
104 std::vector<
char> m_output ;
109template <typename T,
int N>
110G::
fbuf<T,N>::
fbuf(
G::
fbuf<T,N>::read_fn_t read ,
G::
fbuf<T,N>::write_fn_t write ,
G::
fbuf<T,N>::close_fn_t close ) :
114 m_input(static_cast<
std::
size_t>(N)) ,
115 m_output(static_cast<
std::
size_t>(N)) ,
121template <
typename T,
int N>
122G::fbuf<T,N>::fbuf( T
file , G::fbuf<T,N>::read_fn_t read , G::fbuf<T,N>::write_fn_t write , G::fbuf<T,N>::close_fn_t close ) :
126 m_input(static_cast<
std::size_t>(N)) ,
127 m_output(static_cast<
std::size_t>(N)) ,
136 template <
typename T,
int N>
143template <
typename T,
int N>
151 char * input_begin = &m_input[0] ;
152 setg( input_begin , input_begin , input_begin ) ;
154 char * output_begin = &m_output[0] ;
155 char * output_end = output_begin + m_output.size() ;
156 setp( output_begin , output_end-1 ) ;
159template <
typename T,
int N>
165 m_close_fn( m_file ) ;
166 m_file_open = false ;
170template <
typename T,
int N>
173 if( !traits_type::eq_int_type( c , traits_type::eof() ) )
175 *pptr() = traits_type::to_char_type( c ) ;
178 return sync() == sync_fail ? traits_type::eof() : traits_type::not_eof(c) ;
181template <
typename T,
int N>
184 if( pbase() == pptr() )
190 std::size_t size = pptr() - pbase() ;
191 ssize_t nwrite = m_write_fn( m_file , pbase() , size ) ;
196 else if(
static_cast<std::size_t
>(nwrite) < size )
198 std::copy( pbase()+nwrite , pptr() , pbase() ) ;
199 setp( pbase() , epptr() ) ;
200 pbump(
static_cast<int>(size-nwrite) ) ;
205 setp( pbase() , epptr() ) ;
211template <
typename T,
int N>
214 if( gptr() == egptr() )
216 char * input_begin = &m_input[0] ;
217 ssize_t nread = m_read_fn( m_file , input_begin , m_input.size() ) ;
219 return traits_type::eof() ;
220 std::size_t nreadu = nread >= 0 ?
static_cast<std::size_t
>(nread) : std::size_t(0U) ;
221 setg( input_begin , input_begin , input_begin+nreadu ) ;
223 return traits_type::to_int_type( *gptr() ) ;
226template <
typename T,
int N>
A simple file streambuf using a file descriptor and three function pointers for read,...
int underflow() override
Called to pull a character out of the input buffer, and pre-fill the input buffer if necessary.
void open(T file)
Installs the given file descriptor.
int sync() final override
Called to sync the stream.
int overflow(int c) override
Called to put a character into the output buffer.
fbuf(T file, read_fn_t, write_fn_t, close_fn_t)
Constructor passed an open file descriptor.
~fbuf() override
Destructor. Closes the file.
T file() const
Returns the current file descriptor.
fbuf(read_fn_t, write_fn_t, close_fn_t)
Constructor. Use open() to initialise.