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

📄 cfs.cpp

📁 一款运行于ARM上的文件系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:


	cCFS::~cCFS()
{
    
}




///////////////////////////////////////////////////////
//函数名:cfsinit
//功能  :文件系统初始化,在使用文件系统前,必须调用此函数
//        本函数首先检查存储卷中是否存在文件系统,如果没有
//        则会先对存储卷进行格式化
//入参  :void
//出参  :cfs_bool	
//                 CFS_TURE   写数据完成  
//                 CFS_FALSE  写数据出错
///////////////////////////////////////////////////////
	cfs_bool cCFS::cfsinit(void)
{
	cfs_int32 i=0,x=0,y=0;
	CfsDbr.bpbBytsPerSec=CFS_BYTES_PER_SEC;
	pCfsSecBuf = new cfs_int8[CFS_BYTES_PER_SEC];
	if(! CfsDbr.cfsDBRGet(pCfsSecBuf))						//取得第0扇区的数据,并从数据缓冲区中取得DBR的数据
	{
		delete []pCfsSecBuf;
		return CFS_FALSE;
	}
	if((CfsDbr.ExtMarker1 == CFS_EXT_MARKER1) && (CfsDbr.ExtMarker2 == CFS_EXT_MARKER2))//如果存储卷已经格式化
	{
		//保留
	}
	else                                   //如果存储卷还未格式化
	{
		pCfsFat    = new cfs_uint16[ROM_SEC_SIZE];
		if(! cfsformat())
		{
			delete []pCfsFat;
			return CFS_FALSE;              //格式化未成功
		}
		else
		{
			CfsDbr.cfsDBRGet(pCfsSecBuf);						//取得第0扇区的数据,并从数据缓冲区中取得DBR的数据	
		}
		delete []pCfsFat;
	}
	delete []pCfsSecBuf;
	pCfsFat    = new cfs_uint16[CfsDbr.bpbTotSec];
	pCfsSecBuf = new cfs_int8[CfsDbr.bpbBytsPerSec];

	if ((CfsDbr.bpbRootDirEnts * CFS_DIR_ENTYR_SIZE) % CfsDbr.bpbBytsPerSec > 0)
		CfsRootDirSec = CfsDbr.bpbRootDirEnts * CFS_DIR_ENTYR_SIZE / CfsDbr.bpbBytsPerSec +1;
    else
		CfsRootDirSec = CfsDbr.bpbRootDirEnts * CFS_DIR_ENTYR_SIZE / CfsDbr.bpbBytsPerSec ;		
	//	cfsROMSize = CfsDbr.bpbTotSec * CfsDbr.bpbBytsPerSec;

	cfsFATGet();
	cfsRootDirRead();
	return CFS_TURE;
}




///////////////////////////////////////////////////////
//函数名:cfsformat
//功能  :文件系统格式化
//入参  :void
//出参  :cfs_bool	
//                 CFS_TURE   写数据完成  
//                 CFS_FALSE  写数据出错
///////////////////////////////////////////////////////
	cfs_bool   cCFS::cfsformat(void)
{
	if(!CfsDbr.cfsDBRForMat(pCfsSecBuf))
		return CFS_FALSE;
	if(!cfsFATForMat())
		return CFS_FALSE;
	return CFS_TURE;
}





///////////////////////////////////////////////////////
//函数名:cfsSearchFile
//功能  :根据文件名在文件目录缓冲区中搜索该文件,并返回
//        该文件句柄
//入参  :void
//出参  :cfs_int32   文件句柄
///////////////////////////////////////////////////////
	cfs_int32	cCFS::cfsSearchFile(cfs_int8 *pfilename)
{
	cfs_int32 i=0,x=0,k=0;
	cfs_int8 buf1[9];
	cfs_int8 buf2[4];
	cfsFILE p;
	for(; x < 8; x++)
	{
		if((*(pfilename + x) != '\0') && (*(pfilename + x) != '.'))
		{
			buf1[x]=*(pfilename + x);
		}
		else if(*(pfilename + x) == '.')
		{
			k=x;
			for(;x<8;x++)
			{
				buf1[x]='\0';
			}
		}
		else
			break;
	}
	if(x==8)
	{
		k=0;
		for(x=8 ; (*(pfilename + x) != '.');x++)
		{
			if(*(pfilename + x) == '\0')
			{
				//k=0;
				break;
			}
			else
			{
				k++;
			}
		}
		//if(k==0)
		//{
		//	buf2[0]='\0';
		//	buf2[1]='\0';
		//	buf2[3]='\0';
		//}
		//else
		//{
	    	for(x=0 ; (x < 3) && (*(pfilename + x + k + 1 + 8) != '\0'); x++)
			{
		    	buf2[x]=*(pfilename + x + k + 1 + 8);
			}
			for(;x<3;x++)
			{
				buf2[x]='\0';
			}
		//}
	}
	else if(k > 0)
	{
		for(x=0; (x < 3) && (*(pfilename + x + k + 1) != '\0');x++)
		{
			buf2[x]=*(pfilename + x + k + 1);
		}
		for(;x<3;x++)
		{
			buf2[x]='\0';
		}
	}

	for(i=1 ; i <= CfsRootDir.fnCount(); i++)
	{
		p=CfsRootDir.fnGet(i);
		for(x=0; x<8; x++)
		{
			if(buf1[x]!=p.fileName[x])
				goto cfsnextrootdir;
		}
		for(x=0;x<3;x++)
		{
			if(buf2[x]!=p.fileExtension[x])
				goto cfsnextrootdir;
		}
		return i;
cfsnextrootdir:
		continue;
	}
	return(CFS_NULL);
}
///////////////////////////////////////////////////////
//函数名:cfsCrtFile
//功能  :根据文件名在文件目录缓冲区中创建一个文件
//入参  :void
//出参  :cfs_int32   文件句柄
///////////////////////////////////////////////////////
	cfs_int32   cCFS::cfsCrtFile(cfs_int8 *pfilename)
{
	cfs_int32 x=0,k=0;
	cfs_int8 buf1[9];
	cfs_int8 buf2[4];
	cfsFILE p;
	for(; x < 8; x++)
	{
		if((*(pfilename + x) != '\0') && (*(pfilename + x) != '.'))
		{
			buf1[x]=*(pfilename + x);
		}
		else if(*(pfilename + x) == '.')
		{
			k=x;
			for(;x<8;x++)
			{
				buf1[x]='\0';
			}
		}
		else
			break;
	}
	if(x==8)
	{
		k=0;
		for(x=8 ; (*(pfilename + x) != '.');x++)
		{
			if(*(pfilename + x) == '\0')
			{
				//k=0;
				break;
			}
			else
			{
				k++;
			}
		}
		//if(k==0)
		//{
		//	buf2[0]='\0';
		//	buf2[1]='\0';
		//	buf2[3]='\0';
		//}
		//else
		//{
	    	for(x=0 ; (x < 3) && (*(pfilename + x + k + 1 + 8) != '\0'); x++)
			{
		    	buf2[x]=*(pfilename + x + k + 1 + 8);
			}
			for(;x<3;x++)
			{
				buf2[x]='\0';
			}
		//}
	}
	else if(k > 0)
	{
		for(x=0; (x < 3) && (*(pfilename + x + k + 1) != '\0');x++)
		{
			buf2[x]=*(pfilename + x +k + 1);
		}
		for(;x<3;x++)
		{
			buf2[x]='\0';
		}
	}
	for(x=0;x<8;x++)
	{
		p.fileName[x]=buf1[x];
	}
	for(x=0;x<3;x++)
	{
		p.fileExtension[x]=buf2[x];
	}
	//
	return(CfsRootDir.fnAddItem(p));
}

