|
1 // Copyright (c) 2007-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 "TestScripts.h" |
|
17 #include "httptestutils.h" |
|
18 |
|
19 EXPORT_C CScriptFile* CScriptFile::NewL(CHTTPTestUtils& aTestUtils, const TDesC& aComponent, const TDesC& aScript) |
|
20 { |
|
21 CScriptFile* self = NewLC(aTestUtils, aComponent, aScript); |
|
22 CleanupStack::Pop(); |
|
23 return self; |
|
24 } |
|
25 |
|
26 EXPORT_C CScriptFile* CScriptFile::NewLC(CHTTPTestUtils& aTestUtils, const TDesC& aComponent, const TDesC& aScript) |
|
27 { |
|
28 CScriptFile* self = NewLC(aTestUtils, aComponent); |
|
29 self->ReadScriptL(aScript); |
|
30 return self; |
|
31 } |
|
32 |
|
33 EXPORT_C CScriptFile* CScriptFile::NewL(CHTTPTestUtils& aTestUtils, const TDesC& aComponent) |
|
34 { |
|
35 CScriptFile* self = NewLC(aTestUtils, aComponent); |
|
36 CleanupStack::Pop(); |
|
37 return self; |
|
38 } |
|
39 |
|
40 EXPORT_C CScriptFile* CScriptFile::NewLC(CHTTPTestUtils& aTestUtils, const TDesC& aComponent) |
|
41 { |
|
42 CScriptFile* self = new (ELeave) CScriptFile(aTestUtils); |
|
43 CleanupStack::PushL(self); |
|
44 self->ConstructL(aComponent); |
|
45 return self; |
|
46 } |
|
47 |
|
48 void CScriptFile::ConstructL(const TDesC& aComponent) |
|
49 { |
|
50 iSections = new (ELeave) CArrayPtrFlat<CScriptSection>(10); |
|
51 iComponent = aComponent.AllocL(); |
|
52 } |
|
53 |
|
54 CScriptFile::CScriptFile(CHTTPTestUtils& aTestUtils) |
|
55 : iTestUtils(aTestUtils) |
|
56 { |
|
57 } |
|
58 |
|
59 EXPORT_C CScriptFile::~CScriptFile() |
|
60 { |
|
61 if (iSections) |
|
62 { |
|
63 iSections->ResetAndDestroy(); |
|
64 delete iSections; |
|
65 } |
|
66 |
|
67 delete iComponent; |
|
68 } |
|
69 |
|
70 CScriptSection* CScriptFile::Section(const TDesC& aSectionName) |
|
71 { |
|
72 CScriptSection* section = iLastSection; |
|
73 |
|
74 if (!iLastSection || iLastSection->SectionName().CompareF(aSectionName) != 0) |
|
75 { |
|
76 const TInt count = iSections->Count(); |
|
77 |
|
78 for (TInt i = 0; i < count; i++) //order important |
|
79 { |
|
80 section = iSections->At(i); |
|
81 iLastSection = section; |
|
82 |
|
83 if (iLastSection->SectionName().CompareF(aSectionName) == 0) |
|
84 { |
|
85 break; |
|
86 } |
|
87 else |
|
88 { |
|
89 section = NULL; |
|
90 } |
|
91 } |
|
92 } |
|
93 |
|
94 return section; |
|
95 } |
|
96 |
|
97 EXPORT_C const TDesC& CScriptFile::ItemValue(const TDesC& aSection, const TDesC& aItem, const TDesC& aDefault) |
|
98 { |
|
99 CScriptSection* section = Section(aSection); |
|
100 |
|
101 if (section != NULL) |
|
102 return section->ItemValue(aItem, aDefault); |
|
103 |
|
104 return aDefault; |
|
105 } |
|
106 |
|
107 EXPORT_C TInt CScriptFile::ItemValue(const TDesC& aSection, const TDesC& aItem, const TInt aDefault) |
|
108 { |
|
109 TInt output = aDefault; |
|
110 CScriptSection* section = Section(aSection); |
|
111 |
|
112 if (section != NULL) |
|
113 { |
|
114 output = section->ItemValue(aItem, aDefault); |
|
115 } |
|
116 |
|
117 return output; |
|
118 } |
|
119 |
|
120 EXPORT_C HBufC* CScriptFile::ItemValueLC(CHTTPTestUtils& aTestUtils, const TDesC& aComponent, const TDesC& aScript, const TDesC& aSection, const TDesC& aItem, const TDesC& aDefault) |
|
121 { |
|
122 CScriptFile* self = NewLC(aTestUtils, aComponent, aScript); |
|
123 TPtrC output(self->ItemValue(aSection, aItem, aDefault)); |
|
124 HBufC* buf = output.AllocL(); |
|
125 CleanupStack::PopAndDestroy(self); |
|
126 CleanupStack::PushL(buf); |
|
127 return buf; |
|
128 } |
|
129 |
|
130 EXPORT_C TInt CScriptFile::ItemValueL(CHTTPTestUtils& aTestUtils, const TDesC& aComponent, const TDesC& aScript, const TDesC& aSection, const TDesC& aItem, const TInt aDefault) |
|
131 { |
|
132 CScriptFile* self = NewLC(aTestUtils, aComponent, aScript); |
|
133 TInt output = self->ItemValue(aSection, aItem, aDefault); |
|
134 CleanupStack::PopAndDestroy(self); |
|
135 return output; |
|
136 } |
|
137 |
|
138 HBufC8* CScriptFile::ReadFileL(const TDesC& aFile) const |
|
139 { |
|
140 HBufC8* ret = iTestUtils.ReadFileLC(aFile); |
|
141 CleanupStack::Pop(ret); |
|
142 return ret; |
|
143 } |
|
144 |
|
145 HBufC* CScriptFile::ReadFileLC(const TDesC& aScript) const |
|
146 { |
|
147 HBufC8* file = NULL; |
|
148 |
|
149 TRAPD(err, file = ReadFileL(aScript)); |
|
150 |
|
151 if (err) |
|
152 { |
|
153 TParse fileOut; |
|
154 err = iTestUtils.ResolveFile(*iComponent, aScript, fileOut); |
|
155 |
|
156 if (err) |
|
157 { |
|
158 iTestUtils.Test().Printf(_L("Cannot read file %S. Error=%d"), &aScript, err); |
|
159 User::Leave(err); |
|
160 } |
|
161 |
|
162 file = ReadFileL(fileOut.FullName()); |
|
163 } |
|
164 |
|
165 CleanupStack::PushL(file); |
|
166 |
|
167 HBufC* buf = HBufC::NewL(file->Length()); |
|
168 buf->Des().Copy(*file); |
|
169 CleanupStack::PopAndDestroy(file); |
|
170 CleanupStack::PushL(buf); |
|
171 return buf; |
|
172 } |
|
173 |
|
174 EXPORT_C void CScriptFile::ReadScriptL(const TDesC& aScript) |
|
175 { |
|
176 iSections->ResetAndDestroy(); |
|
177 ParseScriptSectionsL(aScript, KErrNotFound); |
|
178 } |
|
179 |
|
180 void CScriptFile::ParseEmbeddedScriptsL(CScriptSection& aSection, TInt aInsertIndex) |
|
181 { |
|
182 // This must be a Script section |
|
183 __ASSERT_DEBUG(aSection.SectionName().CompareF(KScriptsSection)==0, User::Invariant()); |
|
184 |
|
185 // Loop through the script section and load and parse each script file in reverse |
|
186 // order so that the sections are inserted in the correct order |
|
187 TInt scriptCount = aSection.Items().Count(); |
|
188 for(TInt ii=scriptCount-1; ii>=0; --ii) |
|
189 { |
|
190 CScriptSectionItem& item = aSection.Item(ii); |
|
191 |
|
192 // Sometimes comments are included as part of the value. We need to include |
|
193 // some simple parsing here to extract just the filename from the value |
|
194 // by just including the text upto a '\n' or ' ' characters. |
|
195 TPtrC fileName(item.Value()); |
|
196 TInt locateWhiteSpace = fileName.LocateF('\n'); |
|
197 if(locateWhiteSpace>0) |
|
198 fileName.Set(fileName.Left(locateWhiteSpace)); |
|
199 |
|
200 locateWhiteSpace = fileName.LocateF(' '); |
|
201 if(locateWhiteSpace>0) |
|
202 fileName.Set(fileName.Left(locateWhiteSpace)); |
|
203 |
|
204 ParseScriptSectionsL(fileName, aInsertIndex); |
|
205 } |
|
206 } |
|
207 |
|
208 void CScriptFile::ParseScriptSectionsL(const TDesC& aScript, TInt aInsertIndex) |
|
209 { |
|
210 iLastSection = NULL; |
|
211 |
|
212 CScriptSection* section = NULL; |
|
213 CScriptSectionItem* currentItem = NULL; |
|
214 TInt currentItemStart = 0; |
|
215 CScriptSection* sectionAll = NULL; |
|
216 RArray<TInt> sectionIndexArray; |
|
217 CleanupClosePushL(sectionIndexArray); |
|
218 TInt indexIncrement = 0; |
|
219 HBufC* scriptContents = ReadFileLC(aScript); |
|
220 |
|
221 TLex input(*scriptContents); |
|
222 |
|
223 while (!input.Eos()) |
|
224 { |
|
225 input.SkipSpaceAndMark(); |
|
226 |
|
227 input.SkipCharacters(); |
|
228 if ( input.TokenLength() == 0 ) // if valid potential token |
|
229 { |
|
230 break; |
|
231 } |
|
232 |
|
233 TPtrC token = input.MarkedToken(); |
|
234 |
|
235 if (token.CompareF(_L("endscript")) == 0) |
|
236 { |
|
237 break; |
|
238 } |
|
239 else if (token.FindF(KScriptSectionStart) == 0 && token.Length() > 2) |
|
240 { |
|
241 ParseAndSetItemValueL(*scriptContents, input, currentItemStart, currentItem); |
|
242 |
|
243 TInt mid = 1; |
|
244 TInt len = token.Length() - 2; |
|
245 |
|
246 if (sectionAll) |
|
247 section = CScriptSection::NewLC(token.Mid(mid, len), *sectionAll); |
|
248 else |
|
249 section = CScriptSection::NewLC(token.Mid(mid, len)); |
|
250 |
|
251 if (!sectionAll && section->SectionName().CompareF(_L("All")) == 0) |
|
252 sectionAll = section; |
|
253 |
|
254 // We have a Scripts section, store its index as we need to track where |
|
255 // it occured so that the scripts can be added in the same location |
|
256 // after the current file has parsed |
|
257 if (section->SectionName().CompareF(KScriptsSection) == 0) |
|
258 { |
|
259 if(aInsertIndex==KErrNotFound) |
|
260 { |
|
261 // Not an embedded script, simple store its index |
|
262 TInt currectSectionsCount = iSections->Count(); |
|
263 sectionIndexArray.Append(currectSectionsCount); |
|
264 } |
|
265 else |
|
266 { |
|
267 // An embedded script, calculate the index it should be inserted at |
|
268 TInt currectSectionsCount = indexIncrement+aInsertIndex; |
|
269 sectionIndexArray.Append(currectSectionsCount); |
|
270 } |
|
271 } |
|
272 |
|
273 // Check if the section needs to be inserted at a particular location |
|
274 if(aInsertIndex==KErrNotFound) |
|
275 iSections->AppendL(section); |
|
276 else |
|
277 iSections->InsertL(aInsertIndex + indexIncrement++, section); |
|
278 |
|
279 CleanupStack::Pop(section); |
|
280 } |
|
281 else if (section) |
|
282 { |
|
283 TPtrC itemEnd(KScriptItemEnd); |
|
284 |
|
285 if (itemEnd.Length() < token.Length() && token.Right(itemEnd.Length()).CompareF(itemEnd) == 0) |
|
286 { |
|
287 FoundNewItemL(*scriptContents, input, currentItemStart, *section, currentItem); |
|
288 } |
|
289 } |
|
290 } |
|
291 |
|
292 ParseAndSetItemValueL(*scriptContents, input, currentItemStart, currentItem); |
|
293 CleanupStack::PopAndDestroy(scriptContents); |
|
294 |
|
295 // We have been tracking where the script sections have been inserted so we |
|
296 // want to load the sections from the embedded script file and insert them |
|
297 // at the same point. This must be done in reverse order so that the index |
|
298 // values of script sections before the current one is maintained. |
|
299 TInt scriptSectionsCount = sectionIndexArray.Count(); |
|
300 for( TInt ii=scriptSectionsCount-1; ii>=0; --ii) |
|
301 { |
|
302 TInt indexOfScriptSection = sectionIndexArray[ii]; |
|
303 ParseEmbeddedScriptsL(*(iSections->At(indexOfScriptSection)), indexOfScriptSection); |
|
304 } |
|
305 CleanupStack::PopAndDestroy(§ionIndexArray); |
|
306 } |
|
307 |
|
308 TPtrC CScriptFile::ParseValue(const TDesC& aText, const TLex& aInput, TInt aCurrentItemStart) const |
|
309 { |
|
310 TInt mid = aCurrentItemStart; |
|
311 TInt len = aInput.MarkedOffset() - mid; |
|
312 TPtrC ret(KNullDesC); |
|
313 |
|
314 if (len > 0) |
|
315 ret.Set(aText.Mid(mid, len)); |
|
316 |
|
317 return ret; |
|
318 } |
|
319 |
|
320 void CScriptFile::ParseAndSetItemValueL(const TDesC& aText, const TLex& aInput, TInt aCurrentItemStart, CScriptSectionItem*& arCurrentItem) |
|
321 { |
|
322 if (arCurrentItem) |
|
323 { |
|
324 delete arCurrentItem->iValue; |
|
325 arCurrentItem->iValue = NULL; |
|
326 arCurrentItem->iValue = ParseValue(aText, aInput, aCurrentItemStart).AllocL(); |
|
327 arCurrentItem->iValue->Des().Trim(); |
|
328 ReplaceL(KScriptCRLF, KScriptLF, arCurrentItem->iValue); |
|
329 |
|
330 if (arCurrentItem->Item().CompareF(KDefaults) == 0) |
|
331 { |
|
332 CopyInDefaultsL(arCurrentItem->iParent, arCurrentItem->Value()); |
|
333 } |
|
334 } |
|
335 |
|
336 arCurrentItem = NULL; |
|
337 } |
|
338 |
|
339 void CScriptFile::FoundNewItemL(const TDesC& aText, TLex& arInput, TInt& arCurrentItemStart, CScriptSection& aSection, CScriptSectionItem*& arCurrentItem) |
|
340 { |
|
341 TPtrC token = arInput.MarkedToken(); |
|
342 |
|
343 ParseAndSetItemValueL(aText, arInput, arCurrentItemStart, arCurrentItem); |
|
344 |
|
345 arInput.SkipSpaceAndMark(); |
|
346 arCurrentItemStart = arInput.Offset(); |
|
347 |
|
348 TPtrC itemEnd(KScriptItemEnd); |
|
349 const TInt length = token.Length() - itemEnd.Length(); |
|
350 arCurrentItem = &aSection.ReplaceItemL(token.Left(length), KNullDesC); |
|
351 } |
|
352 |
|
353 void CScriptFile::CopyInDefaultsL(CScriptSection& aSection, const TDesC& aDefaultFile) |
|
354 { |
|
355 CScriptFile* file = CScriptFile::NewLC(iTestUtils, *iComponent, aDefaultFile); |
|
356 |
|
357 TInt count = file->Sections().Count(); |
|
358 |
|
359 if (count > 0) |
|
360 { |
|
361 CScriptSection& def = *file->Sections()[0]; |
|
362 aSection.SetDefaultsL(def); |
|
363 } |
|
364 CleanupStack::PopAndDestroy(file); |
|
365 } |
|
366 |
|
367 EXPORT_C TInt CScriptFile::GetNextWord(TLex& aInput, TChar aDelimiter, TInt& aOutput) |
|
368 { |
|
369 aOutput = 0; |
|
370 TPtrC string; |
|
371 TInt err = GetNextWord(aInput, aDelimiter, string); |
|
372 |
|
373 if (!err) |
|
374 { |
|
375 TLex number(string); |
|
376 err = number.Val(aOutput); |
|
377 } |
|
378 |
|
379 return err; |
|
380 } |
|
381 |
|
382 EXPORT_C TInt CScriptFile::GetNextWord(TLex& aInput, TChar aDelimiter, TPtrC& aOutput) |
|
383 { |
|
384 //Get to the start of the descriptor |
|
385 while (!aInput.Peek().IsAlphaDigit() && aInput.Peek() != '+' && !aInput.Eos() && aInput.Peek() != aDelimiter) |
|
386 aInput.Inc(); |
|
387 |
|
388 if (aInput.Eos()) |
|
389 return KErrNotFound; |
|
390 |
|
391 aInput.Mark(); |
|
392 |
|
393 while (aInput.Peek() != aDelimiter && !aInput.Eos()) |
|
394 aInput.Inc(); |
|
395 |
|
396 aOutput.Set(aInput.MarkedToken()); |
|
397 |
|
398 if (!aInput.Eos()) |
|
399 aInput.SkipAndMark(1); |
|
400 |
|
401 return KErrNone; |
|
402 } |
|
403 |
|
404 EXPORT_C void CScriptFile::ReplaceL(const TDesC& aOld, const TDesC& aNew, HBufC*& rString) |
|
405 { |
|
406 HBufC* repl = ReplaceLC(aOld, aNew, *rString); |
|
407 CleanupStack::Pop(repl); |
|
408 delete rString; |
|
409 rString = repl; |
|
410 } |
|
411 |
|
412 EXPORT_C HBufC* CScriptFile::ReplaceLC(const TDesC& aOld, const TDesC& aNew, const TDesC& aOldString) |
|
413 { |
|
414 HBufC* rString = aOldString.AllocLC(); |
|
415 TInt oldLen = aOld.Length(); |
|
416 TInt newLen = aNew.Length(); |
|
417 |
|
418 if (!oldLen) |
|
419 return rString; |
|
420 |
|
421 for (TInt pos = 0; pos < rString->Length(); pos += newLen) |
|
422 { |
|
423 TPtrC ptrC = rString->Mid(pos); |
|
424 TInt find = ptrC.Find(aOld); |
|
425 |
|
426 if (find == KErrNotFound) |
|
427 return rString; |
|
428 |
|
429 pos += find; |
|
430 |
|
431 if (newLen > oldLen) |
|
432 { |
|
433 rString = rString->ReAllocL(rString->Length() + newLen - oldLen); |
|
434 CleanupStack::Pop(); |
|
435 CleanupStack::PushL(rString); |
|
436 } |
|
437 |
|
438 TPtr ptr(rString->Des()); |
|
439 ptr.Replace(pos, oldLen, aNew); |
|
440 } |
|
441 |
|
442 return rString; |
|
443 } |
|
444 |