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

📄 osfile.c

📁 这是嵌入式软件电话本的小软件
💻 C
字号:
#include "..\ucos-ii\includes.h"               /* uC/OS interface */
#include "..\inc\drv.h"
#include "..\inc\OSFile.h"
#include <string.h>


extern U8 tx_buf[16*1024];
extern U8 Page_Buf[528];
extern unsigned int fileSize;

OS_MEM *pFileMem;
INT8U FileMemPart[10][sizeof(FILE)];

void initOSFile()
{
	INT8U err;

	pFileMem=OSMemCreate(FileMemPart,10, sizeof(FILE), &err);
	if(pFileMem==NULL){
		Uart_Printf("Failed to Create File quote");
		LCD_printf("Failed to Create File quote");
	}
}


FILE* OpenOSFile(char filename[], U32 OpenMode)
{
	U32 root_location,filesize;
	int i;
	FILE* pfile;
	INT8U err;
	
	root_location=find_file((U8*)filename);
	
	//get the file first cluster//
	current_block=root_buf[root_location][27]*256+root_buf[root_location][26];
	
	if(root_location==FILE_NO_FOUND){
		Uart_Printf("file %s is not found!\n",filename);
		return NULL;
	}
	filesize=root_buf[root_location][31]<<24;
	filesize|=root_buf[root_location][30]<<16;
	filesize|=root_buf[root_location][29]<<8;
	filesize|=root_buf[root_location][28];

	pfile=(FILE*)OSMemGet(pFileMem,&err);
	pfile->fileblock=current_block;
	pfile->filebufnum=0;
	pfile->fileCurpos=0;
	pfile->filesize=filesize;
	pfile->filemode=OpenMode;

	switch(OpenMode){
	case FILEMODE_READ:
		read_file(pfile->Buffer);
		break;
	case FILEMODE_WRITE:
		for(i=0;i<BLOCK_SIZE;i++)
			pfile->Buffer[i]=0xff;
		break;
	}
	return pfile;
}

/*
FILE* OpenOSFileRead(char filename[])
{
	FILE* pfile;
	if((pfile=OpenOSFile(filename))==NULL)
		return NULL;
	

	return TRUE;
}*/
	
U32 ReadOSFile(FILE* pfile ,U8* ReadBuffer, U32 nReadbyte)
{
	U32 i;
	if(pfile->filemode!=FILEMODE_READ)
		return 0;
	
	current_block=pfile->fileblock;
	for(i=0;i<nReadbyte;i++){
		if(pfile->filesize<=pfile->fileCurpos)
			return i;
		(*ReadBuffer)=pfile->Buffer[pfile->filebufnum++];
		ReadBuffer++;
		pfile->fileCurpos++;
		
		if(pfile->filebufnum==BLOCK_SIZE){
			pfile->filebufnum=0;
			pfile->fileblock=current_block=Page_Buf[512+14]*256+Page_Buf[512+15];
			if(current_block==0xffff)
				return i;
			read_file(pfile->Buffer);
		}
	}
	return i;
}

U32 LineReadOSFile(FILE* pfile, char str[])	//读取指定文件的一行
{
	U8 ch;
	U8 bIs13=FALSE;
	int i=0;
	while(ReadOSFile(pfile, &ch, 1)){
		if(ch==0)
			break;
		*str=ch;
		str++;
		i++;
		if(bIs13){
			if(ch==10)
				break;
			else
				bIs13=FALSE;
		}
		if(ch==13)
			bIs13=TRUE;
	}
	*str=0;
	return i;
}

/*
U8 OpenOSFileWrite(FILE* pfile, char filename[])
{
	U32 root_location;
	int i;
	root_location=find_file((U8*) filename);
	
	//get the file first cluster//
	pfile->fileblock=current_block=root_buf[root_location][27]*256+root_buf[root_location][26];
	
	if(root_location==FILE_NO_FOUND){
		Uart_Printf("file %s is not found!\n",filename);
		return FALSE;
	}
	

	pfile->filebufnum=0;
	pfile->filemode=FILEMODE_WRITE;
	return TRUE;
}*/

U8 WriteOSFile(FILE* pfile, U8* WriteBuffer, U32 nReadbyte)
{
	int i;
	if(pfile->filemode!=FILEMODE_WRITE)
		return FALSE;

	current_block=pfile->fileblock;
	for(i=0;i<nReadbyte;i++){
		pfile->Buffer[pfile->filebufnum++]=*WriteBuffer;
		WriteBuffer++;
		if(pfile->filebufnum==BLOCK_SIZE){	//超过一个block大小
			return FALSE;
			pfile->filebufnum=0;
			pfile->fileblock=current_block=seek_blank_block(1);
			if(current_block==0xffff)
				return FALSE;
			write_file(FALSE,pfile->Buffer);
		}
	}
	return TRUE;
}

void CloseOSFile(FILE* pfile)
{
	switch(pfile->filemode){
	case FILEMODE_WRITE:
		current_block=pfile->fileblock;
		Erase_Cluster(current_block);
		write_file(TRUE, pfile->Buffer);
		break;
	}
	OSMemPut(pFileMem, pfile);
}

//得到指定位置的文件名(包括扩展名),文件位置自动下移
U8 GetNextFileName(U32 *filepos,char filename[])
{
	int i;
	if(root_buf[*filepos][0]==0)
		return FALSE;
	for(i=0;i<11;i++)
		filename[i]=root_buf[*filepos][i];

	filename[i]=0;
	(*filepos)++;
	return TRUE;
}
//列出当前位置开始第一个指定扩展名的文件,如果没有,则返回FALSE
U8 ListNextFileName(U32 *filepos, char FileExName[],char filename[])
{
	char tmpfilename[11];
	for(;;){
		if(!GetNextFileName(filepos,tmpfilename))
			return FALSE;
		if(strncmp(tmpfilename+8,FileExName, 3)==0){
			strncpy(filename,tmpfilename,8);
			filename[8]=0;
			return TRUE;
		}
		else if(*filepos==512)
			return FALSE;
	}
}

⌨️ 快捷键说明

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