|
1 // Copyright (c) 2005-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 "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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <e32base.h> |
|
17 #include "StringParser.h" |
|
18 |
|
19 |
|
20 CStringParser::CStringParser ( const TDesC8& aBuffer ) |
|
21 : iBuffer ( aBuffer ), |
|
22 iCurrentPos ( 0 ) |
|
23 { |
|
24 } |
|
25 |
|
26 CStringParser::~CStringParser () |
|
27 { |
|
28 } |
|
29 |
|
30 CStringParser* CStringParser::NewLC ( const TDesC8& aBuffer ) |
|
31 { |
|
32 CStringParser* self = new ( ELeave ) CStringParser ( aBuffer ); |
|
33 CleanupStack::PushL ( self ); |
|
34 return self; |
|
35 } |
|
36 |
|
37 CStringParser* CStringParser::NewL ( const TDesC8& aBuffer ) |
|
38 { |
|
39 CStringParser* self = CStringParser::NewLC ( aBuffer ); |
|
40 CleanupStack::Pop ( self ); |
|
41 |
|
42 return self; |
|
43 } |
|
44 |
|
45 /** |
|
46 Get the next character from the current position, and increments the |
|
47 current position by 1. |
|
48 |
|
49 @param aChar [out] Next character to be read |
|
50 |
|
51 @return TBool ETrue if the current position is less than the string length |
|
52 else returns EFalse |
|
53 */ |
|
54 TBool CStringParser::GetNextCharacter ( TChar& aChar ) |
|
55 { |
|
56 IncrementCurrentPos (); |
|
57 return ( GetCurrentCharacter ( aChar ) ); |
|
58 } |
|
59 |
|
60 /** |
|
61 Get the character from the current position. |
|
62 |
|
63 @param aChar [out] Current character to be read |
|
64 |
|
65 @return TBool ETrue if the current position is less than the string length |
|
66 else returns EFalse |
|
67 */ |
|
68 TBool CStringParser::GetCurrentCharacter ( TChar& aChar ) |
|
69 { |
|
70 if ( iCurrentPos >= iBuffer.Length () ) |
|
71 { |
|
72 return EFalse; |
|
73 } |
|
74 |
|
75 aChar = iBuffer[ iCurrentPos ]; |
|
76 return ETrue; |
|
77 } |
|
78 |
|
79 /** |
|
80 Parses the string till it finds the aEndChar. |
|
81 |
|
82 @param TChar [in] Character to search for. |
|
83 @see GetNextWorrd |
|
84 */ |
|
85 TBool CStringParser::ParseTill ( TPtrC8& aWord, TChar aEndChar ) |
|
86 { |
|
87 TPtrC8 cur( iBuffer.Mid( iCurrentPos ) ); |
|
88 TBool found = ETrue; |
|
89 TInt pos = cur.Locate( aEndChar ); |
|
90 if ( pos < 0 ) |
|
91 { |
|
92 // Unable to locate the aEndChar. Sets the end position to |
|
93 // the length of the string. |
|
94 pos = iBuffer.Length (); |
|
95 found = EFalse; |
|
96 } |
|
97 |
|
98 aWord.Set ( cur.Left (pos) ); |
|
99 SkipLength ( pos ); |
|
100 return found; |
|
101 } |
|
102 |
|
103 /** |
|
104 Increments the current parsing position by aLen. |
|
105 |
|
106 @param TInt [in] Number of character to skip. |
|
107 |
|
108 @return TBool ETrue if the current parsing position is less than the buffer length |
|
109 else returns EFalse. |
|
110 */ |
|
111 TBool CStringParser::SkipLength ( TInt aLen ) |
|
112 { |
|
113 iCurrentPos += aLen; |
|
114 |
|
115 if ( iCurrentPos < iBuffer.Length () ) |
|
116 { |
|
117 return ETrue; |
|
118 } |
|
119 return EFalse; |
|
120 } |
|
121 |
|
122 /** |
|
123 Finds the remainder string from the current parsing position to the buffer length. |
|
124 |
|
125 @param TPtrC8& [out] Points to the current starting position till end |
|
126 |
|
127 @return TBool ETrue if the current parsing position is less than the buffer length |
|
128 else returns EFalse. |
|
129 */ |
|
130 void CStringParser::GetRemainder ( TPtrC8& aBuffer ) |
|
131 { |
|
132 aBuffer.Set ( iBuffer.Mid ( iCurrentPos ) ); |
|
133 SkipLength ( aBuffer.Length () ); |
|
134 } |
|
135 |
|
136 /** |
|
137 Increments the current parsing position by 1. |
|
138 */ |
|
139 void CStringParser::IncrementCurrentPos ( ) |
|
140 { |
|
141 ++iCurrentPos; |
|
142 } |
|
143 |
|
144 /** |
|
145 Parses till the first occurrence of any one of a set of characters in aCharSet, |
|
146 from the current parsing position. |
|
147 |
|
148 @param TPtrC8& [out] Points to the current starting position. |
|
149 @param const TDesC8& [in] Descriptor containing characters for matching. |
|
150 */ |
|
151 void CStringParser::ParseTill ( TPtrC8& aWord, const TDesC8& aCharSet ) |
|
152 { |
|
153 TPtrC8 cur( iBuffer.Mid( iCurrentPos ) ); |
|
154 |
|
155 TInt lowpos = cur.Length (); |
|
156 TInt pos = KErrNotFound; |
|
157 |
|
158 for ( TInt i = 0; i < aCharSet.Length(); ++i ) |
|
159 { |
|
160 pos = cur.Locate ( aCharSet [ i ] ); |
|
161 |
|
162 if ( (pos != KErrNotFound) && (pos < lowpos ) ) |
|
163 { |
|
164 lowpos = pos; |
|
165 } |
|
166 } |
|
167 |
|
168 aWord.Set ( cur.Left ( lowpos ) ); |
|
169 SkipLength ( lowpos ); |
|
170 } |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |