Пример #1
0
/*
-------------------------------------------------------------------------------

    Class: CStifSectionParser

    Method: SubSectionL

    Description: Parses subsections from the main section.

    If start tag is empty the parsing starts begin of the section.
    If end tag is empty the parsing goes end of section.
    This method will parse next subsection after the earlier subsection if
    aSeeked parameter is not given.
    If parser section includes several subsections with both start and end
    tags so aSeeked parameter seeks the required subsection. The aSeeked
    parameter indicates subsection that will be parsed.

    Parameters: const TDesC& aStartTag: in: Indicates parsing start point
                const TDesC& aEndTag: in: Indicates parsing end point
                TInt aSeeked: in: a seeked subsection which will be parsed

    Return Values:  CStifItemParser* : pointer to CStifItemParser object
                    NULL will return if end tag is not found
                    NULL will return if length is 0 or negative
                    NULL will return if lengthStart is 0

    Errors/Exceptions: Leaves if called CStifSectionParser::NewL method fails

    Status: Proposal

-------------------------------------------------------------------------------
*/
EXPORT_C CStifSectionParser* CStifSectionParser::NextSubSectionL( 
                                                    const TDesC& aStartTag,
                                                    const TDesC& aEndTag,
                                                    TInt aSeeked )
    {
    
    TLex lex( iSection );

    lex.SkipAndMark( iSubOffset );

    // Get the required sub section length
    TInt length( 0 );
    TInt lengthStartPos( 0 );
    TInt lengthEndPos( 0 );
    TBool eos( EFalse );
    TInt tagCount( 1 );

    // Check is aStartTag given
    if ( aStartTag.Length() == 0 )
        {
        // Skip line break, tabs, spaces etc.
        lex.SkipSpace();
        lengthStartPos = lex.Offset();
        }
    else
        {
        // While end of section and aStartTag is given
        while ( !lex.Eos() )
            {
            TPtrC ptr = lex.NextToken();
            // Start of the section is found and correct section
            if ( ptr == aStartTag && tagCount == aSeeked )
                {
                // Start position
                lengthStartPos = lex.Offset();
                break;
                }
            // Start tag is found but not correct section
            else if ( ptr == aStartTag )
                {
                tagCount++;
                }
            }
        }

    // If we are end of section lex.Eos() and eos will be ETrue
    eos = lex.Eos();

    // Seeked section is not found
    if ( tagCount != aSeeked )
        {
        __TRACE( KInfo, ( _L( "STIFPARSER: NextSubSectionL method: Seeked subsection is not found" ) ) );
        User::Leave( KErrNotFound );
        }

    // Check is aEndTag given
    if ( aEndTag.Length() == 0 )
        {
        lengthEndPos = iSection.MaxLength();
        }
    else
        {
        // While end of section and aEndTag is given
        while ( !lex.Eos() )
            {
            TPtrC ptr = lex.NextToken();
            // End tag of the section is found
            if ( ptr == aEndTag )
                {
                // End position
                lengthEndPos = lex.Offset();
                // Because Offset() position is after the aEndTag
                lengthEndPos -= aEndTag.Length();
                break;
                }
            }
        }

    // If we are end of section and lengthEndPos is 0
    if ( lengthEndPos == 0 )
        {
        // lex.Eos() and eos will be ETrue
        eos = lex.Eos();
        }

    // The length includes spaces and end of lines
    length = ( lengthEndPos - lengthStartPos );

    CStifSectionParser* section = NULL;

    // If eos is true or length is negative
    if ( eos || length <= 0  ) 
        {
        __TRACE( KInfo, 
            ( _L( "STIFPARSER: SubSectionL method returns a NULL" ) ) );
        }    
    else
        {
        // Position where start next parsing.(End position,
        // includes white spaces)
        iSubOffset = lex.Offset();
        
        // Make CStifSectionParser object and alloc required length
        section = CStifSectionParser::NewL( length );
        CleanupStack::PushL( section );

        // Copy required data to the section object
        section->SetData( iSection, lengthStartPos, length );

        CleanupStack::Pop( section );
        }

    return section;

    }
Пример #2
0
/*
-------------------------------------------------------------------------------

    Class: CStifParser

    Method: NextSectionMemoryL

    Description: Parses sections from configuration files.

    Open and read configuration source and parses a required section.
    If start tag is empty the parsing starts beginning of the configuration
    file. If end tag is empty the parsing goes end of configuration file.
    This method will parse next section after the earlier section if aSeeked
    parameter is not given.
    If configuration file includes several sections with both start and end
    tags so aSeeked parameter seeks the required section. The aSeeked
    parameters indicates section that will be parsed.

    Parameters: const TDesC& aStartTag: in: Indicates a start tag for parsing
                const TDesC& aEndTag: in: Indicates an end tag for parsing
                TInt aSeeked: in: a seeked section which will be parsed

    Return Values:  CStifSectionParser* : pointer to CStifSectionParser object
                    NULL will return if file size or aSeeked is not positive
                    NULL will return if start tag is not found
                    NULL will return if end tag is not found
                    NULL will return if parsed section length is not positive

    Errors/Exceptions:  Leaves if called Size method fails
                        Leaves if HBufC::NewLC method leaves
                        Leaves if called Read method fails
                        Leaves if CStifSectionParser::NewL methods leaves

    Status: Proposal

-------------------------------------------------------------------------------
*/
CStifSectionParser* CStifParser::NextSectionMemoryL( const TDesC& aStartTag,
                                                 	 const TDesC& aEndTag,
                                                     TInt aSeeked )
    {
    TInt size( 0 );
    // Parser is created straight with data
    if( iParsingMode == EBufferParsing )
        {
        size = iBuffer.Length();
        }
    // Parser is created with path and file informations
    else
        {
        User::LeaveIfError( iFile.Size( size ) );
        }

    // size or aSeeked cannot be 0 or negetive
    if( size <= 0 || aSeeked <= 0)
        {
        __TRACE(
            KInfo, ( _L( "STIFPARSER: NextSectionL method returns a NULL" ) ) );
        return NULL;
        }

    const TInt tmpSize = 128;//--UNICODE-- KMaxName; // 128 - set to even value, because KMaxName may change in the future
    TInt offset( 0 ); // Offset value to parts reading

    // Construct modifiable heap-based descriptor. tmp to CleanupStack
    HBufC* tmp = HBufC::NewLC( size );
    TPtr wholeSection = tmp->Des();

    // Construct modifiable heap-based descriptor. tmp2 to CleanupStack
    HBufC8* tmp2 = HBufC8::NewLC( tmpSize );    // 128
    TPtr8 buf = tmp2->Des();

    // Construct modifiable heap-based descriptor. tmp3 to CleanupStack
    HBufC* tmp3 = HBufC::NewLC( tmpSize );      // 128
    TPtr currentSection = tmp3->Des();

    // Parser is created straight with data
    if( iParsingMode == EBufferParsing )
        {
        // If 8 bit copy changes to 16
        wholeSection.Copy( iBuffer );
        }
    // Parser is created with path and file informations
    else
        {
        TPtrC16 currentSectionUnicode;
        do // Read data in parts(Maximum part size is KMaxName)
            {
            // Read data
            User::LeaveIfError( iFile.Read( offset, buf, tmpSize ) );

            // If file is unicode convert differently
            if(iIsUnicode)
                {
                // 8 bit to 16 with unicode conversion - simply point to byte array as to double-byte array
                currentSectionUnicode.Set((TUint16 *)(buf.Ptr()), buf.Length() / 2);
                // Appends current section to whole section
                wholeSection.Append( currentSectionUnicode );
                }
            else
                {
                // 8 bit to 16
                currentSection.Copy( buf );
                // Appends current section to whole section
                wholeSection.Append( currentSection );
                }

            offset += tmpSize;

            } while( offset < size );
        }

    CleanupStack::PopAndDestroy( tmp3 );
    CleanupStack::PopAndDestroy( tmp2 );

    // User wants section without c-style comments
    if( iCommentType == ECStyleComments )
        {
        ParseCommentsOff( wholeSection );
        }

    TLex lex( wholeSection );
    lex.SkipAndMark( iOffset );

    // For the required section length and positions
    TInt length( 0 );
    TInt lengthStartPos( 0 );
    TInt lengthEndPos( 0 );
    TBool eos( EFalse );
    TInt tagCount( 1 );

    // Check is aStartTag given
    if ( aStartTag.Length() == 0 )
        {
        // Skip line break, tabs, spaces etc.
        lex.SkipSpace();
        lengthStartPos = lex.Offset();
        }
    else
        {
        // While end of section
        while ( !lex.Eos() )
            {
            TPtrC ptr = lex.NextToken();
            // Start of the section is found and correct section
            if ( ptr == aStartTag && tagCount == aSeeked )
                {
                lengthStartPos = lex.Offset();
                break;
                }
            // Start tag is found but not correct section
            else if ( ptr == aStartTag )
                {
                tagCount++;
                }
            }
        }

    // If we are end of section lex.Eos() and eos will be ETrue
    eos = lex.Eos();

    // Seeked section is not found
    if ( tagCount != aSeeked )
        {
        __TRACE( KInfo, ( _L(
            "STIFPARSER: NextSectionL method: Seeked section is not found" ) ) );
        CleanupStack::PopAndDestroy( tmp );
        User::Leave( KErrNotFound );
        }

    // Check is aEndTag given
    if ( aEndTag.Length() == 0 )
        {
        lengthEndPos = wholeSection.Length();
        }
    else
        {
        // While end of section
        while ( !lex.Eos() )
            {
            TPtrC ptr = lex.NextToken();
            // End tag of the section is found
            if ( ptr == aEndTag )
                {
                lengthEndPos = lex.Offset();
                // Because Offset() position is after the aEndTag
                lengthEndPos -= aEndTag.Length();
                break;
                }
            }
        }

    // If we are end of section and lengthEndPos is 0
    if ( lengthEndPos == 0 )
        {
        // lex.Eos() and eos will be ETrue
        eos = lex.Eos();
        }

    // The length includes spaces and end of lines
    length = ( lengthEndPos - lengthStartPos );

    CStifSectionParser* section = NULL;

    // If eos is true or length is negative
    if ( eos || length <= 0  )
        {
        __TRACE(
            KInfo, ( _L( "STIFPARSER: NextSectionL method returns a NULL" ) ) );
        }
    else
        {
        // Make CStifSectionParser object and alloc required length
        section = CStifSectionParser::NewL( length );
        CleanupStack::PushL( section );

        // Copy required data to the section object
        section->SetData( wholeSection, lengthStartPos, length );

        //iOffset += lengthEndPos + aEndTag.Length();
        iOffset = lex.Offset();

        CleanupStack::Pop( section );
        }
    CleanupStack::PopAndDestroy( tmp );

    return section;

    }