|
1 /* |
|
2 * Copyright (c) 2004-2006 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: Data buffer loader for TTPHwDevice test executable |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "ttsplugindataloader.h" |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 // CONSTANTS |
|
26 |
|
27 _LIT( KDriveZ, "z:" ); |
|
28 |
|
29 // Assuming language data file name is something like SRSF_2_x[y|yz].bin |
|
30 const TInt KBeginningOfLanguageId = 7; |
|
31 #ifdef __SIND_AUTOCONF_LID |
|
32 const TUint32 KTtpAutoConfLidPackageType = 1; |
|
33 const TUint32 KTtpAutoConfLidPackageID = 1; |
|
34 const TInt KLidBufGranularity = 1000; |
|
35 #endif // __SIND_AUTOCONF_LID |
|
36 |
|
37 // ============================ MEMBER FUNCTIONS =============================== |
|
38 |
|
39 // ----------------------------------------------------------------------------- |
|
40 // CTTSDataLoaderLoader::CTTSDataLoader |
|
41 // C++ default constructor can NOT contain any code, that |
|
42 // might leave. |
|
43 // ----------------------------------------------------------------------------- |
|
44 // |
|
45 CTTSDataLoader::CTTSDataLoader( RFs& aFs, |
|
46 TFileName& aGeneralFileName, |
|
47 TFileName& aPrefixOfTTPFileName, |
|
48 TFileName& aPostfixOfTTPFileName, |
|
49 TFileName& aTTSFilename, |
|
50 TFileName& aPostfixOfTTSFilename ) : |
|
51 iGeneralFileName( aGeneralFileName ), |
|
52 iPrefixOfTTPFileName( aPrefixOfTTPFileName ), |
|
53 iPostfixOfTTPFileName( aPostfixOfTTPFileName ), |
|
54 iTTSFileName( aTTSFilename ), |
|
55 iPostfixOfTTSFileName( aPostfixOfTTSFilename ), |
|
56 iFs( aFs ) |
|
57 { |
|
58 // Nothing |
|
59 } |
|
60 |
|
61 // ----------------------------------------------------------------------------- |
|
62 // CTTSDataLoader::ConstructL |
|
63 // Symbian 2nd phase constructor can leave. |
|
64 // ----------------------------------------------------------------------------- |
|
65 // |
|
66 void CTTSDataLoader::ConstructL() |
|
67 { |
|
68 // Nothing |
|
69 } |
|
70 |
|
71 // ----------------------------------------------------------------------------- |
|
72 // CTTSDataLoader::NewL |
|
73 // Two-phased constructor. |
|
74 // ----------------------------------------------------------------------------- |
|
75 // |
|
76 CTTSDataLoader* CTTSDataLoader::NewL( RFs& aFs, |
|
77 TFileName& aGeneralFileName, |
|
78 TFileName& aPrefixOfTTPFileName, |
|
79 TFileName& aPostfixOfTTPFileName, |
|
80 TFileName& aTTSFilename, |
|
81 TFileName& aPostfixOfTTSFilename ) |
|
82 { |
|
83 CTTSDataLoader* self = new ( ELeave ) CTTSDataLoader( aFs, |
|
84 aGeneralFileName, |
|
85 aPrefixOfTTPFileName, |
|
86 aPostfixOfTTPFileName, |
|
87 aTTSFilename, |
|
88 aPostfixOfTTSFilename ); |
|
89 |
|
90 CleanupStack::PushL( self ); |
|
91 self->ConstructL(); |
|
92 CleanupStack::Pop( self ); |
|
93 |
|
94 return self; |
|
95 } |
|
96 |
|
97 // ----------------------------------------------------------------------------- |
|
98 // CTTSDataLoader::~CTTSDataLoader |
|
99 // Destructor. |
|
100 // ----------------------------------------------------------------------------- |
|
101 // |
|
102 CTTSDataLoader::~CTTSDataLoader() |
|
103 { |
|
104 } |
|
105 |
|
106 // ----------------------------------------------------------------------------- |
|
107 // CTTSDataLoader::LoadGeneralData |
|
108 // Load general TTP data |
|
109 // ----------------------------------------------------------------------------- |
|
110 // |
|
111 HBufC8* CTTSDataLoader::LoadGeneralData( const TInt aPackageType, |
|
112 const TInt aDataID, |
|
113 TUint32 aStartPosition, |
|
114 TUint32 aEndPosition ) |
|
115 { |
|
116 TFileName fileName( iGeneralFileName ); |
|
117 fileName.AppendNum( aPackageType ); |
|
118 fileName.Append( _L( "_" ) ); |
|
119 fileName.AppendNum( aDataID ); |
|
120 fileName.Append( iPostfixOfTTPFileName ); |
|
121 |
|
122 HBufC8* buffer( NULL ); |
|
123 #ifdef __SIND_AUTOCONF_LID |
|
124 |
|
125 // If LID data is requested, construct it from installed LID binary files |
|
126 // Otherwise run normal data loading |
|
127 |
|
128 if( aPackageType == KTtpAutoConfLidPackageType && |
|
129 aDataID == KTtpAutoConfLidPackageID ) |
|
130 { |
|
131 TRAPD( err, buffer = ConstructLidDataL() ); |
|
132 if ( !err ) |
|
133 { |
|
134 return buffer; |
|
135 } |
|
136 } |
|
137 |
|
138 #endif // __SIND_AUTOCONF_LID |
|
139 TRAPD( error, buffer = DoLoadL( fileName, aStartPosition, aEndPosition ) ); |
|
140 if ( error == KErrNone ) |
|
141 { |
|
142 return buffer; |
|
143 } |
|
144 else |
|
145 { |
|
146 return NULL; |
|
147 } |
|
148 } |
|
149 |
|
150 // ----------------------------------------------------------------------------- |
|
151 // CTTSDataLoader::LoadLanguageData |
|
152 // Load TTP language data |
|
153 // ----------------------------------------------------------------------------- |
|
154 // |
|
155 HBufC8* CTTSDataLoader::LoadLanguageData( const TInt aPackageType, |
|
156 TLanguage aLanguage, |
|
157 TUint32 aStartPosition, |
|
158 TUint32 aEndPosition ) |
|
159 { |
|
160 TFileName fileName( iPrefixOfTTPFileName ); |
|
161 fileName.AppendNum( aPackageType ); |
|
162 fileName.Append( _L( "_" ) ); |
|
163 fileName.AppendNum( ( TInt ) aLanguage ); |
|
164 fileName.Append( iPostfixOfTTPFileName ); |
|
165 |
|
166 HBufC8* buffer( NULL ); |
|
167 TRAPD( error, buffer = DoLoadL( fileName, aStartPosition, aEndPosition ) ); |
|
168 if ( error == KErrNone ) |
|
169 { |
|
170 return buffer; |
|
171 } |
|
172 else |
|
173 { |
|
174 return NULL; |
|
175 } |
|
176 } |
|
177 |
|
178 // ----------------------------------------------------------------------------- |
|
179 // CTTSDataLoader::LoadTtsData |
|
180 // Load TTS general/language data |
|
181 // ----------------------------------------------------------------------------- |
|
182 // |
|
183 HBufC8* CTTSDataLoader::LoadTtsData( const TInt aPackageType, |
|
184 const TInt aDataID, |
|
185 TUint32 aStartPosition, |
|
186 TUint32 aEndPosition ) |
|
187 { |
|
188 TFileName fileName( iTTSFileName ); |
|
189 fileName.AppendNum( aPackageType ); |
|
190 fileName.Append( _L( "_" ) ); |
|
191 fileName.AppendNum( aDataID ); |
|
192 fileName.Append( iPostfixOfTTSFileName ); |
|
193 |
|
194 HBufC8* buffer( NULL ); |
|
195 |
|
196 #ifdef __SIND_AUTOCONF_LID |
|
197 |
|
198 // If LID data is requested, construct it from installed LID binary files |
|
199 // Otherwise run normal data loading |
|
200 if( aPackageType == KTtpAutoConfLidPackageType && |
|
201 aDataID == KTtpAutoConfLidPackageID ) |
|
202 { |
|
203 TRAPD( err, buffer = ConstructLidDataL() ); |
|
204 if ( !err ) |
|
205 { |
|
206 return buffer; |
|
207 } |
|
208 } |
|
209 #endif // __SIND_AUTOCONF_LID |
|
210 |
|
211 |
|
212 TRAPD( error, buffer = DoLoadL( fileName, aStartPosition, aEndPosition ) ); |
|
213 if ( error == KErrNone ) |
|
214 { |
|
215 return buffer; |
|
216 } |
|
217 else |
|
218 { |
|
219 return NULL; |
|
220 } |
|
221 } |
|
222 |
|
223 // ----------------------------------------------------------------------------- |
|
224 // CTTSDataLoader::GetSupportedLanguagesL |
|
225 // Fill aLanguages with supported language ids |
|
226 // ----------------------------------------------------------------------------- |
|
227 // |
|
228 void CTTSDataLoader::GetSupportedLanguagesL( RArray<TLanguage>& aLanguages ) |
|
229 { |
|
230 // Get pts languages |
|
231 TFileName mask; |
|
232 mask.Append( KDriveZ ); |
|
233 mask.Append( iTTSFileName ); |
|
234 mask.Append( _L( "2_*" ) ); |
|
235 |
|
236 DoGetSupportedLanguagesL( aLanguages, mask); |
|
237 |
|
238 // Get ttp languages |
|
239 RArray<TLanguage> ttpLanguages; |
|
240 CleanupClosePushL( ttpLanguages ); |
|
241 |
|
242 mask.Zero(); |
|
243 mask.Append( KDriveZ ); |
|
244 mask.Append( iTTSFileName ); |
|
245 mask.Append( _L( "0_*" ) ); |
|
246 |
|
247 DoGetSupportedLanguagesL( ttpLanguages, mask); |
|
248 |
|
249 // remove languages which are not supported in ttp |
|
250 for ( TInt i(0); i < aLanguages.Count(); i++ ) |
|
251 { |
|
252 if ( ttpLanguages.Find( aLanguages[i] ) == KErrNotFound ) |
|
253 { |
|
254 aLanguages.Remove( i ); |
|
255 } |
|
256 } |
|
257 CleanupStack::PopAndDestroy( &ttpLanguages ); |
|
258 } |
|
259 |
|
260 // ----------------------------------------------------------------------------- |
|
261 // CTTSDataLoader::DoLoadL |
|
262 // Loads the given file. |
|
263 // ----------------------------------------------------------------------------- |
|
264 // |
|
265 HBufC8* CTTSDataLoader::DoLoadL( TFileName& iFile, |
|
266 TUint32 aStartPosition, |
|
267 TUint32 aEndPosition ) |
|
268 { |
|
269 TInt fileSize( 0 ); |
|
270 RFile dataFile; |
|
271 TInt readLength; |
|
272 |
|
273 TFileName completeFile; |
|
274 completeFile.Append( KDriveZ ); |
|
275 completeFile.Append( iFile ); |
|
276 |
|
277 |
|
278 // Try to open file from Z drive |
|
279 TInt err = dataFile.Open( iFs, completeFile, EFileRead ); |
|
280 |
|
281 if ( err != KErrNone ) |
|
282 { |
|
283 return NULL; |
|
284 } |
|
285 |
|
286 |
|
287 // Try to resolve how much should be read |
|
288 readLength = aEndPosition - aStartPosition; |
|
289 if ( dataFile.Size( fileSize ) != KErrNone ) |
|
290 { |
|
291 dataFile.Close(); |
|
292 return NULL; |
|
293 } |
|
294 if ( fileSize == 0 ) |
|
295 { |
|
296 dataFile.Close(); |
|
297 return NULL; |
|
298 } |
|
299 |
|
300 if ( aEndPosition != KMaxTUint32 ) |
|
301 { |
|
302 // Cannot read more than filesize |
|
303 if ( ( readLength > fileSize ) || ( readLength < 0 ) ) |
|
304 { |
|
305 readLength = fileSize; |
|
306 } |
|
307 } |
|
308 else |
|
309 { |
|
310 readLength = fileSize; |
|
311 } |
|
312 |
|
313 // Seek the starting point |
|
314 // Casting needed to convert from uint to int which is needed by Seek() |
|
315 TInt startPosition = (TInt)aStartPosition; |
|
316 dataFile.Seek( ESeekStart, startPosition ); |
|
317 |
|
318 // Reserve enough memory for the data |
|
319 HBufC8* fileBuffer = HBufC8::NewL( readLength ); |
|
320 |
|
321 TPtr8 bufferPtr( fileBuffer->Des() ); |
|
322 |
|
323 err = dataFile.Read( bufferPtr ); |
|
324 |
|
325 dataFile.Close(); |
|
326 |
|
327 if ( err != KErrNone ) |
|
328 { |
|
329 delete fileBuffer; |
|
330 return NULL; |
|
331 } |
|
332 |
|
333 return fileBuffer; |
|
334 } |
|
335 |
|
336 // ----------------------------------------------------------------------------- |
|
337 // CTTSDataLoader::DoGetSupportedLanguagesL |
|
338 // Fill aLanguages with supported language ids |
|
339 // ----------------------------------------------------------------------------- |
|
340 // |
|
341 void CTTSDataLoader::DoGetSupportedLanguagesL( RArray<TLanguage>& aLanguages, |
|
342 TFileName& aMask ) |
|
343 { |
|
344 CDir *directoryList( NULL ); |
|
345 TInt error = iFs.GetDir( aMask, KEntryAttDir, ESortByName, directoryList ); |
|
346 CleanupStack::PushL( directoryList ); |
|
347 |
|
348 if ( !error ) |
|
349 { |
|
350 TInt count = directoryList->Count(); |
|
351 |
|
352 for ( TInt i( 0 ); i < count; i++ ) |
|
353 { |
|
354 TPtrC tmp = (*directoryList)[i].iName; |
|
355 |
|
356 TInt64 id; |
|
357 TLex parser( tmp.Mid( KBeginningOfLanguageId , |
|
358 tmp.Find( _L(".") ) - KBeginningOfLanguageId ) ); |
|
359 |
|
360 User::LeaveIfError( parser.Val( id) ); |
|
361 |
|
362 TLanguage lang( (TLanguage)id ); |
|
363 |
|
364 if ( aLanguages.Find( lang ) == KErrNotFound ) |
|
365 { |
|
366 aLanguages.AppendL( lang ); |
|
367 } |
|
368 } |
|
369 } |
|
370 |
|
371 CleanupStack::PopAndDestroy( directoryList ); |
|
372 } |
|
373 |
|
374 // ----------------------------------------------------------------------------- |
|
375 // CTTSDataLoader::PackageName |
|
376 // Construct filename for requested package |
|
377 // ----------------------------------------------------------------------------- |
|
378 // |
|
379 void CTTSDataLoader::PackageName (TFileName &aFileName, TUint32 aPackageType, TUint32 aPackageID) |
|
380 { |
|
381 // Resolve the name of the file |
|
382 // filename is <iPrefixOfTTPFileName>_<PackageType>_<aPackageID><iPostfixOfFileName> |
|
383 |
|
384 aFileName.Copy( iPrefixOfTTPFileName ); // path/SRSF_ |
|
385 aFileName.AppendNumUC( aPackageType ); // path/SRSF_1 |
|
386 aFileName.Append( _L( "_" ) ); // path/SRSF_1_ |
|
387 aFileName.AppendNumUC( aPackageID ); // path/SRSF_1_326 |
|
388 aFileName.Append( iPostfixOfTTPFileName ); // path/SRSF_1_326.bin |
|
389 } |
|
390 |
|
391 #ifdef __SIND_AUTOCONF_LID |
|
392 // ----------------------------------------------------------------------------- |
|
393 // CTTSDataLoader::AppendLoadL |
|
394 // Load language package file to the end of buffer |
|
395 // ----------------------------------------------------------------------------- |
|
396 // |
|
397 void CTTSDataLoader::AppendLoadL( CBufBase *aBuffer, |
|
398 TUint32 aPackageType, |
|
399 TUint32 aPackageID ) |
|
400 { |
|
401 // Resolve filename |
|
402 TFileName fileName; |
|
403 PackageName (fileName, aPackageType, aPackageID); |
|
404 |
|
405 // Load data |
|
406 HBufC8* buffer = NULL; |
|
407 |
|
408 TRAPD( err, buffer = DoLoadL( fileName, 0, KMaxTUint32 )); // IMPLICIT: LID packages are loaded completely |
|
409 |
|
410 if( err == KErrNone && buffer ) |
|
411 { |
|
412 CleanupStack::PushL( buffer ); // ResizeL may leave below |
|
413 |
|
414 TInt prevSize = aBuffer->Size(); // size before resize |
|
415 TInt newSize = aBuffer->Size() + buffer->Size(); // new size for lid data buffer |
|
416 |
|
417 aBuffer->ResizeL( newSize ); // resize lid data buffer |
|
418 aBuffer->Write( prevSize, *buffer ); // append data from read buffer |
|
419 |
|
420 CleanupStack::PopAndDestroy(buffer); // buffer copied -> free |
|
421 } |
|
422 } |
|
423 |
|
424 |
|
425 |
|
426 // ----------------------------------------------------------------------------- |
|
427 // CTTSDataLoader::LoadSysLangPackagesL |
|
428 // Load LID language data to a buffer according to installed system languages |
|
429 // ----------------------------------------------------------------------------- |
|
430 // |
|
431 void CTTSDataLoader::SysLangPackagesToBufferL( CBufBase *aBuffer ) |
|
432 { |
|
433 |
|
434 // Get installed languages |
|
435 RArray<TLanguage> languages; |
|
436 CleanupClosePushL( languages ); |
|
437 |
|
438 TFileName mask; |
|
439 mask.Append( KDriveZ ); |
|
440 mask.Append( iTTSFileName ); |
|
441 mask.Append( _L( "1_*" ) ); |
|
442 DoGetSupportedLanguagesL( languages, mask); |
|
443 |
|
444 // Loop over installed languages |
|
445 for ( TInt i = 0 ; i < languages.Count(); i++ ) |
|
446 { |
|
447 AppendLoadL(aBuffer, KTtpAutoConfLidPackageType, languages[i] ); |
|
448 }// next language |
|
449 |
|
450 CleanupStack::PopAndDestroy( &languages ); |
|
451 } |
|
452 |
|
453 // ----------------------------------------------------------------------------- |
|
454 // CTTSDataLoader::ConstructLidDataL |
|
455 // Load LID language data to a buffer according to installed system languages |
|
456 // ----------------------------------------------------------------------------- |
|
457 // |
|
458 HBufC8* CTTSDataLoader::ConstructLidDataL() |
|
459 { |
|
460 CBufBase* lidDataBuffer = CBufSeg::NewL( KLidBufGranularity ) ; // accumulates the binary data from lid model files |
|
461 CleanupStack::PushL( lidDataBuffer ) ; |
|
462 |
|
463 SysLangPackagesToBufferL (lidDataBuffer); |
|
464 |
|
465 // Prepare returnable data |
|
466 HBufC8* dataBuffer = NULL; |
|
467 |
|
468 if( lidDataBuffer->Size() > 0 ) |
|
469 { |
|
470 // If any data was loaded, create a buffer for it |
|
471 dataBuffer = HBufC8::NewL( lidDataBuffer->Size() ); // create a returnable buffer |
|
472 |
|
473 TPtr8 dataBufferPtr = dataBuffer->Des(); // get a modifiable pointer to it |
|
474 lidDataBuffer->Read( 0, dataBufferPtr, lidDataBuffer->Size() ); // put the lid data into it |
|
475 } |
|
476 |
|
477 CleanupStack::PopAndDestroy( lidDataBuffer ) ; |
|
478 |
|
479 return dataBuffer; |
|
480 } |
|
481 #endif // __SIND_AUTOCONF_LID |
|
482 |
|
483 // End of File |