1 // Copyright (c) 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 // CStringParser. |
|
15 // |
|
16 |
|
17 //user include |
|
18 #include "stringparser.h" |
|
19 #include "mslogger.h" |
|
20 |
|
21 CStringParser* CStringParser::NewL(TInt aBufferSize) |
|
22 { |
|
23 LOGTEXT(_L8("[Ltsy] Starting CStringParser::NewL()")); |
|
24 |
|
25 CStringParser* self = CStringParser::NewLC(aBufferSize); |
|
26 CleanupStack::Pop(self); |
|
27 return self; |
|
28 } |
|
29 |
|
30 CStringParser* CStringParser::NewLC(TInt aBufferSize) |
|
31 { |
|
32 LOGTEXT(_L8("[Ltsy] Starting CStringParser::NewLC()")); |
|
33 |
|
34 CStringParser* self = new (ELeave) CStringParser; |
|
35 CleanupStack::PushL(self); |
|
36 self->ConstructL(aBufferSize); |
|
37 return self; |
|
38 } |
|
39 |
|
40 CStringParser::~CStringParser() |
|
41 { |
|
42 User::Free(iBuffer); |
|
43 } |
|
44 |
|
45 CStringParser::CStringParser() |
|
46 { |
|
47 } |
|
48 |
|
49 void CStringParser::ConstructL(TInt aBufferSize) |
|
50 { |
|
51 LOGTEXT(_L8("[Ltsy] Starting CStringParser::ConstructL()")); |
|
52 CreateL(aBufferSize); |
|
53 } |
|
54 |
|
55 TPtrC8 CStringParser::Buffer() const |
|
56 { |
|
57 return TPtrC8(iBuffer, iLastChar-iBuffer); |
|
58 } |
|
59 |
|
60 TPtrC8 CStringParser::CurrentLine() const |
|
61 { |
|
62 TInt len=iLastChar-iLineStart; |
|
63 if (len>0 && iInDelimiter) |
|
64 len-=1; |
|
65 return TPtrC8(iLineStart, len); |
|
66 } |
|
67 |
|
68 void CStringParser::ClearBuffer() |
|
69 { |
|
70 iLastChar = iBuffer; |
|
71 iLineStart = iBuffer; |
|
72 iInDelimiter = ETrue; |
|
73 } |
|
74 |
|
75 // |
|
76 // Remove current line, which is always at the end of the buffer |
|
77 // |
|
78 void CStringParser::ClearCurrentLine() |
|
79 { |
|
80 iLastChar = iLineStart; |
|
81 iInDelimiter = ETrue; |
|
82 } |
|
83 |
|
84 void CStringParser::CreateL(TInt aBufSize) |
|
85 { |
|
86 iBuffer = (TText8*)User::AllocL(aBufSize); |
|
87 iBufferEnd = (iBuffer+aBufSize)-1; |
|
88 ClearBuffer(); |
|
89 } |
|
90 |
|
91 |
|
92 void CStringParser::SetReadLineNotify(MCommReadLineNotifier* aNofity) |
|
93 { |
|
94 iReadLineNotify = aNofity; |
|
95 } |
|
96 |
|
97 |
|
98 void CStringParser::ProcessReadString(const TDesC8& aReadString) |
|
99 { |
|
100 LOGTEXT(_L8("[Ltsy] Starting CStringParse::ProcessReadString()")); |
|
101 LOGTEXT2(_L8("[Ltsy] Read String = %S"), &aReadString); |
|
102 for (TInt nBufferOffset = 0; nBufferOffset < aReadString.Length(); nBufferOffset++) |
|
103 { |
|
104 TText8 tChar = aReadString[nBufferOffset]; |
|
105 |
|
106 // we have already found a char which is a delimiter of the Line |
|
107 // so would dismiss any of delimiter following this one already found |
|
108 if (iInDelimiter && (tChar == '\r' || tChar == '\n')) |
|
109 { |
|
110 continue; |
|
111 } |
|
112 |
|
113 // move on lastchar |
|
114 *iLastChar++ = tChar; |
|
115 // Diacarding characters if our buffer was overflowed |
|
116 if (iLastChar >= iBufferEnd) |
|
117 { |
|
118 iLastChar = iBufferEnd; |
|
119 } |
|
120 // we anyway to find out the delimiter and notify the obser the line was found |
|
121 if (tChar == '\r' || tChar == '\n') |
|
122 { |
|
123 iInDelimiter = ETrue; |
|
124 TPtrC8 tLine(CurrentLine()); |
|
125 LOGTEXT2(_L8("[Ltsy] Line Content = %S"), &tLine); |
|
126 TRAPD(err, iReadLineNotify->NotifyOneLineFoundL(KErrNone, tLine)); |
|
127 if (err != KErrNone) |
|
128 { |
|
129 ClearCurrentLine(); |
|
130 } |
|
131 } |
|
132 else |
|
133 { |
|
134 if(iInDelimiter) // the last char is a delimiter |
|
135 { |
|
136 iInDelimiter=EFalse; |
|
137 iLineStart=iLastChar-1; |
|
138 if(tChar == '>') |
|
139 { |
|
140 TPtrC8 tLine(CurrentLine()); |
|
141 TRAPD(err, iReadLineNotify->NotifyOneLineFoundL(KErrNone, tLine)); |
|
142 if (err != KErrNone) |
|
143 { |
|
144 ClearCurrentLine(); |
|
145 } |
|
146 } |
|
147 } |
|
148 } |
|
149 } |
|
150 LOGTEXT(_L8("[Ltsy] End CStringParse::ProcessReadString()")); |
|
151 } |
|
152 //End of file |
|