Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / MFC

CByteArrayFile - storing objects in databases using DAO

Rate me:
Please Sign up or sign in to vote.
4.42/5 (6 votes)
17 Mar 2000 75.7K   2.2K   27  
A class used to serialize object into a database field
// ByteArrayFile.cpp: implementation of the CByteArrayFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ByteArrayFile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CByteArrayFile::CByteArrayFile(UINT nGrowBytes)
	: CMemFile(nGrowBytes)
{
	m_pByteArray = NULL;
	m_bAllowGrow = TRUE;
}

CByteArrayFile::~CByteArrayFile()
{
	if (m_lpBuffer)
		Close();        // call appropriate Close/Free
	ASSERT(m_lpBuffer == NULL);
}

void CByteArrayFile::SetArray(CByteArray* pArray, BOOL bAllowGrow)
{
	ASSERT(m_pByteArray == NULL);        // do once only
	ASSERT(m_lpBuffer == NULL);     // do once only
	ASSERT(m_nPosition == 0);

	m_pByteArray = pArray;
	m_lpBuffer = (BYTE*)m_pByteArray->GetData();
	m_nBufferSize = m_nFileSize = m_pByteArray->GetSize();
	m_bAllowGrow = bAllowGrow;
}

BYTE* CByteArrayFile::Alloc(DWORD nBytes)
{
	ASSERT(m_pByteArray != NULL);       // do once only
	m_pByteArray->SetSize(nBytes);
	return m_pByteArray->GetData();
}

BYTE* CByteArrayFile::Realloc(BYTE*, DWORD nBytes)
{
	TRACE("    CByteArrayFile::Realloc(BYTE*, DWORD nBytes) %d\n", nBytes );
	
	if (!m_bAllowGrow)
		return NULL;
	ASSERT(m_pByteArray != NULL);
	m_pByteArray->SetSize(nBytes);
	return m_pByteArray->GetData();
}

void CByteArrayFile::Free(BYTE*)
{
	ASSERT(m_pByteArray != NULL);
	m_pByteArray->RemoveAll();
}

CByteArray* CByteArrayFile::Detach()
{
	CByteArray* pRet;
	ASSERT(m_pByteArray != NULL);

	// shrink memory
	m_pByteArray->SetSize(m_nFileSize);
	m_pByteArray->FreeExtra();

	pRet = m_pByteArray;

	m_pByteArray = NULL;

	// re-initialize the CMemFile parts too
	m_lpBuffer = NULL;
	m_nBufferSize = 0;
	return pRet;
}

IMPLEMENT_DYNAMIC(CByteArrayFile, CMemFile)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions