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

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
// DBFTableDef.cpp : implementation of the CDBFTableDef class
//
/////////////////////////////////////////////////////////////////////////////
/*
�����:              �������� �.�.
����������:         alxsoft@gazinter.net
*/
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DBFRecordset.h"
#include "DBFException.h"

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

//////////////////////////////////////////////////////////////////////////
// CDBFRecordset
IMPLEMENT_DYNAMIC(CDBFRecordset, CObject )

/* ����������� */
CDBFRecordset::CDBFRecordset(CDBFTableDef* pDBFTable)
{
	m_pTableDef = NULL;

	m_nCurTableRecN = -1;
	m_bBOF = TRUE;			
	m_bEOF = TRUE;

	m_pData = NULL;

	m_nLockMode = LockPessimistic;
	m_nLockType = NoLock;

	m_nEditMode = noMode;
}

/* ���������� */
CDBFRecordset::~CDBFRecordset()
{
	if (IsOpen())
		Close();
}

/* �������� ������ ������� */
void CDBFRecordset::Open(LPCTSTR lpszFileName, UINT nOpenFlag)
{
	ASSERT_VALID(this);

	CString strFileName;

	if(lpszFileName == NULL)
		strFileName = GetDefaultDBName();
	else
		strFileName = lpszFileName;

	ASSERT(strFileName.GetLength() > 4);

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

	m_pTableDef = new CDBFTableDef;
	m_pTableDef->m_bOpen = FALSE;

	// ��������� �������
	m_pTableDef->Open(strFileName, nOpenFlag);

	if(!IsOpen())
		return;

	// �������� ��������� �����
	m_DBFFields = m_pTableDef->m_DBFFields;

	if(m_pTableDef->GetRecordCount() > 0)
		MoveFirst();
	else
		m_nCurTableRecN = 0;
}

/* ��������� ����� ������� */
void CDBFRecordset::Close()
{
	ASSERT_VALID(this);

	if (IsOpen())
		m_pTableDef->Close();

	m_nEditMode = noMode;

	m_nCurTableRecN = -1;
	m_bBOF = TRUE;			
	m_bEOF = TRUE;

	if(m_pTableDef != NULL)
	{
		delete m_pTableDef;
		m_pTableDef = NULL;
		m_DBFFields.m_pFields = NULL;
		m_DBFFields.m_pCurRec = NULL;
	}

	if(m_pData != NULL)
	{
		delete[] m_pData;
		m_pData = NULL;
	}
}

CString CDBFRecordset::GetDefaultDBName()
{
	ASSERT_VALID(this);

	// Override and add UNC path to .DBF file
	return _T("");
}

/* ������� ���������� TRUE ���� ������ ���. ������
   ����� �� ������� ������� */
BOOL CDBFRecordset::IsBOF() const
{
	ASSERT_VALID(this);

	return m_bBOF;
}

/* ������� ���������� TRUE ���� ������ ���. ������
   ����� �� ������� ������� */
BOOL CDBFRecordset::IsEOF() const
{
	ASSERT_VALID(this);

	return m_bEOF;
}

/* ������� ���������� TRUE ���� ���. �������� ��� �������� */
BOOL CDBFRecordset::IsDeleted() const
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	return (m_DBFFields.m_pCurRec[0] == REC_FLAG_DELETED);
}

/* ������� ���������� ��� ������� */
CString CDBFRecordset::GetName()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	return m_pTableDef->GetName();
}

/* ������� ���������� ������ ���� � DBF ������� */
CString CDBFRecordset::GetDBFilePath()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	return m_pTableDef->GetDBFilePath();
}

/* ������� ���������� ���� �������� ������� */
CTime CDBFRecordset::GetDateCreated()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	return m_pTableDef->GetDateCreated();
}

/* ������� ���������� ���� ��������� ������� */
CTime CDBFRecordset::GetDateLastUpdated()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	return m_pTableDef->GetDateLastUpdated();
}

/* ���-�� ������� � ������� */
long CDBFRecordset::GetRecordCount()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	return m_pTableDef->GetRecordCount();
}

/* ���-�� ����� � ������� */
short CDBFRecordset::GetFieldCount()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	return m_DBFFields.GetFieldCount();
}

/* ������� ���������� ���������� ������� ���. ������ */
long CDBFRecordset::GetAbsolutePosition()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	return m_nCurTableRecN;
}

