My application will call GetNextFile very many times.

Boolean CMainForm::GetNextFile(char *filename,UInt32 length)
{
    Int16 totalDatabaseSize;
    
    if(g_currentSelectedDatabase && DmNumRecords(g_currentSelectedDatabase) > 0)
    {
        totalDatabaseSize = DmNumRecords(g_currentSelectedDatabase);
    }
    else 
    {
        // no songs resource
        ErrMessage("Line:%d in MainForm.cpp error, no mp3/sbc files have been 
found in your playlist!",__LINE__); 
        SetPlayingSong("No Available Files");
        return false;
    }   
    GetNextID(totalDatabaseSize);
    return GetCurrentFile(filename,length); 
}

Int16 CMainForm::GetNextID(Int16 totalIDSize)
{    
    Int16 nextID = 0;
    if(m_clickSongToPlay)
    {
        m_clickSongToPlay = false;
        return m_currentPlayingSongID;
    }   
    //else
    if(!m_bShuffleSetting)
    {
        //nextID = (m_currentPlayingSongID +1) % totalIDSize;
        if(g_pPlayerEngine->GetNextOperation() == EOpNext)
            nextID = (m_currentPlayingSongID +1) % totalIDSize;     
        else if(g_pPlayerEngine->GetNextOperation()== EOpPrevious)
        {
            nextID = m_currentPlayingSongID ? ( m_currentPlayingSongID -1) :( 
totalIDSize -1);
        }
        else
            nextID = (m_currentPlayingSongID +1) % totalIDSize;  
    }
    else
    {
        nextID = SysRandom(TimGetTicks()) %totalIDSize;
    }
    m_currentPlayingSongID = nextID;
    return m_currentPlayingSongID;
}

Boolean CMainForm::SetPlayingSong(const Char *pSong)
{
    if(!pSong || !StrLen(pSong))
    {
        ErrMessage("Line: %d in MainForm.cpp error, parameter error!",__LINE__);
        return false;
    }
    
    MyMemPtrNew(m_pCurrentPlayingSong,StrLen(pSong));
    StrPrintF(m_pCurrentPlayingSong, "%s", pSong);
    MyMemPtrFree(m_pCurrentPlayingSongFullPath);

    return true;
}

Boolean CMainForm::GetCurrentFile(char *filename, UInt32 length)
{
    Boolean bValue = true;
    MemHandle h;    
    Int16 totalDatabaseSize;
    Char *record;
    Int16 len = 0;
    UInt16 fullPathLen;

    // just set the default information of the song.
    SetPlayingSong("No Available Files");
    
    // default we set the filename to the top visible song of the table
    if(g_currentSelectedDatabase && DmNumRecords(g_currentSelectedDatabase) > 0)
    {
        totalDatabaseSize = DmNumRecords(g_currentSelectedDatabase);
    }
    else 
    {
        // no songs resource
        ErrMessage("Line:%d in MainForm.cpp error, no mp3/sbc files have been 
found in your playlist!",__LINE__); 
        return false;
    }   


    // if no songs need to play, we need to tell the user.
    if(!m_bRepeatSetting && 
g_pCurrentPlayForm->IsAllSongsPlayedOnce(totalDatabaseSize))
    {       
         m_currentPlayingSongID = 0;   
         h = DmQueryRecord(g_currentSelectedDatabase, m_currentPlayingSongID);
         record = (Char *)MemHandleLock(h);
         fullPathLen = StrLen(record) -1;
         MyMemPtrNew(m_pCurrentPlayingSongFullPath, fullPathLen);    
         StrCopy(m_pCurrentPlayingSongFullPath, record + LENGTH);  
         ParseRecord(record, &m_pCurrentPlayingSong, &len);
         MemHandleUnlock(h);
            
         filename[0] = '\0';
         length = 0;
         
         CurrentSelectedSongTableInit();
         return false;
    }
    
    GetCurrentID(totalDatabaseSize);
    if(!g_pCurrentPlayForm->SetSongsStatus(m_currentPlayingSongID, true))
    {
        m_currentPlayingSongID = 0; // if error, we will just set a default 
value
        ErrMessage("Line:%d in MainForm.cpp error, m_currentPlayingSongID in 
GetCurrentFileName out of bound!",__LINE__);
        return false;
    }   
    
#ifdef UI_Debug   
    MyDebug("m_currentPlayingSongID= %d\n",m_currentPlayingSongID);
#endif 

    h = DmQueryRecord(g_currentSelectedDatabase, m_currentPlayingSongID);
    record = (Char *)MemHandleLock(h);
    
    fullPathLen = StrLen(record) -1;
    
    if(length <= fullPathLen)
    {
        MemHandleUnlock(h);
        ErrMessage(" Line: %d in MainForm.cpp error, the allocated memory is 
not enough!",__LINE__);
        return false;
    }
    
    MyMemPtrNew(m_pCurrentPlayingSongFullPath, fullPathLen);    
    StrCopy(m_pCurrentPlayingSongFullPath, record + LENGTH);  
    StrCopy(filename, m_pCurrentPlayingSongFullPath);
    length = StrLen(filename);    
    
    ParseRecord(record, &m_pCurrentPlayingSong, &len);
    
    MemHandleUnlock(h);
   
#ifdef UI_Debug
    MyDebug("current selected filename = %s\n",filename);
    MyDebug("the length of the playing song is = %ld\n",length);
#endif   

    CurrentSelectedSongTableInit();
    SetPlayingSongInTheMiddleOfTheTable();
    
    return bValue;   
}

Boolean ParseRecord(char *src, char **pDes, Int16 *songNameLength)
{
    Int16 srcLength;
    Char *tmp = 0;
    Char end = '\0';
    
    if(!src || StrLen(src)<=0)
    {
        return 0;
    }
    srcLength = StrLen(src);
    
    tmp = (Char *) MemPtrNew(LENGTH+1); //allocate a more space for '\0'
    StrNCopy(tmp,src, LENGTH);
    StrNCopy(tmp+LENGTH, &end,1);
    *songNameLength = (Int16)(*tmp); 
    
    if(!MyMemPtrNew(*pDes, *songNameLength + 1))
    {
        return false;
    }
    StrNCopy(*pDes, src + srcLength - *songNameLength,*songNameLength);
    StrNCopy(*pDes+ *songNameLength, &end, 1); //copy '\0' at the end of the 
string
    MemPtrFree(tmp);
     
    return true;
}

-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/

Reply via email to