#define CFS_FILE_MAX_SIZE  512
///////////////////////////////////////////////////////
//函数名:cfsLoadFile
//功能  :根据文件句柄从存储卷中载入该文件数据
//入参  :cfs_uint32 x 文件句柄
//出参  :cfs_bool	
//                 CFS_TURE   写数据完成  
//                 CFS_FALSE  写数据出错
///////////////////////////////////////////////////////
	cfs_bool   cCFS::cfsLoadFile(cfs_uint32 x)
{
	cfsFILE *pFile;
	cfs_uint32 i=0,n=0,u=0;
	pFile=CfsRootDir.fnGetp(x);
	u=pFile->fileStartSec;
	for(n=0; u != CFS_FAT_USE_END ;n++)
	{
		if(!rom_read_sec(CfsDbr.bpbBytsPerSec, u ,pFile->pfile + n * CfsDbr.bpbBytsPerSec))	
			return CFS_FALSE;
        u=cfsFATNextSec(u);
	}
	return CFS_TURE; 
}

///////////////////////////////////////////////////////
//函数名:cfsWrtFile
//功能  :根据文件句柄将数据写入该文件的数据区中
//        同时会对FAT进行记录
//入参  : cfs_uint32 x         文件句柄
//出参  :cfs_bool	
//                 CFS_TURE   写数据完成  
//                 CFS_FALSE  写数据出错
///////////////////////////////////////////////////////
	cfs_bool	cCFS::cfsWrtFile(cfs_uint32 x)
{
	cfsFILE *pFile;
	cfs_uint32 i=0,n=0,u=0;
	pFile=CfsRootDir.fnGetp(x);
	u=pFile->fileStartSec;
	for(n=0; u != CFS_FAT_USE_END ;n++)
	{
		if(!rom_write_sec(CfsDbr.bpbBytsPerSec, u ,pFile->pfile + n * CfsDbr.bpbBytsPerSec))	
			return CFS_FALSE;
        u=cfsFATNextSec(u);
	}
	return CFS_TURE; 
}




///////////////////////////////////////////////////////
//函数名:cfsopen
//功能  :根据文件名打开一文件,并将文件数据存储入文件缓冲区
//入参  : cfs_int8 *pfilename        文件名
//出参  :cfs_int32                  文件句柄
///////////////////////////////////////////////////////
	cfs_int32	cCFS::cfsopen(cfs_int8 *pfilename)
{
	cfs_int32 p;
	cfsFILE *pFile;
	p=cfsSearchFile(pfilename);
	if(p != CFS_NULL)
	{
		pFile=CfsRootDir.fnGetp(p);
		if(pFile->fileIsOpen)
			return p;
		pFile->fileIsOpen=CFS_TURE;
		pFile->pfile=new cfs_int8[CFS_FILE_MAX_SIZE];
		cfsLoadFile(p);
	}
    else
	{
		p=cfsCrtFile(pfilename);
		pFile=CfsRootDir.fnGetp(p);
		pFile->fileIsOpen=CFS_TURE;
		pFile->fileSize=0;
		pFile->pfile=new cfs_int8[CFS_FILE_MAX_SIZE];
		pFile->fileStartSec = cfsFATFindNullSec();
		CfsDbr.bpbRootDirEnts ++;
		cfsFATSetSec(pFile->fileStartSec,CFS_FAT_USE_END);
	}
//	CfsDbr.cfsDBRWrt(pCfsSecBuf);
//	cfsFATWrt();
//	cfsRootDirWrtAEntry(p);
 return p;
}

	cfs_uint32  cCFS::cfscntsize(cfs_int8 *p)
{
	cfs_uint32 i=0;
	for(i=0; *(p + i) != '\0';i++);
	return i;
}
///////////////////////////////////////////////////////
//函数名:cfswrite
//功能  :根据文件句柄将数据写入该文件的数据区中
//        同时,函数会自动进行相关设置
//入参  :cfs_int8 *pdat       数据缓冲区
//        cfs_uint32 datsize,  数据的长度
//        cfs_uint32 i         文件句柄
//出参  :cfs_bool	
//                 CFS_TURE   写数据完成  
//                 CFS_FALSE  写数据出错
///////////////////////////////////////////////////////
	cfs_bool	cCFS::cfswrite(cfs_int32 i,cfs_int8 *pdat,cfs_uint32 datsize)
{
	cfsFILE *pFile;
	cfs_uint32 n=0,sec,b=0,e=0,x=0;
	pFile=CfsRootDir.fnGetp(i);
	if(pFile->fileIsOpen)
	{
		if(datsize == 0)
		{
		    datsize=cfscntsize(pdat);
		}
		for(n=0; n<datsize; n++)
		{
			pFile->pfile[n + pFile->fileSize]=pdat[n];
		}
		pFile->fileSize +=datsize;
	//	cfsWrtFile(pFile->pfile,pFile->fileSize,pFile->fileStartSec);
	//	cfsRootDirWrtAEntry(i);
    	sec = datsize % CfsDbr.bpbBytsPerSec > 0 ?
	          datsize / CfsDbr.bpbBytsPerSec + 1 :
		      datsize / CfsDbr.bpbBytsPerSec;
		x=pFile->fileStartSec;
		e=x;
		for(; b< sec ; b++)
		{
			x=e;
	        e=cfsFATFindNullSec();
		    if(b==sec-1)
				e=CFS_FAT_USE_END;
			cfsFATSetSec(x,e);
		
		}
		return CFS_TURE;
	}
	else
	{
		return CFS_FALSE;
	}
}


