示例#1
0
pbool PIni::load(const pchar *filename)
{
    //
    // Look for the file in the specified path
    //
    const pchar* pathList[] =
    {
        ".",
        pPathGetApplicationDirectory(),
        pPathGetExternalStoragePath(),
    };

    for (size_t i = 0; i < sizeof(pathList) / sizeof(pathList[0]); ++i)
    {
        if (pathList[i] != P_NULL)
        {
            PString cfgPath = PString(pathList[i]) + PString(pPathGetDelimiter()) + PString(filename);
            PFile *fp = pfopen(cfgPath.c_str(), "rb");
            if (fp != P_NULL)
            {
                if (ini_parse_file((FILE *)(fp), iniHandler_internal, this) < 0)
                {
                    PLOG_ERROR("Failed to parse %s", cfgPath.c_str());
                    pfclose(fp);
                    return false;
                }
                pfclose(fp);

                return true;
            }
        }
    }

    //
    // Look for the ini file in asset
    //
    PAsset asset = pAssetOpen(filename);
    if (pAssetIsValid(&asset))
    {
        PIniBufferObject iniBufferObject;
        iniBufferObject.m_buffer = (const pchar*)pAssetGetBuffer(&asset);
        iniBufferObject.m_bufferSize = pAssetGetSize(&asset);
        iniBufferObject.m_position = 0;
        
        if (ini_parse_buffer(&iniBufferObject, iniHandler_internal, this) < 0)
        {
            PLOG_ERROR("Failed to parse %s", filename);
            pAssetClose(&asset);
            return false;
        }

        pAssetClose(&asset);
        
        return true;
    }

    PLOG_ERROR("Failed to find %s", filename);
    
    return false;
}
const void * P_APIENTRY pAssetGetBuffer(PAsset *asset)
{
    PASSERT(asset != P_NULL);
    if (asset != P_NULL)
    {
        puint32 len = pAssetGetSize(asset);
        PFile *fp = (PFile *)asset->pHandle;
        
        puint8 *buffer = PNEWARRAY(puint8, len);
        pfseek(fp, 0, P_FILE_SEEK_FROM_BEGINNING);
        puint32 readLen = pfread(buffer, sizeof(puint8), len, fp);
        PASSERT(readLen == len);

        PASSERT(asset->pData == P_NULL);
        asset->pData = buffer;

        return asset->pData;
    }

    return P_NULL;
}
puint32 PStreamAsset::getSize()
{
    return pAssetGetSize(&m_asset);
}