?? fat16.c
字號:
{
for(; i< SectorsPerCluster; i++)
{
Cache = GetSectorData(fp->CurrentSectorNum);
if(Cache == NULL)
return 0;
Cache += fp->SectorOffset;
max_copy_bytes_in_sector = (BytesPerSector - fp->SectorOffset) > (bytes - read_bytes) ? (bytes - read_bytes) : (BytesPerSector- fp->SectorOffset);
memcpy(ReadBuffer1[i], Cache, max_copy_bytes_in_sector);
read_bytes += max_copy_bytes_in_sector;
fp->SectorOffset += max_copy_bytes_in_sector;
fp->offset += max_copy_bytes_in_sector;
buffer = (char*)buffer + max_copy_bytes_in_sector;
if(fp->SectorOffset == BytesPerSector)
{
if(i == SectorsPerCluster -1)
{
Cluster = GetNextClusterNum(Cluster);
if(Cluster != 0xffff)
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
}
else
fp->CurrentSectorNum ++;
fp->SectorOffset = 0;
}
if(read_bytes == bytes)
{
return bytes;
}
}
}
while(Cluster != 0xffff) //讀取數據簇,直到0xffff
{
for(i=0; i< SectorsPerCluster; i++)
{
Cache = GetSectorData(fp->CurrentSectorNum);//取得當前扇區數據
if(Cache == NULL)
return 0;
Cache += fp->SectorOffset;//加上要儲存數據的零頭(在最后有用),一個扇區512字節內的一個偏移量,一般為0
//計算最大數據拷貝字節數,在最后計算出要存放的零頭
//如果 每扇區需要存放的字節數(一般為512) > 還沒有存放的字節總數 那就等于 還沒有存放的字節總數
//如果 每扇區需要存放的字節數(一般為512) < 還沒有存放的字節總數 那就等于 每扇區需要存放的字節數(一般為512)
max_copy_bytes_in_sector = (BytesPerSector- fp->SectorOffset) > (bytes - read_bytes) ? (bytes - read_bytes) : (BytesPerSector- fp->SectorOffset);
memcpy(ReadBuffer1[j*8+i], Cache, max_copy_bytes_in_sector);//存放到緩存
read_bytes += max_copy_bytes_in_sector;//計算已讀取存放的字節總數
//計算偏移量,如果之前存放的是一整扇區512字節的話,接下去會被清0
//SectorOffset一般在數據最后才有意義,因為要讀取的數據不一定都是扇區的整數倍
//SectorOffset就是最后的零頭
fp->SectorOffset += max_copy_bytes_in_sector;
fp->offset += max_copy_bytes_in_sector;
buffer = (char*)buffer + max_copy_bytes_in_sector;
if(fp->SectorOffset == BytesPerSector)//如果之前存放的是一整扇區的話,說明后面還有數據
{
if(i == SectorsPerCluster -1) //如果是一簇中的最后一個扇區
{
Cluster = GetNextClusterNum(Cluster); //取得下一個簇號
if(Cluster != 0xffff) //如果不是結束標志,則計算當前扇區數
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
j++;//將數據緩存往后挪8個扇區,j*8
if(j == 10) j=0;
}
else
fp->CurrentSectorNum ++; //當前扇區加1
fp->SectorOffset = 0; //如果之前存放的是一整扇區512字節的話,SectorOffset清0
}
if(read_bytes == bytes)
{
return bytes;
}
}
}
return 0;
}
/********************************************************************************************************************
** 函數名稱: unsigned int fat_read1
** 功能描述: 從SD/MMC卡中讀取一個文件,按簇讀取(實際是單塊讀取)
** 輸 入: int handle : 句柄
void* buffer : 接收緩沖區
** 輸 出: 0: 成功 >0: 錯誤碼
*********************************************************************************************************************/
unsigned int fat_read1(int handle, void* buffer)
{
BYTE* Cache;
_FILE *fp;
WORD Cluster;
int i,j=0;
int k=0;
if(handle <0 || handle >= sizeof(handles)/sizeof(_FILE))
return 0;
fp = &handles[handle]; //裝入文件句柄(由之前fat_open帶回的x),handles為指向_FILE的數組
Cluster = SectorNum2ClusterNum(fp->CurrentSectorNum);//根據當前扇區算出數據起始簇
i = (fp->CurrentSectorNum - FirstDataSector) % SectorsPerCluster;//計算是否有不是一整簇的扇區
if(i != 0) //讀取不滿一簇的數據扇區
{
for(; i< SectorsPerCluster; i++)
{
Cache = GetSectorData(fp->CurrentSectorNum);
if(Cache == NULL)
return 0;
memcpy(ReadBuffer1[j*8+i], Cache, 512);
if(i == SectorsPerCluster -1)
{
Cluster = GetNextClusterNum(Cluster);
if(Cluster != 0xffff)
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
}
else
fp->CurrentSectorNum ++;
}
}
while(Cluster != 0xffff) //讀取數據簇,直到0xffff
{
for(i=0; i< SectorsPerCluster; i++)
{
Cache = GetSectorData(fp->CurrentSectorNum);//取得當前扇區數據
if(Cache == NULL)
return 0;
memcpy(ReadBuffer1[j*8+i], Cache, 512);//存放到緩存
//if(k==0) memcpy(ReadBuffer2[0], Cache, 512);//保存第一扇區內容
k++;
if(i == SectorsPerCluster -1) //如果是一簇中的最后一個扇區
{
Cluster = GetNextClusterNum(Cluster); //取得下一個簇號
if(Cluster != 0xffff) //如果不是結束標志,則計算當前扇區數
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
else
{
m=i;
n=j;
}
j++;//將數據緩存往后挪8個扇區,j*8
if(j == 10) j=0;
}
else
fp->CurrentSectorNum ++; //當前扇區加1
}
}
return 0;
}
/********************************************************************************************************************
** 函數名稱: unsigned int fat_read2
** 功能描述: 從SD/MMC卡中讀取一個文件,按簇讀取(實際是多塊--以簇為單位讀取)
** 輸 入: int handle : 句柄
void* buffer : 接收緩沖區
** 輸 出: 0: 成功 >0: 錯誤碼
*********************************************************************************************************************/
unsigned int fat_read2(int handle)
{
BYTE* Cache;
_FILE *fp;
WORD Cluster;
int i,j=0;
int k=0,buf1_busy=0,buf2_busy=0;
unsigned long int aa1=0,aa2=0;
if(handle <0 || handle >= sizeof(handles)/sizeof(_FILE))
return 0;
fp = &handles[handle]; //裝入文件句柄(由之前fat_open帶回的x),handles為指向_FILE的數組
Cluster = SectorNum2ClusterNum(fp->CurrentSectorNum);//根據當前扇區算出數據起始簇
i = (fp->CurrentSectorNum - FirstDataSector) % SectorsPerCluster;//計算是否有不是一整簇的扇區
if(i != 0) //讀取不滿一簇的數據扇區
{
for(; i< SectorsPerCluster; i++)
{
Cache = GetSectorData(fp->CurrentSectorNum);
if(Cache == NULL)
return 0;
memcpy(ReadBuffer1[j*8+i], Cache, 512);
if(i == SectorsPerCluster -1)
{
Cluster = GetNextClusterNum(Cluster);
if(Cluster != 0xffff)
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
}
else
fp->CurrentSectorNum ++;
}
}
aa1 = (1<<30)+(1<<28)+(int)( (unsigned char *)ReadBuffer1 + 0x30 );
aa2 = (1<<30)+(1<<28)+(int)( (unsigned char *)ReadBuffer2 + 0x30 );
while(Cluster != 0xffff) //讀取數據簇,直到0xffff
{
//for(i=0; i<50; i++)
//{
SD_ReadMultiBlock1(Cluster, ReadBuffer1+j); //讀取一整簇的數據
Cluster = GetNextClusterNum(Cluster); //取得下一個簇號
//rBDISRC0 = aa1;
j=j+8;
//}
//rIISCON |=0x1;
//SD_ReadMultiBlock1(Cluster, ReadBuffer2); //讀取一整簇的數據
//Cluster = GetNextClusterNum(Cluster); //取得下一個簇號
//rBDISRC0 = aa2;
}
return 0;
}
unsigned int fat_write(int handle, const char* buffer, unsigned int bytes)
{
BYTE* Cache;
unsigned int write_bytes =0;
unsigned int max_write_bytes_in_sector;
_FILE *fp;
WORD Cluster;
WORD PrevCluster;
int i;
if(handle <0 || handle >= sizeof(handles)/sizeof(_FILE))
return 0;
fp = &handles[handle];
Cluster = SectorNum2ClusterNum(fp->CurrentSectorNum);
PrevCluster = Cluster;
i = (fp->CurrentSectorNum - FirstDataSector) % SectorsPerCluster;
if(i != 0)
{
for(; i< SectorsPerCluster; i++)
{
Cache = GetSectorData(fp->CurrentSectorNum);
if(Cache == NULL)
return 0;
Cache += fp->SectorOffset;
max_write_bytes_in_sector = (BytesPerSector- fp->SectorOffset) > (bytes - write_bytes) ? (bytes - write_bytes) : (BytesPerSector - fp->SectorOffset);
memcpy(Cache, buffer, max_write_bytes_in_sector);
Flush();
write_bytes += max_write_bytes_in_sector;
fp->SectorOffset += max_write_bytes_in_sector;
fp->offset += max_write_bytes_in_sector;
buffer = (char*)buffer + max_write_bytes_in_sector;
fp->dir.deFileSize += max_write_bytes_in_sector;
if(fp->SectorOffset == BytesPerSector)
{
if(i == SectorsPerCluster -1)
{
PrevCluster = Cluster;
Cluster = GetNextClusterNum(Cluster);
if(Cluster != 0xffff)
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
else
{
Cluster = AllocCluster(PrevCluster);
if(Cluster == 0xffff)
return 0;
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
}
}
else
fp->CurrentSectorNum ++;
fp->SectorOffset = 0;
}
if(write_bytes == bytes)
{
return bytes;
}
}
}
for(;;)
{
for(i=0; i< SectorsPerCluster; i++)
{
Cache = GetSectorData(fp->CurrentSectorNum);
if(Cache == NULL)
return 0;
Cache += fp->SectorOffset;
max_write_bytes_in_sector = (BytesPerSector - fp->SectorOffset) > (bytes - write_bytes) ? (bytes - write_bytes) : (BytesPerSector - fp->SectorOffset);
memcpy(Cache, buffer, max_write_bytes_in_sector);
Flush();
write_bytes += max_write_bytes_in_sector;
fp->SectorOffset += max_write_bytes_in_sector;
fp->offset += max_write_bytes_in_sector;
buffer = (char*)buffer + max_write_bytes_in_sector;
fp->dir.deFileSize += max_write_bytes_in_sector;
if(fp->SectorOffset == BytesPerSector)
{
if(i == SectorsPerCluster -1)
{
PrevCluster = Cluster;
Cluster = GetNextClusterNum(Cluster);
if(Cluster != 0xffff)
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
else
{
Cluster = AllocCluster(PrevCluster);
if(Cluster == 0xffff)
return 0;
fp->CurrentSectorNum = ClusterNum2SectorNum(Cluster);
}
}
else
fp->CurrentSectorNum ++;
fp->SectorOffset = 0;
}
if(write_bytes == bytes)
{
return bytes;
}
}
}
// we can not reach here.
return 0;
}
int fat_remove( const char *filename)
{
DWORD SectorNum;
_FILE file;
//locate
SectorNum = fat_locate(filename, &file);
if(SectorNum == 0xffffffff)
return 4;
// is it a dir ?
if(file.dir.deAttributes & ATTR_DIRECTORY)
return 6;
if(DeleteDir(&file) != 0)
return 5;
FreeCluster(file.dir.deStartCluster);
return 0;
}
int fat_get_stat( const char *filename, _STAT * stat)
{
DWORD SectorNum;
_FILE file;
//locate
SectorNum = fat_locate(filename, &file);
if(SectorNum == 0xffffffff)
return 1;
stat->Attr = file.dir.deAttributes;
stat->CrtDate = file.dir.CrtDate;
stat->CrtTime = file.dir.CrtTime;
stat->CrtTimeTenth = file.dir.CrtTimeTenth;
stat->FileSize = file.dir.deFileSize;
stat->LstAccDate = file.dir.LstAccDate;
stat->WrtDate = file.dir.WrtDate;
stat->WrtTime = file.dir.WrtTime;
return 0;
}
int fat_set_stat( const char *filename, _STAT * stat)
{
DWORD SectorNum;
_FILE file;
BYTE* Cache;
DIRENTRY *dir;
//locate
SectorNum = fat_locate(filename, &file);
if(SectorNum == 0xffffffff)
return 1;
file.dir.deAttributes = stat->Attr;
file.dir.CrtDate = stat->CrtDate;
file.dir.CrtTime = stat->CrtTime;
file.dir.CrtTimeTenth = stat->CrtTimeTenth;
file.dir.deFileSize = stat->FileSize;
file.dir.LstAccDate = stat->LstAccDate;
file.dir.WrtDate = stat->WrtDate;
file.dir.WrtTime = stat->WrtTime;
Cache = GetSectorData(file.DirSectorNum);
if(Cache == NULL)
return 2;
dir = (DIRENTRY *)Cache;
dir += file.DirIndex;
memcpy((BYTE *)dir, (BYTE *)&file.dir, sizeof(DIRENTRY));
Flush();
return 0;
}
int fat_rename( const char *oldname, const char *newname )
{
DIRENTRY dir;
char path[512];
char newpath[512];
char name[11];
char new_name[11];
char *p;
DWORD ParentDirSectorNum;
_FILE old_file;
//
//check oldname file
//
// is path format correct ?
p = get_valid_format(oldname);
if(p == NULL)
return -2;
//if exist this file ?
if(fat_locate(oldname, &old_file) == 0xffffffff)
return -3;
//separate path into parent and name
strncpy(name, &p[strlen(p)-11], 11);
strcpy(path, oldname);
p = strrchr(path, '\\');
*p = '\0';
//
//check newname file
//
if(strchr(newname, '\\') != NULL)
return -2;
sprintf(newpath, "%s\\%s", path, newname);
// is path format correct ?
p = get_valid_format(newpath);
if(p == NULL)
return -2;
//if exist this file ?
if(fat_locate(newpath, NULL) != 0xffffffff)
return -3;
//separate path into parent and name
strncpy(new_name, &p[strlen(p)-11], 11);
//locate parent path
ParentDirSectorNum = fat_locate(path, NULL);
if(ParentDirSectorNum == 0xffffffff)
return -4;
//fill dir attributes
memcpy((BYTE *)&dir,(BYTE *)(&old_file.dir), sizeof(DIRENTRY));
memcpy((BYTE *)dir.deName, new_name, 11);
//alloc one dir
if(AllocDir(ParentDirSectorNum, &dir, NULL) != 0)
return -5;
//delete old one
if(DeleteDir(&old_file) != 0)
return -6;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -