📄 api.c
字号:
//************************************************************************
//author:dragon
//web:8dragon.com
//2004.2.5完成于桃龙源
//*************************************************************************
#include <c8051F020.h>
#include "api.h"
#include "file.h"
#include "host_811.h"
#include "ufi.h"
#include "string.h"
#include "math.h"
struct _FILE ApiFileControl;
extern struct _BPB SimpleBpb;
extern FAT_PARAMETER FatParameter;
BYTE xdata BuffFile[8*512] _at_ 0xa000;
extern xdata WORD FatCache[2][256] ;
BYTE CreateFile(char* filename, BYTE attribute)
{
struct _DIR dir,*pdir;
BYTE name[11];
struct _FILE file;
// WORD NewCluster;
xdata BYTE Cache[512];
static char NewFileName[12];
char * p=NewFileName;
////////////////////////////////////////////////////////////////////////
//*Get valid filen name
if(!CheckFileName(filename,p))
return FALSE;
//*核对该文件是否已经存在
if(LocateFile(filename, NULL)!=0xffffffff)
return FALSE;
strncpy(name, p, 11);
memset(&dir, 0, sizeof(dir));
memcpy(dir.Name, name, 11);
dir.Attr = attribute;
dir.CrtDate =0;
dir.CrtTime =0;
dir.CrtTimeTenth =0;
dir.FileSize =0;
DelayMs(5);
if(!LocateDir(&dir, &file))
return FALSE;
// NewCluster=SeekEmptyCluseter();
// if(NewCluster==0xffff)
// return FALSE;
DelayMs(15);
if(!Read(file.DirSectorNum,1,(BYTE *)Cache))
return FALSE;
pdir = (struct _DIR *)Cache;
pdir += file.DirIndex;
pdir->FstClusLO =0x00;//WordSwap(NewCluster);
pdir->FileSize=0;
if(!Write(file.DirSectorNum,1,Cache,TRUE))
if(!Write(file.DirSectorNum,1,Cache,TRUE))
return FALSE;
return OpenFile(filename);
}
BYTE DelFile(const char * filename)
{
DWORD FileFirstSector;
BYTE buf[512];
WORD Cluster,Cache[256];
struct _DIR *dir;
FileFirstSector=LocateFile(filename, &ApiFileControl);
if(FileFirstSector==0xffffffff)
return FALSE;
dir=(struct _DIR *)buf;
if(!Read(ApiFileControl.DirSectorNum,1,buf))
return FALSE;
ApiFileControl.dir.Name[0]=0xe5;
dir=dir+ApiFileControl.DirIndex;
memcpy(dir,&ApiFileControl.dir,sizeof(struct _DIR));
if(!Write(ApiFileControl.DirSectorNum ,1,buf,TRUE))
return FALSE;
Cluster=WordSwap(ApiFileControl.dir.FstClusLO);
if(!Read(((2*Cluster/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum),1,(BYTE *)Cache))
return FALSE;
for(;;)
{
// Cluster=GetListCluster(Cluster);
Cache[(Cluster%(SimpleBpb.BytsPerSec/2))]=0;
if((Cluster%(SimpleBpb.BytsPerSec/2))==0)
{
if(!Write((2*Cluster/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum,1,(BYTE *)Cache,TRUE))
return FALSE;
if(!Read(((2*Cluster/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum),1,(BYTE *)Cache))
return FALSE;
}
Cluster=GetListCluster(Cluster);
if((GetListCluster(Cluster)==0xffff)||(Cluster==0))
{
Cache[(Cluster%(SimpleBpb.BytsPerSec/2))]=0;
break;
}
}
if(!Write((2*Cluster/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum,1,(BYTE *)Cache,TRUE))
return FALSE;
return TRUE;
}
BYTE OpenFile(const char* filename)
{
DWORD FileFirstSector;
WORD Cluster;
////////////////////////////////////////////////////////////////////////
FileFirstSector=LocateFile(filename, &ApiFileControl);
if(FileFirstSector==0xffffffff)
return FALSE;
ApiFileControl.StartSectorNum = FileFirstSector;
Cluster=WordSwap(ApiFileControl.dir.FstClusLO);
for(;;)
{
if((GetListCluster(Cluster)==0xffff)||(Cluster==0))
break;
Cluster=GetListCluster(Cluster);
}
//ApiFileControl.CurrentSectorNum为该簇对应的第一个扇区
ApiFileControl.CurrentSectorNum = ClusterToSec(Cluster);
ApiFileControl.SectorOffset=(DWordSwap(ApiFileControl.dir.FileSize)%(SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus));
ApiFileControl.dir.FileSize=DWordSwap(ApiFileControl.dir.FileSize);
return TRUE;
}
BYTE WriteFile(const char* buffer, UINT bytes)
{
BYTE *pCache;
xdata WORD Cache[512];
UINT write_bytes =0,flag=0;
UINT max_write_bytes_in_sector;
WORD Cluster;
////////////////////////////////////////////////////////////////////////
//如果打开的文件是一个空文件,则进入下面
//为其寻找一个开始的数据蔟,标记为0xffff
//数据直接填在该簇对应的扇区内
if(ApiFileControl.StartSectorNum<FatParameter.FirstDataSector)
{
Cluster=SeekEmptyCluster();
if(!Read(FatParameter.FirstFatSecNum+(2*Cluster/SimpleBpb.BytsPerSec),1,(BYTE *)Cache))
return FALSE;
Cache[Cluster%(SimpleBpb.BytsPerSec/2)]=0xffff;
if(!Write(FatParameter.FirstFatSecNum+(2*Cluster/SimpleBpb.BytsPerSec),1,(BYTE *)Cache,0))
return FALSE;
ApiFileControl.dir.FstClusLO=WordSwap(Cluster);
ApiFileControl.StartSectorNum=ApiFileControl.CurrentSectorNum=ClusterToSec(Cluster);
flag=1;
}
///////////////////////////////////////////////////////////////////////////////
//
//如过前一个WriteFile函数的字节或者打开一个已经有为内容的文件的字节偏移刚好为
//SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus的整数倍,为了保持总的算法一致
//(都是先写好某簇为0xffff然后再写该簇的内容)则开始的时候必须先为其准备好
//一个未有内容的簇,标记好为0xffff,然后数据直接填在该簇对应的扇区内。
//
//////////////////////////////////////////////////////////////////////////////
if((ApiFileControl.SectorOffset==0)&&flag==0)
{
Cluster =LinkClusterList(SecToCluster(ApiFileControl.CurrentSectorNum));
if(Cluster == 0xffff)
return FALSE;
ApiFileControl.CurrentSectorNum = ClusterToSec(Cluster);
}
for(;;)
{
pCache=BuffFile;//Cache;
pCache += ApiFileControl.SectorOffset;
if((SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus - ApiFileControl.SectorOffset) > (bytes - write_bytes))
max_write_bytes_in_sector = (bytes - write_bytes);
else
max_write_bytes_in_sector=(SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus - ApiFileControl.SectorOffset);
memcpy(pCache, buffer, max_write_bytes_in_sector);
ApiFileControl.SectorOffset += max_write_bytes_in_sector;
if(ApiFileControl.SectorOffset>=SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus)
{
if(!Write(ApiFileControl.CurrentSectorNum,SimpleBpb.SecPerClus,(BYTE*)BuffFile,FALSE))
if(!Write(ApiFileControl.CurrentSectorNum,SimpleBpb.SecPerClus,(BYTE*)BuffFile,FALSE))
return FALSE;
if((write_bytes+max_write_bytes_in_sector)<bytes)
{
Cluster =LinkClusterList(SecToCluster(ApiFileControl.CurrentSectorNum));
if(Cluster == 0xffff)
return FALSE;
ApiFileControl.CurrentSectorNum = ClusterToSec(Cluster);
}
ApiFileControl.SectorOffset = 0;
}
write_bytes += max_write_bytes_in_sector;
buffer = (char*)buffer + max_write_bytes_in_sector;
ApiFileControl.dir.FileSize += max_write_bytes_in_sector;
if(write_bytes >= bytes)
return TRUE;
}//end for cycle
return FALSE;
}
BYTE CloseFile()
{
xdata BYTE Cache[512];
struct _DIR *dir;
////////////////////////////////////////////////////////////////////////
if(ApiFileControl.SectorOffset!=0)
{
if(!Write(ApiFileControl.CurrentSectorNum,SimpleBpb.SecPerClus,(unsigned char *)BuffFile,TRUE))
if(!Write(ApiFileControl.CurrentSectorNum,SimpleBpb.SecPerClus,(unsigned char *)BuffFile,TRUE))
return FALSE;
}
DelayMs(50);
if(!Read(ApiFileControl.DirSectorNum,1,Cache))
if(!Read(ApiFileControl.DirSectorNum,1,Cache))
return FALSE;
dir = (struct _DIR *)Cache;
dir += ApiFileControl.DirIndex;
ApiFileControl.dir.FileSize=DWordSwap(ApiFileControl.dir.FileSize);
memcpy(dir, &ApiFileControl.dir, sizeof(struct _DIR));
DelayMs(50);
if(!Write(ApiFileControl.DirSectorNum,1,Cache,TRUE))
if(!Write(ApiFileControl.DirSectorNum,1,Cache,TRUE))
return FALSE;
if(FatCache[0][256]!=0)
{
if(!Write(FatCache[0][256],1,(BYTE *)FatCache[0],0))
if(!Write(FatCache[0][256],1,(BYTE *)FatCache[0],0))
return FALSE;
}
return TRUE;
}
BYTE ReadFile(void* buffer, UINT bytes)
{
BYTE*pCache;
UINT read_bytes =0;
UINT max_copy_bytes_in_sector;
WORD Cluster;
DWORD FileSize;
// BYTE buf[600];
// for(i=0;i<600;i++)
// buf[i]='a';
// memcpy(buffer,buf,400);
// return TRUE;
////////////////////////////////////////////////////////////////////////
FileSize = ApiFileControl.dir.FileSize;
if(bytes>=FileSize)
bytes=FileSize;
Cluster=SecToCluster(ApiFileControl.CurrentSectorNum);
// Sector=ClusterToSec(Cluster);
ApiFileControl.SectorOffset=ApiFileControl.SectorOffset%(SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus);
for(; ; )
{
pCache=BuffFile;
pCache+=ApiFileControl.SectorOffset;
if((SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus - ApiFileControl.SectorOffset) > (bytes - read_bytes))
max_copy_bytes_in_sector = (bytes - read_bytes);
else
max_copy_bytes_in_sector=(SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus - ApiFileControl.SectorOffset);
ApiFileControl.SectorOffset+=max_copy_bytes_in_sector;
if(!Read(ApiFileControl.CurrentSectorNum,SimpleBpb.SecPerClus,(BYTE *)BuffFile))
return FALSE;
memcpy((char *)buffer, pCache, max_copy_bytes_in_sector);
// return TRUE;
read_bytes+=max_copy_bytes_in_sector;
if(read_bytes>=bytes)
return TRUE;
buffer = (char*)buffer + max_copy_bytes_in_sector;
Cluster=GetListCluster(Cluster);
ApiFileControl.SectorOffset=0;
ApiFileControl.CurrentSectorNum=ClusterToSec(Cluster);
}//---------end for cycle
return FALSE;
}
BYTE SetFileSector(const char * filename,DWORD fileSector,DWORD offset)
{
DWORD FileFirstSector;
WORD Cluster;
////////////////////////////////////////////////////////////////////////
FileFirstSector=LocateFile(filename, &ApiFileControl);
if(FileFirstSector==0xffffffff)
return FALSE;
ApiFileControl.StartSectorNum = FileFirstSector;
Cluster=WordSwap(ApiFileControl.dir.FstClusLO);
for(;;)
{
if((GetListCluster(Cluster)==0xffff)||(Cluster==0))
break;
Cluster=GetListCluster(Cluster);
}
//ApiFileControl.CurrentSectorNum为该簇对应的第一个扇区
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -