icinga2/lib/base/stdiostream.cpp

58 lines
1.1 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2014-05-25 16:23:35 +02:00
#include "base/stdiostream.hpp"
#include "base/objectlock.hpp"
using namespace icinga;
2012-11-26 08:29:26 +01:00
/**
* Constructor for the StdioStream class.
*
* @param innerStream The inner stream.
* @param ownsStream Whether the new object owns the inner stream. If true
* the stream's destructor deletes the inner stream.
*/
2013-03-16 21:18:53 +01:00
StdioStream::StdioStream(std::iostream *innerStream, bool ownsStream)
2013-04-04 16:08:02 +02:00
: m_InnerStream(innerStream), m_OwnsStream(ownsStream)
{ }
StdioStream::~StdioStream()
{
Close();
}
size_t StdioStream::Read(void *buffer, size_t size, bool allow_partial)
{
2013-03-01 12:07:52 +01:00
ObjectLock olock(this);
2013-04-04 16:08:02 +02:00
m_InnerStream->read(static_cast<char *>(buffer), size);
return m_InnerStream->gcount();
}
void StdioStream::Write(const void *buffer, size_t size)
{
2013-03-01 12:07:52 +01:00
ObjectLock olock(this);
m_InnerStream->write(static_cast<const char *>(buffer), size);
}
void StdioStream::Close()
{
Stream::Close();
if (m_OwnsStream) {
2012-11-23 12:34:06 +01:00
delete m_InnerStream;
m_OwnsStream = false;
}
}
bool StdioStream::IsDataAvailable() const
{
return !IsEof();
}
bool StdioStream::IsEof() const
{
return !m_InnerStream->good();
}