|
1 /* |
|
2 * Copyright (c) 1998-2009 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 the License "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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <s32file.h> |
|
20 #include "t_input.h" |
|
21 #include "t_errorconverter.h" |
|
22 |
|
23 const TUint KMaxTagLength = 80; |
|
24 |
|
25 EXPORT_C TPtrC8 Input::ParseElement(const TDesC8& aBuf, const TDesC8& aStart, const TDesC8& aEnd) |
|
26 { |
|
27 TInt pos = 0; |
|
28 return Input::ParseElement(aBuf, aStart, aEnd, pos); |
|
29 } |
|
30 |
|
31 EXPORT_C TPtrC8 Input::ParseElement(const TDesC8& aBuf, const TDesC8& aStart) |
|
32 { |
|
33 TInt pos = 0; |
|
34 TBuf8<KMaxTagLength> end; |
|
35 end.Copy(aStart); |
|
36 end.Insert(1, _L8("/")); |
|
37 return Input::ParseElement(aBuf, aStart, end, pos); |
|
38 } |
|
39 |
|
40 EXPORT_C TPtrC8 Input::ParseElement(const TDesC8& aBuf, |
|
41 const TDesC8& aStart, |
|
42 const TDesC8& aEnd, |
|
43 TInt& aPos) |
|
44 { |
|
45 TInt err = KErrNone; |
|
46 return Input::ParseElement(aBuf, aStart, aEnd, aPos, err); |
|
47 } |
|
48 |
|
49 |
|
50 // Find the portion of aBuf between aStart and aEnd, allowing for nesting |
|
51 EXPORT_C TPtrC8 Input::ParseElement(const TDesC8& aBuf, |
|
52 const TDesC8& aStart, |
|
53 const TDesC8& aEnd, |
|
54 TInt& aPos, |
|
55 TInt& aError) |
|
56 { |
|
57 aError = KErrNone; |
|
58 TInt startPos = KErrNotFound; |
|
59 TInt level = 0; |
|
60 do |
|
61 { |
|
62 TPtrC8 data = aBuf.Mid(aPos); |
|
63 |
|
64 // Find the first start or end tag |
|
65 TInt nextStart = data.Find(aStart); |
|
66 TInt nextEnd = data.Find(aEnd); |
|
67 |
|
68 if (nextStart == KErrNotFound && nextEnd == KErrNotFound) |
|
69 { |
|
70 // None found |
|
71 aError = KErrNotFound; |
|
72 return TPtrC8(); |
|
73 } |
|
74 else if (nextEnd == KErrNotFound || (nextStart != KErrNotFound && nextStart < nextEnd)) |
|
75 { |
|
76 // Start tag found first |
|
77 ++level; |
|
78 aPos += nextStart + aStart.Length(); |
|
79 |
|
80 if (startPos == KErrNotFound) |
|
81 { |
|
82 // Record position of first start tag |
|
83 startPos = aPos; |
|
84 } |
|
85 } |
|
86 else |
|
87 { |
|
88 // End tag found first |
|
89 --level; |
|
90 aPos += nextEnd + aEnd.Length(); |
|
91 } |
|
92 } |
|
93 while (level > 0); |
|
94 |
|
95 if (level == -1) |
|
96 { |
|
97 // End tag found before start tag |
|
98 aError = KErrArgument; |
|
99 return TPtrC8(); |
|
100 } |
|
101 |
|
102 ASSERT(startPos != KErrNotFound); |
|
103 return aBuf.Mid(startPos, aPos - startPos - aEnd.Length()); |
|
104 } |
|
105 |
|
106 EXPORT_C TPtrC16 Input::ParseElement(const TDesC16& aBuf, const TDesC16& aStart, const TDesC16& aEnd) |
|
107 { |
|
108 TInt pos = 0; |
|
109 return Input::ParseElement(aBuf, aStart, aEnd, pos); |
|
110 } |
|
111 |
|
112 |
|
113 EXPORT_C TPtrC16 Input::ParseElement(const TDesC16& aBuf, |
|
114 const TDesC16& aStart, |
|
115 const TDesC16& aEnd, |
|
116 TInt& aPos) |
|
117 { |
|
118 TInt err = KErrNone; |
|
119 return Input::ParseElement(aBuf, aStart, aEnd, aPos, err); |
|
120 } |
|
121 |
|
122 |
|
123 EXPORT_C TPtrC16 Input::ParseElement(const TDesC16& aBuf, |
|
124 const TDesC16& aStart, |
|
125 const TDesC16& aEnd, |
|
126 TInt& aPos, |
|
127 TInt& aError) |
|
128 { |
|
129 aError = KErrNone; |
|
130 const TInt length = aBuf.Length(); |
|
131 TPtrC16 temp1 = aBuf.Right(length - aPos); |
|
132 TInt elementStart = temp1.Find(aStart); |
|
133 if (elementStart < 0) |
|
134 { |
|
135 aError = elementStart; |
|
136 TPtrC16 res(KNullDesC16); |
|
137 return res; |
|
138 } |
|
139 elementStart+= aStart.Length(); |
|
140 aPos+=elementStart; |
|
141 |
|
142 TPtrC16 temp2 = temp1.Right(temp1.Length() - elementStart); |
|
143 TInt elementEnd = temp2.Find(aEnd); |
|
144 if (elementEnd < 0) |
|
145 { |
|
146 TPtrC16 res(KNullDesC16); |
|
147 return res; |
|
148 } |
|
149 |
|
150 aPos+=elementEnd; |
|
151 aPos+=aEnd.Length(); |
|
152 |
|
153 TPtrC16 element = temp2.Left(elementEnd); |
|
154 return element; |
|
155 } |
|
156 |
|
157 EXPORT_C HBufC8* Input::ParseElementHexL(const TDesC8& aBuf, const TDesC8& aStart) |
|
158 { |
|
159 TPtrC8 data = Input::ParseElement(aBuf, aStart); |
|
160 __ASSERT_ALWAYS(data.Size() % 2 == 0, User::Panic(_L("ParseElementHexL"), 1)); |
|
161 TInt bytes = data.Size()/2; |
|
162 HBufC8* buf = HBufC8::NewLC(bytes); |
|
163 TPtr8 ptr = buf->Des(); |
|
164 ptr.SetLength(bytes); |
|
165 |
|
166 if(bytes) |
|
167 { |
|
168 for (TInt i = 0 ; i < data.Length() ; i += 2) |
|
169 { |
|
170 TUint8 tmp; |
|
171 tmp=(TUint8)(data[i]-(data[i]>'9'?('A'-10):'0')); |
|
172 tmp*=16; |
|
173 tmp|=(TUint8)(data[i+1]-(data[i+1]>'9'?('A'-10):'0')); |
|
174 ptr[i/2] = tmp; |
|
175 } |
|
176 } |
|
177 CleanupStack::Pop(buf); |
|
178 return buf; |
|
179 } |
|
180 |
|
181 EXPORT_C TBool Input::ParseElementBoolL(const TDesC8& aBuf, const TDesC8& aStart) |
|
182 { |
|
183 TPtrC8 value = Input::ParseElement(aBuf, aStart); |
|
184 if( value.CompareF(_L8("ETrue")) == 0 || value.CompareF(_L8("True")) == 0 ) |
|
185 { |
|
186 return ETrue; |
|
187 } |
|
188 else if( value.CompareF(_L8("EFalse")) == 0 || value.CompareF(_L8("False")) == 0 ) |
|
189 { |
|
190 return EFalse; |
|
191 } |
|
192 //if it's neither false nor true, leave |
|
193 User::Leave(KErrArgument); |
|
194 return EFalse; |
|
195 } |
|
196 |
|
197 |
|
198 EXPORT_C TBool Input::GetExpectedResultL(const TDesC& aResult /* in */, TInt &aError /* out */) |
|
199 { |
|
200 TBool result; |
|
201 CErrorConverter *errorConverter = CErrorConverter::NewLC(); |
|
202 |
|
203 result = errorConverter->GetExpectedResultL(aResult, aError); |
|
204 CleanupStack::PopAndDestroy(errorConverter); |
|
205 |
|
206 return(result); |
|
207 } |
|
208 |
|
209 |
|
210 EXPORT_C TBool Input::GetExpectedResultL(const TInt &aError /* in */, HBufC*& aResult /* out */) |
|
211 { |
|
212 TBool result; |
|
213 CErrorConverter *errorConverter = CErrorConverter::NewLC(); |
|
214 |
|
215 result = errorConverter->GetExpectedResultL(aError, aResult); |
|
216 CleanupStack::PopAndDestroy(errorConverter); |
|
217 |
|
218 return(result); |
|
219 } |
|
220 |
|
221 // Allocate memory on the heap and copy in the value of a tagged element |
|
222 EXPORT_C void Input::ParseElementL(HBufC*& aMember, |
|
223 const TDesC& aBuf, |
|
224 const TDesC& aStart) |
|
225 { |
|
226 TInt pos = 0; |
|
227 ParseElementL(aMember, aBuf, aStart, pos); |
|
228 }; |
|
229 |
|
230 // Allocate memory on the heap and copy in the value of a tagged element |
|
231 EXPORT_C void Input::ParseElementL(HBufC8*& aMember, |
|
232 const TDesC& aBuf, |
|
233 const TDesC& aStart) |
|
234 { |
|
235 TInt pos = 0; |
|
236 ParseElementL(aMember, aBuf, aStart, pos); |
|
237 }; |
|
238 |
|
239 // Allocate memory on the heap and copy in the value of a tagged element |
|
240 EXPORT_C void Input::ParseElementL(HBufC*& aMember, |
|
241 const TDesC& aBuf, |
|
242 const TDesC& aStart, |
|
243 TInt& aPos) |
|
244 { |
|
245 TInt error = KErrNone; |
|
246 ParseElementL(aMember, aBuf, aStart, aPos, error); |
|
247 }; |
|
248 |
|
249 // Allocate memory on the heap and copy in the value of a tagged element |
|
250 EXPORT_C void Input::ParseElementL(HBufC8*& aMember, |
|
251 const TDesC& aBuf, |
|
252 const TDesC& aStart, |
|
253 TInt& aPos) |
|
254 { |
|
255 TInt error = KErrNone; |
|
256 ParseElementL(aMember, aBuf, aStart, aPos, error); |
|
257 }; |
|
258 |
|
259 // Allocate memory on the heap and copy in the value of a tagged element |
|
260 EXPORT_C void Input::ParseElementL(HBufC*& aMember, |
|
261 const TDesC& aBuf, |
|
262 const TDesC& aStart, |
|
263 TInt& aPos, |
|
264 TInt& aError) |
|
265 { |
|
266 __ASSERT_DEBUG(!aMember, User::Panic(_L("Input"), 1)); |
|
267 |
|
268 |
|
269 TPtrC value = ParseElement(aBuf, aStart, aPos, aError); |
|
270 // copy file name to unicode buffer |
|
271 aMember = HBufC::NewL(value.Length()); |
|
272 aMember->Des().Copy(value); |
|
273 } |
|
274 |
|
275 // Allocate memory on the heap and copy in the value of a tagged element |
|
276 EXPORT_C void Input::ParseElementL(HBufC8*& aMember, |
|
277 const TDesC& aBuf, |
|
278 const TDesC& aStart, |
|
279 TInt& aPos, |
|
280 TInt& aError) |
|
281 { |
|
282 __ASSERT_DEBUG(!aMember, User::Panic(_L("Input"), 1)); |
|
283 |
|
284 |
|
285 TPtrC value = ParseElement(aBuf, aStart, aPos, aError); |
|
286 // copy file name to narrow buffer |
|
287 aMember = HBufC8::NewL(value.Length()); |
|
288 aMember->Des().Copy(value); |
|
289 } |
|
290 |
|
291 // Allocate memory on the heap and copy in the value of a tagged element |
|
292 EXPORT_C TPtrC Input::ParseElement(const TDesC& aBuf, const TDesC& aStart) |
|
293 { |
|
294 TInt pos = 0; |
|
295 return(ParseElement(aBuf, aStart, pos)); |
|
296 }; |
|
297 |
|
298 |
|
299 // Allocate memory on the heap and copy in the value of a tagged element |
|
300 EXPORT_C TPtrC Input::ParseElement(const TDesC& aBuf, const TDesC& aStart, TInt& aPos) |
|
301 { |
|
302 TInt error = KErrNone; |
|
303 return(ParseElement(aBuf, aStart, aPos, error)); |
|
304 }; |
|
305 |
|
306 |
|
307 // Allocate memory on the heap and copy in the value of a tagged element |
|
308 EXPORT_C TPtrC Input::ParseElement(const TDesC& aBuf, const TDesC& aStart, TInt& aPos, TInt& aError) |
|
309 { |
|
310 // using the start tag <abcd> create a matching tag terminator <\abcd> |
|
311 HBufC* hend = HBufC::NewMaxLC(aStart.Length()+1); |
|
312 |
|
313 TPtr end = hend->Des(); |
|
314 end = aStart; // copy in start tag |
|
315 end.Insert(1,_L("/")); // make it into a terminator tag with same element name |
|
316 // get value from element |
|
317 TPtrC value = Input::ParseElement(aBuf, aStart, end, aPos, aError); |
|
318 |
|
319 CleanupStack::PopAndDestroy(hend); |
|
320 return(value); |
|
321 } |
|
322 |
|
323 EXPORT_C HBufC8* Input::ReadFileL(const TDesC& aFilename, RFs& aFs) |
|
324 { |
|
325 HBufC8* ret = ReadFileLC(aFilename, aFs); |
|
326 CleanupStack::Pop(); |
|
327 return ret; |
|
328 } |
|
329 |
|
330 EXPORT_C HBufC8* Input::ReadFileL(const TDesC& aFilename, const TDesC& aPath, RFs& aFs) |
|
331 { |
|
332 HBufC8* ret = ReadFileLC(aFilename, aPath, aFs); |
|
333 CleanupStack::Pop(); |
|
334 return ret; |
|
335 } |
|
336 |
|
337 EXPORT_C HBufC8* Input::ReadFileLC(const TDesC& aFilename, RFs& aFs) |
|
338 { |
|
339 RFile file; |
|
340 User::LeaveIfError(file.Open(aFs, aFilename, EFileRead)); |
|
341 CleanupClosePushL(file); |
|
342 TInt size; |
|
343 file.Size(size); |
|
344 CleanupStack::PopAndDestroy();//fileClose |
|
345 |
|
346 HBufC8* res = HBufC8::NewLC(size); |
|
347 TPtr8 p(res->Des()); |
|
348 p.SetLength(size); |
|
349 |
|
350 RFileReadStream stream; |
|
351 User::LeaveIfError(stream.Open(aFs, aFilename, EFileStream)); |
|
352 CleanupClosePushL(stream); |
|
353 stream.ReadL(p, size); |
|
354 CleanupStack::PopAndDestroy();//streamClose |
|
355 return res; |
|
356 } |
|
357 |
|
358 EXPORT_C HBufC8* Input::ReadFileLC(const TDesC& aFilename, const TDesC& aPath, RFs& aFS) |
|
359 { |
|
360 TFileName fullname; |
|
361 fullname.Append(aPath); |
|
362 fullname.Append(aFilename); |
|
363 |
|
364 RFile file; |
|
365 User::LeaveIfError(file.Open(aFS, fullname, EFileRead)); |
|
366 CleanupClosePushL(file); |
|
367 TInt size; |
|
368 file.Size(size); |
|
369 CleanupStack::PopAndDestroy();//fileClose |
|
370 |
|
371 HBufC8* res = HBufC8::NewLC(size); |
|
372 TPtr8 p(res->Des()); |
|
373 p.SetLength(size); |
|
374 |
|
375 RFileReadStream stream; |
|
376 User::LeaveIfError(stream.Open(aFS, fullname, EFileStream)); |
|
377 CleanupClosePushL(stream); |
|
378 stream.ReadL(p, size); |
|
379 CleanupStack::PopAndDestroy();//streamClose |
|
380 return res; |
|
381 } |
|
382 |
|
383 EXPORT_C void Input::ParseElementListL(const TDesC& aBuf, |
|
384 const TDesC& aStart, |
|
385 const TDesC& aEnd, |
|
386 RPointerArray<HBufC>& aElements, |
|
387 TInt& aPos) |
|
388 { |
|
389 TPtrC currentElement = ParseElement(aBuf, aStart, aEnd, aPos); |
|
390 while (currentElement != KNullDesC) |
|
391 { |
|
392 HBufC* newElement = HBufC::NewL(currentElement.Size()); |
|
393 *newElement = currentElement; |
|
394 User::LeaveIfError(aElements.Append(newElement)); |
|
395 currentElement.Set(ParseElement(aBuf, aStart, aEnd, aPos)); |
|
396 } |
|
397 } |
|
398 |
|
399 EXPORT_C TUint Input::ParseIntElement(const TDesC8& aBuf, const TDesC8& aStart, const TDesC8& aEnd) |
|
400 { |
|
401 TInt pos = 0; |
|
402 return Input::ParseIntElement(aBuf, aStart, aEnd, pos); |
|
403 } |
|
404 |
|
405 |
|
406 EXPORT_C TUint Input::ParseIntElement(const TDesC8& aBuf, |
|
407 const TDesC8& aStart, |
|
408 const TDesC8& aEnd, |
|
409 TInt& aPos) |
|
410 { |
|
411 TInt err = KErrNone; |
|
412 return Input::ParseIntElement(aBuf, aStart, aEnd, aPos, err); |
|
413 } |
|
414 |
|
415 |
|
416 EXPORT_C TUint Input::ParseIntElement(const TDesC8& aBuf, |
|
417 const TDesC8& aStart, |
|
418 const TDesC8& aEnd, |
|
419 TInt& aPos, |
|
420 TInt& aError) |
|
421 { |
|
422 aError = KErrNone; |
|
423 TPtrC8 temp = ParseElement(aBuf, aStart, aEnd, aPos, aError); |
|
424 |
|
425 TUint result = 0; |
|
426 |
|
427 if(aError == KErrNone) |
|
428 { |
|
429 TLex8 lex; |
|
430 if(temp.Left(2) == _L8("0x")) |
|
431 { |
|
432 lex = temp.Mid(2); |
|
433 aError = lex.Val(result, EHex); |
|
434 } |
|
435 else |
|
436 { |
|
437 lex = temp; |
|
438 aError = lex.Val(result); |
|
439 }; |
|
440 |
|
441 if(aError == KErrNone) |
|
442 { |
|
443 if(!lex.Eos()) |
|
444 { |
|
445 aError = KErrArgument; |
|
446 result = 0; |
|
447 }; |
|
448 }; |
|
449 }; |
|
450 return(result); |
|
451 } |
|
452 |
|
453 EXPORT_C TUint Input::ParseIntElement(const TDesC16& aBuf, const TDesC16& aStart, const TDesC16& aEnd) |
|
454 { |
|
455 TInt pos = 0; |
|
456 return Input::ParseIntElement(aBuf, aStart, aEnd, pos); |
|
457 } |
|
458 |
|
459 |
|
460 EXPORT_C TUint Input::ParseIntElement(const TDesC16& aBuf, |
|
461 const TDesC16& aStart, |
|
462 const TDesC16& aEnd, |
|
463 TInt& aPos) |
|
464 { |
|
465 TInt err = KErrNone; |
|
466 return Input::ParseIntElement(aBuf, aStart, aEnd, aPos, err); |
|
467 } |
|
468 |
|
469 |
|
470 EXPORT_C TUint Input::ParseIntElement(const TDesC16& aBuf, |
|
471 const TDesC16& aStart, |
|
472 const TDesC16& aEnd, |
|
473 TInt& aPos, |
|
474 TInt& aError) |
|
475 { |
|
476 aError = KErrNone; |
|
477 TPtrC16 temp = ParseElement(aBuf, aStart, aEnd, aPos, aError); |
|
478 |
|
479 TUint result = 0; |
|
480 |
|
481 if(aError == KErrNone) |
|
482 { |
|
483 TLex16 lex; |
|
484 if(temp.Left(2) == _L16("0x")) |
|
485 { |
|
486 lex = temp.Mid(2); |
|
487 aError = lex.Val(result, EHex); |
|
488 } |
|
489 else |
|
490 { |
|
491 lex = temp; |
|
492 aError = lex.Val(result); |
|
493 }; |
|
494 |
|
495 if(aError == KErrNone) |
|
496 { |
|
497 if(!lex.Eos()) |
|
498 { |
|
499 aError = KErrArgument; |
|
500 result = 0; |
|
501 }; |
|
502 }; |
|
503 }; |
|
504 return(result); |
|
505 } |
|
506 |
|
507 |
|
508 // Allocate memory on the heap and copy in the value of a tagged element |
|
509 EXPORT_C TUint Input::ParseIntElement(const TDesC& aBuf, const TDesC& aStart) |
|
510 { |
|
511 TInt pos = 0; |
|
512 return(ParseIntElement(aBuf, aStart, pos)); |
|
513 }; |
|
514 |
|
515 |
|
516 // Allocate memory on the heap and copy in the value of a tagged element |
|
517 EXPORT_C TUint Input::ParseIntElement(const TDesC& aBuf, const TDesC& aStart, TInt& aPos) |
|
518 { |
|
519 TInt error = KErrNone; |
|
520 return(ParseIntElement(aBuf, aStart, aPos, error)); |
|
521 }; |
|
522 |
|
523 |
|
524 // Allocate memory on the heap and copy in the value of a tagged element |
|
525 EXPORT_C TUint Input::ParseIntElement(const TDesC& aBuf, const TDesC& aStart, TInt& aPos, TInt& aError) |
|
526 { |
|
527 // get value from element |
|
528 TPtrC temp = Input::ParseElement(aBuf, aStart, aPos, aError); |
|
529 TUint result = 0; |
|
530 |
|
531 if(aError == KErrNone) |
|
532 { |
|
533 TLex16 lex; |
|
534 if(temp.Left(2) == _L16("0x")) |
|
535 { |
|
536 lex = temp.Mid(2); |
|
537 aError = lex.Val(result, EHex); |
|
538 } |
|
539 else |
|
540 { |
|
541 lex = temp; |
|
542 aError = lex.Val(result); |
|
543 }; |
|
544 |
|
545 if(aError == KErrNone) |
|
546 { |
|
547 if(!lex.Eos()) |
|
548 { |
|
549 aError = KErrArgument; |
|
550 result = 0; |
|
551 }; |
|
552 }; |
|
553 }; |
|
554 |
|
555 return(result); |
|
556 } |