Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cfilestream.cpp
#include "cfilestream.h"
using namespace P6R;
//
// Declare the interfaces we plan to use.
//
P6DECLARE_CID(p6UnbufferedFile);
P6DECLARE_IID(IFileStreamInit);
namespace P6EXAMPLES {
CFileStream::CFileStream()
: m_cRef( 0 ),
m_bInitialized( false ),
m_cpFile(),
m_pszFilepath(NULL)
{
}
CFileStream::~CFileStream()
{
if(m_cpStr) {
if(m_pszFilepath) m_cpStr->wstrfree(m_pszFilepath);
}
}
//
// P6COM Helper macro to provide the implementation
// of standard COM Methods:
//
// createInstance() - Used to easily create instance of this component.
// Using this method to create components is preferred
// because it returns an interface which provides
// a well defined API for this component and prevents
// access to the classes internals.
// queryInterface() - Used to query the component for interfaces
// addref() - Increased the interfaces reference count
// release() - Decreases the interfaces reference count
// and destroys the component when the count
// reaches zero.
//
// These helper macros are provided for convenience and currently support
// up to 17 interfaces (this example exposes 2 interfaces). The implementation
// provided by these macros are threadsafe. You can find the defintions
// in p6comhlpr.h. If you need something more that what they provide,
// you are always free implement your own methods.
//
// See CConsoleStream in this example (in ex-load-jsn.cpp) for a non-threadsafe
// example of implementing these methods.
//
// Please note that these macros may only be used in code that is called
// after p6InitializeLoader() has been called.
//
P6_IMPLEMENT_ICOM2(CFileStream,IFileStreamInit,p6IDataStream);
P6COMMETHODIMPL CFileStream::initialize( const P6R::P6WCHAR* pFilePath )
{
P6ERR err = eOk;
if (m_bInitialized)
return eAlreadyInitialized;
//
// Create and initialize the components we need and
// retrieve any runtime interfaces we plan on using.
//
if(P6SUCCEEDED(err = p6CreateInstance(NULL,CID_p6UnbufferedFile,VALIDATECOMPTR(p6IUnbufferedFile,m_cpFile)))) {
if(P6SUCCEEDED(err = m_cpFile->initialize())) {
if(P6SUCCEEDED(err = m_cpStr->wstrdup(pFilePath,&m_pszFilepath))) {
m_bInitialized = P6TRUE;
}
}
}
}
return err;
}
P6COMMETHODIMPL CFileStream::beginStream()
{
if (!m_bInitialized)
return P6R::eNotInitialized;
//
// Create/open the file for write/append
//
return m_cpFile->open(m_pszFilepath,P6FILE_WRITE|P6FILE_OPENALWAYS|P6FILE_APPEND,P6FP_DEFAULT);
}
P6COMMETHODIMPL CFileStream::processStream( const P6R::P6VOID *pData, P6R::P6UINT32 cData )
{
P6SIZE cWritten = 0;
if (!m_bInitialized)
return eNotInitialized;
return m_cpFile->write(pData,cData,&cWritten);
}
P6COMMETHODIMPL CFileStream::endStream()
{
if (!m_bInitialized)
return eNotInitialized;
return m_cpFile->close();
}
}