2012-11-23 11:02:34 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2012-11-23 11:02:34 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/stdiostream.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2012-11-23 11:02:34 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{ }
|
2012-11-23 11:02:34 +01:00
|
|
|
|
2013-04-19 14:59:38 +02:00
|
|
|
StdioStream::~StdioStream(void)
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
size_t StdioStream::Read(void *buffer, size_t size, bool allow_partial)
|
2012-11-23 11:02:34 +01:00
|
|
|
{
|
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();
|
2012-11-23 11:02:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StdioStream::Write(const void *buffer, size_t size)
|
|
|
|
{
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
m_InnerStream->write(static_cast<const char *>(buffer), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StdioStream::Close(void)
|
|
|
|
{
|
2013-04-19 14:59:38 +02:00
|
|
|
if (m_OwnsStream) {
|
2012-11-23 12:34:06 +01:00
|
|
|
delete m_InnerStream;
|
2013-04-19 14:59:38 +02:00
|
|
|
m_OwnsStream = false;
|
|
|
|
}
|
2012-11-23 11:02:34 +01:00
|
|
|
}
|
2013-08-27 12:21:41 +02:00
|
|
|
|
2015-06-24 09:44:59 +02:00
|
|
|
bool StdioStream::IsDataAvailable(void) const
|
|
|
|
{
|
|
|
|
return !IsEof();
|
|
|
|
}
|
|
|
|
|
2013-08-27 12:21:41 +02:00
|
|
|
bool StdioStream::IsEof(void) const
|
|
|
|
{
|
2015-02-27 09:03:23 +01:00
|
|
|
return !m_InnerStream->good();
|
2013-08-27 12:21:41 +02:00
|
|
|
}
|