///////////////////////////////////////////////////////
//函数名:cfsread
//功能  :根据文件句柄读取数据
//入参  :cfs_uint32 i         文件句柄
//出参  :cfs_int8*    指向文件数据缓冲区的指针
//	                   读取错误则返回空指针
///////////////////////////////////////////////////////
	cfs_int8*	cCFS::cfsread(cfs_int32 i)
{
	if(CfsRootDir.fnGetp(i)->fileIsOpen)		
		return	CfsRootDir.fnGetp(i)->pfile;
	else
		return(CFS_NULL);
}




///////////////////////////////////////////////////////
//函数名:cfsclose
//功能  :根据文件句柄关闭文件
//入参  :cfs_uint32 i         文件句柄
//出参  :cfs_bool	
//                 CFS_TURE   完成  
//                 CFS_FALSE  出错
///////////////////////////////////////////////////////
	cfs_bool	cCFS::cfsclose(cfs_uint16 hfile)
{
	cfsFILE *pFile;
	pFile=CfsRootDir.fnGetp(hfile);
	if(pFile->fileIsOpen)
	{
		pFile->fileIsOpen=CFS_FALSE;
		cfsWrtFile(hfile);
		cfsRootDirWrtAEntry(hfile);
		cfsFATWrt();
		CfsDbr.cfsDBRWrt(pCfsSecBuf);
		delete []pFile->pfile;
		return CFS_TURE;
	}
	else
	{
		return CFS_FALSE;
	}
}
///////////////////////////////////////////////////////
//函数名:cfsremove
//功能  :根据文件句柄删除文件
//入参  :cfs_uint32 i         文件句柄
//出参  :cfs_bool	
//                 CFS_TURE   完成  
//                 CFS_FALSE  出错
///////////////////////////////////////////////////////
	cfs_bool	cCFS::cfsremove(cfs_int8 *pfilename)
{
	cfs_int32 p;
	cfsFILE *pFile;
	cfs_uint16 c=0,n=0;
	p = cfsSearchFile(pfilename);
	if(p){
		pFile = CfsRootDir.fnGetp(p);
		c = pFile->fileStartSec;
		do{
			n = cfsFATNextSec(c);
			cfsFATSetSec(c,CFS_FAT_NOT_USE);
			c = n;
		}while(c != CFS_FAT_USE_END);
		if(pFile->fileIsOpen)
		{
			delete []pFile->pfile;
		}
		CfsRootDir.fnDelItem(p);
		cfsFATWrt();
		CfsDbr.bpbRootDirEnts --;
		CfsDbr.cfsDBRWrt(pCfsSecBuf);
		return(CFS_TURE);
	}
	else
	{
		return CFS_FALSE;
	}
}

/*
	cfs_bool	cCFS::cfsrename()
{
}*/

⌨️ 快捷键说明

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