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

📄 auddat.cpp

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************
//
// AUDDAT.CPP - Routines for creating/modifying audible.dat on the
//              Maverick(tm) Internet Audio Player.
//
// Copyright (c) 2000 Cirrus Logic, Inc.
//
//****************************************************************************
#include "stdafx.h"

//****************************************************************************
//
// Reads the Audible meta-data from the device.
//
//****************************************************************************
static unsigned long
ReadMetaData(unsigned long ulDrive, char **ppcBuffer, unsigned long *pulLength)
{
    //
    // Open the Audible meta-data file.
    //
    if(Maverick_Open(ulDrive, L"\\AUDIBLE.DAT", 3) == 0)
    {
        //
        // The initial meta-data file is 1024 bytes in length.
        //
        *pulLength = 1024;

        //
        // Allocate memory for the meta-data file.
        //
        *ppcBuffer = (char *)GlobalAlloc(GMEM_FIXED, *pulLength);
        if(!*ppcBuffer)
        {
            Maverick_Close();
            return(0);
        }

        //
        // Zero fill the meta-data.
        //
        memset(*ppcBuffer, 0, 1024);

        //
        // Fill in the offset of the first directory entry.
        //
        (*ppcBuffer)[1] = 0x02;

        //
        // Indicate that the first two blocks of the file are in use.
        //
        (*ppcBuffer)[4] = (char)0xc0;
    }
    else
    {
        //
        // Get the length of the meta-data file.
        //
        *pulLength = Maverick_Length();

        //
        // Allocate memory for the meta-data file.
        //
        *ppcBuffer = (char *)GlobalAlloc(GMEM_FIXED, *pulLength);
        if(!*ppcBuffer)
        {
            Maverick_Close();
            return(0);
        }

        //
        // Read the meta-data from the file.
        //
        Maverick_Read(*ppcBuffer, *pulLength);

        //
        // Close the meta-data file.
        //
        Maverick_Close();
    }

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

//****************************************************************************
//
// Writes the Audible meta-data to the device.
//
//****************************************************************************
static void
WriteMetaData(unsigned long ulDrive, char *pcBuffer, unsigned long ulLength)
{
    //
    // Open the Audible meta-data file.
    //
    if(Maverick_Open(ulDrive, L"\\AUDIBLE.DAT", 3) == 0)
    {
        //
        // The Audible meta-data file does not yet exist, so create it now.
        //
        if(Maverick_Open(ulDrive, L"\\AUDIBLE.DAT", 7) == 0)
        {
            return;
        }
    }

    //
    // Write the meta-data to the file.
    //
    Maverick_Write(pcBuffer, ulLength);

    //
    // Close the file.
    //
    Maverick_Close();

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

//****************************************************************************
//
// Locates the directory entry for the specified file name.
//
//****************************************************************************
static unsigned long
FindFileName(char *pcBuffer, unsigned long ulLength,
             unsigned short *pusFileName)
{
    unsigned long ulOffset, ulIdx;

    //
    // Get the offset of the first directory block.
    //
    ulOffset = *(unsigned long *)pcBuffer;
    if((ulOffset + 512) > ulLength)
    {
        return(-1);
    }

    //
    // Loop while the offset is non-zero.
    //
    while(ulOffset)
    {
        //
        // Loop through the four directory entries in this block.
        //
        for(ulIdx = 0; ulIdx < 4; ulIdx++)
        {
            //
            // See if the file name in this entry matches the file in question.
            //
            if(memcmp(pcBuffer + ulOffset + (ulIdx * 127) + 4, pusFileName,
                      122) == 0)
            {
                break;
            }
        }

        //
        // See if we found the file name.
        //
        if(ulIdx != 4)
        {
            //
            // Return the offset of the directory entry for this file name.
            //
            return(ulOffset + (ulIdx * 127) + 4);
        }

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

    //
    // If we fall out of the above loop the specified file does not exist in
    // the meta-data directory.
    //
    return(-1);
}

//****************************************************************************
//
// Finds an unused block in the meta-data file.
//
//****************************************************************************
unsigned long
GetUnusedBlock(char **ppcBuffer, unsigned long *pulLength)
{
    char *pcNewBuffer;
    unsigned long ulOffset;

    //
    // Loop through the blocks of the meta-data file until we find one that is
    // unused.
    //
    for(ulOffset = 0; (ulOffset << 9) < *pulLength; ulOffset++)
    {
        //
        // See if this block is being used.
        //
        if(((*ppcBuffer)[(ulOffset >> 3) + 4] &
            (1 << (7 - (ulOffset & 7)))) == 0)
        {
            //
            // This block is not being used, so stop searching.
            //
            break;
        }
    }

    //
    // Set the bit to indicate that this block is being used.
    //
    (*ppcBuffer)[(ulOffset >> 3) + 4] |= 1 << (7 - (ulOffset & 7));

    //
    // See if this block already exists in the file.
    //
    if((ulOffset << 9) < *pulLength)
    {
        //
        // This block is already in the file, so simply return it.
        //
        return(ulOffset << 9);
    }

    //
    // This block will be at the end of the file, so extend the length of the
    // file by one block.
    //
    *pulLength += 512;

    //
    // Allocate new memory for the meta-data.
    //
    pcNewBuffer = (char *)GlobalAlloc(GMEM_FIXED, *pulLength);

    //
    // Copy the meta-data from the original buffer to the new buffer.
    //
    memcpy(pcNewBuffer, *ppcBuffer, *pulLength - 512);

    //
    // Clear the new block.
    //
    memset(pcNewBuffer + *pulLength - 512, 0, 512);

    //
    // Release the old meta-data block.
    //
    GlobalFree(*ppcBuffer);

    //
    // Return the new memory buffer.
    //
    *ppcBuffer = pcNewBuffer;

    //
    // Return the offset of the block.
    //
    return(ulOffset << 9);
}

//****************************************************************************
//
// Adds a directory entry for the specified file.
//
//****************************************************************************
unsigned long
AddFileName(char **ppcBuffer, unsigned long *pulLength,
            unsigned short *pusFileName)
{
    unsigned long ulOffset, ulIdx;

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

    //
    // Loop forever.  We will explicitly break out of this loop when we find an
    // unused directory entry.
    //
    while(1)
    {
        //
        // Loop through the four directory entries in this block.
        //
        for(ulIdx = 0; ulIdx < 4; ulIdx++)
        {
            //
            // See if this entry is unused.
            //
            if((*ppcBuffer)[ulOffset + (ulIdx * 127) + 4] == '\0')
            {
                break;
            }
        }

        //
        // Stop looking if we found an unused directory entry.
        //
        if(ulIdx != 4)
        {

⌨️ 快捷键说明

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