/* ������� ������ ���. ������ ���������� ������� ������� �������� ���. lPosition */
void CDBFRecordset::SetAbsolutePosition(long lPosition)
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(lPosition >= 0 && lPosition < GetRecordCount());

	DBF_LONG lTableRecN = lPosition; 

	// Call Move.
	m_pTableDef->ReadRecord(lTableRecN, m_DBFFields.m_pCurRec);
	m_nCurTableRecN = lTableRecN;
	m_bBOF = m_bEOF = FALSE;

}

/* ������� � ��������� ������ */
void CDBFRecordset::MoveNext()
{
	ASSERT_VALID(this);
	ASSERT(!IsEOF());

	m_nEditMode = noMode;

	DBF_LONG lTableRecN = m_nCurTableRecN;
	
	lTableRecN++;

	if(lTableRecN < GetRecordCount())
	{
		// Call Move.
		m_pTableDef->ReadRecord(lTableRecN, m_DBFFields.m_pCurRec);
		m_nCurTableRecN = lTableRecN;
		m_bBOF = m_bEOF = FALSE;
	}
	else
	{
		m_bEOF = TRUE;
		m_bBOF = GetRecordCount() > 0; 
	}
	
}

/* ������� � ���������� ������ */
void CDBFRecordset::MovePrev()
{
	ASSERT_VALID(this);
	ASSERT(!IsBOF());

	m_nEditMode = noMode;

	DBF_LONG lTableRecN = m_nCurTableRecN; 

	lTableRecN--;

	if(lTableRecN >= 0)
	{
		// Call Move.
		m_pTableDef->ReadRecord(lTableRecN, m_DBFFields.m_pCurRec);
		m_nCurTableRecN = lTableRecN;
		m_bBOF = m_bEOF = FALSE;
	}
	else
	{
		m_bBOF = TRUE;
		m_bEOF = GetRecordCount() > 0; 
	}
}

/* ������� � ������ ������ */
void CDBFRecordset::MoveFirst()
{
	ASSERT_VALID(this);
	ASSERT(GetRecordCount() > 0);

	m_nEditMode = noMode;

	DBF_LONG lTableRecN = 0; 

	// Call Move.
	m_pTableDef->ReadRecord(lTableRecN, m_DBFFields.m_pCurRec);
	m_nCurTableRecN = lTableRecN;
	m_bBOF = m_bEOF = FALSE;
	
}

/* ������� � ��������� ������ */
void CDBFRecordset::MoveLast()
{
	ASSERT_VALID(this);
	ASSERT(GetRecordCount() > 0);

	m_nEditMode = noMode;

	DBF_LONG lTableRecN = GetRecordCount() - 1; 

	// Call Move.
	m_pTableDef->ReadRecord(lTableRecN, m_DBFFields.m_pCurRec);
	m_nCurTableRecN = lTableRecN;
	m_bBOF = m_bEOF = FALSE;
}

/* ������� �� ������ �� �������� ������������ ������� ������ */
void CDBFRecordset::Move(long lOffsetRec)
{
	ASSERT_VALID(this);
	ASSERT(GetRecordCount() > 0);

	m_nEditMode = noMode;

	DBF_LONG lTableRecN = m_nCurTableRecN + lOffsetRec; 

	// ������� � ���������� �������
	if(lOffsetRec < 0)
	{
		if(lTableRecN < 0)
		{
			lTableRecN = 0;
			m_bBOF = TRUE;
		}
		else
			m_bBOF = FALSE;

		m_bEOF = FALSE; 
	}
	// ������� � ��������� �������
	else if(lOffsetRec > 0)
	{
		if(lTableRecN >= GetRecordCount())
		{
			lTableRecN = GetRecordCount() - 1;
			m_bEOF = TRUE;
		}
		else
			m_bEOF = FALSE;

		m_bBOF = FALSE; 
	}

	// Call Move.
	m_pTableDef->ReadRecord(lTableRecN, m_DBFFields.m_pCurRec);
	m_nCurTableRecN = lTableRecN;
}

/* ����� �� ���������� ��������� ��������� � ������ */
void CDBFRecordset::CancelUpdate()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	ASSERT(m_nEditMode != noMode);

	m_pTableDef->ReadRecord(m_nCurTableRecN, m_DBFFields.m_pCurRec);

	m_nEditMode = noMode;
}

/* ���������� ������� ������ */
BOOL CDBFRecordset::LockRecord()
{
	ASSERT_VALID(this);
	ASSERT(!IsBOF());
	ASSERT(!IsEOF());
	ASSERT(m_nEditMode == noMode);

	if(!(m_nOpenType & ALX_DBF_USE_EXCLUSIVE))
	{
		switch(m_nLockType)
		{
		case LockDBase:
			{
				// 0xEFFFFFFFL - rec number
				QWORD offset = 0xEFFFFFFFL - m_nCurTableRecN;
				QWORD range = 1;
				return::LockFile((HANDLE)m_pTableDef->m_DBFile.m_hFile, LODWORD(offset), HIDWORD(offset), LODWORD(range), HIDWORD(range));
			}
		case LockClipper:
			{
				// 0x40000000L + rec number
				QWORD offset = 1000000000 + (m_nCurTableRecN + 1);
				QWORD range = 1;
				return::LockFile((HANDLE)m_pTableDef->m_DBFile.m_hFile, LODWORD(offset), HIDWORD(offset), LODWORD(range), HIDWORD(range));
			}
		case LockFoxpro:
			{
				// 0x40000000L + rec offset
				QWORD offset = 0x40000000L +/* m_pTableDef->m_DBFHead.data_offset +*/ m_pTableDef->m_DBFHead.rec_size * m_nCurTableRecN;
				QWORD range = 1;
				return::LockFile((HANDLE)m_pTableDef->m_DBFile.m_hFile, LODWORD(offset), HIDWORD(offset), LODWORD(range), HIDWORD(range));
			}
		}
	}
	return TRUE;
}

/* ������ ���������� � ������� ������ */
BOOL CDBFRecordset::UnlockRecord()
{
	ASSERT_VALID(this);
	ASSERT(!IsBOF());
	ASSERT(!IsEOF());
	ASSERT(m_nEditMode == noMode);

	if(!(m_nOpenType & ALX_DBF_USE_EXCLUSIVE))
	{
		switch(m_nLockType)
		{
		case LockDBase:
			{
				// 0xEFFFFFFFL - rec number
				QWORD offset = 0xEFFFFFFFL - m_nCurTableRecN;
				QWORD range = 1;
				return::UnlockFile((HANDLE)m_pTableDef->m_DBFile.m_hFile, LODWORD(offset), HIDWORD(offset), LODWORD(range), HIDWORD(range));
			}
		case LockClipper:
			{
				// 0x40000000L + rec number
				QWORD offset = 1000000000 + (m_nCurTableRecN + 1);
				QWORD range = 1;
				return::UnlockFile((HANDLE)m_pTableDef->m_DBFile.m_hFile, LODWORD(offset), HIDWORD(offset), LODWORD(range), HIDWORD(range));
			}
		case LockFoxpro:
			{
				// 0x40000000L + rec offset
				QWORD offset = 0x40000000L +/* m_pTableDef->m_DBFHead.data_offset +*/ m_pTableDef->m_DBFHead.rec_size * m_nCurTableRecN;
				QWORD range = 1;
				return::UnlockFile((HANDLE)m_pTableDef->m_DBFile.m_hFile, LODWORD(offset), HIDWORD(offset), LODWORD(range), HIDWORD(range));
			}
		}
	}
	return TRUE;
}

/* ���������� ����� ������ */
void CDBFRecordset::AddNew()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	m_nEditMode = addnew;

	memset(m_DBFFields.m_pCurRec, 0, m_pTableDef->m_DBFHead.rec_size);

	COleVariant varEmpty;
	varEmpty.Clear();
	// ����������� ���� ����� ������ ��������
	for(short i = 0; i < m_DBFFields.GetFieldCount(); i++)
	{
		FIELD_REC* pFieldRec = m_DBFFields.GetFieldRec(i);
		alxSetFieldValue(m_DBFFields.m_pCurRec, pFieldRec, varEmpty);
	}

	m_DBFFields.m_pCurRec[0] = REC_FLAG_NORMAL;
}

/* ��������� ������� ������ */
void CDBFRecordset::Edit()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	m_nEditMode = edit;
}

