VC中使用SCRIPT 正则写的LRC歌词分析类

来源:互联网 发布:mysql不允许远程连接 编辑:程序博客网 时间:2024/04/18 14:24
 
#include "StdAfx.h"
#
include ".lrcobject.h"
#
include   <comdef.h>

LRCOBJECT::LRCOBJECT(const char 
* text)
: ar(_T(
""))
, by(_T(
""))
, al(_T(
""))
, ti(_T(
""))
, oTime(0)

, isLrcFile(FALSE)
{
    Text
=text;
    steLrcTi();
}

LRCOBJECT::LRCOBJECT(void)
{
}

LRCOBJECT::
~LRCOBJECT(void)
{
    Release();
}
// //查找添加LRC标题


void LRCOBJECT::steLrcTi(void)
{
    CoInitialize(NULL);
    
try{
        IRegExpPtr regExpPtr(
__uuidof(RegExp));
        regExpPtr
->PutGlobal(VARIANT_TRUE);
        regExpPtr
->PutPattern("/[ti:([^/[/]/:]+)/]");
        IMatchCollectionPtr matches 
= regExpPtr->Execute((const char * )Text); //Execute方法接受的是_bstr_t类型,因此要进行(const char * )转换
        ISubMatchesPtr pMatch;
        
if(matches->GetCount() >0 )
        {
            
//IMatch2Ptr类型才有GetSubMatches()方法,因此,要进行显式的类型转换
            pMatch
=((IMatch2Ptr)matches->GetItem(0))->GetSubMatches();//SubMatches;
            
//pMatch->GetItem(0)返回是_variant_t类型,_variant_t类型的属性bstrVal是BSTR类型
            ti
= (pMatch->GetItem(0)).bstrVal;
        }
        
//--------------------------------------------------
        regExpPtr
->PutPattern("/[ar:([^/[/]/:]+)/]");
        matches 
= regExpPtr->Execute((const char * )Text); 
        
if(matches->GetCount()>0)
        {
            pMatch
=((IMatch2Ptr)matches->GetItem(0))->GetSubMatches();//SubMatches;
            ar
= (pMatch->GetItem(0)).bstrVal;
        }
        
//-----------------------------------------
        regExpPtr
->PutPattern("/[al:([^/[/]/:]+)/]");
        matches 
= regExpPtr->Execute((const char * )Text); //用指针要出错
        
if(matches->GetCount()>0)
        {
            pMatch
=((IMatch2Ptr)matches->GetItem(0))->GetSubMatches();//SubMatches;
            al
= (pMatch->GetItem(0)).bstrVal;
        }
        
//---------------------
        
/*
        短整型(int) 
        i 
= atoi(temp); 
        长整型(long) 
        l 
= atol(temp); 
        浮点(double) 
        d 
= atof(temp);
        
*/
        regExpPtr
->PutPattern("/[offset/:(/-?/d+)/]");
        matches 
= regExpPtr->Execute((const char * )Text); //用指针要出错
        
if(matches->GetCount()>0)
        {
            pMatch
=((IMatch2Ptr)matches->GetItem(0))->GetSubMatches();//SubMatches;
            by
= (pMatch->GetItem(0)).bstrVal;
            
try
            {
                oTime
=float(atof(by));
            }catch(...){};
        }
        
//-----------------------------------------
        regExpPtr
->PutPattern("/[by:([^/[/]/:]+)/]");
        matches 
= regExpPtr->Execute((const char * )Text); //用指针要出错
        
if(matches->GetCount()>0)
        {
            pMatch
=((IMatch2Ptr)matches->GetItem(0))->GetSubMatches();//SubMatches;
            by
= (pMatch->GetItem(0)).bstrVal;
        }

        regExpPtr
->PutPattern("/[/d+/:[/d/.]+/]");
        matches 
= regExpPtr->Execute((const char * )Text); //用指针要出错
        
if(matches->GetCount()>0)
        {
            isLrcFile
=TRUE;
        }
        
else
        {
            isLrcFile
=FALSE;
        }
        pMatch
=NULL;
        matches
=NULL;
        regExpPtr
=NULL;
        
//"/[/d+/:[/d/.]+/]"

    }
    
//catch(_com_error& e)
    
//{
    
//}
    catch(...)
    {
    }
    CoUninitialize();


    
//-----test
    
//CString t=delNotes((const char * )Text);
    
//LrcMadeList();
    
//::MessageBox(::GetTopWindow(::GetDesktopWindow()),GetText(), "URL CLIENT", MB_OK);

}

// //删除注释
CString LRCOBJECT::delNotes(const char
* str)
{
    CString m_reText;
    
//CoInitialize(NULL);
    
//-----------
    
try
    {
        IRegExpPtr regExpPtr(
__uuidof(RegExp));
        regExpPtr
->PutGlobal(VARIANT_TRUE);
        regExpPtr
->PutPattern("/[/d+/:[/d/.]+/](.*)[/r/n]");
        IMatchCollectionPtr matches 
= regExpPtr->Execute(str); //Execute方法接受的是_bstr_t类型,因此要进行(const char * )转换
        
//-------
        IRegExpPtr IRegExpSubPtr(
__uuidof(RegExp));
        IRegExpSubPtr
->PutGlobal(VARIANT_TRUE);
        IRegExpSubPtr
->PutPattern("/[/d+/:[/d/.]+/]");
        IMatchCollectionPtr matches2;
        
//-----------------------
        int m_Count
=matches->GetCount();
        int m_count2;
        CString m_SubText;
        
if(m_Count>0)
        {
            
for(int i=0;i<matches->GetCount();i++)
            {
                
//IMatchPtr
                m_SubText
=(LPCTSTR)((IMatchPtr)matches->GetItem(i))->GetValue();


                matches2
=IRegExpSubPtr->Execute((const char *)m_SubText);
                
if((m_count2=matches2->GetCount())>0)
                {
                    
for(int j=0;j<m_count2;j++)
                    {
                        m_SubText.Replace((LPCTSTR)((IMatchPtr)matches2
->GetItem(j))->GetValue(),"");
                    }

                    
for(j=0;j<m_count2;j++)
                    {
                        m_reText
+=(LPCTSTR)((IMatchPtr)matches2->GetItem(j))->GetValue()+m_SubText;
                    }
                }
                
//::MessageBox(::GetTopWindow(::GetDesktopWindow()),m_reText, "URL CLIENT", MB_OK);
            }

        }
        IRegExpSubPtr
=NULL;
        matches
=NULL;
        matches2
=NULL;
        regExpPtr
=NULL;
    }
    catch(...){};
    
//---------------------
    
//CoUninitialize();
    
return m_reText;
}

void LRCOBJECT::AddToList(const char 
* text)
{
    
//lrcBoxclassList
    Release();
    CString m_Text,m_strtmp;
    lrcBox
* pLrcBox;
    float m_t,m_t2;
    
//CoInitialize(NULL);
    CString debg,ttt;
    
try
    {
        IRegExpPtr regExpPtr(
__uuidof(RegExp));
        regExpPtr
->PutGlobal(VARIANT_TRUE);
        regExpPtr
->PutPattern("/[([^:]+):([^/]]+)/](.*)[/r/n]");
        IMatchCollectionPtr matches 
= regExpPtr->Execute(text);
        int m_count
=matches->GetCount();
        ISubMatchesPtr pSubMatch;
        
if(m_count>0)
        {
            
for(int i=0;i<m_count;i++)
            {
                pSubMatch
=((IMatch2Ptr)matches->GetItem(i))->GetSubMatches();//SubMatches;
                m_Text
= (pSubMatch->GetItem(2)).bstrVal;

                
//::MessageBox(::GetTopWindow(::GetDesktopWindow()),m_Text, "URL CLIENT", MB_OK);
                m_t
=float(atof((CString)(pSubMatch->GetItem(0)).bstrVal));
                m_t2
=float(atof((CString)(pSubMatch->GetItem(1)).bstrVal));


                m_t
=m_t*60 + m_t2;
                pLrcBox
=new lrcBox(m_t,m_Text);
                
//Length++;
                lrcBoxclassList.AddTail(pLrcBox);
            }
        }
        pSubMatch
=NULL;
        matches
=NULL;
        regExpPtr
=NULL;

    }
    catch(...)
    {
        Release();
    }
    
//-----------------------------

    
//-----------------------------
    
//CoUninitialize();
    PaiXu();
}


void LRCOBJECT::PaiXu(void)
{
    
if(lrcBoxclassList.IsEmpty())
    {
        
return;
    }
    bool biaoZhi
=false;
    lrcBox 
* tmp1,* tmp2,*tmp3;
    
//var tmp2;
    int i,dangQian
=(int)lrcBoxclassList.GetCount();
    POSITION ps,ps2;
    
//---------------------
    
/*
    CString m_text,tmp,tm;
    int oo;
    oo
=0;
    ps
=lrcBoxclassList.GetHeadPosition();
    
while(ps != NULL)
    {
    tmp.Format(
"%d",++oo);
    m_text
+=tmp+"  - "+((lrcBox *)lrcBoxclassList.GetNext(ps))->lrcText;
    }
    ::MessageBox(::GetTopWindow(::GetDesktopWindow()),m_text, 
"URL CLIENT", MB_OK);
    
*/
    
//----------------------------
    do{
        biaoZhi
=0;//冒泡标志
        
for(i=0;i<(dangQian-1);i++){
            ps
=    lrcBoxclassList.FindIndex(i);
            ps2
=lrcBoxclassList.FindIndex(i+1);
            tmp1
=(lrcBox*)lrcBoxclassList.GetAt(ps);
            tmp2
=(lrcBox*)lrcBoxclassList.GetAt(ps2);
            
if(tmp1->lrcTime >tmp2->lrcTime)
            {
//互换
                tmp3
=tmp1;
                lrcBoxclassList.SetAt(ps,tmp2);
                lrcBoxclassList.SetAt(ps2,tmp3);
                biaoZhi
=true; 
            }
//if    
        }
//for
    }
    
while(biaoZhi);
    
//-------------------------------
    
/*
    oo
=0;
    ps
=lrcBoxclassList.GetHeadPosition();
    m_text.Empty();
    float uu;
    
while(ps != NULL)
    {
    tmp.Format(
"%d",++oo);
    uu
=((lrcBox *)lrcBoxclassList.GetAt(ps))->lrcTime;
    tm.Format(
"%f",uu);
    m_text
+=tmp+"  - " +tm+ "   "+((lrcBox *)lrcBoxclassList.GetNext(ps))->lrcText;
    }
    ::MessageBox(::GetTopWindow(::GetDesktopWindow()),m_text, 
"URL CLIENT", MB_OK);
    
*/
}

void LRCOBJECT::LrcMadeList(void)
{
    CoInitialize(NULL);
    AddToList(delNotes((const char 
* )Text));
    CoUninitialize();
}

CString LRCOBJECT::GetSoundText(void)
{
    CString m_text;
    
if(!lrcBoxclassList.IsEmpty())
    {
        
//lrcBox * tmp1;
        POSITION pos
=lrcBoxclassList.GetHeadPosition();
        
while(pos != NULL)
        {
            m_text
+=((lrcBox *)lrcBoxclassList.GetNext(pos))->lrcText+" ";
        }
    }
    
return m_text;
}

void LRCOBJECT::Release(void)
{
    
if(lrcBoxclassList.IsEmpty())return;
    int c
=(int)lrcBoxclassList.GetCount();
    lrcBox
* pLrcBox;
    
for(int i=0;i<c;i++)
    {
        pLrcBox
=lrcBoxclassList.RemoveTail();
        delete pLrcBox;
    }
}
原创粉丝点击