www.pudn.com > autolrc.rar > abao_lrcfile.cpp
//////////////////////////////////////////////
/// @defgroup lrcfile 歌词文件类
/// @author abao++
/// @version 1.0
/// @date 2006-9-24 9:54:17
/// @{
//////////////////////////////////////////////
#include "abao_lrcfile.h"
#include "abao_lrc.h"
#include
#include
#include
#include
#include
int AbaoLrcFile::checklrcname(string &keyword,string &filename)
{
if(keyword.empty())return 0;
string k=keyword;
int kpos=k.rfind('-');
k=k.substr(kpos+1,k.size()-kpos);
kpos=k.rfind('.');
k=k.substr(kpos+1,k.size()-kpos);
if(filename.find(k)!=-1) return 1;
return 0;
}
string AbaoLrcFile::FindLrcFile(list& filelist)
{
string returnfilename;
int maxcount=0,tcount=0;
for(list::iterator it=filelist.begin();it!=filelist.end();it++)
{
int tcount=0;
string &f=*it;
int pos1,pos2;
pos1=f.rfind('\\');
pos2=f.rfind('.');
if(f.substr(pos1+1,pos2-pos1-1)==this->Mp3Name)
{
return f;//嘿嘿,无条件匹配
}
int tpos=f.find(this->Mp3Name,0);
if(checklrcname(this->id3v2title,f)) tcount+=5;
if(checklrcname(this->id3v1title,f)) tcount+=5;
if(checklrcname(this->id3v1artist,f)) tcount++;
if(checklrcname(this->id3v1album,f)) tcount++;
if(checklrcname(this->id3v2album,f)) tcount++;
if(checklrcname(this->id3v2artist,f)) tcount++;
if(tcount>maxcount)
{
returnfilename=f;
maxcount=tcount;
}
}
if(maxcount<=4) returnfilename="";
return returnfilename;
}
extern AbaoLrcWindow lrc;
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
lrc.IsSearch=1;
if(lrc.lrcfile.SearchFlag==1)
{
if(!lrc.lrcfile.SearchAllFlag)
{
//搜索指定目录,只搜索一次。缓存在LrcFileList中
int tpos=0;
int oldpos=0;
string &temp=lrc.lrcfile.SearchPath;
//解析;隔开的搜索目录
string dirpath;
while((tpos=temp.find(';',oldpos))>0)
{
dirpath=temp.substr(oldpos,tpos-oldpos).c_str();
_chdir(dirpath.c_str());
lrc.lrcfile.GetFileFromDir(dirpath,1);
oldpos=tpos+1;
}
dirpath=temp.substr(oldpos,temp.size()-oldpos).c_str();
_chdir(dirpath.c_str());
if(oldposlrcstr="";
if(p->Search())
{
lrc.lrcfile.MakeLrcMap(p->lrcstr);
lrc.lrcfile.lrcstr=p->lrcstr;
if(lrc.lrcfile.SaveLrcFlag) lrc.lrcfile.SaveToLrcFile();
}
}
else
{
lrc.lrcfile.InitLrcMap();
}
if(!lrc.lrcfile.lrcmap.empty()) lrc.drawlrcbitmap();
lrc.IsSearch=0;
ExitThread(1);
return 1;
}
string AbaoLrcFile::GetLrcFileName()
{
string rfile=FindLrcFile(this->FileList);///<从缓存读取一点数据看看
if(!rfile.empty()) return rfile;
else
{
rfile=FindLrcFile(this->LrcFileList);
if(!rfile.empty()) return rfile;
}
string a=this->LrcFileName;
string dir=a.substr(0,a.rfind("\\")+1);
FileList.clear();
_chdir(dir.c_str());
GetFileFromDir(dir);
rfile=FindLrcFile(this->FileList);
if(rfile.empty())
{
chdir(this->SearchPath.c_str());
HANDLE hThread;
DWORD dwThreadId;
hThread=CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc, // thread function
NULL, // argument to thread function
0, // use default creation flags
&dwThreadId);
//getfilefromdir(this->searchpath);
//rfile=selectfilename();
}
return rfile;
}
int AbaoLrcFile::GetFileFromDir(string dir,int flag)
{
//首先查找dir中符合要求的文件
string a=this->LrcFileName;
long hFile;
int maxcount=0;
_finddata_t fileinfo;
if ((hFile=_findfirst("*.lrc",&fileinfo)) != -1)
{
do
{
if (!(fileinfo.attrib & _A_SUBDIR))
{
char filename[_MAX_PATH];
string f=dir+fileinfo.name;
if(flag) LrcFileList.push_back(f);
else FileList.push_back(f);
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
if(!flag) return 1;
if ((hFile=_findfirst("*.*",&fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if(fileinfo.name[0]!='.')
{
string tpath=dir+fileinfo.name+'\\';
_chdir(tpath.c_str());
GetFileFromDir(tpath,flag);
_chdir("..");
}
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
return 1;
}
int AbaoLrcFile::MakeLrcMap(string& flrc)
{
if(flrc.empty()) return 0;
lrcstr=flrc;
string::size_type pos=0,oldpos=0;
string temp;
while((pos=flrc.find('\n',pos+1))!=string::npos)
{
temp=flrc.substr(oldpos,pos-oldpos);
string::size_type t=temp.find_last_of(']');
if(t!=string::npos)
{
string lrcstr=temp.substr(t+1,temp.length()-t);
//char c=lrcstr[lrcstr.length()-1];
if(lrcstr.length()>0) if(lrcstr[lrcstr.length()-1]=='\r') lrcstr.erase(lrcstr.length()-1);
if(this->FilterFlag==1)
{
for(list::iterator it=FilterList.begin();it!=FilterList.end();it++)
{
int tpos=lrcstr.find(*it);
if(tpos>=0)
{
lrcstr="";
break;
}
}
}
string::size_type startpos=0,endpos=0;
while((startpos=temp.find('[',endpos))!=string::npos)
{
endpos=temp.find(']',startpos);
double s,m;
long ms;
int rpos=sscanf(temp.substr(startpos,endpos-startpos).c_str(),"[%lf:%lf]",&m,&s);
if(rpos==2)
{
ms=(long)((m*60+s)*1000);
lrcmap.insert(val_type(ms,lrcstr));
//lrcmap.push_back(lrcvalue(ms,lrcstr));
}
else
{
if(temp.substr(startpos+1,6)=="offset")
{
sscanf(temp.substr(startpos,endpos-startpos).c_str(),"[offset:%ld]",&offset);
}
}
}
oldpos=pos+1;
}
}
int i=0;
indexmap.clear();
for(LrcMap::iterator it=lrcmap.begin();it!=lrcmap.end();it++,i++)
{
indexmap.insert(make_pair((*it).first,i));
}
return 1;
}
int AbaoLrcFile::InitLrc(string& filename)
{
LrcFileName=filename;
ReadMp3Info(filename);
int pos1,pos2;
pos1=filename.rfind('\\');
pos2=filename.rfind('.');
Mp3Name=filename.substr(pos1+1,pos2-pos1-1);
LrcFileName=this->GetLrcFileName();
lrcmap.clear();
if(filename=="") return 0;
if(LrcFileName=="") return 0;
InitLrcMap();
}
int AbaoLrcFile::SaveToLrcFile()
{
LrcFileName=lrcwin->Mp3Name;
int pos=LrcFileName.rfind('.');
LrcFileName=LrcFileName.substr(0,pos)+".lrc";
HANDLE hFile;
hFile = CreateFile(LrcFileName.c_str(), // file to open
GENERIC_WRITE, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
CREATE_ALWAYS, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
return 0;
}
int bResult;
DWORD ByteRead;
bResult = WriteFile(hFile,
lrcstr.c_str(),
strlen(lrcstr.c_str()),
&ByteRead,
NULL);
CloseHandle(hFile);
return 1;
}
int AbaoLrcFile::InitLrcMap()
{
//setlocale(LC_ALL,"Chinese-simplified");
//std::locale::global(std::locale(""));
//std::locale::global(std::locale(0));
/*
FILE * fp;
if((fp=fopen(lrcfilename.c_str(),"rb|"))==0) return 0;
fseek(fp,0,SEEK_END);
long filesize=ftell(fp);
char* inbuffer=new char[filesize+1];
fread(inbuffer,filesize,1,fp);
inbuffer[filesize]=0;
makelrcmap(string(inbuffer));
delete [] inbuffer;
fclose(fp);
*/
HANDLE hFile;
hFile = CreateFile(LrcFileName.c_str(), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
return 0;
}
string temp;
string filestr;
DWORD hFileSize;
hFileSize=GetFileSize(hFile,&hFileSize);
char* inBuffer=new char[hFileSize+1];
int bResult;
DWORD ByteRead;
bResult = ReadFile(hFile,
inBuffer,
hFileSize,
&ByteRead,
NULL);
inBuffer[hFileSize]=0;
MakeLrcMap(string(inBuffer));
delete [] inBuffer;
CloseHandle(hFile);
return 1;
}
void AbaoLrcFile::ReadMp3Info(string& filename)
{
id3v1title="";
id3v1artist="";
id3v1album="";
id3v2title="";
id3v2artist="";
id3v2album="";
//std::locale::global(std::locale(""));
FILE *fp;
if((fp=fopen(filename.c_str(),"rb+"))==0) return;
char value[100];
char temp1[5];
fread(temp1,4,1,fp);
//file.read(temp1,4);
temp1[3]=0;
strupr(temp1);
if(!strncmp(temp1,"ID3",3))
{
fseek(fp,6,SEEK_SET);
//file.seekg(6,ios_base::beg);
long MaxSize=0;
//file.read(temp1,4);
fread(temp1,4,1,fp);
//fread(temp1,4,1,fp);
MaxSize=(((long)temp1[0])<<24)
+(((long)temp1[1])<<16)
+(((long)temp1[2])<<8)
+((long)temp1[3]);
//while(file.tellg()lrcdownload.lrcfile=this;
}