69,380
社区成员
发帖
与我相关
我的任务
分享
解释一下吧,,,谢谢!!!
#include <stdio.h>
#include "LRC.h"
#include "unistd.h"
#include <stdlib.h>
struct lrc
{
int Time;
char LrcText[100];
struct lrc *next;
};
int main()
{
FILE *fd;
int size,rsize;
char *pbuffer;
struct lrc * head;
unsigned int num = 0;
int minute =0 ,second = 0,msecond=0;
if( (fd = fopen("smlove.lrc","r+")) == NULL )
printf("No Lrc File!\n");
else
{
fseek(fd,0,2);
size = ftell(fd);
rewind(fd);
pbuffer = (char *)malloc(size+1);
rsize = fread(pbuffer,1,size,fd);
fclose(fd);
*(pbuffer+rsize)='\0';
}
Message_Song(pbuffer);
head = GetLrcInfo(pbuffer, size);
printf("%s",head->LrcText);
head=head->next;
while(1)
{
sleep(1);
while ( second++ == 60 )
{
second=0;
while(minute++ ==60)
minute=0;
}
if (head == NULL)
{
printf("the end\n");
}
if( (head != NULL) && (head->Time == minute*60+second-1) )
{
printf("%s\n",head->LrcText);
head=head->next;
}
}
free(pbuffer);
}
struct lrc *GetLrcInfo(char *LrcFile, unsigned int FileSize)
{
char *pb,*mid,*head;
struct lrc *pnew; //当前位置
char temp[100];
int i=0,j=0;
int count=0,getline=0;
struct lrc *pp;
pp=(struct lrc *)malloc( sizeof(struct lrc));
pp->Time=0;
strcpy(pp->LrcText,"designed by 刘桐\n");
pp->next=NULL;//建立空链表;
head = pb = LrcFile;
while ( (*pb != '[') || (*(pb+6) != '.') ) // 这代码是什么意思???
{
pb++;
}
while ( (pb - head) != FileSize ) // 这是什么意思????
{
while ( *pb != '\n')
{
temp[i] = *pb;
pb++;
i++;
}
pb++;
temp[i]='\0';
mid=temp;
for (j=0;j<i;j++)
{
if ( temp[j] == '.')
{
count++; //这个又是什么意思??????
}
}
getline=count;
pnew=(struct lrc *)malloc( sizeof(struct lrc));//
strcpy(pnew->LrcText,mid+count*10);// 什么意思??
pnew->Time = atoi(mid+1)*60+atoi(mid+4);//什么意思???
InsertList( pp, pnew );
while ( count > 1) //这个是什么间思?》???
{
pnew=(struct lrc *)malloc( sizeof(struct lrc));
strcpy(pnew->LrcText,mid+getline*10);
pnew->Time = atoi(mid+10*(count-1)+1)*60+atoi(mid+10*(count-1)+4);
InsertList( pp, pnew );
count--;
}
i=0;
count=0;
}
return pp;
}
void Message_Song(char *Lrc_Buffer)
{
char *pb,*pt; //当前位置
char temp[100];
int i=0;
pb = Lrc_Buffer;
while ( *pb != '\n' )//歌名解析
{
temp[i] = *pb;
pb++;
i++;
}
temp[i-2]='\0';
pt=temp;
if ( strncmp(pt,"[ti:",4) == 0 )
{
printf("歌名: %s\n",pt+4);
}
pb++;
i=0;
while ( *pb != '\n' )//歌手解析
{
temp[i] = *pb;
pb++;
i++;
}
temp[i-2]='\0';
pt=temp;
if ( strncmp(pt,"[ar:",4) == 0 )
{
printf("歌手: %s\n",pt+4);
}
pb++;
i=0;
while ( *pb != '\n' )//专辑解析
{
temp[i] = *pb;
pb++;
i++;
}
temp[i-2]='\0';
pt=temp;
if ( strncmp(pt,"[al:",4) == 0 )
{
printf("专辑: %s\n",pt+4);
}
pb++;
i=0;
while ( *pb != '\n' )//制作
{
temp[i] = *pb;
pb++;
i++;
}
temp[i-2]='\0';
pt=temp;
if ( strncmp(pt,"[by:",4) == 0 )
{
printf("制作: %s\n",pt+4);
}
}
struct lrc *InsertList( struct lrc * head, struct lrc *pi)
{
struct lrc *pb=head ,*pf;
if(head == NULL)//如果为空就建立,空间在传入前申请好
{
head=pi;
pi->next=NULL;
printf("yes");
}
else
{
while((pi->Time > pb->Time)&&(pb->next!=NULL)) //找到一个比插入值大的节点,然后插在它的前面
{
pf=pb;//pf指向前,pb指向后
pb=pb->next; //节点后移
}
if(pi->Time <= pb->Time) //找到所要插入节点位置,插到pb的前面
{
if(head==pb)
{
head=pi; //在第一结点之前插入
pi->next = pb;
}
else
{
pf->next=pi; //在中间位置插入
pi->next=pb;
}
}
else //只有pb->next为空才会成立
{
pb->next=pi;
pi->next=NULL; //在表末插入
}
}
return head;
}
struct lrc *GetLrcInfo(char *LrcFile, unsigned int FileSize)
{
char *pb,*mid,*head;
struct lrc *pnew; //当前位置
char temp[100];
int i=0,j=0;
int count=0,getline=0;
struct lrc *pp;
pp=(struct lrc *)malloc( sizeof(struct lrc));
pp->Time=0;
strcpy(pp->LrcText,"designed by 刘桐\n");
pp->next=NULL;//建立空链表;
head = pb = LrcFile;
// 歌词文件主要有两种类型:[offset:500] 和 [00:02.66]三月里的小雨-杨钰莹
// 和[offset:500]相关的应该是在Message_Song解析了,这里主要做
// [00:02.66]三月里的小雨-杨钰莹 这一类型的解析
while ( (*pb != '[') || (*(pb+6) != '.') ) // 这代码是什么意思???
{
pb++;
}
// 不越界访问,也就是所读取的长度不超过文件大小
while ( (pb - head) != FileSize ) // 这是什么意思????
{
while ( *pb != '\n')
{
temp[i] = *pb;
pb++;
i++;
}
pb++;
temp[i]='\0';
mid=temp;
for (j=0;j<i;j++)
{
if ( temp[j] == '.')
{
// 某行歌词出现的次数, 看看 [02:30.66][00:51.90]三月里的小雨,
count++; //这个又是什么意思??????
}
}
getline=count;
pnew=(struct lrc *)malloc( sizeof(struct lrc));//
// 将歌词出现的时间去掉,比如去掉: [02:30.66][00:51.90] [00:02.66]
strcpy(pnew->LrcText,mid+count*10);// 什么意思??
// 取得歌词的时间: [02:30.66] => 2 * 60 + 30
pnew->Time = atoi(mid+1)*60+atoi(mid+4);//什么意思???
InsertList( pp, pnew );
// 某行歌词出现的多次, 如 [02:30.66][00:51.90]三月里的小雨,
while ( count > 1) //这个是什么间思?》???
{
pnew=(struct lrc *)malloc( sizeof(struct lrc));
strcpy(pnew->LrcText,mid+getline*10);
pnew->Time = atoi(mid+10*(count-1)+1)*60+atoi(mid+10*(count-1)+4);
InsertList( pp, pnew );
count--;
}
i=0;
count=0;
}
return pp;
}
struct lrc *GetLrcInfo(char *LrcFile, unsigned int FileSize)
{
char *pb,*mid,*head;
struct lrc *pnew; //当前位置
char temp[100];
int i=0,j=0;
int count=0,getline=0;
struct lrc *pp;
pp=(struct lrc *)malloc( sizeof(struct lrc));
pp->Time=0;
strcpy(pp->LrcText,"designed by 刘桐\n");
pp->next=NULL;//建立空链表;
head = pb = LrcFile;
//这里是找出首个标识时间偏移的标签,注意时间偏移标签的格式就明白了:
//[00:02.66]
//↑ ↑
//pb pb+6
while ( (*pb != '[') || (*(pb+6) != '.') ) // 这代码是什么意思???
{
pb++;
}
while ( (pb - head) != FileSize ) // 这是什么意思????
{
while ( *pb != '\n')
{
temp[i] = *pb;
pb++;
i++;
}
pb++;
temp[i]='\0';
mid=temp;
for (j=0;j<i;j++)
{
if ( temp[j] == '.')
{
count++; //这个又是什么意思??????
//这里是统计一行中出现多个偏移标签情况下的标签数量
//这里的统计不严谨,如果歌词中出现字符'.'就会统计错误
//想要严谨些的统计结果,可以参考上面查找偏移标签的方法
}
}
getline=count;
pnew=(struct lrc *)malloc( sizeof(struct lrc));//
strcpy(pnew->LrcText,mid+count*10);// 什么意思??
//解析歌词内容,由于时间标签的长度为10Byte([00:02.66])所以从mid+count*10开始为歌词内容
pnew->Time = atoi(mid+1)*60+atoi(mid+4);//什么意思???
//计算偏移时间,mid+1和mid+2为分钟数,mid+4开始为秒数
InsertList( pp, pnew );
while ( count > 1) //这个是什么间思?》???
//为同一行的每个时间标签建立一个结构体
{
pnew=(struct lrc *)malloc( sizeof(struct lrc));
strcpy(pnew->LrcText,mid+getline*10);
pnew->Time = atoi(mid+10*(count-1)+1)*60+atoi(mid+10*(count-1)+4);
InsertList( pp, pnew );
count--;
}
i=0;
count=0;
}
return pp;
}