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

📄 hdblock.h

📁 本科作业虚拟linux操作系统下的文教系统
💻 H
字号:


#ifndef HDblock_tag
#define HDblock_tag

#include <fstream>

#define BLOCK_SIZE  512  // 块大小


class HDblock //磁盘块
{
private:
	char elem[BLOCK_SIZE];
	int  pointer;
	//DevBlock* next;
	int next;
	int free;
	HDblock*pnext;


	int blkcpy(void *des,const void * sour,int num)
	{
/*		if(num>)
		{
		}
		*/
		for(int i=0;i<num;i++)
		{
			((unsigned char*)des)[i]=((unsigned char*)sour)[i];
		}
		return 1;
	}

public:

	HDblock()
	{
		pointer=0;
		next=0;
		free=1;
		pnext=NULL;
	}

	void setFree()
	{
		free=1;
	}

	int setUsed()
	{
		if(free)
		{
			free=0;
			return 1;
		}
		else
		{
			return 0;
		}
	}



	int getNext()
	{
		return next;
	}

	HDblock*getpNext()
	{
		return pnext;
	}


	int hasNext()
	{
		return next;
	}
	void setNext(int nt)
	{
		next=nt;
	}

	int write(int ele)
	{
		if(pointer>BLOCK_SIZE-2*sizeof (int))
		{
			return 0;
		}

		blkcpy(&elem[pointer],&ele,sizeof(int));
		pointer+=sizeof (int);
		return 1;
	}
	
	int write(char ele)
	{
		if(isFull())
		{
			return 0;
		}

		blkcpy(&elem[pointer],&ele,sizeof(char));
		pointer+=sizeof (char);
		return 1;
	}


	int readInt(int *ele)
	{
		if(pointer>BLOCK_SIZE-3*sizeof (int))
		{
			return 0;
		}

		blkcpy(ele,&elem[pointer],sizeof(int));
		pointer+=sizeof (int);
		return 1;
	}

	char readChar()
	{
		if(pointer>BLOCK_SIZE-3*sizeof (int))
		{
			return -1;
		}

		char ele;
		blkcpy(&ele,&elem[pointer],sizeof(char));
		pointer+=sizeof (char);
		return ele;
	}

	void reset(int pos=0)
	{
		pointer=pos;
	}

	int isFull()
	{
		return ((pointer>=BLOCK_SIZE-2*sizeof(int)));
	}


	friend std::ifstream & operator >>(std::ifstream &is , HDblock &hb);
	friend std::ofstream & operator <<( std::ofstream &os ,HDblock &hb);

};

std::ifstream & operator >>(std::ifstream &is , HDblock &hb)
{

	is.read(hb.elem,BLOCK_SIZE);
	hb.next=* ((int*)(hb.elem+BLOCK_SIZE-sizeof (int)));
	return is;
}

std::ofstream & operator <<( std::ofstream &os,HDblock &hb )
{
	os.write(hb.elem,BLOCK_SIZE);
	return os;
}

#endif

⌨️ 快捷键说明

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