pxerror.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 524 行 · 第 1/2 页

CPP
524
字号
        UINT32 ulNumFormatArgs = CountArguments(pszFormat);
        UINT32 ulNumArgs       = 0;
        if (pszArg1)
        {
            ulNumArgs++;
        }
        if (pszArg2)
        {
            ulNumArgs++;
        }
        // Only display the error if the number of arguments match up
        HX_ASSERT(ulNumFormatArgs == ulNumArgs);
        if (ulNumFormatArgs == ulNumArgs)
        {
            // Create error string
            //
            // Max size of error string = strlen(pszFormat) +
            //                            strlen(pszArg1)   +
            //                            strlen(pszArg2)   + 1
            UINT32 ulMaxSize = strlen(pszFormat)               +
                               (pszArg1 ? strlen(pszArg1) : 0) +
                               (pszArg2 ? strlen(pszArg2) : 0) + 1;
            char* pszErr = new char [ulMaxSize];
            if (pszErr)
            {
                switch (ulNumArgs)
                {
                    case 0:
                        strcpy(pszErr, pszFormat); /* Flawfinder: ignore */
                        break;
                    case 1:
                        sprintf(pszErr, pszFormat, pszArg1); /* Flawfinder: ignore */
                        break;
                    case 2:
                        sprintf(pszErr, pszFormat, pszArg1, pszArg2); /* Flawfinder: ignore */
                        break;
                    default:
                        pszErr[0] = '\0';
                }

                retVal = SetString(pszErr, rpErrStr);
            }
            HX_VECTOR_DELETE(pszErr);
        }
    }
    HX_RELEASE(pRes);

    return retVal;
}

HX_RESULT PXError::SetError(const char* pszFileName, UINT32 ulErrorID, UINT32 ulLine,
                            UINT32 ulCol, const char* pszArg1, const char* pszArg2,
                            REF(IHXBuffer*) rpErrStr)
{
    HX_RESULT retVal = HXR_OK;

    const char*    pszFormat = NULL;
    IHXXResource* pRes      = NULL;
    retVal                   = GetErrorResource(ulErrorID, pRes);
    if (SUCCEEDED(retVal))
    {
        pszFormat = (const char*) pRes->ResourceData();
    }
    else
    {
        retVal = GetDefaultErrorFormatString(ulErrorID, pszFormat);
    }

    if (SUCCEEDED(retVal))
    {
        // Make sure num args provided and num args in format string match up
        UINT32 ulNumFormatArgs = CountArguments(pszFormat);
        UINT32 ulNumArgs       = 0;
        if (pszArg1)
        {
            ulNumArgs++;
        }
        if (pszArg2)
        {
            ulNumArgs++;
        }
        // Only display the error if the number of arguments match up
        HX_ASSERT(ulNumFormatArgs == ulNumArgs);
        if (ulNumFormatArgs == ulNumArgs)
        {
            // Create a new format string from the resource format string
            //
            const char* pszFormat1 = "(%s): %s (line %lu, column %lu)";
            // The max size of the new format string = strlen(pszFormat1)  +
            //                                         strlen(pszFileName) +
            //                                         strlen(pszFormat)   +
            //                                         2*MAX_SIZE_OF_UINT32 string + 1
            //                                       = strlen(") + strlen(") + strlen(") + 21;
            UINT32 ulMaxSize = strlen(pszFormat1)  +
                               strlen(pszFileName) +
                               strlen(pszFormat)   + 21;
            char* pszFormat2 = new char [ulMaxSize];
            if (pszFormat2)
            {
                // Create the format string
                sprintf(pszFormat2, pszFormat1, pszFileName, pszFormat, ulLine, ulCol); /* Flawfinder: ignore */
                // Create the error string
                //
                // The max size of the error string = strlen(pszFormat2) +
                //                                    strlen(pszArg1)    +
                //                                    strlen(pszArg2)    + 1
                ulMaxSize = strlen(pszFormat2) +
                            (pszArg1 ? strlen(pszArg1) : 0) +
                            (pszArg2 ? strlen(pszArg2) : 0) + 1;
                char* pszErr = new char [ulMaxSize];
                if (pszErr)
                {
                    switch (ulNumArgs)
                    {
                        case 0:
                            strcpy(pszErr, pszFormat2); /* Flawfinder: ignore */
                            break;
                        case 1:
                            sprintf(pszErr, pszFormat2, pszArg1); /* Flawfinder: ignore */
                            break;
                        case 2:
                            sprintf(pszErr, pszFormat2, pszArg1, pszArg2); /* Flawfinder: ignore */
                            break;
                        default:
                            pszErr[0] = '\0';
                    }

                    retVal = SetString(pszErr, rpErrStr);
                }
                HX_VECTOR_DELETE(pszErr);
            }
            HX_VECTOR_DELETE(pszFormat2);
        }
    }
    HX_RELEASE(pRes);

    return retVal;
}

HX_RESULT PXError::GetErrorResource(UINT32 ulErrorID, REF(IHXXResource*) rpResource)
{
    HX_RESULT retVal = HXR_OK;

    if (m_pContext)
    {
        IHXExternalResourceManager* pMgr = NULL;
        retVal = m_pContext->QueryInterface(IID_IHXExternalResourceManager, (void**) &pMgr);
        if (SUCCEEDED(retVal))
        {
            IHXExternalResourceReader* pRdr = NULL;
            retVal = pMgr->CreateExternalResourceReader(CORE_RESOURCE_SHORT_NAME, pRdr);
            if (SUCCEEDED(retVal))
            {
                IHXXResource* pRes = pRdr->GetResource(HX_RT_STRING, ulErrorID);
                if(pRes)
                {
                    HX_RELEASE(rpResource);
                    rpResource = pRes;
                    rpResource->AddRef();
                }
                else
                {
                    retVal = HXR_FAIL;
                }
                HX_RELEASE(pRes);
            }
            HX_RELEASE(pRdr);
        }
        HX_RELEASE(pMgr);
    }
    else
    {
        retVal = HXR_UNEXPECTED;
    }

    return retVal;
}

HX_RESULT PXError::GetDefaultErrorFormatString(UINT32 ulErrorID, REF(const char*) rpszFormat)
{
    HX_RESULT retVal = HXR_FAIL;

    // Search the table
    PXErrorString* pErr = (PXErrorString*) m_pErrorTable;
    while (pErr->m_pszString)
    {
        if (ulErrorID == pErr->m_ulStringID)
        {
            break;
        }
        pErr++;
    }

    // Assign the out parameter
    if (pErr->m_pszString)
    {
        rpszFormat = pErr->m_pszString;
        retVal     = HXR_OK;
    }

    return retVal;
}

HX_RESULT PXError::SetString(const char* pszErr, REF(IHXBuffer*) rpErrStr)
{
    HX_RESULT retVal = HXR_OK;

    if (m_pContext)
    {
        IHXCommonClassFactory* pFactory = NULL;
        retVal = m_pContext->QueryInterface(IID_IHXCommonClassFactory,
                                            (void**) &pFactory);
        if (SUCCEEDED(retVal))
        {
            IHXBuffer* pBuffer = NULL;
            retVal = pFactory->CreateInstance(CLSID_IHXBuffer, (void**) &pBuffer);
            if (SUCCEEDED(retVal))
            {
                retVal = pBuffer->Set((const unsigned char*) pszErr,
                                      strlen(pszErr) + 1);
                if (SUCCEEDED(retVal))
                {
                    HX_RELEASE(rpErrStr);
                    rpErrStr = pBuffer;
                    rpErrStr->AddRef();
                }
            }
            HX_RELEASE(pBuffer);
        }
        HX_RELEASE(pFactory);
    }
    else
    {
        retVal = HXR_UNEXPECTED;
    }

    return retVal;
}

UINT32 PXError::CountArguments(const char* pszFormat)
{
    UINT32 ulRet = 0;

    if (pszFormat)
    {
        char* pszPct = (char*) pszFormat;
        do
        {
            pszPct = (char*)strchr((const char*) pszPct, '%');
            if (pszPct)
            {
                ulRet++;
                pszPct++;
            }
        }
        while (pszPct);
    }

    return ulRet;
}

⌨️ 快捷键说明

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