os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServResourceInterpreter.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServResourceInterpreter.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,161 @@
     1.4 +// Copyright (c) 2002-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +
    1.20 +#include <bautils.h>
    1.21 +#include "logservpanic.h"
    1.22 +#include "LogServResourceInterpreter.h"
    1.23 +
    1.24 +//LogWrap resoure file name
    1.25 +_LIT(KResourceFileWrapper, "\\resource\\logeng\\logwrap.rsc");
    1.26 +
    1.27 +const TInt KStringsArrayGranularity = 20;
    1.28 +
    1.29 +/**
    1.30 +Creates a CLogServResourceInterpreter instance.
    1.31 +The created CLogServResourceInterpreter instance will load into memory the content of the LogWrap
    1.32 +resource file.
    1.33 +  
    1.34 +@param aFs Reference to a file session object, used later for reading the content of the resource file.
    1.35 +
    1.36 +@leave  KErrNoMemory, an out of memory condition has occurred;
    1.37 +                      Note that the function may leave with database specific errors and
    1.38 +                      other system-wide error codes.
    1.39 +*/
    1.40 +CLogServResourceInterpreter* CLogServResourceInterpreter::NewL(RFs& aFs)
    1.41 +	{
    1.42 +	CLogServResourceInterpreter* self = new (ELeave) CLogServResourceInterpreter(aFs);
    1.43 +	CleanupStack::PushL(self);
    1.44 +	self->ConstructL();
    1.45 +	CleanupStack::Pop(self);
    1.46 +	return self;
    1.47 +	}
    1.48 +
    1.49 +/**
    1.50 +Destroys CLogServResourceInterpreter object deallocating all previously allocated resources
    1.51 +(the resource file, the memory for the loaded resource strings) 
    1.52 +*/
    1.53 +CLogServResourceInterpreter::~CLogServResourceInterpreter()
    1.54 +	{
    1.55 +	iFile.Close();
    1.56 +	for(TInt i=iStrings.Count()-1;i>=0;--i)
    1.57 +		{
    1.58 +		delete iStrings[i].iString;
    1.59 +		}
    1.60 +	iStrings.Close();
    1.61 +    }
    1.62 +
    1.63 +/**
    1.64 +Creates a resource reader object for the resource identified by the aId parameter.
    1.65 +
    1.66 +@param aReader Output parameter. The resource reader object, created by this call.
    1.67 +@param aId Resource item identifier.
    1.68 +
    1.69 +@leave  KErrNoMemory, an out of memory condition has occurred;
    1.70 +                      Note that the function may leave with database specific errors and
    1.71 +                      other system-wide error codes. 
    1.72 +*/
    1.73 +void CLogServResourceInterpreter::CreateResourceReaderLC(TResourceReader& aReader, TInt aId)
    1.74 +	{
    1.75 +	HBufC8* string = GetStringL(aId);		
    1.76 +	//Resource reader needs to be created with a copy of the string resource as otherwise
    1.77 +	//our "HBufC8*" string will be deleted when the resource reader is deleted!
    1.78 +	aReader.SetBuffer(string->AllocLC());
    1.79 +	}
    1.80 +
    1.81 +/**
    1.82 +Initializes CLogServResourceInterpreter data members with their default values.
    1.83 +@param aFs Reference to a file session object, used later for reading the content of the resource file.  
    1.84 +*/
    1.85 +CLogServResourceInterpreter::CLogServResourceInterpreter(RFs& aFs) :	
    1.86 +	iFs(aFs), 
    1.87 +	iStrings(KStringsArrayGranularity)
    1.88 +	{
    1.89 +	}
    1.90 +
    1.91 +/**
    1.92 +Loads the content of the LogWrap resource file.
    1.93 +*/
    1.94 +void CLogServResourceInterpreter::ConstructL()
    1.95 +	{
    1.96 +	// Find the resource file
    1.97 +  TFileName fileName;
    1.98 +	fileName.Copy(RProcess().FileName());
    1.99 +
   1.100 +  TParse parse;
   1.101 +  parse.Set(KResourceFileWrapper, &fileName, NULL);
   1.102 +	fileName = parse.FullName();
   1.103 +
   1.104 +	// Get language of resource file
   1.105 +	BaflUtils::NearestLanguageFile(iFs, fileName);
   1.106 +
   1.107 +	// Check the entry exists on this drive (e.g. if we are running the log server
   1.108 +	// from RAM, then default to the ROM if no RSC on the current drive exists).
   1.109 +	TEntry fsEntry;
   1.110 +	TInt err = iFs.Entry(fileName, fsEntry);
   1.111 +	if(err == KErrNotFound || err == KErrPathNotFound)
   1.112 +		{
   1.113 +		// Switch to ROM (we might already have been launching from the ROM,
   1.114 +		// in which case this will have no effect anyway).
   1.115 +		fileName[0] = 'Z';
   1.116 +		}
   1.117 +	iFile.OpenL(iFs, fileName);
   1.118 +	}
   1.119 +
   1.120 +/**
   1.121 +Attempts to read the resource string, identified by the aId parameter, from 
   1.122 +the iStrings array. If the resource stringis not there, it will be loaded from the
   1.123 +resource file and put in iStrings array.
   1.124 +@param aId Resource string identifier.
   1.125 +@return HBufC8 object containing the requested string 
   1.126 +@leave  KErrNoMemory, an out of memory condition has occurred;
   1.127 +                      Note that the function may leave with database specific errors and
   1.128 +                      other system-wide error codes. 
   1.129 +*/
   1.130 +HBufC8* CLogServResourceInterpreter::GetStringL(TInt aId)
   1.131 +	{
   1.132 +	TLinearOrder<TResourceString> order(&CLogServResourceInterpreter::Compare);
   1.133 +	//Try to find the requested resource in the cached strings
   1.134 +	TInt idx = iStrings.FindInOrder(TResourceString(aId, NULL), order);
   1.135 +	if(idx == KErrNotFound)
   1.136 +		{
   1.137 +		//String not in the cache, load it from the resource file
   1.138 +		iStrings.ReserveL(iStrings.Count() + 1);
   1.139 +		HBufC8* buf = iFile.AllocReadL(aId);
   1.140 +		TResourceString entry(aId, buf);
   1.141 +		#ifdef _DEBUG
   1.142 +		TInt err = 
   1.143 +		#endif	
   1.144 +		iStrings.InsertInOrder(entry, order);
   1.145 +		__ASSERT_DEBUG(err == KErrNone, User::Invariant());
   1.146 +		return buf;
   1.147 +		}
   1.148 +	return iStrings[idx].iString;
   1.149 +	}
   1.150 +
   1.151 +/**
   1.152 +Compares two iStrings entries.
   1.153 +Used for sorting/finding entries in iStrings sorted array.
   1.154 +
   1.155 +@param  aLeft  Left entry to be compared
   1.156 +@param  aRight Right entry to be compared
   1.157 +@return -1 aLeft is less than aRight, 0 entries are equal, 1 aLeft is bigger than aRight 
   1.158 +*/
   1.159 +TInt CLogServResourceInterpreter::Compare(const CLogServResourceInterpreter::TResourceString& aLeft, 
   1.160 + 										  const CLogServResourceInterpreter::TResourceString& aRight)
   1.161 +	{
   1.162 +	TInt64 res = (TInt64)aLeft.iId - (TInt64)aRight.iId;
   1.163 +	return res > 0LL ? 1 : (res < 0LL ? -1 : 0);
   1.164 +	}