/* ���������� ���������� ������ */
void CDBFRecordset::Update()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	ASSERT(m_nEditMode != noMode);

	switch(m_nEditMode)
	{
	case edit:
		{
			m_pTableDef->ReadHeader();
			m_pTableDef->WriteRecord(m_nCurTableRecN, m_DBFFields.m_pCurRec);
//			m_pTableDef->WriteHeader();
			break;
		}
	case addnew:
		{
			m_pTableDef->ReadHeader();
			m_pTableDef->m_DBFHead.last_rec++;
			m_nCurTableRecN = max(0,GetRecordCount() - 1);
			m_pTableDef->WriteRecord(m_nCurTableRecN, m_DBFFields.m_pCurRec);
			m_pTableDef->WriteHeader();
			m_bBOF = m_bEOF = FALSE;
			break;
		}
	}

	m_nEditMode = noMode;
}

/* ������ ���������� ��� �������� */
void CDBFRecordset::Delete()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	ASSERT(m_nEditMode == noMode);

	m_DBFFields.m_pCurRec[0] = REC_FLAG_DELETED;

	m_pTableDef->WriteRecord(m_nCurTableRecN, m_DBFFields.m_pCurRec);
}

/* ��������� ������� �� �������� */
void CDBFRecordset::Undelete()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	ASSERT(m_nEditMode == noMode);

	m_DBFFields.m_pCurRec[0] = REC_FLAG_NORMAL;

	m_pTableDef->WriteRecord(m_nCurTableRecN, m_DBFFields.m_pCurRec);
}

/* �������� �������� ���� */
COleVariant CDBFRecordset::GetFieldValue(LPCTSTR lpszName)
{
	COleVariant varValue;

	GetFieldValue(lpszName, varValue);

	return varValue;
}

/* �������� �������� ���� */
COleVariant CDBFRecordset::GetFieldValue(int nIndex)
{
	COleVariant varValue;

	GetFieldValue(nIndex, varValue);

	return varValue;
}

/* �������� �������� ���� */
long CDBFRecordset::GetFieldValue(LPCTSTR lpszName, COleVariant& varValue)
{
	for(short nIndex = 0;
		strcmp(m_DBFFields.m_pFields[nIndex].field_name, lpszName) != 0 &&
		nIndex <= m_DBFFields.m_nFieldCount;
		nIndex++)
		{};
	
	return GetFieldValue(nIndex, varValue);
}

/* �������� �������� ���� */
long CDBFRecordset::GetFieldValue(int nIndex, COleVariant& varValue)
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(nIndex < m_DBFFields.m_nFieldCount);

	FIELD_REC* pFieldRec = m_DBFFields.GetFieldRec(nIndex);
	// �������� �������� �� �������
	long lLen = alxGetFieldValue(m_DBFFields.m_pCurRec, pFieldRec, varValue);
	// ���� ����� �������� �������� �� ���� �����
	switch(pFieldRec->field_type)
	{
	case FLD_TYPE_MEMO:
		{
			if(varValue.vt == VT_UI4)
				if(varValue.ulVal > 0) // ���� ���� ������ � ���� �����
				{
					CString strValue;
					m_pTableDef->ReadMemoRecord(varValue.ulVal, lLen, strValue);
					varValue.Clear();
					varValue = strValue;
				}
				else
				{
					lLen = 0;
					varValue.Clear();
					varValue.vt = VT_EMPTY;
				}
			else if(varValue.vt == VT_DECIMAL) // SIX
				if(varValue.decVal.Lo32 > 0) // ���� ���� ������ � ���� �����
				{
					CString strValue;
					lLen = varValue.decVal.Hi32;
					m_pTableDef->ReadMemoRecord(varValue.ulVal, lLen, strValue);
					varValue.Clear();
					varValue = strValue;
				}
				else
				{
					lLen = 0;
					varValue.Clear();
					varValue.vt = VT_EMPTY;
				}
			break;
		}
	case FLD_TYPE_DOUBLE: // �������� FLD_TYPE_BINARY
		{
			// ���� FLD_TYPE_DOUBLE
			if(pFieldRec->len_info.num_size.len == sizeof(DBF_DOUBLE))
				break;
		}
	case FLD_TYPE_GENERAL:
	case FLD_TYPE_PICTURE:
		{
			if(m_pData != NULL)	{ delete[] m_pData;	m_pData = NULL;	}
			if(varValue.ulVal > 0) // ���� ���� ������ � ���� �����
			{
				m_pTableDef->ReadMemoRecord(varValue.ulVal, lLen, &m_pData);
				varValue.vt = VT_BYREF|VT_UI1; 
				varValue.pbVal = m_pData;
			}
			else
			{
				lLen = 0;
				varValue.Clear();
				varValue.vt = VT_EMPTY;
			}
			break;
		}
	}
	return lLen;
}

/* ��������� �������� ���� */
void CDBFRecordset::SetFieldValue(LPCTSTR lpszName,	const COleVariant& varValue)
{
	for(short nIndex = 0;
		strcmp(m_DBFFields.m_pFields[nIndex].field_name, lpszName) != 0 &&
		nIndex <= m_DBFFields.m_nFieldCount;
		nIndex++)
		{};

	SetFieldValue(nIndex, varValue);
}

/* ��������� �������� ���� */
void CDBFRecordset::SetFieldValue(int nIndex, const COleVariant& varValue)
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(nIndex < m_DBFFields.m_nFieldCount);

	FIELD_REC* pFieldRec = m_DBFFields.GetFieldRec(nIndex);

	// ���� ����� ���������� � ���� ����
	switch(pFieldRec->field_type)
	{
	case FLD_TYPE_MEMO:
		{
			COleVariant varDBFValue;
			if(varValue.vt == VT_BSTR)
			{
				CString strValue = varValue.bstrVal;
				DBF_CHAR*  pCurRec = new DBF_CHAR[m_pTableDef->m_DBFHead.rec_size];

				// �������� �������� �� �������
				alxGetFieldValue(m_DBFFields.m_pCurRec, pFieldRec, varDBFValue);

				if(varDBFValue.vt == VT_UI4)
				{
					DBF_LONG lLen = strValue.GetLength();
					m_pTableDef->WriteMemoRecord(varDBFValue.ulVal, lLen, strValue);
				}
				else if(varDBFValue.vt == VT_DECIMAL) // SIX
				{
					DBF_LONG lLen = varDBFValue.decVal.Hi32 > 0 ? varDBFValue.decVal.Hi32 : strValue.GetLength();
					m_pTableDef->WriteMemoRecord(varDBFValue.ulVal, lLen, strValue);
					varDBFValue.decVal.Hi32 = lLen;
				}

				delete[] pCurRec;

				alxSetFieldValue(m_DBFFields.m_pCurRec, pFieldRec, varDBFValue);
			}
			break;
		}
	case FLD_TYPE_DOUBLE: // �������� FLD_TYPE_BINARY
		{
			// ���� FLD_TYPE_DOUBLE
			if(pFieldRec->len_info.num_size.len == sizeof(DBF_DOUBLE))
			{
				alxSetFieldValue(m_DBFFields.m_pCurRec, pFieldRec, varValue);
				break;
			}
		}
	case FLD_TYPE_GENERAL:
	case FLD_TYPE_PICTURE:
		{
			break;
		}
	default:
		alxSetFieldValue(m_DBFFields.m_pCurRec, pFieldRec, varValue);
	}
}

/* ������� ���������� TRUE ���� ����� ��� ������� ������ */
BOOL CDBFRecordset::IsOpen() const
{
	ASSERT_VALID(this);
	return (m_pTableDef == NULL ? FALSE : m_pTableDef->m_bOpen);
}

/* ������� ����������� ���������� */
void CDBFRecordset::ThrowDBFException(long nALXDBFError)
{
	ASSERT_VALID(this);

	ALXThrowDBFException(nALXDBFError);
}

#ifdef _DEBUG
void CDBFRecordset::AssertValid() const
{
	CObject::AssertValid();
}

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

	CObject::Dump(dc);

	dc << "\nm_nOpenType = " << m_nOpenType;
	dc << "\nm_nEditMode = " << m_nEditMode;
	dc << "\nm_nCurTableRecN = " << m_nCurTableRecN;
	dc << "\nm_bBOF = " << m_bBOF;			
	dc << "\nm_bEOF = " << m_bEOF;			

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




BOOL CDBFRecordset::ChangeOpenFlag(UINT nNewFlag)
{
	ASSERT(m_pTableDef != NULL);

	return m_pTableDef->ChangeOpenFlag(nNewFlag);
}

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

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
Web04 | 2.8.140721.1 | Last Updated 6 Nov 2002
Article Copyright 2002 by Alexey
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid