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