Skip to content
Snippets Groups Projects
Commit 75d11e43 authored by Radovan Červený's avatar Radovan Červený
Browse files

implemented fdstream, introduced iostream extension with new fdstreams

parent e5cf4ea9
No related branches found
No related tags found
1 merge request!14BP_cervera3 - new measurements
/*
* Author: Radovan Cerveny
*/
#include "fdstream.hpp"
namespace std {
fdstreambuf::fdstreambuf ( int fd ) : fd ( fd ) {
setp ( buff.begin ( ), buff.end ( ) - 1 );
}
bool fdstreambuf::flush ( ) {
std::ptrdiff_t n = pptr ( ) - pbase ( );
ssize_t osz = write ( fd, buff.data ( ), n );
pbump ( -n );
return osz != -1;
}
fdstreambuf::int_type fdstreambuf::overflow ( fdstreambuf::int_type ch ) {
if ( ch == traits_type::eof ( ) ) return traits_type::eof ( );
buff.back ( ) = ch;
pbump ( 1 );
if ( !flush ( ) ) return traits_type::eof ( );
return ch;
}
int fdstreambuf::sync ( ) {
return flush ( ) ? 0 : -1;
}
fdaccessor::fdaccessor ( int new_fd ) {
int res = fcntl ( new_fd, F_GETFD );
fd = res == 0 ? new_fd : -1;
}
int fdaccessor::get_fd ( ) {
return fd;
}
ofdstream::ofdstream ( int fd ) : fda ( fd ), fdbuf ( fda.get_fd ( ) ) {
this->init ( & fdbuf );
if ( fda.get_fd ( ) == -1 )
setstate ( ios_base::failbit );
}
}
/*
* Author: Radovan Cerveny
*/
/*
* ofdstream
* ofdstream takes a file descriptor (fd) by constructor to use for output
* if the file descriptor is not valid for any reason during construction of the ofdstream, ofdstream will set its state to ios::fail
* if the file descriptor is valid, it is used for successive buffered output
*
* supplied file descriptor has to be already open in order for ofdstream to work
* 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
*/
#ifndef FDSTREAM_HPP_
#define FDSTREAM_HPP_
#include <unistd.h>
#include <fcntl.h>
#include <ostream>
#include <array>
namespace std {
class fdstreambuf : public streambuf {
static const size_t buff_sz = 512;
int fd;
array < char_type, buff_sz > buff;
bool flush ( );
protected:
int_type overflow ( int_type );
int sync ( );
public:
explicit fdstreambuf ( int );
fdstreambuf ( const fdstreambuf & ) = delete;
fdstreambuf & operator =( const fdstreambuf & ) = delete;
};
class fdaccessor {
int fd;
public:
explicit fdaccessor ( int );
int get_fd ( );
};
class ofdstream : public ostream {
fdaccessor fda;
fdstreambuf fdbuf;
public:
explicit ofdstream ( int );
virtual ~ofdstream ( ) { }
};
}
#endif /* FDSTREAM_HPP_ */
/*
* Author: Radovan Cerveny
*/
#include "iostream.hpp"
namespace std {
// used for measurements output
const int CMEASURE_FD = 5;
ofdstream cmeasure ( CMEASURE_FD );
}
/*
* Author: Radovan Cerveny
*/
#ifndef IOSTREAM_HPP_
#define IOSTREAM_HPP_
#include "fdstream.hpp"
namespace std {
// used for measurements output
extern const int CMEASURE_FD;
extern ofdstream cmeasure;
}
#endif /* IOSTREAM_HPP_ */
#ifndef __FDSTREAM_HEADER_WRAPPER_
#define __FDSTREAM_HEADER_WRAPPER_
#include "extensions/fdstream.hpp"
#endif /* __FDSTREAM_HEADER_WRAPPER_ */
#ifndef __IOSTREAM_HEADER_WRAPPER_
#define __IOSTREAM_HEADER_WRAPPER_
#include <bits/../iostream>
#include "extensions/iostream.hpp"
#endif /* __IOSTREAM_HEADER_WRAPPER_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment