posix.h
Go to the documentation of this file.
#ifndef WIBBLE_STREAM_POSIX_H
#define WIBBLE_STREAM_POSIX_H
#include <wibble/exception.h>
#include <streambuf>
#include <unistd.h>
#include <cstdio>
// http://www.icce.rug.nl/documents/cplusplus/cplusplus21.html#CONCRETE
// 21.2.1: Classes for output operations
namespace wibble {
namespace stream {
class PosixBuf : public std::streambuf
{
private:
// The output buffer
char* m_buf;
size_t m_buf_size;
int m_fd;
// Disallow copy
PosixBuf(const PosixBuf&);
PosixBuf& operator=(const PosixBuf&);
public:
// PosixBuf functions
PosixBuf() : m_buf(0), m_buf_size(0), m_fd(-1) {}
PosixBuf(int fd, size_t bufsize = 4096) : m_buf(0), m_buf_size(0), m_fd(-1)
{
attach(fd, bufsize);
}
~PosixBuf()
{
if (m_buf)
{
sync();
delete[] m_buf;
}
if (m_fd != -1)
{
::close(m_fd);
}
}
void attach(int fd, size_t bufsize = 4096)
{
m_buf = new char[1024];
if (!m_buf)
throw wibble::exception::Consistency("allocating 1024 bytes for posix stream buffer", "allocation failed");
m_fd = fd;
m_buf_size = 1024;
setp(m_buf, m_buf + m_buf_size);
}
int detach()
{
sync();
int res = m_fd;
delete[] m_buf;
m_buf = 0;
m_buf_size = 0;
m_fd = -1;
// TODO: setp or anything like that, to tell the streambuf that there's
// no buffer anymore?
setp(0, 0);
return res;
}
int fd() const { return m_fd; }
// Functions from strbuf
int overflow(int c)
{
sync();
if (c != EOF)
{
*pptr() = c;
pbump(1);
}
return c;
}
int sync()
{
if (pptr() > pbase())
{
int amount = pptr() - pbase();
int res = ::write(m_fd, m_buf, amount);
if (res != amount)
throw wibble::exception::System("writing to file");
setp(m_buf, m_buf + m_buf_size);
}
return 0;
}
};
}
}
#endif
wibble::stream::PosixBuf::overflow
int overflow(int c)
Definition: posix.h:91
wibble::exception::Consistency
Exception thrown when some consistency check fails.
Definition: exception.h:254
wibble::stream::PosixBuf::detach
int detach()
Sync the PosixBuf and detach it from the file descriptor.
Definition: posix.h:71
wibble::stream::PosixBuf::~PosixBuf
~PosixBuf()
Definition: posix.h:35
exception.h
wibble::stream::PosixBuf
Definition: posix.h:15
wibble::stream::PosixBuf::attach
void attach(int fd, size_t bufsize=4096)
Attach the stream to a file descriptor, using the given stream size.
Definition: posix.h:54
wibble::stream::PosixBuf::PosixBuf
PosixBuf(int fd, size_t bufsize=4096)
Definition: posix.h:31
wibble
Definition: amorph.h:17
wibble::stream::PosixBuf::fd
int fd() const
Access the underlying file descriptor.
Definition: posix.h:86
wibble::stream::PosixBuf::PosixBuf
PosixBuf()
Definition: posix.h:30
wibble::exception::System
Base class for system exceptions.
Definition: exception.h:396
wibble::stream::PosixBuf::sync
int sync()
Definition: posix.h:101