📄 fat.c
字号:
DWORD cluster;
DWORD tempclust;
unsigned char cnt;
unsigned int offset;
unsigned int i=0;
unsigned char j;//long name buffer offset;
unsigned char *p;//long name buffer pointer
struct direntry *item = 0;
struct winentry *we =0;
//cluster = FAT_OpenDir(dir);
//if(cluster == 1)return 1;
WORD addr = RECORD_ADDR_START;
while(1)
{
#if FAT_DEBUG
printf_P(PSTR("\r\n"));
#endif
cluster = GetFolderCluster(addr);
#if FAT_DEBUG
printf_P(PSTR("\r\nSearch the folder: cluster: %ld"),cluster);
#endif
addr += 4;
if(cluster == 0xffffffff)break;
else
{
if(music_record_addr != 0) *music_record_addr = addr - 4; /* record in which record found the right file */
if(cluster==0 && FAT32_Enable==0)// root directory
{
buffer=malloc(512);//apply memory
if(buffer==0)return 1;//if failed
for(cnt=0;cnt<RootDirSectors;cnt++)
{
if(FAT_ReadSector(FirstDirSector+cnt,buffer)){free(buffer);return 1;}
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);//pointer convert
//find a valid item and display it
if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
{
if(item->deAttributes == 0x0f)
{
we = (struct winentry *)(&buffer[offset]);
j = 26 *( (we->weCnt-1) & WIN_CNT);
if(j<MAX_LONG_NAME_SIZE-25)
{
p = &LongNameBuffer[j];
for (j=0;j<10;j++) *p++ = we->wePart1[j];
for (j=0;j<12;j++) *p++ = we->wePart2[j];
for (j=0;j<4;j++) *p++ = we->wePart3[j];
if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;
if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1;
}
LongNameBuffer[MAX_LONG_NAME_SIZE-1] = 0;
LongNameBuffer[MAX_LONG_NAME_SIZE-2] = 0;
/*p = &LongNameBuffer[j];
for (j=0;j<10;j++) *p++ = we->wePart1[j];
for (j=0;j<12;j++) *p++ = we->wePart2[j];
for (j=0;j<4;j++) *p++ = we->wePart3[j];
if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;
if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1; */
}
else if((item->deExtension[0] == 'M')&&(item->deExtension[1] == 'P')&&(item->deExtension[2] == '3'))
{
#if FAT_DEBUG
printf_P(PSTR("\r\nFound mp3 file! index = %d name: "),i+1);
for(j=0;j<8;j++)USART_putchar(item->deName[j]);
USART_putchar('.');
for(j=0;j<3;j++)USART_putchar(item->deExtension[j]);
#endif
CopyDirentruyItem(MusicInfo,item);
*type=1;
i++;
if(i==*Count){free(buffer);return 0;}
else LongNameFlag = 0;
}
else if((item->deExtension[0] == 'W')&&(item->deExtension[1] == 'M')&&(item->deExtension[2] == 'A'))
{
#if FAT_DEBUG
printf_P(PSTR("\r\nFound wma file! index = %d name: "),i+1);
for(j=0;j<8;j++)USART_putchar(item->deName[j]);
USART_putchar('.');
for(j=0;j<3;j++)USART_putchar(item->deExtension[j]);
#endif
CopyDirentruyItem(MusicInfo,item);
*type=2;
i++;
if(i==*Count){free(buffer);return 0;}
else LongNameFlag = 0;
}
else if((item->deExtension[0] == 'M')&&(item->deExtension[1] == 'I')&&(item->deExtension[2] == 'D'))
{
#if FAT_DEBUG
printf_P(PSTR("\r\nFound mp3 file! index = %d name: "),i+1);
for(j=0;j<8;j++)USART_putchar(item->deName[j]);
USART_putchar('.');
for(j=0;j<3;j++)USART_putchar(item->deExtension[j]);
#endif
CopyDirentruyItem(MusicInfo,item);
*type=3;
i++;
if(i==*Count){free(buffer);return 0;}
else LongNameFlag = 0;
}
else if((item->deExtension[0] == 'W')&&(item->deExtension[1] == 'A')&&(item->deExtension[2] == 'V'))
{
#if FAT_DEBUG
printf_P(PSTR("\r\nFound wav file! index = %d name: "),i+1);
for(j=0;j<8;j++)USART_putchar(item->deName[j]);
USART_putchar('.');
for(j=0;j<3;j++)USART_putchar(item->deExtension[j]);
#endif
CopyDirentruyItem(MusicInfo,item);
*type=4;
i++;
if(i==*Count){free(buffer);return 0;}
else LongNameFlag = 0;
}
else LongNameFlag = 0;
}
}
}
free(buffer);//release
}
else//other folders
{
tempclust=cluster;
while(1)
{
sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number
buffer=malloc(512);//apply memory
if(buffer==0)return 1;//if failed
for(cnt=0;cnt<SectorsPerClust;cnt++)
{
if(FAT_ReadSector(sector+cnt,buffer)){free(buffer);return 1;}
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);
if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
{
if(item->deAttributes == 0x0f)
{
we = (struct winentry *)(&buffer[offset]);
j = 26 *( (we->weCnt-1) & WIN_CNT);
if(j<MAX_LONG_NAME_SIZE-25)
{
p = &LongNameBuffer[j];
for (j=0;j<10;j++) *p++ = we->wePart1[j];
for (j=0;j<12;j++) *p++ = we->wePart2[j];
for (j=0;j<4;j++) *p++ = we->wePart3[j];
if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;
if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1;
}
LongNameBuffer[MAX_LONG_NAME_SIZE-1] = 0;
LongNameBuffer[MAX_LONG_NAME_SIZE-2] = 0;
/*p = &LongNameBuffer[j];
for (j=0;j<10;j++) *p++ = we->wePart1[j];
for (j=0;j<12;j++) *p++ = we->wePart2[j];
for (j=0;j<4;j++) *p++ = we->wePart3[j];
if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;
if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1; */
}
else if((item->deExtension[0] == 'M')&&(item->deExtension[1] == 'P')&&(item->deExtension[2] == '3'))
{
#if FAT_DEBUG
printf_P(PSTR("\r\nFound mp3 file! index = %d name: "),i+1);
for(j=0;j<8;j++)USART_putchar(item->deName[j]);
USART_putchar('.');
for(j=0;j<3;j++)USART_putchar(item->deExtension[j]);
#endif
CopyDirentruyItem(MusicInfo,item);
*type = 1;
i++;
if(i==*Count){free(buffer);return 0;}
else LongNameFlag = 0;
}
else if((item->deExtension[0] == 'W')&&(item->deExtension[1] == 'M')&&(item->deExtension[2] == 'A'))
{
#if FAT_DEBUG
printf_P(PSTR("\r\nFound wma file! index = %d name: "),i+1);
for(j=0;j<8;j++)USART_putchar(item->deName[j]);
USART_putchar('.');
for(j=0;j<3;j++)USART_putchar(item->deExtension[j]);
#endif
CopyDirentruyItem(MusicInfo,item);
*type = 2;
i++;
if(i==*Count){free(buffer);return 0;}
else LongNameFlag = 0;
}
else if((item->deExtension[0] == 'M')&&(item->deExtension[1] == 'I')&&(item->deExtension[2] == 'D'))
{
#if FAT_DEBUG
printf_P(PSTR("\r\nFound mid file! index = %d name: "),i+1);
for(j=0;j<8;j++)USART_putchar(item->deName[j]);
USART_putchar('.');
for(j=0;j<3;j++)USART_putchar(item->deExtension[j]);
#endif
CopyDirentruyItem(MusicInfo,item);
*type = 3;
i++;
if(i==*Count){free(buffer);return 0;}
else LongNameFlag = 0;
}
else if((item->deExtension[0] == 'W')&&(item->deExtension[1] == 'A')&&(item->deExtension[2] == 'V'))
{
#if FAT_DEBUG
printf_P(PSTR("\r\nFound wav file! index = %d name: "),i+1);
for(j=0;j<8;j++)USART_putchar(item->deName[j]);
USART_putchar('.');
for(j=0;j<3;j++)USART_putchar(item->deExtension[j]);
#endif
CopyDirentruyItem(MusicInfo,item);
*type=4;
i++;
if(i==*Count){free(buffer);return 0;}
else LongNameFlag = 0;
}
else LongNameFlag = 0;
}
}
}
free(buffer);//release
tempclust=FAT_NextCluster(tempclust);//next cluster
if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 || (FAT32_Enable == 0 && tempclust == 0xffff))break;
}
}
}
}
if(*Count==0)*Count=i;
return 0;
}
//search the file
BYTE SearchLrc(BYTE *dir,BYTE * longnamebuffer,struct direntry *LrcInfo,WORD music_record_addr, BYTE isLongName)
{
BYTE *buffer;
BYTE longbuffertemp[130];
BYTE * p1, * p2;
BYTE match = 0;
DWORD sector;
DWORD cluster;
DWORD tempclust;
BYTE i;
unsigned char cnt;
unsigned int offset;
//unsigned int i=0;
unsigned char j;//long name buffer offset;
unsigned char *p;//long name buffer pointer
struct direntry *item = 0;
struct winentry *we =0;
//cluster = FAT_OpenDir(dir);
//if(cluster == 1)return 1;
#if FAT_DEBUG
printf_P(PSTR("\r\nmusic_record_addr = %d"),music_record_addr);
#endif
for(i=0;i<2;i++)
{
if(i==0)cluster = GetFolderCluster(music_record_addr); /* the same folder where the music file placed */
else cluster = FAT_OpenDir(dir); /* search the default lyric folder */
if(cluster == 1)return 1;
if(cluster==0 && FAT32_Enable==0)// root directory
{
buffer=malloc(512);//apply memory
if(buffer==0)return 1;//if failed
for(cnt=0;cnt<RootDirSectors;cnt++)
{
if(FAT_ReadSector(FirstDirSector+cnt,buffer)){free(buffer);return 1;}
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);//pointer convert
//find a valid item and display it
if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
{
if(item->deAttributes == 0x0f)
{
we = (struct winentry *)(&buffer[offset]);
j = 26 *( (we->weCnt-1) & WIN_CNT);
if(j<MAX_LONG_NAME_SIZE-25)
{
p = &longbuffertemp[j];
for (j=0;j<10;j++) *p++ = we->wePart1[j];
for (j=0;j<12;j++) *p++ = we->wePart2[j];
for (j=0;j<4;j++) *p++ = we->wePart3[j];
if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;
if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1;
}
longbuffertemp[MAX_LONG_NAME_SIZE-1] = 0;
longbuffertemp[MAX_LONG_NAME_SIZE-2] = 0;
}
else if((item->deExtension[0] == 'L')&&(item->deExtension[1] == 'R')&&(item->deExtension[2] == 'C'))
{
CopyDirentruyItem(LrcInfo,item);
p1 = longnamebuffer;
p2 = longbuffertemp;
match = 1;
if(LongNameFlag && isLongName)
{
while(*((unsigned int*)p1))
{
if(*(unsigned int*)p1 != *(unsigned int*)p2)
{
match = 0;
break;
}
p1 += 2;
p2 += 2;
}
}
else if(!LongNameFlag && !isLongName)
{
p2 = item->deName;
while(*p1)
{
if(*p1++ != *p2++)
{
match = 0;
break;
}
}
}
else
{
match = 0;
}
if(match){free(buffer);return 0;}
else LongNameFlag = 0;
}
else LongNameFlag = 0;
}
}
}
free(buffer);//release
}
else//other folders
{
tempclust=cluster;
while(1)
{
sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number
buffer=malloc(512);//apply memory
if(buffer==0)return 1;//if failed
for(cnt=0;cnt<SectorsPerClust;cnt++)
{
if(FAT_ReadSector(sector+cnt,buffer)){free(buffer);return 1;}
for(offset=0;offset<512;offset+=32)
{
item=(struct direntry *)(&buffer[offset]);
if((item->deName[0] != '.') && (item->deName[0] != 0x00) && (item->deName[0] != 0xe5))
{
if(item->deAttributes == 0x0f)
{
we = (struct winentry *)(&buffer[offset]);
j = 26 *( (we->weCnt-1) & WIN_CNT);
if(j<MAX_LONG_NAME_SIZE-25)
{
p = &longbuffertemp[j];
for (j=0;j<10;j++) *p++ = we->wePart1[j];
for (j=0;j<12;j++) *p++ = we->wePart2[j];
for (j=0;j<4;j++) *p++ = we->wePart3[j];
if (we->weCnt & 0x40) (*(unsigned int *)p) = 0;
if ((we->weCnt & WIN_CNT) == 1) LongNameFlag = 1;
}
longbuffertemp[MAX_LONG_NAME_SIZE-1] = 0;
longbuffertemp[MAX_LONG_NAME_SIZE-2] = 0;
}
else if((item->deExtension[0] == 'L')&&(item->deExtension[1] == 'R')&&(item->deExtension[2] == 'C'))
{
CopyDirentruyItem(LrcInfo,item);
p1 = longnamebuffer;
p2 = longbuffertemp;
match = 1;
if(LongNameFlag && isLongName)
{
while(*((unsigned int*)p1))
{
if(*(unsigned int*)p1 != *(unsigned int*)p2)
{
match = 0;
break;
}
p1 += 2;
p2 += 2;
}
}
else if(!LongNameFlag && !isLongName)
{
p2 = item->deName;
while(*p1)
{
if(*p1++ != *p2++)
{
match = 0;
break;
}
}
}
else
{
match = 0;
}
if(match){free(buffer);return 0;}
else LongNameFlag = 0;
}
else LongNameFlag = 0;
}
}
}
free(buffer);//release
tempclust=FAT_NextCluster(tempclust);//next cluster
if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 || (FAT32_Enable == 0 && tempclust == 0xffff))break;
}
}
}
return 1; /*can't find lrc file*/
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -