Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » General » Downloads
 
Add your own
alternative version
Go to top

The alxBase classes for work with DBF files

, 5 Nov 2002
The alxBase classes for work with dbf files.
alxbasea2.zip
alxBASE
alxBASE.dsp
alxBASE.dsw
alxBaseDLL.dsp
DLL
INCLUDE
res
nodrop.cur
Lib
Samples
DBFView
DBFViewApp.clw
DBFViewApp.dep
DBFViewApp.dsp
DBFViewApp.dsw
Release
DBFView.exe
res
bitmap1.bmp
DBFViewApp.ico
DBFViewDoc.ico
Toolbar.bmp
SRC
alxbasea3.zip
alxBASE.dep
alxBASE.dsp
alxBASE.dsw
alxBASE.mak
alxBASE.suo
alxBaseDLL.dsp
l.rus
nodrop.cur
DBFViewApp.clw
DBFViewApp.dep
DBFViewApp.dsp
DBFViewApp.dsw
RCa00472
DBFView.exe
bitmap1.bmp
DBFViewApp.ico
DBFViewDoc.ico
Toolbar.bmp
// FPTMemoFile.cpp : implementation of the CFPTMemoFile class
//
/////////////////////////////////////////////////////////////////////////////
/*
�����:              �������� �.�.
����������:         alxsoft@gazinter.net
*/
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FPTMemoFile.h"
#include <math.h>

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

//////////////////////////////////////////////////////////////////////////
// CFPTMemoFile
IMPLEMENT_DYNAMIC(CFPTMemoFile, CMemoFile)

/* ����������� */
CFPTMemoFile::CFPTMemoFile()
{

}

/* ���������� */
CFPTMemoFile::~CFPTMemoFile()
{
	Close();
}

/* ��������� ������� */
void CFPTMemoFile::Open(LPCTSTR lpszName, UINT nOpenFlag)
{
	ASSERT_VALID(this);
	ASSERT(lpszName != NULL);

	// ��������� ��� �������� ����
	nOpenFlag = nOpenFlag | CFile::typeBinary;

	// Re-open is invalid
	if (IsOpen())
	{
		ASSERT(FALSE);
		return;
	}


TRY
{

	// ��������� ���� ����
	DBF_CHECK_ERROR(
		CFile::Open(lpszName, nOpenFlag), ALX_DBF_ERROR_MEMO_OPEN);

	// ������ ���������
	DBF_CHECK_ERROR(
		ReadHeader(),ALX_DBF_ERROR_MEMO_HEAD_READ);

	// ��������� ������ �����
	DBF_CHECK_ERROR(m_nBlockSize > 0,ALX_DBF_ERROR_MEMO_BLOCK_SIZE);

	m_bOpen = TRUE;
}
CATCH(CFileException, e)
{
	e->ReportError();
	e->Delete();
	Close();
}
END_CATCH

}

/* ��������� ������� */
void CFPTMemoFile::Close()
{
	if(m_bOpen)
		CMemoFile::Close();
}

UINT CFPTMemoFile::GetMemoFileType()
{
	return MEMO_TYPE_FOXPRO;
}

/* ������ ��������� memo ����� */
BOOL CFPTMemoFile::ReadHeader()
{
	ASSERT_VALID(this);
	ASSERT(m_hFile != NULL);

TRY
{
	Seek(0, CFile::begin);

	// ������ ��������� ���� �����
	if(Read(&m_FPTHead, sizeof(FPT_HEAD)) != sizeof(FPT_HEAD))
		return FALSE;

	m_nNextBlock = SWAP_DWORD_BYTE(m_FPTHead.next_block);
	m_nBlockSize = SWAP_WORD_BYTE(m_FPTHead.block_size);

	return TRUE;
}
CATCH(CFileException, e)
{
	e->ReportError();
	e->Delete();

} END_CATCH

	return FALSE;
}

/* ���������� ��������� ������� */
BOOL CFPTMemoFile::WriteHeader()
{
	ASSERT_VALID(this);
	ASSERT(m_hFile != NULL);

TRY
{
	Seek(0, CFile::begin);

	// ���������� ��������� ���� �����
	Write(&m_FPTHead, sizeof(FPT_HEAD));

	return TRUE;
}
CATCH(CFileException, e)
{
	e->ReportError();
	e->Delete();

} END_CATCH

	return FALSE;
}

/* ������ ���� ���� */
void CFPTMemoFile::ReadMemoRecord(DBF_ULONG lOffsetRec, DBF_LONG& nLen, CString& strMemo)
{
	ASSERT_VALID(this);
	ASSERT(lOffsetRec > 0);

	strMemo = "";

	if(!IsOpen())
		return;

TRY
{
	DBF_LONG  nLenMemo = 0;

	Seek(lOffsetRec * m_nBlockSize + sizeof(DBF_LONG),CFile::begin);

	// �������� ����� ����
	DBF_CHECK_ERROR(
		Read(&nLenMemo, sizeof(nLenMemo)) == sizeof(DBF_LONG),
		ALX_DBF_ERROR_MEMO_FIELD_READ);

	nLenMemo = SWAP_DWORD_BYTE(nLenMemo);

	// ��������� ����� ����
	DBF_CHECK_ERROR(nLenMemo > 0,ALX_DBF_ERROR_MEMO_FIELD_READ);

	char* buffer = strMemo.GetBuffer(nLenMemo + 1);

	// ������ ���� ����
	DBF_CHECK_ERROR(
		Read(buffer, nLenMemo) == (UINT)nLenMemo,
		ALX_DBF_ERROR_MEMO_FIELD_READ);

	buffer[nLenMemo] = '\0';
	strMemo.ReleaseBuffer(nLenMemo + 1);

	nLen = nLenMemo;

}
CATCH(CFileException, e)
{
	e->ReportError();
	e->Delete();

	ALXThrowDBFException(ALX_DBF_ERROR_MEMO_FIELD_READ);

} END_CATCH

}

/* ������ ���� ���� */
void CFPTMemoFile::ReadMemoRecord(DBF_ULONG lOffsetRec, DBF_LONG& nLen, unsigned char** pMemoData)
{
	ASSERT_VALID(this);
	ASSERT(lOffsetRec > 0);

	*pMemoData = NULL;

	if(!IsOpen())
		return;

TRY
{
	DBF_LONG  nLenMemo = 0;

	Seek(lOffsetRec * m_nBlockSize + sizeof(DBF_LONG),CFile::begin);

	// �������� ����� ����
	DBF_CHECK_ERROR(
		Read(&nLenMemo, sizeof(nLenMemo)) == sizeof(DBF_LONG),
		ALX_DBF_ERROR_MEMO_FIELD_READ);

	nLenMemo = SWAP_DWORD_BYTE(nLenMemo);

	// ��������� ����� ����
	DBF_CHECK_ERROR(nLenMemo > 0,ALX_DBF_ERROR_MEMO_FIELD_READ);

	*pMemoData = new unsigned char[nLenMemo];

	// ������ ���� ����
	DBF_CHECK_ERROR(
		Read(*pMemoData, nLenMemo) == (UINT)nLenMemo,
		ALX_DBF_ERROR_MEMO_FIELD_READ);

	nLen = nLenMemo;

}
CATCH(CFileException, e)
{
	e->ReportError();
	e->Delete();

	ALXThrowDBFException(ALX_DBF_ERROR_MEMO_FIELD_READ);

} END_CATCH

}

