📄 faccess2.c
字号:
#define FILE_ATTRIB 11
#define FILE_OR_DIRECTORY 16
#define FILE_HEAD_CLUSTER 26
#define FILESIZE 0x1C
#define FILEDATA 0x18
#define FILETIME 0x16
#define FILE_EXTENDNAME 0x8
struct LogicAddStack Stack[6];
#define STACKLEN 6
xdata unsigned char StackPointer;
#ifdef DEBUG
//xdata unsigned int M1;//debug var
#endif
xdata unsigned int TotalFile=0;
xdata unsigned int FileCounter=0;//FileNumber=0,DirCounter=0;
bit FileEnd=0;
xdata struct LogicAddress LogicA={0,0,0,0};
extern xdata unsigned char FileArrg[99];
//xdata unsigned int M1;
//-------------------------------------------------------------------
//fat operator
//input:current cluster
//output:fat table next cluster
//-------------------------------------------------------------------
unsigned int FindNextCluster(unsigned int CurrentCluster)
{ xdata unsigned int temp;
if((CurrentCluster>=0xFFF0)||(CurrentCluster==0))return CurrentCluster;
LogicA.Sector=CurrentCluster/(PbrInfo.BytePerSector/2);
if(LogicA.Sector>=PbrInfo.NumFatSector) return 0xFFF8;
LogicA.Cluster=0;
XBYTE[0x0f] =1;
ReadLogicPage(&LogicA);
XBYTE[0x0f] = 0;
temp=XBYTE[0x1200+(CurrentCluster%(PbrInfo.BytePerSector/2))*2+1];
temp<<=8;
temp|=XBYTE[0x1200+(CurrentCluster%(PbrInfo.BytePerSector/2))*2];
return temp;
}
//---------------------------------------------------------------------
//get file fcb table
//input:file number
//output: 1,success
// 0,error
//---------------------------------------------------------------------
//--------------------------------------------------------------------
//open file function
//input: file number
//output:0,error;1,success
//--------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//open file function
//input: file number
//output:0,error;1,success
//------------------------------------------------------------------------------------------
bit OpenFileByName(struct File *fp,unsigned char *Name,unsigned int FirstCluster)
{
xdata unsigned char Tname[13];
data unsigned char i,m,k;
if(FirstCluster == 0)
{
LogicA.Cluster = 1;
}
else
{
LogicA.Cluster = FirstCluster;
}
LogicA.Sector=0;
while(1)
{ MemoryRw;
ReadLogicPage(&LogicA);
McuRw;
for(i=0;i<16;i++)
{ if((XBYTE[0x1000+i*32]==0)||(XBYTE[0x1000+i*32]==0xe5)) continue;
if((XBYTE[0x1000+i*32+FILE_ATTRIB]&0x20)&&(!(XBYTE[0x1000+i*32+FILE_ATTRIB]&0x18)))
{ k=0;
for(m=0;m<8;m++)
{ if((XBYTE[0x1000+i*32+m])==0x20) break;
Tname[k++]=XBYTE[0x1000+i*32+m];
}
Tname[k++]='.';
for(m=0;m<3;m++)
{ if((XBYTE[0x1000+i*32+8+m])==0x20) break;
Tname[k++]=XBYTE[0x1000+i*32+8+m];
}
if(!m) k--;
Tname[k]='\0';
if(!strcmp(Tname,Name))
{
fp->FirstClustor=XBYTE[0x1000+i*32+FILE_HEAD_CLUSTER+1];
fp->FirstClustor<<=8;
fp->FirstClustor|=XBYTE[0x1000+i*32+FILE_HEAD_CLUSTER];
#ifdef DEBUG
//M1=fp->FirstClustor;
#endif
fp->FileSize1=XBYTE[0x1000+i*32+FILESIZE+3];
fp->FileSize1<<=8;
fp->FileSize1|=XBYTE[0x1000+i*32+FILESIZE+2];
#ifdef DEBUG
//M1=fp->FileSize1;
#endif
fp->FileSize0=XBYTE[0x1000+i*32+FILESIZE+1];
fp->FileSize0<<=8;
fp->FileSize0 = fp->FileSize0 + XBYTE[0x1000+i*32+FILESIZE];
#ifdef DEBUG
//M1 = XBYTE[0x1000+i*32+FILESIZE+1];
//M1 = XBYTE[0x1000+i*32+FILESIZE];
//M1=fp->FileSize0;
#endif
fp->Pointer=0;
fp->CurrentClustor=fp->FirstClustor;
return 1;
}
}
}
if(FirstCluster == 0)
{
if(++LogicA.Sector == PbrInfo.RootDirEntry/16)
{
return 0;
}
}
else
{
if(++LogicA.Sector == PbrInfo.SectorPerCluster)
{ LogicA.Cluster = FindNextCluster(LogicA.Cluster);
if(LogicA.Cluster >= 0xFFF0)
{
return 0;
}
LogicA.Sector=0;
}
}
}
}
//--------------------------------------------------------
//delete cluster chain
//input:frist cluster
//output:0,failure. 1,success
//This function fit any disk format--->modified cluster type
//--------------------------------------------------------
bit DeleteCluster(unsigned int Cluster)
{ xdata unsigned int temp;
if(Cluster < 2) return 0;
LogicA.Cluster = 0;
switch(PbrInfo.FatFormat)
{ case 0x12:
break;
case 0x16:
LogicA.Sector = Cluster >> 8;
break;
case 0x32:
break;
}
while(1)
{ MemoryRw;
ReadLogicPage(&LogicA);
McuRw;
switch(PbrInfo.FatFormat)
{ case 0x12:
break;
case 0x16:
while(1)
{ temp = XBYTE[0x1000 + ((Cluster & 0xFF) << 1) + 1];//(Cluster & 0xFF) << 2 == cluster%256*2
temp <<= 8;
temp |= XBYTE[0x1000 + ((Cluster & 0xFF) << 1)];
if(temp != 0)
{
XBYTE[0x1000 + ((Cluster & 0xFF) << 1) + 1] = 0;
XBYTE[0x1000 + ((Cluster & 0xFF) << 1)] = 0;
Cluster = temp;
}
if((temp>=0xFFF0) || (LogicA.Sector!=(temp >> 8)) ||(temp==0))//temp>>8 == temp/256
{ MemoryRw;
WriteFat(LogicA.Sector); //modfiy fat
McuRw;
#ifdef DEBUG
MemoryRw;
ReadLogicPage(&LogicA);
McuRw;
#endif
break;
}
}
if(temp >= 0xFFF8) return 1;
else if((temp >= 0xFFF0)||(temp == 0)) return 0;
if((temp >> 8) >= PbrInfo.NumFatSector) return 0;
LogicA.Cluster = 0;
LogicA.Sector = temp >> 8;
break;
case 0x32:
break;
}
}
}
bit DelFile(unsigned char *Name,unsigned int FirstCluster)
{
xdata unsigned char Tname[12];
xdata unsigned char m,k;
xdata unsigned int i,j,temp;
xdata unsigned long cc,dd;
LogicA.Cluster=FirstCluster;
LogicA.Sector=0;
while(1)
{ MemoryRw;
ReadLogicPage(&LogicA);
McuRw;
for( i= 0;i < 16; i++)
{ if((XBYTE[0x1000 + (i << 5)] == 0)||(XBYTE[0x1000 + (i << 5)] == 0xe5)) continue;
if(!(XBYTE[0x1000 + (i << 5) + FILE_ATTRIB] & 0x18))
{ k = 0;
for(m = 0;m < 8;m++)
{ if((XBYTE[0x1000 + (i << 5) + m])==0x20) break;
Tname[k++]=XBYTE[0x1000 + (i << 5) + m];
}
Tname[k++] = '.';
for(m = 0;m < 3;m++)
{ if((XBYTE[0x1000 + (i << 5) +8+m]) == 0x20) break;
Tname[k++]=XBYTE[0x1000 + (i << 5) + 8 + m];
}
if(!m) k--;
Tname[k]='\0';
if(Tname[0] == 5) Tname[0] = 0xE5;//changed by Lawrence at 8,6
k = 0;
while(Tname[k++]) Tname[k] = toupper(Tname[k]);//changed by Lawrence at 8,6
if(!strcmp(Tname,Name))
{
temp = XBYTE[0x1000 + (i << 5) + FILE_HEAD_CLUSTER + 1];
temp <<= 8;
temp |= XBYTE[0x1000 + (i << 5) + FILE_HEAD_CLUSTER];
XBYTE[0x1000 + (i << 5)] = 0xE5; //delete this file
cc = DataBase.Block*32 + DataBase.Page + LogicA.Sector;
dd = LogicA.Cluster - 2;
dd *= PbrInfo.SectorPerCluster;
dd += cc;
dd = dd + (dd/(32*496))*512;
j = dd>>5;//Block addr
k = dd%32;//page addr
BackupData(j,k,0);
//del fat
if(DeleteCluster(temp)) return 1;
else return 0;
}
}
}
LogicA.Sector++;
switch(PbrInfo.FatFormat)
{ case 0x12:
case 0x16:
if(LogicA.Sector == PbrInfo.SectorPerCluster)
{
LogicA.Cluster = FindNextCluster(LogicA.Cluster);
if(LogicA.Cluster >= 0xFFF0)
{
return 0;
}
LogicA.Sector=0;
}
break;
case 0x32:
break;
}
}
}
unsigned int Traverse(unsigned char Number,unsigned char FileType,unsigned int FirstCluster)
{
data unsigned int m;
data unsigned char StackPointer;
xdata unsigned char Name[2];
xdata unsigned int FileCounter = 0;
xdata unsigned char ExName[4];
bit SearchFile = 1;
XBYTE[0x0f] = 0;
StackPointer = 1;
Stack[0].Sector = 0;
Stack[0].Number = 0;
if(FirstCluster)
Stack[0].Cluster = FirstCluster;
else
Stack[0].Cluster = 1;
while(StackPointer)
{
Flush:
LogicA.Cluster = Stack[StackPointer - 1].Cluster;
LogicA.Sector = Stack[StackPointer - 1].Sector;
m = Stack[StackPointer - 1].Number ;
Loop:
MemoryRw;
ReadLogicPage(&LogicA);
McuRw;
for(;m<16;m++)
{ if((XBYTE[0x1000 + (m << 5)] == 0) ||(XBYTE[0x1000 + (m << 5)] == 0xE5)) continue;
if(XBYTE[0x1000 + (m << 5) + FILE_ATTRIB] & 0x8) continue;//disk volume
//search mp3 directory
if((FirstCluster==1) && (FileType&0x40))
{
if(XBYTE[0x1000 + (m << 5) + FILE_ATTRIB] & 0x10)
{
if((XBYTE[0x1000 + (m << 5)] == 'V')
&&(XBYTE[0x1000 + (m << 5) + 1] == 'O')
&&(XBYTE[0x1000 + (m << 5)+ 2] == 'I')
&&(XBYTE[0x1000 + (m << 5)+ 3] == 'C')
&&(XBYTE[0x1000 + (m << 5)+ 4] == 'E')
&&(XBYTE[0x1000 + (m << 5) + 8] == ' '))
{
FirstCluster = XBYTE[0x1000 + (m << 5) + FILE_HEAD_CLUSTER + 1];
FirstCluster <<= 8;
FirstCluster |= XBYTE[0x1000 + (m << 5) + FILE_HEAD_CLUSTER];
return FirstCluster;
}
}
continue;
}
if(FirstCluster)
{
if(XBYTE[0x1000 + (m << 5) + FILE_ATTRIB] & 0x10)
{
continue;
}
else
{
if(SearchFile)
{
strncpy(ExName,&XBYTE[0x1000 + (m << 5) + FILE_EXTENDNAME],3);
ExName[3] = '\0';
if(FileType & 0x81)
if(strcmp(ExName,"MP3")) continue;
if(FileType & 0x82)
if(strcmp(ExName,"PCM")) continue;
if(FileType & 0x84)
{
if(strcmp(ExName,"G72")) continue;
if((XBYTE[0x1000 + (m << 5)]!='R')&&(XBYTE[0x1000 + (m<<5)]!='r'))continue;
Name[0] = XBYTE[0x1000 + (m << 5) +1];
Name[1] = XBYTE[0x1000 + (m << 5) +2];
if((Name[0] < 0x30)||(Name[0]>0x39))continue;
if((Name[1] < 0x30)||(Name[1]>0x39))continue;
if((Name[0] ==0)&&(Name[1] == 0))continue;
Name[0] -= 0x30;
Name[1] -= 0x30;
Name[0] *=10;
Name[0] += Name[1];
FileArrg[Name[0]-1] = 1;
}
}
else
continue;
}
}
if(++FileCounter == Number)
{ //return FILE struct
Stack[0].Cluster = XBYTE[0x1000 + (m << 5) + FILE_HEAD_CLUSTER + 1];
Stack[0].Cluster <<= 8;
Stack[0].Cluster |= XBYTE[0x1000 + (m << 5) + FILE_HEAD_CLUSTER];
Stack[1].Cluster = XBYTE[0x1000 + (m << 5) + FILESIZE + 3];
Stack[1].Cluster <<= 8;
Stack[1].Cluster |= XBYTE[0x1000 + (m << 5) + FILESIZE + 2];
Stack[1].Sector = XBYTE[0x1000 + (m << 5) + FILESIZE + 1];
Stack[1].Sector <<= 8;
Stack[1].Sector |= XBYTE[0x1000 + (m << 5) + FILESIZE];
//return file fcb
Stack[2].Cluster = LogicA.Cluster;
Stack[2].Sector = LogicA.Sector;
Stack[3].Cluster = m << 5;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -