svgtopt/SVG/SVGImpl/src/SVGTokenizer.cpp
changeset 46 88edb906c587
child 19 df65ec4f2d28
equal deleted inserted replaced
-1:000000000000 46:88edb906c587
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  SVG Implementation source file
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "SVGTokenizer.h"
       
    20 
       
    21 TTokenizer::TTokenizer( const TPtrC& aString )
       
    22 {
       
    23     iString.Set( Trim( aString ) );
       
    24     iCurrentIndex = 0;
       
    25     iPreviousIndex = 0;
       
    26 }
       
    27 
       
    28 TBool TTokenizer::IsAtEnd()
       
    29 {
       
    30     return iCurrentIndex >= EndIndex();
       
    31 }
       
    32 
       
    33 TInt TTokenizer::EndIndex()
       
    34 {
       
    35     return iString.Length();
       
    36 }
       
    37 
       
    38 TUint16 TTokenizer::NextChar()
       
    39 {
       
    40     if ( IsAtEnd() )
       
    41         return '\0';
       
    42     else
       
    43         return iString[iCurrentIndex];
       
    44 }
       
    45 
       
    46 void TTokenizer::Advance( TInt aCount )
       
    47 {
       
    48     iCurrentIndex += aCount;
       
    49     if ( iCurrentIndex > EndIndex() )
       
    50         iCurrentIndex = EndIndex();
       
    51     else if ( iCurrentIndex < 0 )
       
    52         iCurrentIndex = 0;
       
    53 
       
    54     iPreviousIndex = iCurrentIndex;
       
    55 }
       
    56 
       
    57 TBool TTokenizer::SkipInteger()
       
    58 {
       
    59     if ( iCurrentIndex >= EndIndex() )
       
    60         return EFalse;
       
    61 
       
    62     if ( NextChar() == '-' || NextChar() == '+' )
       
    63     {
       
    64         TInt index = iCurrentIndex;
       
    65         iCurrentIndex++;
       
    66         if ( SkipInteger() )
       
    67         {
       
    68             iPreviousIndex = index;
       
    69             return ETrue;
       
    70         }
       
    71         else
       
    72         {
       
    73             iCurrentIndex = index;
       
    74             iPreviousIndex = iCurrentIndex;
       
    75             return EFalse;
       
    76         }
       
    77     }
       
    78 
       
    79     iPreviousIndex = iCurrentIndex;
       
    80     TInt index = iCurrentIndex;
       
    81 
       
    82     while ( index < EndIndex() && IsDigit( iString[index] ) )
       
    83     {
       
    84         index++;
       
    85     }
       
    86     if ( IsAtEnd() )
       
    87         return EFalse;
       
    88 
       
    89     iCurrentIndex = index;
       
    90     return ETrue;
       
    91 }
       
    92 
       
    93 TBool TTokenizer::SkipDecimal()
       
    94 {
       
    95     if ( iCurrentIndex >= EndIndex() )
       
    96         return EFalse;
       
    97 
       
    98     if ( NextChar() == '-' || NextChar() == '+' )
       
    99     {
       
   100         TInt index = iCurrentIndex;
       
   101         iCurrentIndex++;
       
   102         if ( NextChar() != '-' && NextChar() != '+' && SkipDecimal() )
       
   103         {
       
   104             iPreviousIndex = index;
       
   105             return ETrue;
       
   106         }
       
   107         else
       
   108         {
       
   109             iCurrentIndex = index;
       
   110             iPreviousIndex = iCurrentIndex;
       
   111             return EFalse;
       
   112         }
       
   113     }
       
   114 
       
   115     if ( NextChar() == '.' )
       
   116     {
       
   117         TInt index = iCurrentIndex;
       
   118         Advance( 1 ); // skip '.'
       
   119         if ( !IsDigit( NextChar() ) )
       
   120         {
       
   121             // Only found '.' and no digits following
       
   122             Advance( -1 );
       
   123             return EFalse;
       
   124         }
       
   125         else
       
   126         {
       
   127             // '.xxx'
       
   128             SkipInteger();
       
   129             iPreviousIndex = index;
       
   130             return ETrue;
       
   131         }
       
   132     }
       
   133     else if ( IsDigit( NextChar() ) )
       
   134     {
       
   135         SkipInteger();
       
   136         if ( NextChar() != '.' )
       
   137         {
       
   138             // 'xxx'
       
   139             return ETrue;
       
   140         }
       
   141         else
       
   142         {
       
   143             TInt index = iPreviousIndex;//iCurrentIndex;
       
   144             Advance( 1 ); // skip '.'
       
   145 
       
   146             SkipInteger();
       
   147             iPreviousIndex = index;
       
   148             return ETrue;
       
   149         }
       
   150 
       
   151     }
       
   152     return EFalse;
       
   153 }
       
   154 
       
   155 TBool TTokenizer::SkipHex()
       
   156 {
       
   157     if ( iCurrentIndex >= EndIndex() )
       
   158         return EFalse;
       
   159 
       
   160     iPreviousIndex = iCurrentIndex;
       
   161     TInt index = iCurrentIndex;
       
   162 
       
   163     while ( index < EndIndex() && IsHexDigit( iString[index] ) )
       
   164     {
       
   165         index++;
       
   166     }
       
   167     if ( IsAtEnd() )
       
   168         return EFalse;
       
   169 
       
   170     iCurrentIndex = index;
       
   171     return ETrue;
       
   172 }
       
   173 
       
   174 TBool TTokenizer::SkipWhiteSpace()
       
   175 {
       
   176     if ( iCurrentIndex >= EndIndex() )
       
   177         return EFalse;
       
   178 
       
   179     iPreviousIndex = iCurrentIndex;
       
   180     TInt index = iCurrentIndex;
       
   181 
       
   182     while ( index < EndIndex() && IsWhiteSpace( iString[index] ) )
       
   183     {
       
   184         index++;
       
   185     }
       
   186     if ( IsAtEnd() )
       
   187         return EFalse;
       
   188 
       
   189     iCurrentIndex = index;
       
   190     return ETrue;
       
   191 }
       
   192 
       
   193 TBool TTokenizer::SkipUntil( TUint16 aChar )
       
   194 {
       
   195     if ( IsAtEnd() )
       
   196         return EFalse;
       
   197 
       
   198     iPreviousIndex = iCurrentIndex;
       
   199 
       
   200     TPtrC remaining( &iString.Ptr()[iCurrentIndex], iString.Length() - iCurrentIndex );
       
   201     TInt index = IndexOf( remaining, aChar );
       
   202     if ( index == -1 )
       
   203         return EFalse;
       
   204 
       
   205     iCurrentIndex += index;
       
   206     return ETrue;
       
   207 }
       
   208 
       
   209 TBool TTokenizer::SkipUntilEnd( )
       
   210 {
       
   211     if ( IsAtEnd() )
       
   212         return EFalse;
       
   213 
       
   214     iPreviousIndex = iCurrentIndex;
       
   215 
       
   216     TPtrC remaining( &iString.Ptr()[iCurrentIndex], iString.Length() - iCurrentIndex );
       
   217     TInt index = remaining.Length();
       
   218 
       
   219     iCurrentIndex += index;
       
   220     return ETrue;
       
   221 }
       
   222 
       
   223 TBool TTokenizer::StartsWith( const TDesC& aString )
       
   224 {
       
   225     if ( IsAtEnd() || aString.Length() > iString.Length() - iCurrentIndex )
       
   226         return EFalse;
       
   227 
       
   228     TPtrC remaining( &iString.Ptr()[iCurrentIndex], aString.Length() );
       
   229     return remaining == aString;
       
   230 }
       
   231 
       
   232 TPtrC TTokenizer::SkippedString()
       
   233 {
       
   234     return TPtrC( &iString.Ptr()[iPreviousIndex], iCurrentIndex - iPreviousIndex );
       
   235 }
       
   236 
       
   237 TPtrC TTokenizer::Remainder()
       
   238 {
       
   239     if ( iString.Length() == 0 )
       
   240         return TPtrC( NULL, 0 );
       
   241     else
       
   242         return TPtrC( &iString.Ptr()[iCurrentIndex], iString.Length() -  iCurrentIndex );
       
   243 }
       
   244 //------------------------------------------------------
       
   245 // Helping Functions
       
   246 //------------------------------------------------------
       
   247 TBool TTokenizer::IsWhiteSpace( TUint16 aChar )
       
   248 {
       
   249     return ( aChar == ' ' ) || ( aChar == '\t' ) || ( aChar == '\n' );
       
   250 }
       
   251 
       
   252 TBool TTokenizer::IsDigit( TUint16 aChar )
       
   253 {
       
   254     return ( aChar >= '0' && aChar <= '9' );
       
   255 }
       
   256 
       
   257 TBool TTokenizer::IsHexDigit( TUint16 aChar )
       
   258 {
       
   259     return ( aChar >= '0' && aChar <= '9' ) ||
       
   260            ( aChar >= 'a' && aChar <= 'f' ) ||
       
   261            ( aChar >= 'A' && aChar <= 'F' );
       
   262 }
       
   263 
       
   264 TInt TTokenizer::IndexOf( const TDesC& aString, TUint16 aChar )
       
   265 {
       
   266     TInt length = aString.Length();
       
   267     for ( TInt i = 0; i < length; i++ )
       
   268     {
       
   269         if ( aString[i] == aChar )
       
   270             return i;
       
   271     }
       
   272     return -1;
       
   273 }
       
   274 
       
   275 TPtrC TTokenizer::Trim( const TDesC& aString )
       
   276 {
       
   277     TInt startIndex = 0;
       
   278     for ( ; startIndex < aString.Length() && IsWhiteSpace( aString[startIndex] ); startIndex++ )
       
   279     {
       
   280     }
       
   281 
       
   282     TInt endIndex = aString.Length();
       
   283     for ( ; endIndex > 0 && IsWhiteSpace( aString[endIndex-1] ); endIndex-- )
       
   284     {
       
   285     }
       
   286 
       
   287     return TPtrC( &aString.Ptr()[startIndex], endIndex - startIndex );
       
   288 }
       
   289 
       
   290 TBool TTokenizer::ParseDecimal( const TDesC& aString, TReal32& aValue, TPtrC& aRemainder )
       
   291     {
       
   292     TTokenizer tokenizer( aString );
       
   293     tokenizer.SkipWhiteSpace();
       
   294     
       
   295     // blank, setting to zero
       
   296     if ( tokenizer.IsAtEnd() )
       
   297         {
       
   298         return EFalse;
       
   299         }
       
   300     
       
   301     if ( tokenizer.SkipDecimal() )
       
   302         {
       
   303         // Decimal number, extract it
       
   304         TPtrC decimalString = tokenizer.SkippedString();
       
   305         TLex lex( decimalString );
       
   306         // Specify the decimal seperator, instead of using
       
   307         // locale specific seperator.
       
   308         lex.Val( aValue, '.' );
       
   309         
       
   310         tokenizer.SkipWhiteSpace();
       
   311         aRemainder.Set( tokenizer.Remainder() );
       
   312         return ETrue;
       
   313         }
       
   314     return EFalse;
       
   315     }