First public contribution.
1 // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\euser\us_lex8.cpp
23 EXPORT_C TLex8::TLex8()
24 : iNext(NULL),iBuf(NULL),iEnd(NULL),iMark(NULL)
28 Constructs a TLex8, initialising its members to NULL.
35 EXPORT_C void TLex8::Assign(const TUint8 *aString)
37 Assigns a string to this object from another string.
39 @param aString A pointer to a string to be assigned.
43 iMark.iPtr=iNext=iBuf=aString;
44 iEnd=iBuf+User::StringLength(aString);
50 EXPORT_C void TLex8::Assign(const TDesC8 &aDes)
52 Assigns a string to this object from a descriptor.
54 @param aDes The descriptor to be assigned.
58 iMark.iPtr=iNext=iBuf=aDes.Ptr();
59 iEnd=iBuf+aDes.Length();
65 EXPORT_C void TLex8::UnGet()
67 Decrements the next character position, allowing a previously "got" character
70 @panic USER 59, if the previous character is before the start of the string.
74 __ASSERT_ALWAYS(iNext>iBuf,Panic(ETLex8UnGetUnderflow));
81 EXPORT_C void TLex8::UnGetToMark(const TLexMark8 aMark)
83 Sets the next character position to the supplied extraction mark position.
85 @param aMark Mark to copy to the next character position.
87 @panic USER 63, if the extraction mark is before the start or beyond the end
99 EXPORT_C void TLex8::Inc(TInt aNumber)
101 Increments the next character position by aNumber.
103 @param aNumber The number of characters to increment the next character position
106 @panic USER 60, if the increment puts the next character position before the
107 start or beyond the end of the string.
112 __ASSERT_ALWAYS(iNext>=iBuf && iNext<=iEnd,Panic(ETLex8IncOutOfRange));
118 EXPORT_C void TLex8::Inc()
120 Increments to the next character position.
122 @panic USER 60, if the increment puts the next character position beyond the
129 __ASSERT_ALWAYS(iNext<=iEnd,Panic(ETLex8IncOutOfRange));
135 EXPORT_C TChar TLex8::Get()
137 Gets the next character in the string, and increments the next
140 @return Next character to be read.0 if at the end of the string.
152 EXPORT_C TChar TLex8::Peek() const
154 Shows the next character to be returned by Get().
156 @return Character to be returned by the next call to Get(). 0 if at the
171 EXPORT_C void TLex8::SkipSpace()
173 Moves the next character position past any white space (space or separator).
175 Stops if at the end of string.
179 while (!Eos() && Peek().IsSpace())
186 EXPORT_C void TLex8::SkipAndMark(TInt aNumber,TLexMark8& aMark)
188 Moves the next character position a specified number of characters and copies
189 it to the specified extraction mark.
191 @param aNumber Number of characters to skip.
192 @param aMark On return, set to the next character position.
194 @panic USER 61, if the skip moves the next character position either to before
195 the start or beyond the end of the string.
200 __ASSERT_ALWAYS(iNext>=iBuf && iNext<=iEnd,Panic(ETLex8SkipOutOfRange));
207 EXPORT_C void TLex8::SkipSpaceAndMark(TLexMark8& aMark)
209 Moves the next character position past any white space and copies it to the
210 specified extraction mark.
212 Stops if at the end of the string.
214 @param aMark On return, contains a reference to the next character position.
225 EXPORT_C void TLex8::SkipCharacters()
227 Moves the next character position to the next white space (space or separator).
229 Stops if at the end of the string.
233 while (!Eos() && !Peek().IsSpace())
240 EXPORT_C TInt TLex8::TokenLength(const TLexMark8 aMark) const
242 Gets the length of the token starting at the specified extraction mark.
244 @param aMark Extraction mark indicating the start of the token.
246 @return Length of the token.
248 @panic USER 63, if the specified mark is before the start or beyond the end
254 return (iNext-aMark.iPtr);
260 EXPORT_C TPtrC8 TLex8::MarkedToken(const TLexMark8 aMark) const
262 Extracts the token, starting at the specified mark
264 @param aMark Extraction mark indicating the start of the token.
266 @return Extracted token.
268 @panic USER 63, if the specified mark is before the start or beyond the end
273 return(TPtrC8(aMark.iPtr,TokenLength(aMark)));
279 EXPORT_C TPtrC8 TLex8::MarkedToken() const
281 // Extract the internally marked token
282 // there is the assumption here that iMark is always valid
284 Extracts the marked token.
286 Note that the function assumes that the current extraction mark is valid.
288 @return Extracted token.
290 @panic USER 63, if the specified mark is before the start or beyond the end
295 return(TPtrC8(iMark.iPtr,TokenLength()));
301 EXPORT_C TPtrC8 TLex8::NextToken()
303 Strips any white space and extracts the next token.
305 @return Extracted token.
310 SkipSpaceAndMark(mark);
312 return(TPtrC8(mark.iPtr,TokenLength(mark)));
318 EXPORT_C TPtrC8 TLex8::Remainder() const
320 Gets a descriptor containing all the text from the next character position
321 to the end of the string.
323 @return Text from the next character position onwards.
325 @panic USER 29, if the value of (next character position - extraction mark) is
330 return(TPtrC8(iNext,iEnd-iNext));
336 EXPORT_C TPtrC8 TLex8::RemainderFromMark(const TLexMark8 aMark) const
338 Gets a descriptor containing all the text from the specified extraction mark
339 to the end of the string.
341 @param aMark Extraction mark indicating where remaining text starts.
343 @return Text from the specified extraction mark onwards.
345 @panic USER 29, if the value of (next character position - extraction mark) is
347 @panic USER 63, if the specified mark is before the start or beyond the end
353 return(TPtrC8(aMark.iPtr,iEnd-aMark.iPtr));
359 EXPORT_C TPtrC8 TLex8::RemainderFromMark() const
361 // Return the remainder from iMark
362 // There is an assumption here that the internal mark will always be valid
365 Gets a descriptor containing all the text from the current extraction mark to the end
368 The function assumes that the current extraction mark is valid.
370 @return Text from the extraction mark onwards.
374 return(TPtrC8(iMark.iPtr,iEnd-iMark.iPtr));
380 EXPORT_C TInt TLex8::Offset() const
382 // Return the offset from iNext to the lex start.
385 Gets the offset of the next character position from the start of the string.
387 @return Offset of next character position.
391 return((TUint)(iNext-iBuf));
397 EXPORT_C TInt TLex8::MarkedOffset(const TLexMark8 aMark) const
399 Gets the offset of the specified extraction mark from the start of the string.
401 @param aMark Extraction mark.
403 @return The offset of the extraction mark.
405 @panic USER 63, if the specified mark is before the start or beyond the end
411 return((TUint)(aMark.iPtr-iBuf));
417 EXPORT_C TInt TLex8::BoundedVal(TUint32 &aVal,TRadix aRadix,TUint aLimit)
419 Parses the string to extract a 32-bit unsigned integer, using the
420 specified radix, and checks that it is within the specified limit.
422 The specified radix is one of binary, octal, decimal, or hexadecimal.
424 @param aVal On return, contains the extracted integer.
425 @param aRadix The radix to use when converting the number.
426 @param aLimit The upper limit.
428 @return KErrNone if successful.
429 KErrGeneral if the next character position is initially at the end of the string
430 or no valid characters found initially.
431 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
432 If error codes KErrGeneral or KErrOverflow are returned, the object's
433 members are left unaltered.
437 TUint l=aLimit/aRadix;
440 TLexMark8 mark(iNext);
454 if (c>=(TUint)aRadix)
462 return(KErrOverflow);
470 return(KErrOverflow);
482 return(KErrOverflow);
491 EXPORT_C TInt TLex8::BoundedVal(TInt32 &aVal,TInt aLimit)
493 Parses the string to extract a 32-bit signed integer, and checks that it is
494 within the specified limit.
496 @param aVal On return, contains the extracted integer.
497 @param aLimit The upper limit.
499 @return KErrNone if successful.
500 KErrGeneral if the next character position is initially at the end of the string
501 or no valid characters found initially.
502 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
503 If error codes KErrGeneral or KErrOverflow are returned, the object's
504 members are left unaltered.
511 TLexMark8 mark(iNext);
523 TInt r=BoundedVal(v,EDecimal,lim);
541 EXPORT_C TInt TLex8::BoundedVal(TInt64& aVal, TRadix aRadix, const TInt64& aLimit)
543 Parses the string to extract a 64-bit signed integer, using the
544 specified radix, and checks that it is within the specified limit.
546 The specified radix is one of binary, octal, decimal, or hexadecimal.
548 @param aVal On return, contains the extracted integer.
549 @param aRadix The radix to use when converting the number.
550 @param aLimit The upper limit.
552 @return KErrNone if successful.
553 KErrGeneral if the next character position is initially at the end of the string
554 or no valid characters found initially.
555 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
556 If error codes KErrGeneral or KErrOverflow are returned, the object's
557 members are left unaltered.
560 TUint64 rad = aRadix;
561 TUint64 lim = static_cast<TUint64>(aLimit);
567 TLexMark8 mark(iNext);
589 return(KErrOverflow);
597 return(KErrOverflow);
606 if (v > static_cast<TUint64>(aLimit))
609 return(KErrOverflow);
611 aVal = static_cast<TInt64>(v);
618 EXPORT_C TInt TLex8::BoundedVal(TInt64& aVal, const TInt64& aLimit)
620 Parses the string to extract a 64-bit signed integer, and checks that it
621 is within the specified limit.
623 @param aVal On return, contains the extracted integer.
624 @param aLimit The upper limit.
626 @return KErrNone if successful.
627 KErrGeneral if the next character position is initially at the end of the string
628 or no valid characters found initially.
629 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
630 If error codes KErrGeneral or KErrOverflow are returned, the object's
631 members are left unaltered.
638 TLexMark8 mark(iNext);
650 TInt r=BoundedVal(v,EDecimal,lim);
668 EXPORT_C TInt TLex8::Val(TInt8 &aVal)
670 Parses the string to extract a signed 8-bit integer.
672 @param aVal On return, contains the extracted integer.
674 @return KErrNone if successful.
675 KErrGeneral if the next character position is initially at the end of the string
676 or no valid characters found initially.
677 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
678 If error codes KErrGeneral or KErrOverflow are returned, the object's
679 members are left unaltered.
684 TInt r=BoundedVal(v,0x7fu);
693 EXPORT_C TInt TLex8::Val(TInt16 &aVal)
695 Parses the string to extract a signed 16-bit integer.
697 @param aVal On return, contains the extracted integer.
699 @return KErrNone if successful.
700 KErrGeneral if the next character position is initially at the end of the string
701 or no valid characters found initially.
702 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
703 If error codes KErrGeneral or KErrOverflow are returned, the object's
704 members are left unaltered.
709 TInt r=BoundedVal(v,0x7fffu);
718 EXPORT_C TInt TLex8::Val(TInt32 &aVal)
720 Parses the string to extract a signed 32-bit integer.
722 @param aVal On return, contains the extracted integer.
724 @return KErrNone if successful.
725 KErrGeneral if the next character position is initially at the end of the string
726 or no valid characters found initially.
727 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
728 If error codes KErrGeneral or KErrOverflow are returned, the object's
729 members are left unaltered.
734 TInt r=BoundedVal(v,0x7fffffffu);
742 EXPORT_C TInt TLex8::Val(TInt64& aVal)
744 Parses the string to extract a signed 64-bit integer.
746 @param aVal On return, contains the extracted integer.
748 @return KErrNone if successful.
749 KErrGeneral if the next character position is initially at the end of the string
750 or no valid characters found initially.
751 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
752 If error codes KErrGeneral or KErrOverflow are returned, the object's
753 members are left unaltered.
758 if (c=='-' || c=='+')
763 r=BoundedVal(aVal, EDecimal, UI64LIT(0x8000000000000000));
768 r=BoundedVal(aVal, UI64LIT(0x7fffffffffffffff));
775 EXPORT_C TInt TLex8::Val(TUint8 &aVal,TRadix aRadix)
777 Parses the string to extract an 8-bit unsigned integer, using the
780 The specified radix is one of binary, octal, decimal, or hexadecimal.
782 @param aVal On return, contains the extracted integer.
783 @param aRadix The radix to use when converting the number.
785 @return KErrNone if successful.
786 KErrGeneral if the next character position is initially at the end of the string
787 or no valid characters found initially.
788 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
789 If error codes KErrGeneral or KErrOverflow are returned, the object's
790 members are left unaltered.
795 TInt r=BoundedVal(v,aRadix,0xffu);
801 EXPORT_C TInt TLex8::Val(TUint16 &aVal,TRadix aRadix)
803 Parses the string to extract a 16-bit unsigned integer, using the
806 The specified radix is one of binary, octal, decimal, or hexadecimal.
808 @param aVal On return, contains the extracted integer.
809 @param aRadix The radix to use when converting the number.
811 @return KErrNone if successful.
812 KErrGeneral if the next character position is initially at the end of the string
813 or no valid characters found initially.
814 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
815 If error codes KErrGeneral or KErrOverflow are returned, the object's
816 members are left unaltered.
821 TInt r=BoundedVal(v,aRadix,0xffffu);
827 EXPORT_C TInt TLex8::Val(TUint32 &aVal,TRadix aRadix)
829 Parses the string to extract a 32-bit unsigned integer, using the
832 The specified radix is one of binary, octal, decimal, or hexadecimal.
834 @param aVal On return, contains the extracted integer.
835 @param aRadix The radix to use when converting the number.
837 @return KErrNone if successful.
838 KErrGeneral if the next character position is initially at the end of the string
839 or no valid characters found initially.
840 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
841 If error codes KErrGeneral or KErrOverflow are returned, the object's
842 members are left unaltered.
847 TInt r=BoundedVal(v,aRadix,0xffffffffu);
853 EXPORT_C TInt TLex8::Val(TInt64& aVal, TRadix aRadix)
855 Parses the string to extract a 64-bit integer (treated as unsigned), using the
858 The specified radix is one of binary, octal, decimal, or hexadecimal.
860 @param aVal On return, contains the extracted integer.
861 @param aRadix The radix to use when converting the number.
863 @return KErrNone if successful.
864 KErrGeneral if the next character position is initially at the end of the string
865 or no valid characters found initially.
866 KErrOverflow if there is sign overflow, i.e. converted value greater than limit.
867 If error codes KErrGeneral or KErrOverflow are returned, the object's
868 members are left unaltered.
872 return BoundedVal(aVal,aRadix,TInt64(UI64LIT(0xffffffffffffffff)));
875 void TLex8::ValidateMark(const TLexMark8 aMark) const
877 // Asserts that the mark is valid for this lex
880 __ASSERT_ALWAYS(aMark.iPtr>=iBuf && aMark.iPtr<=iEnd,Panic(ETLex8MarkOutOfRange));