os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServResourceInterpreter.cpp
First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
18 #include "logservpanic.h"
19 #include "LogServResourceInterpreter.h"
21 //LogWrap resoure file name
22 _LIT(KResourceFileWrapper, "\\resource\\logeng\\logwrap.rsc");
24 const TInt KStringsArrayGranularity = 20;
27 Creates a CLogServResourceInterpreter instance.
28 The created CLogServResourceInterpreter instance will load into memory the content of the LogWrap
31 @param aFs Reference to a file session object, used later for reading the content of the resource file.
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.
37 CLogServResourceInterpreter* CLogServResourceInterpreter::NewL(RFs& aFs)
39 CLogServResourceInterpreter* self = new (ELeave) CLogServResourceInterpreter(aFs);
40 CleanupStack::PushL(self);
42 CleanupStack::Pop(self);
47 Destroys CLogServResourceInterpreter object deallocating all previously allocated resources
48 (the resource file, the memory for the loaded resource strings)
50 CLogServResourceInterpreter::~CLogServResourceInterpreter()
53 for(TInt i=iStrings.Count()-1;i>=0;--i)
55 delete iStrings[i].iString;
61 Creates a resource reader object for the resource identified by the aId parameter.
63 @param aReader Output parameter. The resource reader object, created by this call.
64 @param aId Resource item identifier.
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.
70 void CLogServResourceInterpreter::CreateResourceReaderLC(TResourceReader& aReader, TInt aId)
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());
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.
82 CLogServResourceInterpreter::CLogServResourceInterpreter(RFs& aFs) :
84 iStrings(KStringsArrayGranularity)
89 Loads the content of the LogWrap resource file.
91 void CLogServResourceInterpreter::ConstructL()
93 // Find the resource file
95 fileName.Copy(RProcess().FileName());
98 parse.Set(KResourceFileWrapper, &fileName, NULL);
99 fileName = parse.FullName();
101 // Get language of resource file
102 BaflUtils::NearestLanguageFile(iFs, fileName);
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).
107 TInt err = iFs.Entry(fileName, fsEntry);
108 if(err == KErrNotFound || err == KErrPathNotFound)
110 // Switch to ROM (we might already have been launching from the ROM,
111 // in which case this will have no effect anyway).
114 iFile.OpenL(iFs, fileName);
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.
127 HBufC8* CLogServResourceInterpreter::GetStringL(TInt aId)
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)
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);
141 iStrings.InsertInOrder(entry, order);
142 __ASSERT_DEBUG(err == KErrNone, User::Invariant());
145 return iStrings[idx].iString;
149 Compares two iStrings entries.
150 Used for sorting/finding entries in iStrings sorted array.
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
156 TInt CLogServResourceInterpreter::Compare(const CLogServResourceInterpreter::TResourceString& aLeft,
157 const CLogServResourceInterpreter::TResourceString& aRight)
159 TInt64 res = (TInt64)aLeft.iId - (TInt64)aRight.iId;
160 return res > 0LL ? 1 : (res < 0LL ? -1 : 0);