os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServResourceInterpreter.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2002-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
sl@0
    17
#include <bautils.h>
sl@0
    18
#include "logservpanic.h"
sl@0
    19
#include "LogServResourceInterpreter.h"
sl@0
    20
sl@0
    21
//LogWrap resoure file name
sl@0
    22
_LIT(KResourceFileWrapper, "\\resource\\logeng\\logwrap.rsc");
sl@0
    23
sl@0
    24
const TInt KStringsArrayGranularity = 20;
sl@0
    25
sl@0
    26
/**
sl@0
    27
Creates a CLogServResourceInterpreter instance.
sl@0
    28
The created CLogServResourceInterpreter instance will load into memory the content of the LogWrap
sl@0
    29
resource file.
sl@0
    30
  
sl@0
    31
@param aFs Reference to a file session object, used later for reading the content of the resource file.
sl@0
    32
sl@0
    33
@leave  KErrNoMemory, an out of memory condition has occurred;
sl@0
    34
                      Note that the function may leave with database specific errors and
sl@0
    35
                      other system-wide error codes.
sl@0
    36
*/
sl@0
    37
CLogServResourceInterpreter* CLogServResourceInterpreter::NewL(RFs& aFs)
sl@0
    38
	{
sl@0
    39
	CLogServResourceInterpreter* self = new (ELeave) CLogServResourceInterpreter(aFs);
sl@0
    40
	CleanupStack::PushL(self);
sl@0
    41
	self->ConstructL();
sl@0
    42
	CleanupStack::Pop(self);
sl@0
    43
	return self;
sl@0
    44
	}
sl@0
    45
sl@0
    46
/**
sl@0
    47
Destroys CLogServResourceInterpreter object deallocating all previously allocated resources
sl@0
    48
(the resource file, the memory for the loaded resource strings) 
sl@0
    49
*/
sl@0
    50
CLogServResourceInterpreter::~CLogServResourceInterpreter()
sl@0
    51
	{
sl@0
    52
	iFile.Close();
sl@0
    53
	for(TInt i=iStrings.Count()-1;i>=0;--i)
sl@0
    54
		{
sl@0
    55
		delete iStrings[i].iString;
sl@0
    56
		}
sl@0
    57
	iStrings.Close();
sl@0
    58
    }
sl@0
    59
sl@0
    60
/**
sl@0
    61
Creates a resource reader object for the resource identified by the aId parameter.
sl@0
    62
sl@0
    63
@param aReader Output parameter. The resource reader object, created by this call.
sl@0
    64
@param aId Resource item identifier.
sl@0
    65
sl@0
    66
@leave  KErrNoMemory, an out of memory condition has occurred;
sl@0
    67
                      Note that the function may leave with database specific errors and
sl@0
    68
                      other system-wide error codes. 
sl@0
    69
*/
sl@0
    70
void CLogServResourceInterpreter::CreateResourceReaderLC(TResourceReader& aReader, TInt aId)
sl@0
    71
	{
sl@0
    72
	HBufC8* string = GetStringL(aId);		
sl@0
    73
	//Resource reader needs to be created with a copy of the string resource as otherwise
sl@0
    74
	//our "HBufC8*" string will be deleted when the resource reader is deleted!
sl@0
    75
	aReader.SetBuffer(string->AllocLC());
sl@0
    76
	}
sl@0
    77
sl@0
    78
/**
sl@0
    79
Initializes CLogServResourceInterpreter data members with their default values.
sl@0
    80
@param aFs Reference to a file session object, used later for reading the content of the resource file.  
sl@0
    81
*/
sl@0
    82
CLogServResourceInterpreter::CLogServResourceInterpreter(RFs& aFs) :	
sl@0
    83
	iFs(aFs), 
sl@0
    84
	iStrings(KStringsArrayGranularity)
sl@0
    85
	{
sl@0
    86
	}
sl@0
    87
sl@0
    88
/**
sl@0
    89
Loads the content of the LogWrap resource file.
sl@0
    90
*/
sl@0
    91
void CLogServResourceInterpreter::ConstructL()
sl@0
    92
	{
sl@0
    93
	// Find the resource file
sl@0
    94
  TFileName fileName;
sl@0
    95
	fileName.Copy(RProcess().FileName());
sl@0
    96
sl@0
    97
  TParse parse;
sl@0
    98
  parse.Set(KResourceFileWrapper, &fileName, NULL);
sl@0
    99
	fileName = parse.FullName();
sl@0
   100
sl@0
   101
	// Get language of resource file
sl@0
   102
	BaflUtils::NearestLanguageFile(iFs, fileName);
sl@0
   103
sl@0
   104
	// Check the entry exists on this drive (e.g. if we are running the log server
sl@0
   105
	// from RAM, then default to the ROM if no RSC on the current drive exists).
sl@0
   106
	TEntry fsEntry;
sl@0
   107
	TInt err = iFs.Entry(fileName, fsEntry);
sl@0
   108
	if(err == KErrNotFound || err == KErrPathNotFound)
sl@0
   109
		{
sl@0
   110
		// Switch to ROM (we might already have been launching from the ROM,
sl@0
   111
		// in which case this will have no effect anyway).
sl@0
   112
		fileName[0] = 'Z';
sl@0
   113
		}
sl@0
   114
	iFile.OpenL(iFs, fileName);
sl@0
   115
	}
sl@0
   116
sl@0
   117
/**
sl@0
   118
Attempts to read the resource string, identified by the aId parameter, from 
sl@0
   119
the iStrings array. If the resource stringis not there, it will be loaded from the
sl@0
   120
resource file and put in iStrings array.
sl@0
   121
@param aId Resource string identifier.
sl@0
   122
@return HBufC8 object containing the requested string 
sl@0
   123
@leave  KErrNoMemory, an out of memory condition has occurred;
sl@0
   124
                      Note that the function may leave with database specific errors and
sl@0
   125
                      other system-wide error codes. 
sl@0
   126
*/
sl@0
   127
HBufC8* CLogServResourceInterpreter::GetStringL(TInt aId)
sl@0
   128
	{
sl@0
   129
	TLinearOrder<TResourceString> order(&CLogServResourceInterpreter::Compare);
sl@0
   130
	//Try to find the requested resource in the cached strings
sl@0
   131
	TInt idx = iStrings.FindInOrder(TResourceString(aId, NULL), order);
sl@0
   132
	if(idx == KErrNotFound)
sl@0
   133
		{
sl@0
   134
		//String not in the cache, load it from the resource file
sl@0
   135
		iStrings.ReserveL(iStrings.Count() + 1);
sl@0
   136
		HBufC8* buf = iFile.AllocReadL(aId);
sl@0
   137
		TResourceString entry(aId, buf);
sl@0
   138
		#ifdef _DEBUG
sl@0
   139
		TInt err = 
sl@0
   140
		#endif	
sl@0
   141
		iStrings.InsertInOrder(entry, order);
sl@0
   142
		__ASSERT_DEBUG(err == KErrNone, User::Invariant());
sl@0
   143
		return buf;
sl@0
   144
		}
sl@0
   145
	return iStrings[idx].iString;
sl@0
   146
	}
sl@0
   147
sl@0
   148
/**
sl@0
   149
Compares two iStrings entries.
sl@0
   150
Used for sorting/finding entries in iStrings sorted array.
sl@0
   151
sl@0
   152
@param  aLeft  Left entry to be compared
sl@0
   153
@param  aRight Right entry to be compared
sl@0
   154
@return -1 aLeft is less than aRight, 0 entries are equal, 1 aLeft is bigger than aRight 
sl@0
   155
*/
sl@0
   156
TInt CLogServResourceInterpreter::Compare(const CLogServResourceInterpreter::TResourceString& aLeft, 
sl@0
   157
 										  const CLogServResourceInterpreter::TResourceString& aRight)
sl@0
   158
	{
sl@0
   159
	TInt64 res = (TInt64)aLeft.iId - (TInt64)aRight.iId;
sl@0
   160
	return res > 0LL ? 1 : (res < 0LL ? -1 : 0);
sl@0
   161
	}