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

📄 block.h

📁 使用C++编写的数据库管理系统; 拥有自定义数据文件格式
💻 H
字号:

const int BLOCK_SIZE = 2048;
const int keyPerBlock = BLOCK_SIZE/256;
fstream * f;


class Block
{
private:	
	unsigned long position;
	unsigned char * content;
public:
	
	Block();	
	Block(unsigned long p);	
	~Block()
	{
		delete content;
	}	
	void setPosition(unsigned long p)
	{
		position = p;
	};

	void save();
	void load(unsigned long);	
	void clear();
	void write(unsigned char *,int num,int index);
	void write(unsigned long n,int num,int index);
	void writeString(char * string,int num,int index);
	void readString(char * string,int num,int index);
	unsigned long read(int count,int index);
	void read(unsigned char * c,int num,int index);
	
	static void writeEmpty(unsigned long beginPosition,int blockNum);

};

void getUcWord(unsigned char * ucword,int length,FIELDREC v)
{
	int i;
	long n;
	switch(v.type){
	case INT://length 2
	case LONG://length 4
	case BYTE://length 1
			   n =atoi(v.value);
			   for(i=0;i<length;i++)
			   {
				   ucword[length-1-i] = (unsigned char)n%256;
				   n=n/256;					
			   }
			   ucword[length]=0;
			   break;
	case CHAR:
				i=0;
				while(v.value[i])
				{
					ucword[i] = v.value[i];
					i++;
				}
				for(;i<length;i++)
					ucword[i] = 0;
				break;
	case FLOAT:;

	}
}

void getFieldRec(unsigned char * ucword,int length,FIELDREC & v)
{

	int n=0,i;
	switch(v.type){
	case INT:
	case LONG:
	case BYTE:
				for(i=0;i<length;i++)
				{
					n= n*256+ucword[i];				
				}
				v.value= new char [30]; 
				itoa(n,v.value,10);
				break;
	case CHAR:
				i=0;
				while(ucword[i])
				{
					 v.value[i]=ucword[i];
					i++;
				}
				v.value[i]=0;
				break;		
			
	}
	
}



Block::Block()
{
		content = new unsigned char[BLOCK_SIZE];
		for(int i=0;i<BLOCK_SIZE;i++)
			content[i]=0;
		position=0;
}

Block::Block(unsigned long p)
{
		content = new unsigned char[BLOCK_SIZE];
		for(int i=0;i<BLOCK_SIZE;i++)
			content[i]=0;
		position = p;
}
void Block::save()
{
	long offset = position*BLOCK_SIZE;
	f->seekp(offset,ios::beg);
	f->write(content,BLOCK_SIZE);
	f->flush();

}

void Block::load(unsigned long p)
{
	unsigned long offset = p*BLOCK_SIZE;
	f->seekg(offset,ios::beg);
	position=p;
	f->read(content,BLOCK_SIZE);
}

void Block::clear()
{
	position=0;
	for(int i=0;i<BLOCK_SIZE;i++)
			content[i]=0;
}

void Block::readString(char * string,int num,int index)
{
	
	for(int i=0;i<num;i++,index++)
	{
		string[i]=content[index];
		if(content[index]==0)
			break;
	}

}


void Block::write(unsigned char * c,int num,int index)
{	
	for(int i=0;i<num;i++,index++)
		content[index]=c[i];
}
void Block::read(unsigned char * c,int num,int index)
{
	for(int i=0;i<num;i++,index++)
		c[i]=content[index];
}

void Block::write(unsigned long n,int count,int index)
{
	unsigned char * cn=new unsigned char[count];
	for(int i=0;i<count;i++)
	{
		cn[count-1-i]=(unsigned char)n%256;
		n=n/256;
	}
	
	write(cn,count,index);
	delete cn;
}

void Block::writeString(char * string,int num,int index)
{
	int i=0;
	while(string[i]!='\0')
	{
		content[index++]=string[i++];
	}
	for(;i<num;i++)
		content[index++]='\0';	
}


unsigned long Block::read(int count,int index)
{
	unsigned long result=0;
	for(int i=0;i<count;i++)
	{
		result=result*256+content[index++];
	}
	return result;
}





 void Block::writeEmpty(unsigned long beginPosition,int blockNum)
{
	long offset = beginPosition*BLOCK_SIZE;
	f->seekp(offset,ios::beg);
	char * cnt =new char[BLOCK_SIZE];
	for(int i=0;i<BLOCK_SIZE;i++)
		cnt[i]=0;
	for(int j=0;j<blockNum;j++)
		f->write(cnt,BLOCK_SIZE);
	f->flush();
	delete cnt;
}




⌨️ 快捷键说明

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