📄 dir.c
字号:
//###########################################################
// File: dir.c
//
// Directory functions
//
// For FAT12, FAT16
// Only for first Partition
// Only for drives with 512 bytes per sector (the most)
//#########################################################################
#include <avr/io.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dos.h"
//############################################################
// search for a filename or directoryname in current directory
//############################################################
unsigned char FindName(char *name)
{
U8 i;
unsigned char result;
result=NO_MATCH;
for(i=0; i<RootDirSectors; i++)
{
result = ScanOneDirectorySector(FirstRootSector+i, name);
if(result!=NO_MATCH)
break; //break sector loop
}
return result;
}
//###########################################################
// find a filename
//
// Following global variables will be set if filename
// was found:
//
// FileFirstCluster First cluster of the file
// FileSize
//
//###########################################################
unsigned char ScanOneDirectorySector(unsigned long sector, char *name)
{
U16 count;
unsigned long tmp;
struct DirEntry *di;
char nam[8];
unsigned char i,match;
MakeFileName(name,nam);
// by=ReadSector(sector,dirbuf); //read one directory sector.
count=0;
match=NO_MATCH;
MMC_CS_OFF();
MMCStartReadSector(sector);
do
{
for(i=0; i<32; i++)
{
SPI_WRITE(0xFF);
dirbuf[i]=SPDR;
}
di=(struct DirEntry *)dirbuf;
if((unsigned char)di->DIR_Name[0]!=0xE5 && di->DIR_Name[0]>0 && match!=FULL_MATCH)
{
di->DIR_Attr&=0x3F; //smash upper two bits
if(di->DIR_Attr==ATTR_LONG_NAME)
{
}
else
{
if(strncmp(nam,di->DIR_Name,8)==0) match=FULL_MATCH;
if(di->DIR_Attr & ATTR_VOLUME_ID)
{
}
else
{
if(di->DIR_Attr & ATTR_DIRECTORY) //this is a directory
{
}//if(di->DIR_Attr & ATTR_DIRECTORY)
else //is not a directory. this is a file
{
tmp=di->DIR_FstClusHI; //Highword of first cluster number
tmp<<=16;
tmp+=di->DIR_FstClusLO; //Lowword of first cluster number
if(match==FULL_MATCH)
{
FileFirstCluster=tmp;
FileSize=di->DIR_FileSize;
}
}//if(di->DIR_Attr & ATTR_DIRECTORY)
}
}
}//if(di->DIR_Name[0]!=0xE5 && di->DIR_Name[0]>0)
count+=32;
}
while(count<BYTE_PER_SEC);
SPI_WRITE(0xFF); //CRC
SPI_WRITE(0xFF);
MMC_CS_ON();
return match;
}
//###########################################################
//
//###########################################################
void MakeFileName(char *inname, char *outname)
{
unsigned char by,i;
char *p;
p=outname;
for(i=0; i<8; i++) *p++=' '; //fill filename buffer with spaces
p=outname;
i=0; //get filename
do
{
by=inname[i];
if(by!=0) *p++=by;
i++;
}while(i<8 && by!=0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -