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

📄 indexfile.cpp

📁 有计算机图形学、图像处理、dbms、sniffer、中游俄罗斯外挂、othello、遗传算法、舌苔分析等程序。
💻 CPP
字号:
// IndexFile.cpp: implementation of the CIndexFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Database.h"
#include "IndexFile.h"

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

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

CIndexFile::CIndexFile()
{

}

CIndexFile::CIndexFile(LPCTSTR lpszFileName, UINT nOpenFlags
					   ,UINT id)
{
	status=OPEN;////////////////////////////////////////
	CFile f(lpszFileName,nOpenFlags);
	UINT filelength=f.GetLength();
	if(filelength==0)//是新文件
	{
		const char *magic="IDX";
		f.SetLength(2*MAIN_BLOCK);//初始8192字节
		ID=id;
        wBlockNum=0;
		NextFile=0;
		
        f.SeekToBegin();
		f.Write(magic,MAGIC);
		f.Write(&ID,sizeof(UINT));
		f.Write(&wBlockNum,sizeof(WORD));
		f.Write(&NextFile,sizeof(UINT));
	
	}
	else
	{
		char *magic[4];//?????????????
		f.SeekToBegin();
		f.Read(magic,MAGIC);
		//if(strcmp(magic,"IDX"))
			//::AfxThrowFileException(
		f.Read(&ID,sizeof(UINT));
		f.Read(&wBlockNum,sizeof(WORD));
	
		f.Read(&NextFile,sizeof(UINT));
	}
}
CIndexFile::~CIndexFile()
{
	if(status==OPEN)
        Close();
}

PDB CIndexFile::AllocateNewBlock()
{
	PDB Ret=0;
	CString filename("");
	filename.Format("D:\\DB\\%d.index",this->ID);
	CFile f;
	f.Open(LPCTSTR(filename),CFile::modeReadWrite);
	status=OPEN;////////////////////////////////////////////////////////////
    if(wBlockNum>=(FILE_MAX/MAIN_BLOCK))
	{
		f.Close();
        return DB_NULL;
	}
	else
	{
        wBlockNum++;
		f.Seek(MAGIC+sizeof(UINT),CFile::begin);
		f.Write(&wBlockNum,sizeof(WORD));
		f.SetLength(f.GetLength()+MAIN_BLOCK);//当前长度+4096
		Ret=(PDB(ID)<<32)+PDB(MAIN_BLOCK*wBlockNum);
		f.Close();
		return Ret;
	}
}



BOOL CIndexFile::Open(LPCTSTR lpszFileName, 
					  UINT nOpenFlags,UINT id)
{
	CFile f;
	status=OPEN;///////////////////////////////////////////////
	if(f.Open(lpszFileName,nOpenFlags))
	{
		UINT filelength=f.GetLength();
		if(filelength==0)//是新文件
		{
			const char *magic="IDX";
			f.SetLength(2*MAIN_BLOCK);//初始8192字节
			ID=id;
			wBlockNum=0;
			NextFile=0;
			
			f.SeekToBegin();
			f.Write(magic,MAGIC);
			f.Write(&ID,sizeof(UINT));
			f.Write(&wBlockNum,sizeof(WORD));
			f.Write(&NextFile,sizeof(UINT));
		
		}
		else
		{
			char *magic[4];//??????????????????????
			f.SeekToBegin();
			f.Read(magic,MAGIC);
			//if(strcmp(magic,"IDX"))
				//::AfxThrowFileException(
			f.Read(&ID,sizeof(UINT));
			f.Read(&wBlockNum,sizeof(WORD));
		
			f.Read(&NextFile,sizeof(UINT));
		}
		return TRUE;
	}
	else
		return FALSE;
}

void CIndexFile::SetNextFile(UINT FileNo)
{
	CString filename("");
	filename.Format("D:\\DB\\%d.index",this->ID);
	CFile f;
	f.Open(LPCTSTR(filename),CFile::modeReadWrite);
    this->NextFile=FileNo;
	f.Seek(MAGIC+sizeof(UINT)+sizeof(WORD),CFile::begin);
	f.Write(&FileNo,sizeof(UINT));
	f.Close();
}


void CIndexFile::Close()
{
	status=CLOSE;
	CString filename("");
	filename.Format("D:\\DB\\%d.index",this->ID);
	CFile f;
	f.Open(LPCTSTR(filename),CFile::modeReadWrite);
    f.Seek(MAGIC,CFile::begin);
	f.Write(&ID,sizeof(UINT));
	f.Write(&wBlockNum,sizeof(WORD));
	f.Write(&NextFile,sizeof(UINT));
	f.Close();
}

⌨️ 快捷键说明

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