⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 auddat.cpp

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            break;
        }

        //
        // See if there is a next block in the directory.
        //
        if(*(unsigned long *)(*ppcBuffer + ulOffset) == 0)
        {
            //
            // Allocate a new block for the directory.
            //
            *(unsigned long *)(*ppcBuffer + ulOffset) =
                GetUnusedBlock(ppcBuffer, pulLength);
        }

        //
        // Go to the next block of the directory.
        //
        ulOffset = *(unsigned long *)(*ppcBuffer + ulOffset);
        if((ulOffset + 512) > *pulLength)
        {
            return(-1);
        }
    }

    //
    // Copy the file name into the directory entry we just found.
    //
    memcpy((*ppcBuffer) + ulOffset + (ulIdx * 127) + 4, pusFileName, 122);

    //
    // Allocate a new block for the actual meta-data.
    //
    *(unsigned long *)((*ppcBuffer) + ulOffset + (ulIdx * 127) + 127) =
        GetUnusedBlock(ppcBuffer, pulLength);

    //
    // Return the offset of the directory entry.
    //
    return(ulOffset + (ulIdx * 127) + 4);
}

//****************************************************************************
//
// Retrieves the meta-data associated with the given file name.
//
//****************************************************************************
unsigned long
GetAudibleMetaData(unsigned long ulDrive, unsigned short *pusFileName,
                   char *pcData, unsigned long ulSize)
{
    char *pcBuffer;
    unsigned long ulCopy, ulOffset, ulLength;

    //
    // Read the meta-data file from the device.
    //
    if(ReadMetaData(ulDrive, &pcBuffer, &ulLength) == 0)
    {
        return(0);
    }

    //
    // Find the directory entry for this file.
    //
    ulOffset = FindFileName(pcBuffer, ulLength, pusFileName);
    if(ulOffset == -1)
    {
        GlobalFree(pcBuffer);
        return(0);
    }

    //
    // Get the offset of the meta-data block.
    //
    ulOffset = *(unsigned long *)(pcBuffer + ulOffset + 123);
    if(ulOffset > ulLength)
    {
        GlobalFree(pcBuffer);
        return(0);
    }

    //
    // Loop through the meta-data, copying it to the output buffer.
    //
    while(ulOffset && ulSize)
    {
        //
        // Determine the number of bytes to copy from this block.
        //
        ulCopy = ulSize > 508 ? 508 : ulSize;

        //
        // Copy the meta-data from this block.
        //
        memcpy(pcData, pcBuffer + ulOffset + 4, ulCopy);

        //
        // Increment the output buffer pointer and decrement the size by the
        // number of bytes just copied into the buffer.
        //
        pcData += ulCopy;
        ulSize -= ulCopy;

        //
        // Skip to the next block of meta-data.
        //
        ulOffset = *(unsigned long *)(pcBuffer + ulOffset);
        if(ulOffset > ulLength)
        {
            GlobalFree(pcBuffer);
            return(0);
        }
    }

    //
    // Free the buffer containing the meta-data.
    //
    GlobalFree(pcBuffer);

    //
    // Success.
    //
    return(1);
}

//****************************************************************************
//
// Saves the meta-data associated with the given file name.
//
//****************************************************************************
unsigned long
SetAudibleMetaData(unsigned long ulDrive, unsigned short *pusFileName,
                   char *pcData, unsigned long ulSize)
{
    char *pcBuffer;
    unsigned long ulCopy, ulOffset, ulLength;

    //
    // Read the meta-data file from the device.
    //
    if(ReadMetaData(ulDrive, &pcBuffer, &ulLength) == 0)
    {
        return(0);
    }

    //
    // Find the directory entry for this file.
    //
    ulOffset = FindFileName(pcBuffer, ulLength, pusFileName);
    if(ulOffset == -1)
    {
        //
        // A directory entry does not yet exist for this file, so add one now.
        //
        ulOffset = AddFileName(&pcBuffer, &ulLength, pusFileName);
        if(ulOffset == -1)
        {
            GlobalFree(pcBuffer);
            return(0);
        }
    }

    //
    // Get the offset of the meta-data block.
    //
    ulOffset = *(unsigned long *)(pcBuffer + ulOffset + 123);
    if(ulOffset > ulLength)
    {
        GlobalFree(pcBuffer);
        return(0);
    }

    //
    // Loop through the meta-data, copying it from the input buffer.
    //
    while(ulSize)
    {
        //
        // Determine the number of bytes to copy from this block.
        //
        ulCopy = ulSize > 508 ? 508 : ulSize;

        //
        // Copy the meta-data from this block.
        //
        memcpy(pcBuffer + ulOffset + 4, pcData, ulCopy);

        //
        // Increment the input buffer pointer and decrement the size by the
        // number of bytes just copied from the buffer.
        //
        pcData += ulCopy;
        ulSize -= ulCopy;

        //
        // See if this is the end of the meta-data for this file.
        //
        if(!ulSize)
        {
            //
            // Set the next block address to zero.
            //
            *(unsigned long *)(pcBuffer + ulOffset) = 0;
        }
        else
        {
            //
            // Allocate a new block for the next portion of the meta-data.
            //
            *(unsigned long *)(pcBuffer + ulOffset) =
                GetUnusedBlock(&pcBuffer, &ulLength);

            //
            // Get the offset of the new block.
            //
            ulOffset = *(unsigned long *)(pcBuffer + ulOffset);
        }
    }

    //
    // Write the updated meta-data file back to the device.
    //
    WriteMetaData(ulDrive, pcBuffer, ulLength);

    //
    // Success.
    //
    return(1);
}

//****************************************************************************
//
// Deletes the meta-data associated with the given file name.
//
//****************************************************************************
unsigned long
RemoveAudibleMetaData(unsigned long ulDrive, unsigned short *pusFileName)
{
    char *pcBuffer;
    unsigned long ulOffset, ulNewOffset, ulLength;

    //
    // Read the meta-data file from the device.
    //
    if(ReadMetaData(ulDrive, &pcBuffer, &ulLength) == 0)
    {
        return(0);
    }

    //
    // Find the directory entry for this file.
    //
    ulOffset = FindFileName(pcBuffer, ulLength, pusFileName);
    if(ulOffset == -1)
    {
        GlobalFree(pcBuffer);
        return(0);
    }

    //
    // Get the offset of the meta-data block.
    //
    ulNewOffset = *(unsigned long *)(pcBuffer + ulOffset + 123);

    //
    // Clear the directory entry for this file.
    //
    memset(pcBuffer + ulOffset, 0, 127);

    //
    // Advance to the first meta-data block.
    //
    ulOffset = ulNewOffset;

    //
    // Loop through all the meta-data blocks.
    //
    while(ulOffset)
    {
        //
        // Do nothing further if this block is not a valid block.
        //
        if((ulOffset + 512) > ulLength)
        {
            break;
        }

        //
        // Get the offset of the next meta-data block.
        //
        ulNewOffset = *(unsigned long *)(pcBuffer + ulOffset);

        //
        // Clear this block.
        //
        memset(pcBuffer + ulOffset, 0, 512);

        //
        // Mark this block as being unused.
        //
        pcBuffer[(ulOffset >> 12) + 4] &= ~(1 << (7 - ((ulOffset >> 9) & 7)));

        //
        // Advance to the next block.
        //
        ulOffset = ulNewOffset;
    }

    //
    // Write the updated meta-data file back to the device.
    //
    WriteMetaData(ulDrive, pcBuffer, ulLength);

    //
    // Success.
    //
    return(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -