⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbfile.cpp

📁 mfc internals 源码 mfc internals 源码
💻 CPP
字号:
// dbfile.cpp : implemematation of the CDBaseFile class
//

// This is a part of the Objective Grid C++ Library.
// Copyright (C) 1995 ClassWorks, Stefan Hoenig.
// All rights reserved.
//
// This source code is only intended as a supplement to
// the Objective Grid Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding
// the Objective Grid product.
//

#include "stdafx.h"
#include "dbfile.h"

static CString& StripTrailingChars (CString& sEdit, TCHAR cChar)
{
	ASSERT(cChar != '\0');

	int nLength = sEdit.GetLength();

	if (nLength > 0)
	{
		while (nLength > 0 && sEdit.GetAt(nLength - 1) == cChar)
			nLength--;

		if (nLength > 0)
			sEdit = sEdit.Left(nLength);
		else
			sEdit.Empty();
	}

	return sEdit;
}

CDBaseFile::CDBaseFile()
{
	recordBuf = NULL;
	fd = NULL;
	nRecordCount = 0;
	nCurrentRecord = -1;
	nFieldCount = 0;
	bWriteFlag = FALSE;
}

CDBaseFile::~CDBaseFile()
{
	Close();
}

void CDBaseFile::Close()
{
	if (fd)
	{
		Flush();
		delete recordBuf;
		for (int i = 0; i < nFieldCount; i++)
			delete fieldArray[i];
		fieldArray.RemoveAll();
		fclose(fd);
	}
	fd = NULL;
}

BOOL CDBaseFile::Open(LPCTSTR szFileName, BOOL readOnly)
{
	fd = fopen (sFileName = szFileName, (bReadOnly = readOnly) != FALSE ? "rb" : "rb+");

	if (fd == 0)
		return FALSE;

	int nBufSize = InitFields ();

	recordBuf = new char[nBufSize+1];

	return Seek(1);
}

BOOL CDBaseFile::Seek(long nRecord)
{
	ASSERT(nRecord >= 0 && nRecord < nRecordCount);

	if (nRecord < 0 || nRecord >= nRecordCount)
		return FALSE;
	else if (nRecord == nCurrentRecord)
		return TRUE;

	// new record
	if (bWriteFlag)
		Flush();

	nCurrentRecord = nRecord;
	long pos = (nRecord * size) + offset;

	fseek (fd, pos, 0);
	fread (recordBuf, size, 1, fd);
	recordBuf[size] = 0;

	return TRUE;
}

void CDBaseFile::Flush()
{
	if (!bWriteFlag || bReadOnly)
		return;

	bWriteFlag = FALSE;

	long pos = (nCurrentRecord * size) + offset;

	fseek (fd, pos, 0);
	fwrite (recordBuf, size, 1, fd);
}

CField* CDBaseFile::GetField(int n) const
{
	ASSERT(n >= 0 && n <= nFieldCount);
	return (CField*) fieldArray.GetAt(n);
}

BOOL CDBaseFile::IsDeleted() const
{
	return *recordBuf != ' ';
}

BOOL CDBaseFile::GetValue(int n, CString& result) const
{
	ASSERT(n >= 0 && n <= nFieldCount);

	if (!(n >= 0 && n <= nFieldCount))
		return FALSE;

	CField* fld = GetField(n);
	result.Empty();
	char* p = result.GetBuffer(fld->len+1);
	memcpy(p, recordBuf+fld->offset, fld->len);
	p[fld->len] = 0;
	result.ReleaseBuffer();
	StripTrailingChars(result, ' ');

	return TRUE;
}

BOOL CDBaseFile::SetValue(int n, LPCTSTR s)
{
	ASSERT(n >= 0 && n <= nFieldCount);

	if (bReadOnly)
		return FALSE;

	if (!(n >= 0 && n <= nFieldCount))
		return FALSE;

	CField* fld = GetField(n);

	int len = fld->len;
	char* p = recordBuf+fld->offset;
	while (len-- > 0)
		*p++ = (*s) ? *s++ : ' ';

	bWriteFlag = TRUE;

	return TRUE;
}


int CDBaseFile::InitFields ()
{
	fseek (fd, 4L, 0);
	fread (&nRecordCount, 4, 1, fd);

	if (nRecordCount == 0L)
		AfxThrowFileException (CFileException::endOfFile);

	fread (&offset, 2, 1, fd);
	fread (&size, 2, 1, fd);

	nFieldCount = (offset-33)/32;

	int off = 1;  // field offset

	long pos = 32;
	for (int i = 0; i < nFieldCount; i++, pos += 32)
	{
		CField* fld = new CField;
		fieldArray.SetAtGrow(i, fld);
		fseek (fd, pos, 0);
		fread (fld->name, 10, 1, fd);
		fld->name[10] = 0;

		fseek (fd, pos+11, 0);
		fread (&fld->type, 1, 1, fd);

		fseek (fd, pos+16, 0);
		unsigned char width, decimals;
		fread (&width, 1, 1, fd);
		fread (&decimals, 1, 1, fd);
		fld->len = width;
		fld->width = width;
		fld->decimals = decimals;
		fld->offset = off;
		off += width;

		int l = strlen(fld->name);
		if (fld->width < l)
			fld->width = (short)l;
	}

	return off;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -