summaryrefslogtreecommitdiffstats
path: root/lib/base/stdiostream.cpp
blob: 449036f0372be6b452f9175eb45ba1e8fee05dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/stdiostream.hpp"
#include "base/objectlock.hpp"

using namespace icinga;

/**
 * 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.
 */
StdioStream::StdioStream(std::iostream *innerStream, bool ownsStream)
	: m_InnerStream(innerStream), m_OwnsStream(ownsStream)
{ }

StdioStream::~StdioStream()
{
	Close();
}

size_t StdioStream::Read(void *buffer, size_t size, bool allow_partial)
{
	ObjectLock olock(this);

	m_InnerStream->read(static_cast<char *>(buffer), size);
	return m_InnerStream->gcount();
}

void StdioStream::Write(const void *buffer, size_t size)
{
	ObjectLock olock(this);

	m_InnerStream->write(static_cast<const char *>(buffer), size);
}

void StdioStream::Close()
{
	Stream::Close();

	if (m_OwnsStream) {
		delete m_InnerStream;
		m_OwnsStream = false;
	}
}

bool StdioStream::IsDataAvailable() const
{
	return !IsEof();
}

bool StdioStream::IsEof() const
{
	return !m_InnerStream->good();
}