Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



Using TLex

The following code fragments show the use of several TLex8 and TLex16 methods. All the code uses the build independent form TLex.


Constructing TLex objects

This converts an real number into a string, which is then assigned to a TLex.

TBuf<0x100> convertRealToString;
// want a TLex from a value
if (convertRealToString.Num(value,format) < KErrNone )
    {
       ...
       }
else
    {
    convertRealToString.ZeroTerminate();
    TLex string(convertRealToString) ;
       }

This takes a descriptor as a function parameter, and copies it to a TLex.

TInt RPNCalc(const TDesC& iCommand, TReal& returnValue)
    {
    TLex input (iCommand) ;
       }

[Top]


Peeking the next character

This shows a code flow decision made according to next character to be read from the TLex:

if (!(input.Peek()).IsDigit()) // found non-digit after decimal point if

[Top]


Moving past a character that has been peeked

This shows the use of the Inc() function to move past a character that has been peeked:

if (input.Peek() == '-')
    {
    input.Inc() ; // move past minus sign & flag
    negative = ETrue ;
    }

[Top]


Restoring a previously “got” character

This shows the use of UnGet() to restore the previously "got" character.

....
if (input.Offset() > 0) // if not at start of line
    {
    input.UnGet() ; // restore 'got' character
    }

If the previous character is before the start of the string, then the function raises a USER 59 panic for the TLex8 variant and a USER 64 panic for the TLex16 variant.

[Top]


Reset the next character to the supplied mark

This shows how to allow part of a TLex can be parsed again:

if (!(input.Peek()).IsDigit())
    {
    // found non-digit after decimal point. Error, so rewind
    input.UnGetToMark(startMark);
    }

[Top]


Skipping any non-white space to get the next token

This parses a TLex for the next token:

input.Mark() ; // remember where we are
input.SkipCharacters() ; // move to end of character token
if ( input.TokenLength() != 0 ) // if valid potential token
...

[Top]


Getting length of a token

This shows how TokenLength() is used to return the difference between the position of the next character and the extraction mark. This gives a check as to whether the token length is valid. An invalid token length implies an invalid token.

if ( input.TokenLength() != 0 ) // if valid token length
...

[Top]


Extracting a token

This extracts a marked token.

TPtrC token = input.MarkedToken() ; // extract token

[Top]


Getting the offset of next character position

This shows how to return the offset of the next character position from the start of the string.

if (input.Offset() > 0) // if not at start of line
    {
    input.UnGet() ; // restore 'got' character
    ...
    }

[Top]


Extracting an unknown number type

This example shows how to return the offset of the next character position from the start of the string.

if (input.Val(extractUint) == KErrNone)
    {
    stack.Push(TReal(extractUint)) ;
    }
else
    {
    if (input.Val(extractReal) == KErrNone)
        {
        stack.Push(extractReal) ;
        }
    }

This extracts an unknown number type. Tries an integer first and then, if this fails, tries a real:

if (input.Val(extractUint) == KErrNone)
    stack.Push(TReal(extractUint)) ;
else if (input.Val(extractReal) == KErrNone)
    stack.Push(extractReal) ;