/* ���������� � ���� ���� */
void CFPTMemoFile::WriteMemoRecord(DBF_ULONG& lOffsetRec, DBF_LONG& nLen, CString& strMemo)
{
	ASSERT_VALID(this);

	if(!IsOpen())
		return;

TRY
{
	DBF_LONG  nLenMemo = 0;

	// ��������� ����� ����
	if(strMemo.IsEmpty())
		lOffsetRec = 0;
	// ���� ������ ����������
	else if(lOffsetRec > 0)
	{
		Seek(lOffsetRec * m_nBlockSize + sizeof(DBF_LONG),CFile::begin);

		// �������� ����� ����
		DBF_CHECK_ERROR(
			Read(&nLenMemo, sizeof(nLenMemo)) == sizeof(DBF_LONG),
			ALX_DBF_ERROR_MEMO_FIELD_READ);

		// �������� ������� ����� ����
		nLenMemo = SWAP_DWORD_BYTE(nLenMemo);
		// �������� ����������� ��������� ����� (� ������ ������������ �� ������� �����)
		nLenMemo = (DBF_LONG) ceil(((double)(nLenMemo + sizeof(DBF_LONG)))/((double)m_nBlockSize)) * m_nBlockSize - sizeof(DBF_LONG);

		// ���� ������ �� �������� �� �������
		if(nLenMemo < strMemo.GetLength())
			// ��������� ������
			lOffsetRec = AddMemoRecord(strMemo);
		else
			// ��������� ������
			UpdateMemoRecord(lOffsetRec, strMemo);
	}
	else
		// ��������� ������
		lOffsetRec = AddMemoRecord(strMemo);

}
CATCH(CFileException, e)
{
	e->ReportError();
	e->Delete();

	ALXThrowDBFException(ALX_DBF_ERROR_MEMO_FIELD_WRITE);

} END_CATCH

}

/* ��������� memo ���� */
DBF_LONG CFPTMemoFile::AddMemoRecord(CString& strMemo)
{
	// ����� ������
	DBF_ULONG lOffsetRec = 0;

TRY
{
	DBF_LONG nSign = MF_TEXTREC;
	DBF_LONG nLenMemo = strMemo.GetLength();

	ReadHeader();

	lOffsetRec = m_nNextBlock;
	m_nNextBlock += (DBF_LONG) ceil((double)(nLenMemo + sizeof(nSign)) / (double)m_nBlockSize);
	m_FPTHead.next_block = SWAP_DWORD_BYTE(m_nNextBlock);

	Seek(lOffsetRec * m_nBlockSize,CFile::begin);
		
	// ���������� ��������� �����
	nSign = SWAP_DWORD_BYTE(nSign); // ������ � ������������ ����
	Write(&nSign, sizeof(nSign));

	// ���������� ����� ������
	nLenMemo = SWAP_DWORD_BYTE(nLenMemo); // ������ � ������������ ����
	Write(&nLenMemo, sizeof(nLenMemo));
		
	// ���������� ������
	Write((LPCSTR)strMemo, strMemo.GetLength());


	WriteHeader();

}
CATCH(CFileException, e)
{
	e->ReportError();
	e->Delete();

} END_CATCH

	return lOffsetRec;
}

/* �������������� memo ���� */
void CFPTMemoFile::UpdateMemoRecord(DBF_ULONG lOffsetRec, CString& strMemo)
{

TRY
{
	DBF_LONG nLenMemo = strMemo.GetLength();

	Seek(lOffsetRec * m_nBlockSize + sizeof(DBF_LONG),CFile::begin);
		
	// ���������� ����� ������
	nLenMemo = SWAP_DWORD_BYTE(nLenMemo); // ������ � ������������ ����
	Write(&nLenMemo, sizeof(nLenMemo));
		
	// ���������� ������
	Write((LPCSTR)strMemo, strMemo.GetLength());

}
CATCH(CFileException, e)
{
	e->ReportError();
	e->Delete();

} END_CATCH

}

#ifdef _DEBUG
void CFPTMemoFile::AssertValid() const
{
	CMemoFile::AssertValid();
}

void CFPTMemoFile::Dump(CDumpContext& dc) const
{
	ASSERT_VALID(this);

	CMemoFile::Dump(dc);

	dc << "m_nNextBlock = " << m_nNextBlock;
	dc << "m_nBlockSize = " << m_nBlockSize;

	dc << "\n";
}
#endif //_DEBUG

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

Share

About the Author

Alexey
Web Developer
Russian Federation Russian Federation
Year of birth - 1974.
Eeducation - was ended by Kaliningrad State University in 1997.
Now I work as the engineer-programmer in Kaliningrad (RUSSIA).

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 6 Nov 2002
Article Copyright 2002 by Alexey
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid