Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cconsolestream.cpp
#include <stdio.h> // printf
#include <stdlib.h>
#include <string.h> // memcpy()
#include <memory> // nothrow
#include "cconsolestream.h"
using namespace P6R;
P6DECLARE_IID(p6ICom); // Base p6Com interface
P6DECLARE_IID(p6IDataStream); // Data stream interface
extern "C" P6ERR createStream(p6IDataStream **ppStream)
{
return P6EXAMPLES::CConsoleStream::createInstance( NULL, VALIDATEIF( p6IDataStream, ppStream ));
}
namespace P6EXAMPLES {
CConsoleStream::CConsoleStream() : m_cRef(0),
m_pBuffer(NULL),
m_cBuffer(0)
{ }
CConsoleStream::~CConsoleStream()
{
if (m_pBuffer) free(m_pBuffer);
}
//
// Standard p6COM queryInterface() method.
//
P6COMMETHODIMPL CConsoleStream::queryInterface(const P6REFIID iid,P6R::P6VOID **ppIface)
{
if (!ppIface) return P6R::eAccessFault;
*ppIface = NULL;
if(iid == IID_p6ICom) *ppIface = static_cast<p6IDataStream*>(this);
else if(iid == IID_p6IDataStream) *ppIface = static_cast<p6IDataStream*>(this);
else return P6R::eNoInterface;
reinterpret_cast<p6ICom*>(*ppIface)->addref();
return P6R::eOk;
}
//
// Standard p6COM addref() method.
//
P6R::P6INT32 CConsoleStream::addref()
{
return m_cRef++;
}
//
// Standard p6COM release() method.
//
P6R::P6INT32 CConsoleStream::release()
{
P6R::P6INT32 tmp = 0;
if(0 == (tmp = (--m_cRef))) { delete this; }
return tmp;
}
//
// beginStream() method prepares the stream for use.
// For this simple case there is nothing to do here.
//
P6COMMETHODIMPL CConsoleStream::beginStream() { return P6R::eOk; }
//
// processStream() is call to pass data to the data stream.
// This method then performs it's stream specific
// operations on the data (in this case, writing it out
// to stdout).
//
// Since printf() requires an ASCIIZ string (NULL terminated string),
// this method first copies the data into a buffer and NULL
// terminates, then passes that to printf().
//
P6COMMETHODIMPL CConsoleStream::processStream(const P6R::P6VOID* pData,P6R::P6UINT32 cData)
{
//
// Free the buffer if we do not have enough room
//
if (m_cBuffer && cData+1 > m_cBuffer)
{
free(m_pBuffer);
m_pBuffer = NULL;
m_cBuffer = 0;
}
//
// Allocate the buffer if not already allocated
//
if (!m_pBuffer)
{
m_pBuffer = (P6R::P6CHAR*) malloc(cData+1);
m_cBuffer = cData;
}
//
// Output the data to the stdout
//
if (m_pBuffer)
{
memcpy(m_pBuffer,pData,cData);
m_pBuffer[cData] = '\0';
printf(m_pBuffer);
}
return P6R::eOk;
}
//
// endStream() is called to notify the stream that there is
// no more data to be processed. In this simple case there is
// nothing to do.
//
P6COMMETHODIMPL CConsoleStream::endStream() { return P6R::eOk; }
//
// This method creates a new uninitialized instance
// of our component and returns a pointer to the
// requested interface.
//
P6R::P6ERR CConsoleStream::createInstance(P6R::p6ICom *pOuter,const P6REFIID iid,P6R::P6VOID **ppIface)
{
P6R::P6ERR err = P6R::eNoMemory;
CConsoleStream *pObj = NULL;
if(NULL != pOuter) return P6R::eNoAgregation;
if(NULL == ppIface) return P6R::eAccessFault;
*ppIface = NULL;
if(NULL != (pObj = new (std::nothrow) CConsoleStream())) {
pObj->addref();
err = pObj->queryInterface(iid,ppIface);
pObj->release();
}
return err;
}
} // namespace