2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-11-23 05:02:34 -05:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/stdiostream.hpp"
|
|
|
|
|
#include "base/objectlock.hpp"
|
2012-11-23 05:02:34 -05:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2012-11-26 02:29:26 -05: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 16:18:53 -04:00
|
|
|
StdioStream::StdioStream(std::iostream *innerStream, bool ownsStream)
|
2013-04-04 10:08:02 -04:00
|
|
|
: m_InnerStream(innerStream), m_OwnsStream(ownsStream)
|
|
|
|
|
{ }
|
2012-11-23 05:02:34 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
StdioStream::~StdioStream()
|
2013-04-19 08:59:38 -04:00
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 11:01:24 -04:00
|
|
|
size_t StdioStream::Read(void *buffer, size_t size)
|
2012-11-23 05:02:34 -05:00
|
|
|
{
|
2013-03-01 06:07:52 -05:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
2013-04-04 10:08:02 -04:00
|
|
|
m_InnerStream->read(static_cast<char *>(buffer), size);
|
|
|
|
|
return m_InnerStream->gcount();
|
2012-11-23 05:02:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StdioStream::Write(const void *buffer, size_t size)
|
|
|
|
|
{
|
2013-03-01 06:07:52 -05:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
2012-11-23 05:02:34 -05:00
|
|
|
m_InnerStream->write(static_cast<const char *>(buffer), size);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void StdioStream::Close()
|
2012-11-23 05:02:34 -05:00
|
|
|
{
|
2015-11-08 15:23:04 -05:00
|
|
|
Stream::Close();
|
|
|
|
|
|
2013-04-19 08:59:38 -04:00
|
|
|
if (m_OwnsStream) {
|
2012-11-23 06:34:06 -05:00
|
|
|
delete m_InnerStream;
|
2013-04-19 08:59:38 -04:00
|
|
|
m_OwnsStream = false;
|
|
|
|
|
}
|
2012-11-23 05:02:34 -05:00
|
|
|
}
|
2013-08-27 06:21:41 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
bool StdioStream::IsDataAvailable() const
|
2015-06-24 03:44:59 -04:00
|
|
|
{
|
|
|
|
|
return !IsEof();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
bool StdioStream::IsEof() const
|
2013-08-27 06:21:41 -04:00
|
|
|
{
|
2015-02-27 03:03:23 -05:00
|
|
|
return !m_InnerStream->good();
|
2013-08-27 06:21:41 -04:00
|
|
|
}
|