|
1 // Copyright (c) 2002-2009 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 // Constants |
|
22 const TInt KLogResourceFileGranularity = 2; |
|
23 |
|
24 // Literal constants |
|
25 _LIT(KResourceFileClient,"\\private\\101f401d\\logserv.rsc"); |
|
26 _LIT(KResourceFileWrapper,"\\resource\\logeng\\logwrap.rsc"); |
|
27 |
|
28 |
|
29 ///////////////////////////////////////////////////////////////////////////////////////// |
|
30 // -----> CLogServResourceInterpreter (source) |
|
31 ///////////////////////////////////////////////////////////////////////////////////////// |
|
32 CLogServResourceInterpreter::CLogServResourceInterpreter(RFs& aFsSession) : |
|
33 iFsSession(aFsSession), iResourceFiles(KLogResourceFileGranularity), |
|
34 iResourceStringCache(CResourceStringCacheEntry::Offset()), |
|
35 iResourceStringCacheIter(iResourceStringCache) |
|
36 { |
|
37 } |
|
38 |
|
39 CLogServResourceInterpreter::~CLogServResourceInterpreter() |
|
40 { |
|
41 const TInt count = iResourceFiles.Count(); |
|
42 for(TInt i=0; i<count; i++) |
|
43 { |
|
44 TResourceFileEntry& entry = iResourceFiles[i]; |
|
45 entry.iFile.Close(); |
|
46 } |
|
47 iResourceFiles.Close(); |
|
48 |
|
49 // Iterate through list of cache entries deleting them in turn. |
|
50 CResourceStringCacheEntry* item ; |
|
51 iResourceStringCacheIter.SetToFirst() ; |
|
52 while ((item = iResourceStringCacheIter++) != NULL) |
|
53 { |
|
54 iResourceStringCache.Remove(*item); |
|
55 delete item; |
|
56 } |
|
57 } |
|
58 |
|
59 void CLogServResourceInterpreter::ConstructL() |
|
60 { |
|
61 // Load standard resource files |
|
62 LoadResourceFileL(KResourceFileClient, ELogServer); |
|
63 LoadResourceFileL(KResourceFileWrapper, ELogWrap); |
|
64 } |
|
65 |
|
66 CLogServResourceInterpreter* CLogServResourceInterpreter::NewL(RFs& aFsSession) |
|
67 { |
|
68 CLogServResourceInterpreter* self = new(ELeave) CLogServResourceInterpreter(aFsSession); |
|
69 CleanupStack::PushL(self); |
|
70 self->ConstructL(); |
|
71 CleanupStack::Pop(self); |
|
72 return self; |
|
73 } |
|
74 |
|
75 ///////////////////////////////////////////////////////////////////////////////////////// |
|
76 ///////////////////////////////////////////////////////////////////////////////////////// |
|
77 ///////////////////////////////////////////////////////////////////////////////////////// |
|
78 |
|
79 void CLogServResourceInterpreter::CreateResourceReaderLC(TResourceReader& aReader, TInt aId, TResourceType aType) |
|
80 { |
|
81 HBufC8* buf = GetResourceBufferL(aId, aType) ; |
|
82 |
|
83 // Resource reader needs to be created with a copy of the cache buffer as otherwise |
|
84 // our cache buffer will be deleted when the resource reader is deleted! |
|
85 aReader.SetBuffer(buf->AllocLC()); |
|
86 } |
|
87 |
|
88 HBufC8* CLogServResourceInterpreter::GetResourceBufferL(TInt aId, TResourceType aType) |
|
89 { |
|
90 |
|
91 // Attempt to find the requested resource in the cache |
|
92 CResourceStringCacheEntry* cacheEntry = FindCachedResourceString(aId, aType) ; |
|
93 |
|
94 HBufC8* buf ; |
|
95 if (!cacheEntry) |
|
96 { |
|
97 // Not found in cache, load from resource file and add to the |
|
98 // linked list which forms the cache. |
|
99 buf = ResourceFileForType(aType).AllocReadLC(aId); |
|
100 cacheEntry = CResourceStringCacheEntry::NewL(aId, aType, buf) ; |
|
101 iResourceStringCache.AddLast(*cacheEntry) ; |
|
102 |
|
103 // buf is now the responsibility of iResourceStringCache and |
|
104 // will be tidied up by the destructor... |
|
105 CleanupStack::Pop(buf); |
|
106 } |
|
107 else |
|
108 { |
|
109 buf = cacheEntry->ResourceString(); |
|
110 } |
|
111 return buf ; |
|
112 } |
|
113 |
|
114 ///////////////////////////////////////////////////////////////////////////////////////// |
|
115 ///////////////////////////////////////////////////////////////////////////////////////// |
|
116 ///////////////////////////////////////////////////////////////////////////////////////// |
|
117 |
|
118 void CLogServResourceInterpreter::LoadResourceFileL(const TDesC& aName, TResourceType aType) |
|
119 { |
|
120 LOGTEXT3("CLogServResourceInterpreter::LoadResourceFileL(%S, %d)", &aName, aType); |
|
121 |
|
122 // Find the resource file |
|
123 TFileName fileName; |
|
124 fileName.Copy(RProcess().FileName()); |
|
125 |
|
126 TParse parse; |
|
127 parse.Set(aName, &fileName, NULL); |
|
128 fileName = parse.FullName(); |
|
129 |
|
130 // Get language of resource file |
|
131 BaflUtils::NearestLanguageFile(iFsSession, fileName); |
|
132 |
|
133 // Check the entry exists on this drive (e.g. if we are running the log server |
|
134 // from RAM, then default to the ROM if no RSC on the current drive exists). |
|
135 TEntry fsEntry; |
|
136 if (iFsSession.Entry(fileName, fsEntry) == KErrNotFound) |
|
137 { |
|
138 // Switch to ROM (we might already have been launching from the ROM, |
|
139 // in which case this will have no effect anyway). |
|
140 fileName[0] = 'Z'; |
|
141 } |
|
142 |
|
143 // Open resource file |
|
144 TResourceFileEntry entry; |
|
145 entry.iType = aType; |
|
146 CleanupClosePushL(entry.iFile); |
|
147 |
|
148 LOGTEXT2("CLogServResourceInterpreter::LoadResourceFileL() - localized name is: %S", &fileName); |
|
149 |
|
150 entry.iFile.OpenL(iFsSession, fileName); |
|
151 |
|
152 LOGTEXT("CLogServResourceInterpreter::LoadResourceFileL() - resource file open"); |
|
153 |
|
154 // I'm leaving this in just in case somebody decides to try and add this code |
|
155 // without realising the consequences... |
|
156 #ifdef __CAN_BREAK_BC__ |
|
157 // |
|
158 // Can't use BAFL 'NAME' support in resource file, since this |
|
159 // will break data compatibility with old software using the existing |
|
160 // logwrap and logcli RSG header files :(( |
|
161 entry.iFile.ConfirmSignatureL(); |
|
162 __ASSERT_ALWAYS(entry.iFile.Offset() != 0, Panic(ELogNoResourceName)); |
|
163 #endif |
|
164 |
|
165 User::LeaveIfError(iResourceFiles.Append(entry)); |
|
166 CleanupStack::Pop(&entry.iFile); |
|
167 |
|
168 LOGTEXT("CLogServResourceInterpreter::LoadResourceFileL() - end"); |
|
169 } |
|
170 |
|
171 const RResourceFile& CLogServResourceInterpreter::ResourceFileForType(TResourceType aType) const |
|
172 { |
|
173 const RResourceFile* ret = NULL; |
|
174 // |
|
175 const TInt count = iResourceFiles.Count(); |
|
176 for(TInt i=0; i<count; i++) |
|
177 { |
|
178 const TResourceFileEntry& entry = iResourceFiles[i]; |
|
179 if (entry.iType == aType) |
|
180 { |
|
181 ret = &entry.iFile; |
|
182 break; |
|
183 } |
|
184 } |
|
185 __ASSERT_ALWAYS(ret != NULL, Panic(ELogNoResourceForId)); |
|
186 return *ret; |
|
187 } |
|
188 |
|
189 |
|
190 CLogServResourceInterpreter::CResourceStringCacheEntry * CLogServResourceInterpreter::FindCachedResourceString(const TInt aId, TResourceType aType) |
|
191 { |
|
192 CLogServResourceInterpreter::CResourceStringCacheEntry* item ; |
|
193 |
|
194 // Iterate through linked list of cache entries looking for a |
|
195 // match - if no match found will drop out with a NULL pointer. |
|
196 iResourceStringCacheIter.SetToFirst() ; |
|
197 while ((item = iResourceStringCacheIter++) != NULL) |
|
198 { |
|
199 if ((item->ResourceType() == aType) && (item->ResourceId() == aId)) |
|
200 { |
|
201 break ; |
|
202 } |
|
203 }; |
|
204 return item ; |
|
205 } |
|
206 |
|
207 |
|
208 ///////////////////////////////////////////////////////////////////////////////////////// |
|
209 // -----> CLogServResourceInterpreter::CResourceStringCacheEntry (source) |
|
210 ///////////////////////////////////////////////////////////////////////////////////////// |
|
211 CLogServResourceInterpreter::CResourceStringCacheEntry::CResourceStringCacheEntry(TInt aResourceId, CLogServResourceInterpreter::TResourceType aType, HBufC8* aResourceString) : iResourceId (aResourceId), iResourceType (aType), iResourceString(aResourceString) |
|
212 { |
|
213 } |
|
214 |
|
215 |
|
216 CLogServResourceInterpreter::CResourceStringCacheEntry::~CResourceStringCacheEntry() |
|
217 { |
|
218 delete iResourceString ; |
|
219 } |
|
220 |
|
221 |
|
222 CLogServResourceInterpreter::CResourceStringCacheEntry* CLogServResourceInterpreter::CResourceStringCacheEntry::NewL(TInt aResourceId, CLogServResourceInterpreter::TResourceType aType, HBufC8* aResourceString) |
|
223 { |
|
224 return new(ELeave) CResourceStringCacheEntry(aResourceId, aType, aResourceString); |
|
225 } |
|
226 |
|
227 TUint CLogServResourceInterpreter::CResourceStringCacheEntry::ResourceId(void) |
|
228 { |
|
229 return iResourceId ; |
|
230 } |
|
231 |
|
232 CLogServResourceInterpreter::TResourceType CLogServResourceInterpreter::CResourceStringCacheEntry::ResourceType(void) |
|
233 { |
|
234 return iResourceType ; |
|
235 } |
|
236 |
|
237 HBufC8* CLogServResourceInterpreter::CResourceStringCacheEntry::ResourceString (void) |
|
238 { |
|
239 return iResourceString ; |
|
240 } |
|
241 |
|
242 TInt CLogServResourceInterpreter::CResourceStringCacheEntry::Offset() |
|
243 { |
|
244 return (_FOFF(CLogServResourceInterpreter::CResourceStringCacheEntry, iLink)); |
|
245 } |
|
246 |