parseurl.cpp

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

CPP
904
字号
        return FALSE;
    }

    savedEndChar = pTimeBuffer[timeBufferLen-1];

    //Get rid of start & terminating quotation mark, if they exist:
    if(pTimeBuffer[0] == '\"')
    {
        pTimeBuffer++;
        timeBufferLen--;
        //Added this check to kill bug if (pTimeBuffer==")
        // and got shortened to an empty string:
        if(!timeBufferLen)
        {
            return FALSE;
        }
    }
    if(pTimeBuffer[timeBufferLen - 1] == '\"')
    {
        pTimeBuffer[timeBufferLen - 1] = '\0'; //get rid of end '\"'.
        timeBufferLen--;
        endCharWasChanged = TRUE;
    }
    
    // Work from right to left, searching first for milliseconds and then for
    // seconds (or seconds only if no '.' found):
    BOOL bColonWasFound = FALSE;
    for(bufIndx=timeBufferLen-1; 0L<=bufIndx; bufIndx--)
    {
        char ch = toupper(pTimeBuffer[bufIndx]);
        if('0' > ch  ||  '9' < ch)
        {
            if(' '==ch  ||  '\t'==ch  ||  '\n'==ch  ||  '\r'==ch)
            {
                //Added everything up to "break;" to
                // handle (valid) strings with leading space(s) like " 39":
                //previous found was seconds_, so translate into ULONG:
                seconds_ = atol(&pTimeBuffer[bufIndx+1L]);
                timeValInMillisec += seconds_*1000; //converts seconds to ms.
                break; //we're done; we found seconds only.
            }
            else if('.' == ch)
            {
                if(bDotEncounteredAlready)
                {
                    //Make sure pTimeBuffer is in original state:
                    if(endCharWasChanged)
                    {
                        timeBufferLen++;
                        pTimeBuffer[timeBufferLen-1] = savedEndChar;
                    }
                    if(indexOfDot >= 0)
                    {
                        pTimeBuffer[indexOfDot] = '.';
                    }
                    //this second '.' is unexpected, so return with
                    //  timeValInMillisec set to whatever was read so far:
                    return FALSE;
                }
                bDotEncounteredAlready = TRUE;
                indexOfDot = bufIndx;
                pTimeBuffer[bufIndx] = '\0'; //end the buffr at the '.' .

                //previously-read #'s are milliseconds, so count them:
                //added "-1" to fix bug if buf ends with ".":
                if(1L > timeBufferLen-bufIndx - 1)
                {
                    milliseconds_ = 0L;
                }
                else
                {
                    //Now, make sure that more than three digits (base 10)
                    //  are not present, e.g., reduce "46371" to "463" since
                    //  we only allow millisecond precision (3 digits past
                    //  the decimal point:
                    char    chTmp = '\0';
                    ULONG32 ulNumDecimalDigitsFound = timeBufferLen-1 - bufIndx;
                    if(NUM_DECIMAL_DIGITS_OF_SECONDS < ulNumDecimalDigitsFound)
                    {
                        chTmp                                                = pTimeBuffer[bufIndx + 1L];
                        pTimeBuffer[bufIndx+NUM_DECIMAL_DIGITS_OF_SECONDS+1] = '\0';
                    }
                    milliseconds_ = atol(&pTimeBuffer[bufIndx+1L]);

                    //Added this to fix "y.x" being converted
                    // to y00x instead of yx00 milliseconds:
                    if(ulNumDecimalDigitsFound < NUM_DECIMAL_DIGITS_OF_SECONDS)
                    {
                        for(ULONG32 ulDiff= NUM_DECIMAL_DIGITS_OF_SECONDS - ulNumDecimalDigitsFound; ulDiff > 0; ulDiff--)
                        {
                            milliseconds_ *= 10;
                        }
                    }

                    if(NUM_DECIMAL_DIGITS_OF_SECONDS < ulNumDecimalDigitsFound)
                    {
                        //restore the changed char in the pTimeBuffer:
                        pTimeBuffer[bufIndx+ NUM_DECIMAL_DIGITS_OF_SECONDS + 1] = chTmp;
                    }
                }
        
                timeValInMillisec = milliseconds_;
            }
            else if(':' == ch)
            {
                bColonWasFound = TRUE;
                //previous found was seconds_, so translate into ULONG:
                seconds_           = atol(&pTimeBuffer[bufIndx + 1L]);
                timeValInMillisec += seconds_ * 1000; //converts seconds to ms.
                break; //done with "seconds_[.milliseconds_]" part.
            }
            else  //unexpected char found, so quit parsing:
            {
                //Make sure pTimeBuffer is in original state:
                if(endCharWasChanged)
                {
                    timeBufferLen++;
                    pTimeBuffer[timeBufferLen - 1] = savedEndChar;
                }
                if(indexOfDot >= 0)
                {
                    pTimeBuffer[indexOfDot] = '.';
                }
                //this char is unexpected, so return FALSE with
                //  timeValInMillisec set to whatever was read so far:
                return FALSE;
            }
        }
        else if(0L == bufIndx) //we're done with the buffer:
        {
            //previous found was seconds_, so translate into ULONG:
            seconds_           = atol(pTimeBuffer);
            timeValInMillisec += seconds_*1000; //converts seconds to ms.
            break; //done with "seconds_[.milliseconds_]" part.
        }
    }

    if(bColonWasFound) //then get the "minutes" part:
    {
        bColonWasFound = FALSE;
        // We've got the ":seconds.msecs" part, so lets get the hours part:
        for(bufIndx--; 0L<=bufIndx; bufIndx--)
        {
            char ch = toupper(pTimeBuffer[bufIndx]);
            if('0' > ch  ||  '9' < ch)
            {
                if(' ' == ch  ||  '.' == ch)
                {
                    break;
                }
                else if(':' == ch)
                {
                    bColonWasFound = TRUE;
                    //previous found was seconds_, so translate into ULONG:
                    // (Note: this will do atol("min:sec") which ignores
                    // everything at & beyond the first non-num (":") char):
                    minutes_           = atol(&pTimeBuffer[bufIndx+1L]);
                    timeValInMillisec += minutes_*60000; //minutes to msec
                    break; //done w/ "minutes_:seconds_[milliseconds_]" part.
                }
                else  //unexpected char found, so quit parsing:
                {
                    //Make sure pTimeBuffer is in original state:
                    if(endCharWasChanged)
                    {
                        timeBufferLen++;
                        pTimeBuffer[timeBufferLen-1] = savedEndChar;
                    }
                    if(indexOfDot >= 0)
                    {
                        pTimeBuffer[indexOfDot] = '.';
                    }
                    //this char is unexpected, so return FALSE with
                    //  timeValInMillisec set to whatever was read so far:
                    return FALSE;
                }
            }
            else if(0L == bufIndx) //we're done with the buffer:
            {
                //previous found was seconds_, so translate into ULONG:
                minutes_           = atol(pTimeBuffer);
                timeValInMillisec += minutes_*60000; //minutes to msec
                break; //done w/ "minutes_:seconds_[milliseconds_]" part.
            }
        }
    }

    if(bColonWasFound) //then get the "hours" part:
    {
        bColonWasFound = FALSE;
        //We've got the ":minutes.seconds.msec" part, so lets get the hours:
        for(bufIndx--; 0L <= bufIndx; bufIndx--)
        {
            char ch = toupper(pTimeBuffer[bufIndx]);
            if('0' > ch  ||  '9' < ch)
            {
                if(' ' == ch  ||  '.' == ch)
                {
                    break;
                }
                else if(':' == ch)
                {
                    bColonWasFound = TRUE;
                    //previous found was minutes_, so translate into ULONG:
                    // (Note: this will do atol("hrs:min:sec") which ignores
                    // everything at & beyond the first non-num (":") char):
                    hours_             = atol(&pTimeBuffer[bufIndx + 1L]);
                    timeValInMillisec += hours_ * 3600000; //hours to msec
                    break;//done w/ "hours_:minutes_:seconds_[milliseconds_]"
                }
                else  //unexpected char found, so quit parsing:
                {
                    //Make sure pTimeBuffer is in original state:
                    if(endCharWasChanged)
                    {
                        timeBufferLen++;
                        pTimeBuffer[timeBufferLen-1] = savedEndChar;
                    }
                    if(indexOfDot >= 0)
                    {
                        pTimeBuffer[indexOfDot] = '.';
                    }
                    //this char is unexpected, so return FALSE with
                    //  timeValInMillisec set to whatever was read so far:
                    return FALSE;
                }
            }
            else if(0L == bufIndx) //we're done with the buffer:
            {
                //previous found was seconds_, so translate into ULONG:
                hours_             = atol(pTimeBuffer);
                timeValInMillisec += hours_ * 3600000; //hours to msec
                break; //done w/ "hours_:minutes_:seconds_[milliseconds_]".
            }
        }
    }

    if(bColonWasFound) //then get the "days" part:
    {
        bColonWasFound = FALSE;
        //We've got the "hours:minutes.seconds.msec" part, so lets get the days:
        for(bufIndx--; 0L <= bufIndx; bufIndx--)
        {
            char ch = toupper(pTimeBuffer[bufIndx]);
            if('0' > ch  ||  '9' < ch)
            {
                if(' ' == ch  ||  '.' == ch)
                {
                    break;
                }
                else if(':' == ch)
                {
                    bColonWasFound = TRUE;
                    //previous found was minutes_, so translate into ULONG:
                    // (Note: this will do atol("hrs:min:sec") which ignores
                    // everything at & beyond the first non-num (":") char):
                    days_              = atol(&pTimeBuffer[bufIndx+1L]);
                    timeValInMillisec += days_ * 86400000; //days to msec
                    break;//done w/ "days_:hours_:minutes_:seconds_[msecs_]"
                }
                else  //unexpected char found, so quit parsing:
                {
                    //Make sure pTimeBuffer is in original state:
                    if(endCharWasChanged)
                    {
                        timeBufferLen++;
                        pTimeBuffer[timeBufferLen - 1] = savedEndChar;
                    }
                    if(indexOfDot >= 0)
                    {
                        pTimeBuffer[indexOfDot] = '.';
                    }
                    //this char is unexpected, so return FALSE with
                    //  timeValInMillisec set to whatever was read so far:
                    return FALSE;
                }
            }
            else if(0L == bufIndx) //we're done with the buffer:
            {
                //previous found was seconds_, so translate into ULONG:
                hours_             = atol(pTimeBuffer);
                timeValInMillisec += hours_ * 86400000; //days to msec
                break; //done w/ "days_:hours_:minutes_:seconds_[msec_]".
            }
        }
    }

    if(endCharWasChanged)
    {
        timeBufferLen++;
        //Restore the orignial pTimeBuffer, in case end quote char was removed:
        pTimeBuffer[timeBufferLen - 1] = savedEndChar;
    }

    if(indexOfDot >= 0)
    {
        pTimeBuffer[indexOfDot] = '.';
    }

    return TRUE;

}

BOOL IsURLRelative(const char* pszURL)
{
    BOOL   bRelative = TRUE;
    CHXURL cFullURL(pszURL);
    if (cFullURL.GetLastError() == HXR_OK)
    {
        IHXValues* pHeader = cFullURL.GetProperties();
        if (pHeader)
        {
            IHXBuffer *pDummy = NULL;
            HX_RESULT   retVal = pHeader->GetPropertyBuffer(PROPERTY_SCHEME, pDummy);
            if (retVal != HXR_OK)
            {
                // If we do NOT have a PROPERTY_SCHEME property, then we are relative.
                bRelative = TRUE;
            }
            else
            {
                bRelative = FALSE;
            }
            HX_RELEASE(pDummy);
        }
        HX_RELEASE(pHeader);
    }

    return bRelative;
}

HX_RESULT MakeAbsoluteURL(const CHXString& rOrigURL, const CHXString& rRelURL, CHXString& rAbsURL)
{
    // Create the CHXURL object
    CHXURL     cURLObj((const char*) rOrigURL);
    IHXValues* pHeader = cURLObj.GetProperties();
    if(!pHeader)
    {
        return HXR_FAIL;
    }

    // Initialize the output string
    rAbsURL.Empty();

    // Get the scheme
    IHXBuffer* pBuffer = NULL;
    HX_RESULT   retVal = pHeader->GetPropertyBuffer(PROPERTY_SCHEME, pBuffer);
    if (retVal == HXR_OK)
    {
        rAbsURL = (const char*) pBuffer->GetBuffer();
        rAbsURL += "://";
        HX_RELEASE(pBuffer);
    }

    // Get the host
    BOOL bHasHost = FALSE;
    retVal        = pHeader->GetPropertyBuffer(PROPERTY_HOST, pBuffer);
    if (retVal == HXR_OK)
    {
        rAbsURL += (const char*) pBuffer->GetBuffer();
        HX_RELEASE(pBuffer);
        bHasHost = TRUE;
    }
    
    // Get the port
    UINT32 ulPort;
    retVal = pHeader->GetPropertyULONG32(PROPERTY_PORT, ulPort);
    if (retVal == HXR_OK)
    {
        char szTemp[16]; /* Flawfinder: ignore */
        sprintf(szTemp, ":%d", (UINT16) ulPort); /* Flawfinder: ignore */
        rAbsURL += szTemp;
    }

    if(bHasHost)
    {
        rAbsURL += "/";
    }


    retVal = pHeader->GetPropertyBuffer(PROPERTY_RESOURCE, pBuffer);
    if (retVal == HXR_OK)
    {
        const char* pResource   = (const char*)pBuffer->GetBuffer();
        const char  cDelimiter1 = '/';
        const char  cDelimiter2 = '\\';

        CHXString   strURLWork = pResource; 
        char*       pFirstChar = strURLWork.GetBuffer(strURLWork.GetLength());
        char*       pLastChar  = NULL;
        char*       pOptions   = NULL;
        char*       pFragment  = NULL;

        pOptions = strchr(pFirstChar, '?');
        if (pOptions)
        {
            pLastChar = pOptions - 1;
        }
        else
        {
            pLastChar = pFirstChar + strlen(pFirstChar) - 1;
        }

        while ((pLastChar > pFirstChar) && (*pLastChar != cDelimiter1) && (*pLastChar != cDelimiter2))
        {
            pLastChar--;
        }

        // If we hit a delimiter before hitting the end, back up one character!
        if(pLastChar > pFirstChar) 
        {
            *(++pLastChar) = '\0';

            rAbsURL += pFirstChar;
        }

        HX_RELEASE(pBuffer);
    }

    HX_RELEASE(pHeader);

    // Now tack on the relative URL
    rAbsURL += rRelURL;

    return HXR_OK;
}

HX_RESULT AddURLOrRequestParam(IHXRequest* pRequest, const char* pszParamName,
                               IUnknown* pContext, IHXValues* pValues)
{
    HX_RESULT retVal = HXR_OK;

    if (pRequest && pszParamName && pValues)
    {
        IHXBuffer* pBuf = NULL;
        HX_RESULT rv = PXUtilities::GetURLOrRequestParam(pRequest, FALSE, pContext,
                                                         pszParamName, pBuf);
        if (SUCCEEDED(rv))
        {
            retVal = pValues->SetPropertyCString(pszParamName, pBuf);
        }
        HX_RELEASE(pBuf);
    }
    else
    {
        retVal = HXR_FAIL;
    }

    return retVal;
}

⌨️ 快捷键说明

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