|
1 // Copyright (c) 1999-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 // |
|
15 |
|
16 #include <msgtestscripts.h> |
|
17 |
|
18 EXPORT_C CScriptFile* CScriptFile::NewLC(CTestUtils& aTestUtils, const TDesC& aComponent, const TDesC& aScript) |
|
19 { |
|
20 CScriptFile* self = NewLC(aTestUtils, aComponent); |
|
21 self->ReadScriptL(aScript); |
|
22 return self; |
|
23 } |
|
24 |
|
25 EXPORT_C CScriptFile* CScriptFile::NewLC(CTestUtils& aTestUtils, const TDesC& aComponent) |
|
26 { |
|
27 CScriptFile* self = new (ELeave) CScriptFile(aTestUtils); |
|
28 CleanupStack::PushL(self); |
|
29 self->ConstructL(aComponent); |
|
30 return self; |
|
31 } |
|
32 |
|
33 void CScriptFile::ConstructL(const TDesC& aComponent) |
|
34 { |
|
35 iSections = new (ELeave) CArrayPtrFlat<CScriptSection>(10); |
|
36 iComponent = aComponent.AllocL(); |
|
37 } |
|
38 |
|
39 CScriptFile::CScriptFile(CTestUtils& aTestUtils) |
|
40 : iTestUtils(aTestUtils) |
|
41 { |
|
42 } |
|
43 |
|
44 EXPORT_C CScriptFile::~CScriptFile() |
|
45 { |
|
46 if (iSections) |
|
47 { |
|
48 iSections->ResetAndDestroy(); |
|
49 delete iSections; |
|
50 } |
|
51 |
|
52 delete iComponent; |
|
53 } |
|
54 |
|
55 CScriptSection* CScriptFile::Section(const TDesC& aSectionName) |
|
56 { |
|
57 CScriptSection* section = iLastSection; |
|
58 |
|
59 if (!iLastSection || iLastSection->SectionName().CompareF(aSectionName) != 0) |
|
60 { |
|
61 const TInt count = iSections->Count(); |
|
62 |
|
63 for (TInt i = 0; i < count; i++) //order important |
|
64 { |
|
65 section = iSections->At(i); |
|
66 iLastSection = section; |
|
67 |
|
68 if (iLastSection->SectionName().CompareF(aSectionName) == 0) |
|
69 { |
|
70 break; |
|
71 } |
|
72 else |
|
73 { |
|
74 section = NULL; |
|
75 } |
|
76 } |
|
77 } |
|
78 |
|
79 return section; |
|
80 } |
|
81 |
|
82 EXPORT_C const TDesC& CScriptFile::ItemValue(const TDesC& aSection, const TDesC& aItem, const TDesC& aDefault) |
|
83 { |
|
84 CScriptSection* section = Section(aSection); |
|
85 |
|
86 if (section != NULL) |
|
87 return section->ItemValue(aItem, aDefault); |
|
88 |
|
89 return aDefault; |
|
90 } |
|
91 |
|
92 EXPORT_C TInt CScriptFile::ItemValue(const TDesC& aSection, const TDesC& aItem, const TInt aDefault) |
|
93 { |
|
94 TInt output = aDefault; |
|
95 CScriptSection* section = Section(aSection); |
|
96 |
|
97 if (section != NULL) |
|
98 { |
|
99 output = section->ItemValue(aItem, aDefault); |
|
100 } |
|
101 |
|
102 return output; |
|
103 } |
|
104 |
|
105 EXPORT_C HBufC* CScriptFile::ItemValueLC(CTestUtils& aTestUtils, const TDesC& aComponent, const TDesC& aScript, const TDesC& aSection, const TDesC& aItem, const TDesC& aDefault) |
|
106 { |
|
107 CScriptFile* self = NewLC(aTestUtils, aComponent, aScript); |
|
108 TPtrC output(self->ItemValue(aSection, aItem, aDefault)); |
|
109 HBufC* buf = output.AllocL(); |
|
110 CleanupStack::PopAndDestroy(self); |
|
111 CleanupStack::PushL(buf); |
|
112 return buf; |
|
113 } |
|
114 |
|
115 EXPORT_C TInt CScriptFile::ItemValueL(CTestUtils& aTestUtils, const TDesC& aComponent, const TDesC& aScript, const TDesC& aSection, const TDesC& aItem, const TInt aDefault) |
|
116 { |
|
117 CScriptFile* self = NewLC(aTestUtils, aComponent, aScript); |
|
118 TInt output = self->ItemValue(aSection, aItem, aDefault); |
|
119 CleanupStack::PopAndDestroy(self); |
|
120 return output; |
|
121 } |
|
122 |
|
123 HBufC8* CScriptFile::ReadFileL(const TDesC& aFile) const |
|
124 { |
|
125 HBufC8* ret = iTestUtils.ReadFileLC(aFile); |
|
126 CleanupStack::Pop(ret); |
|
127 return ret; |
|
128 } |
|
129 |
|
130 HBufC* CScriptFile::ReadFileLC(const TDesC& aScript) const |
|
131 { |
|
132 HBufC8* file = NULL; |
|
133 |
|
134 TRAPD(err, file = ReadFileL(aScript)); |
|
135 |
|
136 if (err) |
|
137 { |
|
138 TParse fileOut; |
|
139 err = iTestUtils.ResolveFile(*iComponent, aScript, fileOut); |
|
140 |
|
141 if (err) |
|
142 { |
|
143 iTestUtils.Printf(_L("Cannot read file %S. Error=%d"), &aScript, err); |
|
144 User::Leave(err); |
|
145 } |
|
146 |
|
147 file = ReadFileL(fileOut.FullName()); |
|
148 } |
|
149 |
|
150 CleanupStack::PushL(file); |
|
151 |
|
152 HBufC* buf = HBufC::NewL(file->Length()); |
|
153 buf->Des().Copy(*file); |
|
154 CleanupStack::PopAndDestroy(file); |
|
155 CleanupStack::PushL(buf); |
|
156 return buf; |
|
157 } |
|
158 |
|
159 EXPORT_C void CScriptFile::ReadScriptL(const TDesC& aScript) |
|
160 { |
|
161 iSections->ResetAndDestroy(); |
|
162 iLastSection = NULL; |
|
163 |
|
164 CScriptSection* section = NULL; |
|
165 CScriptSectionItem* currentItem = NULL; |
|
166 TInt currentItemStart = 0; |
|
167 CScriptSection* sectionAll = NULL; |
|
168 |
|
169 HBufC* scriptContents = ReadFileLC(aScript); |
|
170 |
|
171 TLex input(*scriptContents); |
|
172 |
|
173 while (!input.Eos()) |
|
174 { |
|
175 input.SkipSpaceAndMark(); |
|
176 |
|
177 input.SkipCharacters(); |
|
178 if ( input.TokenLength() == 0 ) // if valid potential token |
|
179 { |
|
180 break; |
|
181 } |
|
182 |
|
183 TPtrC token = input.MarkedToken(); |
|
184 |
|
185 if (token.CompareF(_L("endscript")) == 0) |
|
186 { |
|
187 break; |
|
188 } |
|
189 else if (token.FindF(KScriptSectionStart) == 0 && token.Length() > 2) |
|
190 { |
|
191 ParseAndSetItemValueL(*scriptContents, input, currentItemStart, currentItem); |
|
192 |
|
193 TInt mid = 1; |
|
194 TInt len = token.Length() - 2; |
|
195 |
|
196 if (sectionAll) |
|
197 section = CScriptSection::NewLC(token.Mid(mid, len), *sectionAll); |
|
198 else |
|
199 section = CScriptSection::NewLC(token.Mid(mid, len)); |
|
200 |
|
201 if (!sectionAll && section->SectionName().CompareF(_L("All")) == 0) |
|
202 sectionAll = section; |
|
203 |
|
204 iSections->AppendL(section); |
|
205 CleanupStack::Pop(); //section |
|
206 } |
|
207 else if (section) |
|
208 { |
|
209 TPtrC itemEnd(KScriptItemEnd); |
|
210 |
|
211 if (itemEnd.Length() < token.Length() && token.Right(itemEnd.Length()).CompareF(itemEnd) == 0) |
|
212 { |
|
213 FoundNewItemL(*scriptContents, input, currentItemStart, *section, currentItem); |
|
214 } |
|
215 } |
|
216 } |
|
217 |
|
218 ParseAndSetItemValueL(*scriptContents, input, currentItemStart, currentItem); |
|
219 CleanupStack::PopAndDestroy(scriptContents); |
|
220 } |
|
221 |
|
222 TPtrC CScriptFile::ParseValue(const TDesC& aText, const TLex& aInput, TInt aCurrentItemStart) const |
|
223 { |
|
224 TInt mid = aCurrentItemStart; |
|
225 TInt len = aInput.MarkedOffset() - mid; |
|
226 TPtrC ret(KNullDesC); |
|
227 |
|
228 if (len > 0) |
|
229 ret.Set(aText.Mid(mid, len)); |
|
230 |
|
231 return ret; |
|
232 } |
|
233 |
|
234 void CScriptFile::ParseAndSetItemValueL(const TDesC& aText, const TLex& aInput, TInt aCurrentItemStart, CScriptSectionItem*& arCurrentItem) |
|
235 { |
|
236 if (arCurrentItem) |
|
237 { |
|
238 delete arCurrentItem->iValue; |
|
239 arCurrentItem->iValue = NULL; |
|
240 arCurrentItem->iValue = ParseValue(aText, aInput, aCurrentItemStart).AllocL(); |
|
241 arCurrentItem->iValue->Des().Trim(); |
|
242 ReplaceL(KScriptCRLF, KScriptLF, arCurrentItem->iValue); |
|
243 |
|
244 if (arCurrentItem->Item().CompareF(KDefaults) == 0) |
|
245 { |
|
246 CopyInDefaultsL(arCurrentItem->iParent, arCurrentItem->Value()); |
|
247 } |
|
248 } |
|
249 |
|
250 arCurrentItem = NULL; |
|
251 } |
|
252 |
|
253 void CScriptFile::FoundNewItemL(const TDesC& aText, TLex& arInput, TInt& arCurrentItemStart, CScriptSection& aSection, CScriptSectionItem*& arCurrentItem) |
|
254 { |
|
255 TPtrC token = arInput.MarkedToken(); |
|
256 |
|
257 ParseAndSetItemValueL(aText, arInput, arCurrentItemStart, arCurrentItem); |
|
258 |
|
259 arInput.SkipSpaceAndMark(); |
|
260 arCurrentItemStart = arInput.Offset(); |
|
261 |
|
262 TPtrC itemEnd(KScriptItemEnd); |
|
263 const TInt length = token.Length() - itemEnd.Length(); |
|
264 arCurrentItem = &aSection.ReplaceItemL(token.Left(length), KNullDesC); |
|
265 } |
|
266 |
|
267 void CScriptFile::CopyInDefaultsL(CScriptSection& aSection, const TDesC& aDefaultFile) |
|
268 { |
|
269 CScriptFile* file = CScriptFile::NewLC(iTestUtils, *iComponent, aDefaultFile); |
|
270 |
|
271 TInt count = file->Sections().Count(); |
|
272 |
|
273 if (count > 0) |
|
274 { |
|
275 CScriptSection& def = *file->Sections()[0]; |
|
276 aSection.SetDefaultsL(def); |
|
277 } |
|
278 |
|
279 CleanupStack::PopAndDestroy(file); |
|
280 } |
|
281 |
|
282 EXPORT_C TInt CScriptFile::GetNextWord(TLex& aInput, TChar aDelimiter, TInt& aOutput) |
|
283 { |
|
284 aOutput = 0; |
|
285 TPtrC string; |
|
286 TInt err = GetNextWord(aInput, aDelimiter, string); |
|
287 |
|
288 if (!err) |
|
289 { |
|
290 TLex number(string); |
|
291 err = number.Val(aOutput); |
|
292 } |
|
293 |
|
294 return err; |
|
295 } |
|
296 |
|
297 EXPORT_C TInt CScriptFile::GetNextWord(TLex& aInput, TChar aDelimiter, TPtrC& aOutput) |
|
298 { |
|
299 //Get to the start of the descriptor |
|
300 while (!aInput.Peek().IsAlphaDigit() && aInput.Peek() != '+' && !aInput.Eos() && aInput.Peek() != aDelimiter) |
|
301 aInput.Inc(); |
|
302 |
|
303 if (aInput.Eos()) |
|
304 return KErrNotFound; |
|
305 |
|
306 aInput.Mark(); |
|
307 |
|
308 while (aInput.Peek() != aDelimiter && !aInput.Eos()) |
|
309 aInput.Inc(); |
|
310 |
|
311 aOutput.Set(aInput.MarkedToken()); |
|
312 |
|
313 if (!aInput.Eos()) |
|
314 aInput.SkipAndMark(1); |
|
315 |
|
316 return KErrNone; |
|
317 } |
|
318 |
|
319 EXPORT_C void CScriptFile::ReplaceL(const TDesC& aOld, const TDesC& aNew, HBufC*& rString) |
|
320 /** |
|
321 * Replaces every instance of aOld in rString with aNew |
|
322 * rString is replaced by the new string |
|
323 */ |
|
324 { |
|
325 HBufC* repl = ReplaceLC(aOld, aNew, *rString); |
|
326 CleanupStack::Pop(repl); |
|
327 delete rString; |
|
328 rString = repl; |
|
329 } |
|
330 |
|
331 EXPORT_C HBufC* CScriptFile::ReplaceLC(const TDesC& aOld, const TDesC& aNew, const TDesC& aOldString) |
|
332 { |
|
333 HBufC* rString = aOldString.AllocLC(); |
|
334 TInt oldLen = aOld.Length(); |
|
335 TInt newLen = aNew.Length(); |
|
336 |
|
337 if (!oldLen) |
|
338 return rString; |
|
339 |
|
340 for (TInt pos = 0; pos < rString->Length(); pos += newLen) |
|
341 { |
|
342 TPtrC ptrC = rString->Mid(pos); |
|
343 TInt find = ptrC.Find(aOld); |
|
344 |
|
345 if (find == KErrNotFound) |
|
346 return rString; |
|
347 |
|
348 pos += find; |
|
349 |
|
350 if (newLen > oldLen) |
|
351 { |
|
352 rString = rString->ReAllocL(rString->Length() + newLen - oldLen); |
|
353 CleanupStack::Pop(); |
|
354 CleanupStack::PushL(rString); |
|
355 } |
|
356 |
|
357 TPtr ptr(rString->Des()); |
|
358 ptr.Replace(pos, oldLen, aNew); |
|
359 } |
|
360 |
|
361 return rString; |
|
362 } |
|
363 |