TLex
The following code fragments show the use of several
TLex8
and TLex16
methods. All the code uses the build
independent form TLex
.
TLex
objectsThis 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) ;
}
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
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 ;
}
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.
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);
}
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
...
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
...
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
...
}
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) ;