|
1 /* |
|
2 * Copyright (c) 2003 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: Pin parser main module. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include <e32base.h> |
|
21 |
|
22 #include "pinparser.h" |
|
23 #include "fileutil.h" |
|
24 |
|
25 TPinParser::TPinParser(TFileUtil& aFileUtil) : iFileUtil(aFileUtil) |
|
26 { |
|
27 } |
|
28 |
|
29 void TPinParser::ParsePolicyInfoL(const TFileName& aPinFile, TVpnPolicyInfo& aPolicyInfo) |
|
30 { |
|
31 HBufC* fileData = iFileUtil.LoadFileDataUL(aPinFile); |
|
32 |
|
33 iLexer.Assign(*fileData); |
|
34 |
|
35 TPtrC token(NULL, 0); |
|
36 |
|
37 while (!iLexer.Eos()) |
|
38 { |
|
39 token.Set(iLexer.NextToken()); |
|
40 |
|
41 ParseSectionInfo(token, aPolicyInfo); |
|
42 } |
|
43 |
|
44 delete fileData; |
|
45 } |
|
46 |
|
47 void TPinParser::ParsePolicyDetailsL(const TDesC& aPinFile, TVpnPolicyDetails& aPolicyDetails) |
|
48 { |
|
49 HBufC* fileData = iFileUtil.LoadFileDataUL(aPinFile); |
|
50 |
|
51 iLexer.Assign(*fileData); |
|
52 |
|
53 TPtrC token(NULL, 0); |
|
54 |
|
55 while (!iLexer.Eos()) |
|
56 { |
|
57 token.Set(iLexer.NextToken()); |
|
58 |
|
59 ParseSectionDetails(token, aPolicyDetails); |
|
60 } |
|
61 |
|
62 delete fileData; |
|
63 } |
|
64 |
|
65 void TPinParser::ParseSectionInfo(const TDesC& aSectionTag, TVpnPolicyInfo& aPolicyInfo) |
|
66 { |
|
67 TPtrC content = GetSectionContent(); |
|
68 |
|
69 if (aSectionTag.CompareF(KNameSection) == 0) |
|
70 { |
|
71 aPolicyInfo.iName.Copy(content.Left(aPolicyInfo.iName.MaxLength())); |
|
72 aPolicyInfo.iName.Trim(); |
|
73 } |
|
74 } |
|
75 |
|
76 void TPinParser::ParseSectionDetails(const TDesC& aSectionTag, TVpnPolicyDetails& aPolicyDetails) |
|
77 { |
|
78 TPtrC content = GetSectionContent(); |
|
79 |
|
80 if (aSectionTag.CompareF(KNameSection) == 0) |
|
81 { |
|
82 aPolicyDetails.iName.Copy(content.Left(aPolicyDetails.iName.MaxLength())); |
|
83 aPolicyDetails.iName.Trim(); |
|
84 } |
|
85 if (aSectionTag.CompareF(KDescriptionSection) == 0) |
|
86 { |
|
87 aPolicyDetails.iDescription.Copy(content.Left(aPolicyDetails.iDescription.MaxLength())); |
|
88 aPolicyDetails.iDescription.Trim(); |
|
89 } |
|
90 if (aSectionTag.CompareF(KVersionSection) == 0) |
|
91 { |
|
92 aPolicyDetails.iVersion.Copy(content.Left(aPolicyDetails.iVersion.MaxLength())); |
|
93 aPolicyDetails.iVersion.Trim(); |
|
94 } |
|
95 if (aSectionTag.CompareF(KIssuerNameSection) == 0) |
|
96 { |
|
97 aPolicyDetails.iIssuerName.Copy(content.Left(aPolicyDetails.iIssuerName.MaxLength())); |
|
98 aPolicyDetails.iIssuerName.Trim(); |
|
99 } |
|
100 if (aSectionTag.CompareF(KContactSection) == 0) |
|
101 { |
|
102 aPolicyDetails.iContactInfo.Copy(content.Left(aPolicyDetails.iContactInfo.MaxLength())); |
|
103 aPolicyDetails.iContactInfo.Trim(); |
|
104 } |
|
105 } |
|
106 |
|
107 TPtrC TPinParser::GetSectionContent() |
|
108 { |
|
109 iLexer.Mark(); |
|
110 |
|
111 while (!iLexer.Eos()) |
|
112 { |
|
113 iLexer.Inc(); |
|
114 |
|
115 if (iLexer.Eos() || (iLexer.Peek() == KSectionBeginChar)) |
|
116 { |
|
117 break; |
|
118 } |
|
119 } |
|
120 |
|
121 return iLexer.MarkedToken(); |
|
122 } |
|
123 |
|
124 HBufC* TPinParser::PolicyDetailsAsTextL(const TVpnPolicyDetails& aPolicyDetails) |
|
125 { |
|
126 HBufC* text = HBufC::NewL(KInitialDetailsTextLength); |
|
127 CleanupStack::PushL(text); |
|
128 |
|
129 SmartAppendL(text, KNameSection()); |
|
130 SmartAppendL(text, KNewLine()); |
|
131 SmartAppendL(text, aPolicyDetails.iName); |
|
132 SmartAppendL(text, KNewLine()); |
|
133 |
|
134 SmartAppendL(text, KDescriptionSection()); |
|
135 SmartAppendL(text, KNewLine()); |
|
136 SmartAppendL(text, aPolicyDetails.iDescription); |
|
137 SmartAppendL(text, KNewLine()); |
|
138 |
|
139 SmartAppendL(text, KVersionSection()); |
|
140 SmartAppendL(text, KNewLine()); |
|
141 SmartAppendL(text, aPolicyDetails.iVersion); |
|
142 SmartAppendL(text, KNewLine()); |
|
143 |
|
144 SmartAppendL(text, KIssuerNameSection()); |
|
145 SmartAppendL(text, KNewLine()); |
|
146 SmartAppendL(text, aPolicyDetails.iIssuerName); |
|
147 SmartAppendL(text, KNewLine()); |
|
148 |
|
149 SmartAppendL(text, KContactSection()); |
|
150 SmartAppendL(text, KNewLine()); |
|
151 SmartAppendL(text, aPolicyDetails.iContactInfo); |
|
152 SmartAppendL(text, KNewLine()); |
|
153 |
|
154 CleanupStack::Pop(); // text |
|
155 |
|
156 return text; |
|
157 } |
|
158 |
|
159 void TPinParser::SmartAppendL(HBufC*& aBuf, const TDesC& aText) |
|
160 { |
|
161 // Make sure that we have enough space for the new text |
|
162 |
|
163 TInt spaceLeft = aBuf->Des().MaxLength() - aBuf->Des().Length(); |
|
164 |
|
165 if (aText.Length() > spaceLeft) |
|
166 { |
|
167 // Allocate enough space for the new text + some additional |
|
168 // free space so that allocations are not too frequent |
|
169 |
|
170 TInt newMaxLength = aBuf->Des().MaxLength() + aText.Length() + KInitialDetailsTextLength; |
|
171 |
|
172 aBuf = aBuf->ReAllocL(newMaxLength); |
|
173 } |
|
174 |
|
175 aBuf->Des().Append(aText); |
|
176 } |