|
1 /* |
|
2 * Copyright (c) 2005 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: Collection of string parsing methods. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "caistrparser.h" |
|
20 #include "aistrcnv.h" |
|
21 |
|
22 // CONSTANTS |
|
23 |
|
24 const TInt KThreeOctets = 3; |
|
25 const TInt KTwoOctets = 2; |
|
26 |
|
27 namespace |
|
28 { |
|
29 |
|
30 _LIT8( KHexPrefix8, "0x" ); |
|
31 _LIT16( KHexPrefix16, "0x" ); |
|
32 |
|
33 const TInt KMaxOneOctetValue = 0x007F; |
|
34 const TInt KMaxTwoOctetValue = 0x07FF; |
|
35 |
|
36 TInt Utf8OctetCount( const TDesC16& aString ) |
|
37 { |
|
38 TInt numOctets(0); |
|
39 TInt length(aString.Length()); |
|
40 const TUint16* stringData = aString.Ptr(); |
|
41 |
|
42 for ( TInt i = 0; i < length; ++i) |
|
43 { |
|
44 if (stringData[i] > KMaxOneOctetValue) |
|
45 { |
|
46 if (stringData[i] > KMaxTwoOctetValue) |
|
47 { |
|
48 numOctets += KThreeOctets; |
|
49 } |
|
50 else |
|
51 { |
|
52 numOctets += KTwoOctets; |
|
53 } |
|
54 } |
|
55 else |
|
56 { |
|
57 ++numOctets; |
|
58 } |
|
59 } |
|
60 |
|
61 return numOctets; |
|
62 } |
|
63 |
|
64 TPtr8 EnsureBufMaxLengthL(HBufC8*& aBuffer, TInt aMaxLength ) |
|
65 { |
|
66 if ( !aBuffer ) |
|
67 { |
|
68 aBuffer = HBufC8::NewL( aMaxLength ); |
|
69 } |
|
70 else if ( aBuffer->Des().MaxLength() < aMaxLength ) |
|
71 { |
|
72 aBuffer = aBuffer->ReAllocL( aMaxLength ); |
|
73 } |
|
74 |
|
75 return aBuffer->Des(); |
|
76 } |
|
77 |
|
78 } |
|
79 |
|
80 namespace AiUtility |
|
81 { |
|
82 |
|
83 template< class DescType, class LexType > inline |
|
84 TInt ParseIntImpl |
|
85 ( TInt32& aValue, const DescType& aStringValue, |
|
86 const DescType& aHexPrefix ) |
|
87 { |
|
88 const TInt pos = aStringValue.FindF( aHexPrefix ); |
|
89 if (pos != KErrNotFound) |
|
90 { |
|
91 LexType lex( aStringValue.Mid( pos + aHexPrefix.Length() ) ); |
|
92 // Hex parsing needs unsigned int |
|
93 TUint32 value = 0; |
|
94 const TInt parseResult = lex.Val( value, EHex ); |
|
95 if ( parseResult == KErrNone ) |
|
96 { |
|
97 aValue = value; |
|
98 } |
|
99 return parseResult; |
|
100 } |
|
101 else |
|
102 { |
|
103 LexType lex( aStringValue ); |
|
104 return lex.Val(aValue); |
|
105 } |
|
106 } |
|
107 |
|
108 EXPORT_C TInt ParseInt( TInt32& aResult, const TDesC8& aSourceString ) |
|
109 { |
|
110 return ParseIntImpl< TDesC8, TLex8 > |
|
111 ( aResult, aSourceString, KHexPrefix8 ); |
|
112 } |
|
113 |
|
114 EXPORT_C TInt ParseInt( TInt32& aResult, const TDesC16& aSourceString ) |
|
115 { |
|
116 return ParseIntImpl< TDesC16, TLex16 > |
|
117 ( aResult, aSourceString, KHexPrefix16 ); |
|
118 } |
|
119 |
|
120 EXPORT_C HBufC16* CopyToBufferL(HBufC16* aTargetBuffer, const TDesC16& aSourceText) |
|
121 { |
|
122 EnsureBufMaxLengthL( aTargetBuffer, aSourceText.Length() ).Copy( aSourceText ); |
|
123 |
|
124 return aTargetBuffer; |
|
125 } |
|
126 |
|
127 EXPORT_C HBufC16* CopyToBufferL(HBufC16* aTargetBuffer, const TDesC8& aSourceText) |
|
128 { |
|
129 if (!aTargetBuffer) |
|
130 { |
|
131 return CnvUtfConverter::ConvertToUnicodeFromUtf8L( aSourceText ); |
|
132 } |
|
133 |
|
134 TPtr16 des( EnsureBufMaxLengthL( aTargetBuffer, aSourceText.Length() ) ); |
|
135 |
|
136 CnvUtfConverter::ConvertToUnicodeFromUtf8( des, aSourceText ); |
|
137 |
|
138 return aTargetBuffer; |
|
139 } |
|
140 |
|
141 EXPORT_C HBufC8* CopyToBufferL(HBufC8* aTargetBuffer, const TDesC8& aSourceText) |
|
142 { |
|
143 ::EnsureBufMaxLengthL( aTargetBuffer, aSourceText.Length() ).Copy( aSourceText ); |
|
144 return aTargetBuffer; |
|
145 } |
|
146 |
|
147 EXPORT_C HBufC8* CopyToBufferL(HBufC8* aTargetBuffer, const TDesC16& aSourceText) |
|
148 { |
|
149 if ( !aTargetBuffer ) |
|
150 { |
|
151 return CnvUtfConverter::ConvertFromUnicodeToUtf8L( aSourceText ); |
|
152 } |
|
153 |
|
154 TInt length = Utf8OctetCount( aSourceText ); |
|
155 |
|
156 TPtr8 des( ::EnsureBufMaxLengthL( aTargetBuffer, length ) ); |
|
157 CnvUtfConverter::ConvertFromUnicodeToUtf8( des, aSourceText ); |
|
158 return aTargetBuffer; |
|
159 } |
|
160 |
|
161 EXPORT_C TPtr16 EnsureBufMaxLengthL(HBufC16*& aTargetBuffer, TInt aMaxLength ) |
|
162 { |
|
163 if ( !aTargetBuffer ) |
|
164 { |
|
165 aTargetBuffer = HBufC16::NewL( aMaxLength ); |
|
166 } |
|
167 else |
|
168 { |
|
169 if ( aTargetBuffer->Des().MaxLength() < aMaxLength ) |
|
170 { |
|
171 aTargetBuffer = aTargetBuffer->ReAllocL( aMaxLength ); |
|
172 } |
|
173 } |
|
174 |
|
175 return aTargetBuffer->Des(); |
|
176 } |
|
177 |
|
178 } |
|
179 |
|
180 |
|
181 // --------------------------------------------------------------------------- |
|
182 // CStrParser() |
|
183 // --------------------------------------------------------------------------- |
|
184 // |
|
185 CStrParser::CStrParser() |
|
186 { |
|
187 } |
|
188 |
|
189 // --------------------------------------------------------------------------- |
|
190 // CStrParser* NewL() |
|
191 // --------------------------------------------------------------------------- |
|
192 // |
|
193 CStrParser* CStrParser::NewL() |
|
194 { |
|
195 CStrParser* self = new ( ELeave ) CStrParser(); |
|
196 CleanupStack::PushL( self ); |
|
197 self->ConstructL(); |
|
198 CleanupStack::Pop( self ); |
|
199 return self; |
|
200 } |
|
201 |
|
202 // --------------------------------------------------------------------------- |
|
203 // void ConstructL() |
|
204 // --------------------------------------------------------------------------- |
|
205 // |
|
206 void CStrParser::ConstructL() |
|
207 { |
|
208 } |
|
209 |
|
210 // --------------------------------------------------------------------------- |
|
211 // void Release() |
|
212 // --------------------------------------------------------------------------- |
|
213 // |
|
214 void CStrParser::Release() |
|
215 { |
|
216 delete this; |
|
217 } |
|
218 |
|
219 // --------------------------------------------------------------------------- |
|
220 // TInt ParseInt( TInt32& aValue, const TDesC8& aStringValue ) |
|
221 // --------------------------------------------------------------------------- |
|
222 // |
|
223 TInt CStrParser::ParseInt( TInt32& aResult, const TDesC8& aSourceString ) |
|
224 { |
|
225 return AiUtility::ParseInt( aResult, aSourceString ); |
|
226 } |
|
227 |
|
228 // --------------------------------------------------------------------------- |
|
229 // HBufC16* CopyToBufferL( HBufC16* aTargetBuffer, const TDesC16& aSourceText ) |
|
230 // --------------------------------------------------------------------------- |
|
231 // |
|
232 HBufC16* CStrParser::CopyToBufferL( HBufC16* aTargetBuffer, const TDesC16& aSourceText ) |
|
233 { |
|
234 return AiUtility::CopyToBufferL( aTargetBuffer, aSourceText ); |
|
235 } |
|
236 |
|
237 // --------------------------------------------------------------------------- |
|
238 // HBufC16* CopyToBufferL( HBufC16* aTargetBuffer, const TDesC8& aSourceText ) |
|
239 // --------------------------------------------------------------------------- |
|
240 // |
|
241 HBufC16* CStrParser::CopyToBufferL( HBufC16* aTargetBuffer, const TDesC8& aSourceText ) |
|
242 { |
|
243 return AiUtility::CopyToBufferL( aTargetBuffer, aSourceText ); |
|
244 } |
|
245 |
|
246 // --------------------------------------------------------------------------- |
|
247 // HBufC8* CopyToBufferL( HBufC8* aTargetBuffer, const TDesC8& aSourceText ) |
|
248 // --------------------------------------------------------------------------- |
|
249 // |
|
250 HBufC8* CStrParser::CopyToBufferL( HBufC8* aTargetBuffer, const TDesC8& aSourceText ) |
|
251 { |
|
252 return AiUtility::CopyToBufferL( aTargetBuffer, aSourceText ); |
|
253 } |
|
254 |
|
255 // --------------------------------------------------------------------------- |
|
256 // HBufC8* CopyToBufferL( HBufC8* aTargetBuffer, const TDesC16& aSourceText ) |
|
257 // --------------------------------------------------------------------------- |
|
258 // |
|
259 HBufC8* CStrParser::CopyToBufferL( HBufC8* aTargetBuffer, const TDesC16& aSourceText ) |
|
260 { |
|
261 return AiUtility::CopyToBufferL( aTargetBuffer, aSourceText ); |
|
262 } |
|
263 |
|
264 // End of File. |
|
265 |
|
266 |