sl@0: // Copyright (c) 2002-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: sl@0: #include sl@0: #include "logservpanic.h" sl@0: #include "LogServResourceInterpreter.h" sl@0: sl@0: //LogWrap resoure file name sl@0: _LIT(KResourceFileWrapper, "\\resource\\logeng\\logwrap.rsc"); sl@0: sl@0: const TInt KStringsArrayGranularity = 20; sl@0: sl@0: /** sl@0: Creates a CLogServResourceInterpreter instance. sl@0: The created CLogServResourceInterpreter instance will load into memory the content of the LogWrap sl@0: resource file. sl@0: sl@0: @param aFs Reference to a file session object, used later for reading the content of the resource file. sl@0: sl@0: @leave KErrNoMemory, an out of memory condition has occurred; sl@0: Note that the function may leave with database specific errors and sl@0: other system-wide error codes. sl@0: */ sl@0: CLogServResourceInterpreter* CLogServResourceInterpreter::NewL(RFs& aFs) sl@0: { sl@0: CLogServResourceInterpreter* self = new (ELeave) CLogServResourceInterpreter(aFs); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: /** sl@0: Destroys CLogServResourceInterpreter object deallocating all previously allocated resources sl@0: (the resource file, the memory for the loaded resource strings) sl@0: */ sl@0: CLogServResourceInterpreter::~CLogServResourceInterpreter() sl@0: { sl@0: iFile.Close(); sl@0: for(TInt i=iStrings.Count()-1;i>=0;--i) sl@0: { sl@0: delete iStrings[i].iString; sl@0: } sl@0: iStrings.Close(); sl@0: } sl@0: sl@0: /** sl@0: Creates a resource reader object for the resource identified by the aId parameter. sl@0: sl@0: @param aReader Output parameter. The resource reader object, created by this call. sl@0: @param aId Resource item identifier. sl@0: sl@0: @leave KErrNoMemory, an out of memory condition has occurred; sl@0: Note that the function may leave with database specific errors and sl@0: other system-wide error codes. sl@0: */ sl@0: void CLogServResourceInterpreter::CreateResourceReaderLC(TResourceReader& aReader, TInt aId) sl@0: { sl@0: HBufC8* string = GetStringL(aId); sl@0: //Resource reader needs to be created with a copy of the string resource as otherwise sl@0: //our "HBufC8*" string will be deleted when the resource reader is deleted! sl@0: aReader.SetBuffer(string->AllocLC()); sl@0: } sl@0: sl@0: /** sl@0: Initializes CLogServResourceInterpreter data members with their default values. sl@0: @param aFs Reference to a file session object, used later for reading the content of the resource file. sl@0: */ sl@0: CLogServResourceInterpreter::CLogServResourceInterpreter(RFs& aFs) : sl@0: iFs(aFs), sl@0: iStrings(KStringsArrayGranularity) sl@0: { sl@0: } sl@0: sl@0: /** sl@0: Loads the content of the LogWrap resource file. sl@0: */ sl@0: void CLogServResourceInterpreter::ConstructL() sl@0: { sl@0: // Find the resource file sl@0: TFileName fileName; sl@0: fileName.Copy(RProcess().FileName()); sl@0: sl@0: TParse parse; sl@0: parse.Set(KResourceFileWrapper, &fileName, NULL); sl@0: fileName = parse.FullName(); sl@0: sl@0: // Get language of resource file sl@0: BaflUtils::NearestLanguageFile(iFs, fileName); sl@0: sl@0: // Check the entry exists on this drive (e.g. if we are running the log server sl@0: // from RAM, then default to the ROM if no RSC on the current drive exists). sl@0: TEntry fsEntry; sl@0: TInt err = iFs.Entry(fileName, fsEntry); sl@0: if(err == KErrNotFound || err == KErrPathNotFound) sl@0: { sl@0: // Switch to ROM (we might already have been launching from the ROM, sl@0: // in which case this will have no effect anyway). sl@0: fileName[0] = 'Z'; sl@0: } sl@0: iFile.OpenL(iFs, fileName); sl@0: } sl@0: sl@0: /** sl@0: Attempts to read the resource string, identified by the aId parameter, from sl@0: the iStrings array. If the resource stringis not there, it will be loaded from the sl@0: resource file and put in iStrings array. sl@0: @param aId Resource string identifier. sl@0: @return HBufC8 object containing the requested string sl@0: @leave KErrNoMemory, an out of memory condition has occurred; sl@0: Note that the function may leave with database specific errors and sl@0: other system-wide error codes. sl@0: */ sl@0: HBufC8* CLogServResourceInterpreter::GetStringL(TInt aId) sl@0: { sl@0: TLinearOrder order(&CLogServResourceInterpreter::Compare); sl@0: //Try to find the requested resource in the cached strings sl@0: TInt idx = iStrings.FindInOrder(TResourceString(aId, NULL), order); sl@0: if(idx == KErrNotFound) sl@0: { sl@0: //String not in the cache, load it from the resource file sl@0: iStrings.ReserveL(iStrings.Count() + 1); sl@0: HBufC8* buf = iFile.AllocReadL(aId); sl@0: TResourceString entry(aId, buf); sl@0: #ifdef _DEBUG sl@0: TInt err = sl@0: #endif sl@0: iStrings.InsertInOrder(entry, order); sl@0: __ASSERT_DEBUG(err == KErrNone, User::Invariant()); sl@0: return buf; sl@0: } sl@0: return iStrings[idx].iString; sl@0: } sl@0: sl@0: /** sl@0: Compares two iStrings entries. sl@0: Used for sorting/finding entries in iStrings sorted array. sl@0: sl@0: @param aLeft Left entry to be compared sl@0: @param aRight Right entry to be compared sl@0: @return -1 aLeft is less than aRight, 0 entries are equal, 1 aLeft is bigger than aRight sl@0: */ sl@0: TInt CLogServResourceInterpreter::Compare(const CLogServResourceInterpreter::TResourceString& aLeft, sl@0: const CLogServResourceInterpreter::TResourceString& aRight) sl@0: { sl@0: TInt64 res = (TInt64)aLeft.iId - (TInt64)aRight.iId; sl@0: return res > 0LL ? 1 : (res < 0LL ? -1 : 0); sl@0: }