|
1 /* |
|
2 * Copyright (c) 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 "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: This module contains implementation of CStifParser |
|
15 * class member functions. |
|
16 * |
|
17 */ |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <e32std.h> |
|
21 #include "StifParser.h" |
|
22 #include "ParserTracing.h" |
|
23 #include "StifFileParser.h" |
|
24 |
|
25 // EXTERNAL DATA STRUCTURES |
|
26 // None |
|
27 |
|
28 // EXTERNAL FUNCTION PROTOTYPES |
|
29 // None |
|
30 |
|
31 // CONSTANTS |
|
32 // None |
|
33 |
|
34 // MACROS |
|
35 // None |
|
36 |
|
37 // LOCAL CONSTANTS AND MACROS |
|
38 // None |
|
39 |
|
40 // MODULE DATA STRUCTURES |
|
41 // None |
|
42 |
|
43 // LOCAL FUNCTION PROTOTYPES |
|
44 // None |
|
45 |
|
46 // FORWARD DECLARATIONS |
|
47 // None |
|
48 |
|
49 // ==================== LOCAL FUNCTIONS ======================================= |
|
50 // None |
|
51 |
|
52 // ================= MEMBER FUNCTIONS ========================================= |
|
53 |
|
54 /* |
|
55 ------------------------------------------------------------------------------- |
|
56 |
|
57 Class: CStifParser |
|
58 |
|
59 Method: CStifParser |
|
60 |
|
61 Description: Default constructor |
|
62 |
|
63 C++ default constructor can NOT contain any code, that |
|
64 might leave. |
|
65 |
|
66 Parameters: TCommentType aCommentType: in: Comment type's indication |
|
67 |
|
68 Return Values: None |
|
69 |
|
70 Errors/Exceptions: None |
|
71 |
|
72 Status: Approved |
|
73 |
|
74 ------------------------------------------------------------------------------- |
|
75 */ |
|
76 CStifParser::CStifParser( TCommentType aCommentType ) : |
|
77 iBuffer( 0, 0 ) |
|
78 { |
|
79 iCommentType = aCommentType; |
|
80 |
|
81 iParsingMode = EFileParsing; |
|
82 |
|
83 iIsUnicode = EFalse; |
|
84 |
|
85 iFileParser = NULL; |
|
86 } |
|
87 |
|
88 /* |
|
89 ------------------------------------------------------------------------------- |
|
90 |
|
91 Class: CStifParser |
|
92 |
|
93 Method: ConstructL |
|
94 |
|
95 Description: Symbian OS second phase constructor |
|
96 |
|
97 Symbian OS default constructor can leave. |
|
98 |
|
99 Connecting and opening configuration file if path and file information is |
|
100 given. If path and file information is not given and information is given |
|
101 in buffer then create parser according to the buffer. |
|
102 |
|
103 Parameters: const TDesC& aPath: in: Source path definition |
|
104 const TDesC& aConfig: in: Configuration filename |
|
105 const TDesC& aBuffer: in: Buffer of the parsed information |
|
106 |
|
107 Return Values: None |
|
108 |
|
109 Errors/Exceptions: Leaves if called Connect method fails |
|
110 Leaves if called SetSessionPath method fails |
|
111 Leaves if called Open method fails |
|
112 Leaves if HBufC::NewL operation leaves |
|
113 |
|
114 Status: Proposal |
|
115 |
|
116 ------------------------------------------------------------------------------- |
|
117 */ |
|
118 void CStifParser::ConstructL( const TDesC& aPath, |
|
119 const TDesC& aConfig, |
|
120 const TDesC& aBuffer) |
|
121 { |
|
122 if( aPath == KNullDesC && aConfig == KNullDesC && aBuffer != KNullDesC ) |
|
123 { |
|
124 // Set mode |
|
125 iParsingMode = EBufferParsing; |
|
126 |
|
127 // Construct modifiable heap-based descriptor. |
|
128 iBufferTmp = HBufC::NewL( aBuffer.Length() ); |
|
129 //iBuffer = iBufferTmp->Des(); |
|
130 iBuffer.Set( iBufferTmp->Des() ); |
|
131 // Copy content |
|
132 iBuffer.Copy( aBuffer ); |
|
133 } |
|
134 |
|
135 else |
|
136 { |
|
137 User::LeaveIfError( iFileServer.Connect() ); |
|
138 |
|
139 __TRACE( KInfo, ( _L( "STIFPARSER: Open configfile '%S%S'" ), |
|
140 &aPath, &aConfig ) ); |
|
141 |
|
142 User::LeaveIfError( iFileServer.SetSessionPath( aPath ) ); |
|
143 User::LeaveIfError( iFile.Open( |
|
144 iFileServer, aConfig, EFileRead | EFileShareAny ) ); |
|
145 |
|
146 //Check whether the file is unicoded |
|
147 __TRACE(KInfo, (_L("STIFPARSER: Check if the file is unicode"))); |
|
148 _LIT(KUnicode, "#UNICODE"); |
|
149 const TInt KUnicodeLength(8 * 2 + 2); //times two, because we want to read unicode string using 8bit descriptor |
|
150 //two characters more because on some systems FEFF is always added on the beginning of unicode file |
|
151 TInt size(0); |
|
152 |
|
153 User::LeaveIfError(iFile.Size(size)); |
|
154 |
|
155 if(size >= KUnicodeLength) |
|
156 { |
|
157 TBuf8<KUnicodeLength> buf; |
|
158 |
|
159 User::LeaveIfError(iFile.Read(0, buf)); |
|
160 TPtrC16 bufuni((TUint16 *)(buf.Ptr()), buf.Length() / 2); |
|
161 if(bufuni.Find(KUnicode) != KErrNotFound) |
|
162 { |
|
163 iIsUnicode = ETrue; |
|
164 __TRACE(KInfo, (_L("STIFPARSER: File is unicode"))); |
|
165 } |
|
166 } |
|
167 |
|
168 //Create file parser object |
|
169 iFileParser = CStifFileParser::NewL(iFileServer, iFile, iIsUnicode, iCommentType); |
|
170 } |
|
171 |
|
172 iOffset = 0; |
|
173 |
|
174 } |
|
175 |
|
176 /* |
|
177 ------------------------------------------------------------------------------- |
|
178 |
|
179 Class: CStifParser |
|
180 |
|
181 Method: NewL |
|
182 |
|
183 Description: Two-phased constructor. |
|
184 |
|
185 Starting creating parser with path and file information. |
|
186 |
|
187 Parameters: const TDesC& aPath: in: Source path definition |
|
188 const TDesC& aConfig: in: Configuration filename |
|
189 TCommentType aCommentType: in: Comment type's indication |
|
190 |
|
191 Return Values: CStifParser* : pointer to CStifParser object |
|
192 |
|
193 Errors/Exceptions: Leaves if ConstructL leaves |
|
194 |
|
195 Status: Proposal |
|
196 |
|
197 ------------------------------------------------------------------------------- |
|
198 */ |
|
199 EXPORT_C CStifParser* CStifParser::NewL( const TDesC& aPath, |
|
200 const TDesC& aConfig, |
|
201 TCommentType aCommentType ) |
|
202 { |
|
203 __TRACE( KInfo, ( _L( "STIFPARSER: Debug information is used" ) ) ); |
|
204 |
|
205 // Create CStifParser object |
|
206 CStifParser* parser = new (ELeave) CStifParser( aCommentType ); |
|
207 |
|
208 CleanupStack::PushL( parser ); |
|
209 parser->ConstructL( aPath, aConfig ); |
|
210 CleanupStack::Pop( parser ); |
|
211 |
|
212 return parser; |
|
213 |
|
214 } |
|
215 |
|
216 /* |
|
217 ------------------------------------------------------------------------------- |
|
218 |
|
219 Class: CStifParser |
|
220 |
|
221 Method: NewL |
|
222 |
|
223 Description: Two-phased constructor. |
|
224 |
|
225 Starting creating parser with buffer information. |
|
226 |
|
227 Parameters: const TDesC& aBuffer: in: Buffer of the parsed informations |
|
228 TCommentType aCommentType: in: Comment type's indication |
|
229 |
|
230 Return Values: CStifParser* : pointer to CStifParser object |
|
231 |
|
232 Errors/Exceptions: Leaves if ConstructL leaves |
|
233 |
|
234 Status: Proposal |
|
235 |
|
236 ------------------------------------------------------------------------------- |
|
237 */ |
|
238 EXPORT_C CStifParser* CStifParser::NewL( const TDesC& aBuffer, |
|
239 TCommentType aCommentType ) |
|
240 { |
|
241 __TRACE( KInfo, ( _L( "STIFPARSER: Debug information is used" ) ) ); |
|
242 |
|
243 // Create CStifParser object |
|
244 CStifParser* parser = new (ELeave) CStifParser( aCommentType ); |
|
245 |
|
246 CleanupStack::PushL( parser ); |
|
247 // No path and file name informations. Buffer is given |
|
248 parser->ConstructL( KNullDesC, KNullDesC, aBuffer ); |
|
249 CleanupStack::Pop( parser ); |
|
250 |
|
251 return parser; |
|
252 |
|
253 } |
|
254 |
|
255 /* |
|
256 ------------------------------------------------------------------------------- |
|
257 |
|
258 Class: CStifParser |
|
259 |
|
260 Method: ~CStifParser |
|
261 |
|
262 Description: Destructor |
|
263 |
|
264 Close file and the fileserver handles. |
|
265 |
|
266 Parameters: None |
|
267 |
|
268 Return Values: None |
|
269 |
|
270 Errors/Exceptions: None |
|
271 |
|
272 Status: Proposal |
|
273 |
|
274 ------------------------------------------------------------------------------- |
|
275 */ |
|
276 EXPORT_C CStifParser::~CStifParser() |
|
277 { |
|
278 |
|
279 if( iParsingMode == EBufferParsing ) |
|
280 { |
|
281 delete iBufferTmp; |
|
282 } |
|
283 else |
|
284 { |
|
285 delete iFileParser; |
|
286 iFile.Close(); |
|
287 iFileServer.Close(); |
|
288 } |
|
289 |
|
290 } |
|
291 |
|
292 /* |
|
293 ------------------------------------------------------------------------------- |
|
294 |
|
295 Class: CStifParser |
|
296 |
|
297 Method: SectionL |
|
298 |
|
299 Description: Parses sections from configuration files. |
|
300 |
|
301 Open and read configuration source and parses a required section. |
|
302 If start tag is empty the parsing starts beginning of the configuration |
|
303 file. If end tag is empty the parsing goes end of configuration file. |
|
304 This method starts always from beginning of configuration file and parses |
|
305 first section if aSeeked parameters is not given. |
|
306 If configuration file includes several sections with both start and end |
|
307 tags so aSeeked parameter seeks the required section. The aSeeked |
|
308 parameters indicates section that will be parsed. |
|
309 |
|
310 Parameters: const TDesC& aStartTag: in: Indicates a start tag for parsing |
|
311 const TDesC& aEndTag: in: Indicates an end tag for parsing |
|
312 TInt aSeeked: in: a seeked section which will be parsed |
|
313 |
|
314 Return Values: See NextSectionL() method |
|
315 |
|
316 Errors/Exceptions: See NextSectionL() method |
|
317 |
|
318 Status: Approved |
|
319 |
|
320 ------------------------------------------------------------------------------- |
|
321 */ |
|
322 EXPORT_C CStifSectionParser* CStifParser::SectionL( const TDesC& aStartTag, |
|
323 const TDesC& aEndTag, |
|
324 TInt aSeeked ) |
|
325 { |
|
326 iOffset = 0; |
|
327 return NextSectionL( aStartTag, aEndTag, aSeeked ,ETrue); |
|
328 |
|
329 } |
|
330 |
|
331 /* |
|
332 ------------------------------------------------------------------------------- |
|
333 |
|
334 Class: CStifParser |
|
335 |
|
336 Method: NextSectionL |
|
337 |
|
338 Description: Parses sections from configuration files. |
|
339 |
|
340 Open and read configuration source and parses a required section. |
|
341 If start tag is empty the parsing starts beginning of the configuration |
|
342 file. If end tag is empty the parsing goes end of configuration file. |
|
343 This method will parse next section after the earlier section if aSeeked |
|
344 parameter is not given. |
|
345 If configuration file includes several sections with both start and end |
|
346 tags so aSeeked parameter seeks the required section. The aSeeked |
|
347 parameters indicates section that will be parsed. |
|
348 |
|
349 Parameters: const TDesC& aStartTag: in: Indicates a start tag for parsing |
|
350 const TDesC& aEndTag: in: Indicates an end tag for parsing |
|
351 TInt aSeeked: in: a seeked section which will be parsed |
|
352 |
|
353 Return Values: CStifSectionParser* : pointer to CStifSectionParser object |
|
354 NULL will return if NextSectionFileL (or NextSectionMemoryL) returns NULL |
|
355 |
|
356 Errors/Exceptions: Leaves if NextSectionFileL leaves |
|
357 Leaves if NextSectionMemoryL leaves |
|
358 |
|
359 Status: Proposal |
|
360 |
|
361 ------------------------------------------------------------------------------- |
|
362 */ |
|
363 EXPORT_C CStifSectionParser* CStifParser::NextSectionL( const TDesC& aStartTag, |
|
364 const TDesC& aEndTag, |
|
365 TInt aSeeked ) |
|
366 { |
|
367 //If parsing mode is set to file, we parse directly in the file |
|
368 if(iParsingMode == EFileParsing) |
|
369 { |
|
370 return NextSectionFileL(aStartTag, aEndTag, aSeeked,ETrue ); |
|
371 } |
|
372 |
|
373 //If parsing mode is set to buffer, process in old way |
|
374 return NextSectionMemoryL(aStartTag, aEndTag, aSeeked,ETrue ); |
|
375 } |
|
376 /* |
|
377 ------------------------------------------------------------------------------- |
|
378 |
|
379 Class: CStifParser |
|
380 |
|
381 Method: SectionL |
|
382 |
|
383 Description: Parses sections from configuration files. |
|
384 |
|
385 Open and read configuration source and parses a required section. |
|
386 If start tag is empty the parsing starts beginning of the configuration |
|
387 file. If end tag is empty the parsing goes end of configuration file. |
|
388 This method starts always from beginning of configuration file and parses |
|
389 first section if aSeeked parameters is not given. |
|
390 If configuration file includes several sections with both start and end |
|
391 tags so aSeeked parameter seeks the required section. The aSeeked |
|
392 parameters indicates section that will be parsed. |
|
393 |
|
394 Parameters: const TDesC& aStartTag: in: Indicates a start tag for parsing |
|
395 const TDesC& aEndTag: in: Indicates an end tag for parsing |
|
396 TInt aSeeked: in: a seeked section which will be parsed |
|
397 |
|
398 Return Values: See NextSectionL() method |
|
399 |
|
400 Errors/Exceptions: See NextSectionL() method |
|
401 |
|
402 Status: Approved |
|
403 |
|
404 ------------------------------------------------------------------------------- |
|
405 */ |
|
406 EXPORT_C CStifSectionParser* CStifParser::SectionL( const TDesC& aStartTag, |
|
407 const TDesC& aEndTag, |
|
408 TInt aSeeked,TBool aIsHasEndTag ) |
|
409 { |
|
410 iOffset = 0; |
|
411 return NextSectionL( aStartTag, aEndTag, aSeeked ,aIsHasEndTag); |
|
412 |
|
413 } |
|
414 |
|
415 /* |
|
416 ------------------------------------------------------------------------------- |
|
417 |
|
418 Class: CStifParser |
|
419 |
|
420 Method: NextSectionL |
|
421 |
|
422 Description: Parses sections from configuration files. |
|
423 |
|
424 Open and read configuration source and parses a required section. |
|
425 If start tag is empty the parsing starts beginning of the configuration |
|
426 file. If end tag is empty the parsing goes end of configuration file. |
|
427 This method will parse next section after the earlier section if aSeeked |
|
428 parameter is not given. |
|
429 If configuration file includes several sections with both start and end |
|
430 tags so aSeeked parameter seeks the required section. The aSeeked |
|
431 parameters indicates section that will be parsed. |
|
432 |
|
433 Parameters: const TDesC& aStartTag: in: Indicates a start tag for parsing |
|
434 const TDesC& aEndTag: in: Indicates an end tag for parsing |
|
435 TInt aSeeked: in: a seeked section which will be parsed |
|
436 |
|
437 Return Values: CStifSectionParser* : pointer to CStifSectionParser object |
|
438 NULL will return if NextSectionFileL (or NextSectionMemoryL) returns NULL |
|
439 |
|
440 Errors/Exceptions: Leaves if NextSectionFileL leaves |
|
441 Leaves if NextSectionMemoryL leaves |
|
442 |
|
443 Status: Proposal |
|
444 |
|
445 ------------------------------------------------------------------------------- |
|
446 */ |
|
447 EXPORT_C CStifSectionParser* CStifParser::NextSectionL( const TDesC& aStartTag, |
|
448 const TDesC& aEndTag, |
|
449 TInt aSeeked,TBool aIsHasEndTag ) |
|
450 { |
|
451 //If parsing mode is set to file, we parse directly in the file |
|
452 if(iParsingMode == EFileParsing) |
|
453 { |
|
454 return NextSectionFileL(aStartTag, aEndTag, aSeeked,aIsHasEndTag ); |
|
455 } |
|
456 |
|
457 //If parsing mode is set to buffer, process in old way |
|
458 return NextSectionMemoryL(aStartTag, aEndTag, aSeeked,aIsHasEndTag ); |
|
459 } |
|
460 |
|
461 /* |
|
462 ------------------------------------------------------------------------------- |
|
463 |
|
464 Class: CStifParser |
|
465 |
|
466 Method: NextSectionMemoryL |
|
467 |
|
468 Description: Parses sections from configuration files. |
|
469 |
|
470 Open and read configuration source and parses a required section. |
|
471 If start tag is empty the parsing starts beginning of the configuration |
|
472 file. If end tag is empty the parsing goes end of configuration file. |
|
473 This method will parse next section after the earlier section if aSeeked |
|
474 parameter is not given. |
|
475 If configuration file includes several sections with both start and end |
|
476 tags so aSeeked parameter seeks the required section. The aSeeked |
|
477 parameters indicates section that will be parsed. |
|
478 |
|
479 Parameters: const TDesC& aStartTag: in: Indicates a start tag for parsing |
|
480 const TDesC& aEndTag: in: Indicates an end tag for parsing |
|
481 TInt aSeeked: in: a seeked section which will be parsed |
|
482 |
|
483 Return Values: CStifSectionParser* : pointer to CStifSectionParser object |
|
484 NULL will return if file size or aSeeked is not positive |
|
485 NULL will return if start tag is not found |
|
486 NULL will return if end tag is not found |
|
487 NULL will return if parsed section length is not positive |
|
488 |
|
489 Errors/Exceptions: Leaves if called Size method fails |
|
490 Leaves if HBufC::NewLC method leaves |
|
491 Leaves if called Read method fails |
|
492 Leaves if CStifSectionParser::NewL methods leaves |
|
493 |
|
494 Status: Proposal |
|
495 |
|
496 ------------------------------------------------------------------------------- |
|
497 */ |
|
498 CStifSectionParser* CStifParser::NextSectionMemoryL( const TDesC& aStartTag, |
|
499 const TDesC& aEndTag, |
|
500 TInt aSeeked, TBool /*aIsHasEndTag*/ ) |
|
501 { |
|
502 TInt size( 0 ); |
|
503 // Parser is created straight with data |
|
504 if( iParsingMode == EBufferParsing ) |
|
505 { |
|
506 size = iBuffer.Length(); |
|
507 } |
|
508 // Parser is created with path and file informations |
|
509 else |
|
510 { |
|
511 User::LeaveIfError( iFile.Size( size ) ); |
|
512 } |
|
513 |
|
514 // size or aSeeked cannot be 0 or negetive |
|
515 if( size <= 0 || aSeeked <= 0) |
|
516 { |
|
517 __TRACE( |
|
518 KInfo, ( _L( "STIFPARSER: NextSectionL method returns a NULL" ) ) ); |
|
519 return NULL; |
|
520 } |
|
521 |
|
522 const TInt tmpSize = 128;//--UNICODE-- KMaxName; // 128 - set to even value, because KMaxName may change in the future |
|
523 TInt offset( 0 ); // Offset value to parts reading |
|
524 |
|
525 // Construct modifiable heap-based descriptor. tmp to CleanupStack |
|
526 HBufC* tmp = HBufC::NewLC( size ); |
|
527 TPtr wholeSection = tmp->Des(); |
|
528 |
|
529 // Construct modifiable heap-based descriptor. tmp2 to CleanupStack |
|
530 HBufC8* tmp2 = HBufC8::NewLC( tmpSize ); // 128 |
|
531 TPtr8 buf = tmp2->Des(); |
|
532 |
|
533 // Construct modifiable heap-based descriptor. tmp3 to CleanupStack |
|
534 HBufC* tmp3 = HBufC::NewLC( tmpSize ); // 128 |
|
535 TPtr currentSection = tmp3->Des(); |
|
536 |
|
537 // Parser is created straight with data |
|
538 if( iParsingMode == EBufferParsing ) |
|
539 { |
|
540 // If 8 bit copy changes to 16 |
|
541 wholeSection.Copy( iBuffer ); |
|
542 } |
|
543 // Parser is created with path and file informations |
|
544 else |
|
545 { |
|
546 TPtrC16 currentSectionUnicode; |
|
547 do // Read data in parts(Maximum part size is KMaxName) |
|
548 { |
|
549 // Read data |
|
550 User::LeaveIfError( iFile.Read( offset, buf, tmpSize ) ); |
|
551 |
|
552 // If file is unicode convert differently |
|
553 if(iIsUnicode) |
|
554 { |
|
555 // 8 bit to 16 with unicode conversion - simply point to byte array as to double-byte array |
|
556 currentSectionUnicode.Set((TUint16 *)(buf.Ptr()), buf.Length() / 2); |
|
557 // Appends current section to whole section |
|
558 wholeSection.Append( currentSectionUnicode ); |
|
559 } |
|
560 else |
|
561 { |
|
562 // 8 bit to 16 |
|
563 currentSection.Copy( buf ); |
|
564 // Appends current section to whole section |
|
565 wholeSection.Append( currentSection ); |
|
566 } |
|
567 |
|
568 offset += tmpSize; |
|
569 |
|
570 } while( offset < size ); |
|
571 } |
|
572 |
|
573 CleanupStack::PopAndDestroy( tmp3 ); |
|
574 CleanupStack::PopAndDestroy( tmp2 ); |
|
575 |
|
576 // User wants section without c-style comments |
|
577 if( iCommentType == ECStyleComments ) |
|
578 { |
|
579 ParseCommentsOff( wholeSection ); |
|
580 } |
|
581 |
|
582 TLex lex( wholeSection ); |
|
583 lex.SkipAndMark( iOffset ); |
|
584 |
|
585 // For the required section length and positions |
|
586 TInt length( 0 ); |
|
587 TInt lengthStartPos( 0 ); |
|
588 TInt lengthEndPos( 0 ); |
|
589 TBool eos( EFalse ); |
|
590 TInt tagCount( 1 ); |
|
591 |
|
592 // Check is aStartTag given |
|
593 if ( aStartTag.Length() == 0 ) |
|
594 { |
|
595 // Skip line break, tabs, spaces etc. |
|
596 lex.SkipSpace(); |
|
597 lengthStartPos = lex.Offset(); |
|
598 } |
|
599 else |
|
600 { |
|
601 // While end of section |
|
602 while ( !lex.Eos() ) |
|
603 { |
|
604 TPtrC ptr = lex.NextToken(); |
|
605 // Start of the section is found and correct section |
|
606 if ( ptr == aStartTag && tagCount == aSeeked ) |
|
607 { |
|
608 lengthStartPos = lex.Offset(); |
|
609 break; |
|
610 } |
|
611 // Start tag is found but not correct section |
|
612 else if ( ptr == aStartTag ) |
|
613 { |
|
614 tagCount++; |
|
615 } |
|
616 } |
|
617 } |
|
618 |
|
619 // If we are end of section lex.Eos() and eos will be ETrue |
|
620 eos = lex.Eos(); |
|
621 |
|
622 // Seeked section is not found |
|
623 if ( tagCount != aSeeked ) |
|
624 { |
|
625 __TRACE( KInfo, ( _L( |
|
626 "STIFPARSER: NextSectionL method: Seeked section is not found" ) ) ); |
|
627 CleanupStack::PopAndDestroy( tmp ); |
|
628 User::Leave( KErrNotFound ); |
|
629 } |
|
630 |
|
631 // Check is aEndTag given |
|
632 if ( aEndTag.Length() == 0 ) |
|
633 { |
|
634 lengthEndPos = wholeSection.Length(); |
|
635 } |
|
636 else |
|
637 { |
|
638 // While end of section |
|
639 while ( !lex.Eos() ) |
|
640 { |
|
641 TPtrC ptr = lex.NextToken(); |
|
642 // End tag of the section is found |
|
643 if ( ptr == aEndTag ) |
|
644 { |
|
645 lengthEndPos = lex.Offset(); |
|
646 // Because Offset() position is after the aEndTag |
|
647 lengthEndPos -= aEndTag.Length(); |
|
648 break; |
|
649 } |
|
650 } |
|
651 } |
|
652 |
|
653 // If we are end of section and lengthEndPos is 0 |
|
654 if ( lengthEndPos == 0 ) |
|
655 { |
|
656 // lex.Eos() and eos will be ETrue |
|
657 eos = lex.Eos(); |
|
658 } |
|
659 |
|
660 // The length includes spaces and end of lines |
|
661 length = ( lengthEndPos - lengthStartPos ); |
|
662 |
|
663 CStifSectionParser* section = NULL; |
|
664 |
|
665 // If eos is true or length is negative |
|
666 if ( eos || length <= 0 ) |
|
667 { |
|
668 __TRACE( |
|
669 KInfo, ( _L( "STIFPARSER: NextSectionL method returns a NULL" ) ) ); |
|
670 } |
|
671 else |
|
672 { |
|
673 // Make CStifSectionParser object and alloc required length |
|
674 section = CStifSectionParser::NewL( length ); |
|
675 CleanupStack::PushL( section ); |
|
676 |
|
677 // Copy required data to the section object |
|
678 section->SetData( wholeSection, lengthStartPos, length ); |
|
679 |
|
680 //iOffset += lengthEndPos + aEndTag.Length(); |
|
681 iOffset = lex.Offset(); |
|
682 |
|
683 CleanupStack::Pop( section ); |
|
684 } |
|
685 CleanupStack::PopAndDestroy( tmp ); |
|
686 |
|
687 return section; |
|
688 |
|
689 } |
|
690 |
|
691 /* |
|
692 ------------------------------------------------------------------------------- |
|
693 |
|
694 Class: CStifParser |
|
695 |
|
696 Method: NextSectionFileL |
|
697 |
|
698 Description: Parses sections from configuration files. |
|
699 |
|
700 Open and read configuration source and parses a required section. |
|
701 If start tag is empty the parsing starts beginning of the configuration |
|
702 file. If end tag is empty the parsing goes end of configuration file. |
|
703 This method will parse next section after the earlier section if aSeeked |
|
704 parameter is not given. |
|
705 If configuration file includes several sections with both start and end |
|
706 tags so aSeeked parameter seeks the required section. The aSeeked |
|
707 parameters indicates section that will be parsed. |
|
708 |
|
709 Parameters: const TDesC& aStartTag: in: Indicates a start tag for parsing |
|
710 const TDesC& aEndTag: in: Indicates an end tag for parsing |
|
711 TInt aSeeked: in: a seeked section which will be parsed |
|
712 |
|
713 Return Values: CStifSectionParser* : pointer to CStifSectionParser object |
|
714 NULL will return if file size or aSeeked is not positive |
|
715 NULL will return if start tag is not found |
|
716 NULL will return if end tag is not found |
|
717 NULL will return if parsed section length is not positive |
|
718 |
|
719 Errors/Exceptions: Leaves if called Size method fails |
|
720 Leaves if HBufC::NewLC method leaves |
|
721 Leaves if called Read method fails |
|
722 Leaves if CStifSectionParser::NewL methods leaves |
|
723 |
|
724 Status: Proposal |
|
725 |
|
726 ------------------------------------------------------------------------------- |
|
727 */ |
|
728 CStifSectionParser* CStifParser::NextSectionFileL( const TDesC& aStartTag, |
|
729 const TDesC& aEndTag, |
|
730 TInt aSeeked ,TBool aIsHasEndTag ) |
|
731 { |
|
732 HBufC *bufSection = iFileParser->NextSectionL(aStartTag, aEndTag, iOffset, aSeeked,aIsHasEndTag); |
|
733 |
|
734 if(bufSection) |
|
735 { |
|
736 CleanupStack::PushL(bufSection); |
|
737 TPtr bufSectionPtr(bufSection->Des()); |
|
738 |
|
739 if(iCommentType == ECStyleComments) |
|
740 { |
|
741 ParseCommentsOff(bufSectionPtr); |
|
742 } |
|
743 |
|
744 // Make CStifSectionParser object and alloc required length |
|
745 CStifSectionParser* section = CStifSectionParser::NewL(bufSection->Length()); |
|
746 CleanupStack::PushL(section); |
|
747 |
|
748 // Copy required data to the section object |
|
749 section->SetData(bufSectionPtr, 0, bufSection->Length()); |
|
750 |
|
751 // Clean |
|
752 CleanupStack::Pop(section); |
|
753 CleanupStack::PopAndDestroy(bufSection); |
|
754 |
|
755 return section; |
|
756 } |
|
757 |
|
758 return NULL; |
|
759 } |
|
760 |
|
761 /* |
|
762 ------------------------------------------------------------------------------- |
|
763 |
|
764 Class: CStifParser |
|
765 |
|
766 Method: ParseCommentsOff |
|
767 |
|
768 Description: Convert a section without comments. |
|
769 |
|
770 Parameters: TPtr& aBuf: inout: section to parsed |
|
771 |
|
772 Return Values: None |
|
773 |
|
774 Errors/Exceptions: None |
|
775 |
|
776 Status: Proposal |
|
777 |
|
778 ------------------------------------------------------------------------------- |
|
779 */ |
|
780 void CStifParser::ParseCommentsOff( TPtr& aBuf ) |
|
781 { |
|
782 TInt startPos( 0 ); |
|
783 TInt endPos( 0 ); |
|
784 TInt length( 0 ); |
|
785 enum TSearchType |
|
786 { |
|
787 ENormalSearch, // Search a '//' or a '/*' |
|
788 ECStyleSlashs, // Search is '//' |
|
789 ECStyleSlashAndAsterisk,// Search is '/*' |
|
790 EDoRemove, // Remove comment |
|
791 }; |
|
792 |
|
793 TSearchType searchType( ENormalSearch ); |
|
794 |
|
795 TLex lex( aBuf ); |
|
796 |
|
797 // Remove comments |
|
798 do |
|
799 { |
|
800 switch( searchType ) |
|
801 { |
|
802 case ENormalSearch: |
|
803 { |
|
804 if( lex.Get() == '/' ) |
|
805 { |
|
806 // Peek next character( '/' ) |
|
807 if( lex.Peek() == '/' ) |
|
808 { |
|
809 startPos = lex.Offset(); |
|
810 startPos--; |
|
811 lex.Inc(); |
|
812 searchType = ECStyleSlashs; |
|
813 } |
|
814 // Peek next character( '*' ) |
|
815 else if( lex.Peek() == '*' ) |
|
816 { |
|
817 startPos = lex.Offset(); |
|
818 startPos--; |
|
819 lex.Inc(); |
|
820 searchType = ECStyleSlashAndAsterisk; |
|
821 } |
|
822 } |
|
823 break; |
|
824 } |
|
825 case ECStyleSlashs: |
|
826 { |
|
827 // Peek next character(10 or '\n' in UNIX style ) |
|
828 if( lex.Peek() == 0x0A ) |
|
829 { |
|
830 // Don't remove line break!!( Else this fails: |
|
831 // 1st line:"this is parsed text 1" |
|
832 // 2nd line:"this is parsed text 2 // this is comments" |
|
833 // 1st and 2nd lines will be together and following |
|
834 // operations may fail) |
|
835 endPos = lex.Offset(); |
|
836 searchType = EDoRemove; |
|
837 break; |
|
838 } |
|
839 |
|
840 // Peek next character(13 or '\r' in Symbian OS) |
|
841 if ( lex.Peek() == 0x0D ) |
|
842 { |
|
843 // Increment the lex position |
|
844 lex.Inc(); |
|
845 // Peek next character(10 or '\n' in Symbian OS) |
|
846 if ( lex.Peek() == 0x0A ) |
|
847 { |
|
848 // Don't remove line break!!( Else this fails: |
|
849 // 1st line:"this is parsed text 1" |
|
850 // 2nd line:"this is parsed text 2 // this is comments" |
|
851 // 1st and 2nd lines will be together and following |
|
852 // operations may fail) |
|
853 endPos = lex.Offset(); |
|
854 endPos = endPos - 1; // Two line break characters |
|
855 searchType = EDoRemove; |
|
856 break; |
|
857 } |
|
858 // 0x0A not found, decrement position |
|
859 lex.UnGet(); |
|
860 } |
|
861 // Increment the lex position |
|
862 lex.Inc(); |
|
863 // Take current end position |
|
864 endPos = lex.Offset(); |
|
865 break; |
|
866 } |
|
867 |
|
868 case ECStyleSlashAndAsterisk: |
|
869 { |
|
870 // Peek next character( '*' ) |
|
871 if ( lex.Peek() == '*' ) |
|
872 { |
|
873 // Increment the lex position |
|
874 lex.Inc(); |
|
875 // Peek next character( '/') |
|
876 if ( lex.Peek() == '/' ) |
|
877 { |
|
878 // End of the section is found and increment the lex position |
|
879 lex.Inc(); |
|
880 endPos = lex.Offset(); |
|
881 searchType = EDoRemove; |
|
882 break; |
|
883 } |
|
884 // '/' not found, decrement position |
|
885 lex.UnGet(); |
|
886 } |
|
887 // Increment the lex position |
|
888 lex.Inc(); |
|
889 |
|
890 // Take current end position |
|
891 endPos = lex.Offset(); |
|
892 break; |
|
893 } |
|
894 default: |
|
895 { |
|
896 searchType = ENormalSearch; |
|
897 break; |
|
898 } |
|
899 |
|
900 } // End of switch |
|
901 |
|
902 // Remove comment |
|
903 if( searchType == EDoRemove ) |
|
904 { |
|
905 length = endPos - startPos; |
|
906 aBuf.Delete( startPos, length ); |
|
907 lex = aBuf; |
|
908 searchType = ENormalSearch; |
|
909 } |
|
910 |
|
911 } while ( !lex.Eos() ); |
|
912 |
|
913 // If comment is started and configure file ends to eof we remove |
|
914 // comments althougt there are no end of line or '*/' characters |
|
915 if( searchType == ECStyleSlashs || searchType == ECStyleSlashs ) |
|
916 { |
|
917 length = lex.Offset() - startPos; |
|
918 aBuf.Delete( startPos, length ); |
|
919 } |
|
920 |
|
921 HandleSpecialMarks( aBuf ); |
|
922 |
|
923 } |
|
924 |
|
925 // |
|
926 //----------------------------------------------------------------------------- |
|
927 // |
|
928 // Class: CStifParser |
|
929 // |
|
930 // Method: HandleSpecialMarks |
|
931 // |
|
932 // Description: Handles special marks.( '\/\/', '\/\*' and '*/\/' ). This |
|
933 // is used when ECStyleComments comment type is used. |
|
934 // |
|
935 // Parameters: TPtr& aBuf: inout: section to parsed |
|
936 // |
|
937 // Return Values: None |
|
938 // |
|
939 // Errors/Exceptions: None |
|
940 // |
|
941 // Status: Proposal |
|
942 // |
|
943 //----------------------------------------------------------------------------- |
|
944 // |
|
945 void CStifParser::HandleSpecialMarks( TPtr& aBuf ) |
|
946 { |
|
947 TLex lex( aBuf ); |
|
948 TInt firstPos( 0 ); |
|
949 TInt secondPos( 0 ); |
|
950 |
|
951 // // => \/\/ => // |
|
952 // /* => \/\* => /* |
|
953 // */ => \*\/ => */ |
|
954 |
|
955 do |
|
956 { |
|
957 firstPos = lex.Offset(); |
|
958 TChar get = lex.Get(); |
|
959 // Check is '\' |
|
960 if( get == '\\' ) |
|
961 { |
|
962 // Peek next character( '/' ) |
|
963 if( lex.Peek() == '/' ) |
|
964 { |
|
965 lex.Inc(); |
|
966 secondPos = lex.Offset(); |
|
967 // Peek next character( '\' ) |
|
968 if( lex.Peek() == '\\' ) |
|
969 { |
|
970 lex.Inc(); |
|
971 // Peek next character( '/' ) |
|
972 if( lex.Peek() == '/' ) |
|
973 { |
|
974 // Delete mark '\/\/' and replace this with '//' |
|
975 aBuf.Delete( secondPos, 1 ); |
|
976 aBuf.Delete( firstPos, 1 ); |
|
977 lex = aBuf; |
|
978 } |
|
979 // Peek next character( '/' ) |
|
980 else if( lex.Peek() == '*' ) |
|
981 { |
|
982 // Delete mark '\/\*' and replace this with '/*' |
|
983 aBuf.Delete( secondPos, 1 ); |
|
984 aBuf.Delete( firstPos, 1 ); |
|
985 lex = aBuf; |
|
986 } |
|
987 } |
|
988 } |
|
989 // Peek next character( '/' ) |
|
990 else if( lex.Peek() == '*' ) |
|
991 { |
|
992 lex.Inc(); |
|
993 secondPos = lex.Offset(); |
|
994 // Peek next character( '\' ) |
|
995 if( lex.Peek() == '\\' ) |
|
996 { |
|
997 lex.Inc(); |
|
998 // Peek next character( '/' ) |
|
999 if( lex.Peek() == '/' ) |
|
1000 { |
|
1001 // Delete mark '\*\/' and replace this with '*\' |
|
1002 aBuf.Delete( secondPos, 1 ); |
|
1003 aBuf.Delete( firstPos, 1 ); |
|
1004 lex = aBuf; |
|
1005 } |
|
1006 } |
|
1007 } |
|
1008 } |
|
1009 firstPos = 0; |
|
1010 secondPos = 0; |
|
1011 } while ( !lex.Eos() ); |
|
1012 |
|
1013 } |
|
1014 |
|
1015 // End of File |