|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: CNcdKeyValueMap implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <s32strm.h> |
|
20 #include <s32mem.h> |
|
21 |
|
22 #include "ncdkeyvaluemap.h" |
|
23 #include "ncdkeyvaluepair.h" |
|
24 |
|
25 #include "catalogsdebug.h" |
|
26 #include "ncdutils.h" |
|
27 |
|
28 // ======== MEMBER FUNCTIONS ======== |
|
29 |
|
30 // --------------------------------------------------------------------------- |
|
31 // Creator |
|
32 // --------------------------------------------------------------------------- |
|
33 // |
|
34 CNcdKeyValueMap* CNcdKeyValueMap::NewL() |
|
35 { |
|
36 CNcdKeyValueMap* self = CNcdKeyValueMap::NewLC(); |
|
37 CleanupStack::Pop(); |
|
38 return self; |
|
39 } |
|
40 |
|
41 |
|
42 // --------------------------------------------------------------------------- |
|
43 // Creator |
|
44 // --------------------------------------------------------------------------- |
|
45 // |
|
46 CNcdKeyValueMap* CNcdKeyValueMap::NewLC() |
|
47 { |
|
48 CNcdKeyValueMap* self = new ( ELeave ) CNcdKeyValueMap(); |
|
49 CleanupStack::PushL( self ); |
|
50 return self; |
|
51 } |
|
52 |
|
53 |
|
54 // --------------------------------------------------------------------------- |
|
55 // Copy creator |
|
56 // --------------------------------------------------------------------------- |
|
57 // |
|
58 CNcdKeyValueMap* CNcdKeyValueMap::NewL( const |
|
59 CNcdKeyValueMap& aOther ) |
|
60 { |
|
61 CNcdKeyValueMap* self = new ( ELeave ) |
|
62 CNcdKeyValueMap( aOther ); |
|
63 CleanupStack::PushL( self ); |
|
64 self->ConstructL( aOther ); |
|
65 CleanupStack::Pop( self ); |
|
66 return self; |
|
67 } |
|
68 |
|
69 |
|
70 // --------------------------------------------------------------------------- |
|
71 // Cloner |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 CNcdKeyValueMap* CNcdKeyValueMap::CloneL() const |
|
75 { |
|
76 return NewL( *this ); |
|
77 } |
|
78 |
|
79 |
|
80 // --------------------------------------------------------------------------- |
|
81 // Destructor |
|
82 // --------------------------------------------------------------------------- |
|
83 // |
|
84 CNcdKeyValueMap::~CNcdKeyValueMap() |
|
85 { |
|
86 iPairs.ResetAndDestroy(); |
|
87 } |
|
88 |
|
89 |
|
90 // --------------------------------------------------------------------------- |
|
91 // Pair adder |
|
92 // --------------------------------------------------------------------------- |
|
93 // |
|
94 void CNcdKeyValueMap::AddL( const TDesC& aKey, const TDesC& aValue ) |
|
95 { |
|
96 DLTRACEIN( ( "" ) ); |
|
97 |
|
98 // Add the replacement |
|
99 CNcdKeyValuePair* pair = CNcdKeyValuePair::NewLC( |
|
100 aKey, aValue ); |
|
101 User::LeaveIfError( iPairs.Append( pair ) ); |
|
102 CleanupStack::Pop( pair ); |
|
103 DLTRACEOUT( ( "" ) ); |
|
104 } |
|
105 |
|
106 |
|
107 // --------------------------------------------------------------------------- |
|
108 // Pair adder |
|
109 // --------------------------------------------------------------------------- |
|
110 // |
|
111 void CNcdKeyValueMap::AddL( CNcdKeyValuePair* aPair ) |
|
112 { |
|
113 DLTRACEIN(("")); |
|
114 |
|
115 // Add the replacement |
|
116 iPairs.AppendL( aPair ); |
|
117 } |
|
118 |
|
119 |
|
120 |
|
121 // --------------------------------------------------------------------------- |
|
122 // Pair replacer |
|
123 // --------------------------------------------------------------------------- |
|
124 // |
|
125 void CNcdKeyValueMap::ReplaceL( CNcdKeyValuePair* aPair ) |
|
126 { |
|
127 DLTRACEIN(("")); |
|
128 // Remove existing pair with the same key if any |
|
129 TInt err = Remove( aPair->Key() ); |
|
130 if ( err != KErrNone && err != KErrNotFound ) |
|
131 { |
|
132 User::Leave( err ); |
|
133 } |
|
134 AddL( aPair ); |
|
135 } |
|
136 |
|
137 |
|
138 // --------------------------------------------------------------------------- |
|
139 // Pair remover |
|
140 // --------------------------------------------------------------------------- |
|
141 // |
|
142 TInt CNcdKeyValueMap::Remove( const TDesC& aKey ) |
|
143 { |
|
144 DLTRACEIN(("")); |
|
145 TRAPD(err, |
|
146 { |
|
147 err = KErrNotFound; |
|
148 TIdentityRelation<CNcdKeyValuePair> match( |
|
149 CNcdKeyValuePair::MatchByKey ); |
|
150 |
|
151 CNcdKeyValuePair* pair = CNcdKeyValuePair::NewLC( aKey, |
|
152 KNullDesC ); |
|
153 |
|
154 TInt index = iPairs.Find( pair, match ); |
|
155 |
|
156 if ( index != KErrNotFound ) |
|
157 { |
|
158 CNcdKeyValuePair* removed = iPairs[index]; |
|
159 iPairs.Remove( index ); |
|
160 delete removed; |
|
161 |
|
162 err = KErrNone; |
|
163 } |
|
164 |
|
165 CleanupStack::PopAndDestroy( pair ); |
|
166 |
|
167 } ); //TRAPD |
|
168 return err; |
|
169 } |
|
170 |
|
171 |
|
172 // --------------------------------------------------------------------------- |
|
173 // Pair list getter |
|
174 // --------------------------------------------------------------------------- |
|
175 // |
|
176 RPointerArray<CNcdKeyValuePair>& CNcdKeyValueMap::Pairs() |
|
177 { |
|
178 return iPairs; |
|
179 } |
|
180 |
|
181 |
|
182 // --------------------------------------------------------------------------- |
|
183 // Pair list getter |
|
184 // --------------------------------------------------------------------------- |
|
185 // |
|
186 const RPointerArray<CNcdKeyValuePair>& CNcdKeyValueMap::Pairs() |
|
187 const |
|
188 { |
|
189 return iPairs; |
|
190 } |
|
191 |
|
192 |
|
193 // --------------------------------------------------------------------------- |
|
194 // Search for a value by key |
|
195 // --------------------------------------------------------------------------- |
|
196 // |
|
197 const TDesC& CNcdKeyValueMap::ValueByKeyL( const TDesC& aKey ) const |
|
198 { |
|
199 CNcdKeyValuePair* pair = CNcdKeyValuePair::NewLC( aKey, |
|
200 KNullDesC ); |
|
201 return FindValueL( pair ); |
|
202 } |
|
203 |
|
204 |
|
205 // --------------------------------------------------------------------------- |
|
206 // Value exists |
|
207 // --------------------------------------------------------------------------- |
|
208 // |
|
209 CNcdKeyValueMap::KeyValueIndex CNcdKeyValueMap::KeyExists( |
|
210 const TDesC& aKey ) const |
|
211 { |
|
212 DLTRACEIN(( _L("Key: %S"), &aKey )); |
|
213 |
|
214 for ( TInt i = 0; i < iPairs.Count(); ++i ) |
|
215 { |
|
216 if ( iPairs[i]->Key().Compare( aKey ) == 0 ) |
|
217 { |
|
218 DLTRACEOUT(("index: %d", i)); |
|
219 return i; |
|
220 } |
|
221 } |
|
222 |
|
223 DLTRACEOUT(("Key not found")); |
|
224 return KErrNotFound; |
|
225 } |
|
226 |
|
227 // --------------------------------------------------------------------------- |
|
228 // Returns a value by index |
|
229 // --------------------------------------------------------------------------- |
|
230 // |
|
231 const TDesC& CNcdKeyValueMap::ValueByIndex( const KeyValueIndex& aIndex ) const |
|
232 { |
|
233 DASSERT( aIndex >= 0 && aIndex < iPairs.Count() ); |
|
234 return iPairs[aIndex]->Value(); |
|
235 } |
|
236 |
|
237 |
|
238 // --------------------------------------------------------------------------- |
|
239 // Pair exists |
|
240 // --------------------------------------------------------------------------- |
|
241 // |
|
242 CNcdKeyValueMap::KeyValueIndex CNcdKeyValueMap::PairExists( |
|
243 const CNcdKeyValuePair& aPair ) const |
|
244 { |
|
245 DLTRACEIN(("")); |
|
246 for ( TInt i = 0; i < iPairs.Count(); ++i ) |
|
247 { |
|
248 if ( iPairs[i]->Key().Compare( aPair.Key() ) == 0 && |
|
249 iPairs[i]->Value().Compare( aPair.Value() ) == 0 ) |
|
250 { |
|
251 return i; |
|
252 } |
|
253 } |
|
254 |
|
255 DLTRACEOUT(("Key not found")); |
|
256 return KErrNotFound; |
|
257 } |
|
258 |
|
259 |
|
260 // --------------------------------------------------------------------------- |
|
261 // Reset and destroy |
|
262 // --------------------------------------------------------------------------- |
|
263 // |
|
264 void CNcdKeyValueMap::ResetAndDestroy() |
|
265 { |
|
266 iPairs.ResetAndDestroy(); |
|
267 } |
|
268 |
|
269 |
|
270 // --------------------------------------------------------------------------- |
|
271 // Reset |
|
272 // --------------------------------------------------------------------------- |
|
273 // |
|
274 void CNcdKeyValueMap::Reset() |
|
275 { |
|
276 iPairs.Reset(); |
|
277 } |
|
278 |
|
279 |
|
280 // --------------------------------------------------------------------------- |
|
281 // Reset |
|
282 // --------------------------------------------------------------------------- |
|
283 // |
|
284 void CNcdKeyValueMap::AppendL( const CNcdKeyValueMap& aMap ) |
|
285 { |
|
286 DLTRACEIN(("")); |
|
287 const RPointerArray<CNcdKeyValuePair>& pairs( aMap.Pairs() ); |
|
288 TInt count = pairs.Count(); |
|
289 iPairs.ReserveL( iPairs.Count() + count ); |
|
290 for ( TInt i = 0; i < count; ++i ) |
|
291 { |
|
292 CNcdKeyValuePair* pair = CNcdKeyValuePair::NewLC( |
|
293 *aMap.iPairs[i] ); |
|
294 iPairs.Append( pair ); |
|
295 CleanupStack::Pop( pair ); |
|
296 } |
|
297 } |
|
298 |
|
299 |
|
300 // --------------------------------------------------------------------------- |
|
301 // ExternalizeL |
|
302 // --------------------------------------------------------------------------- |
|
303 // |
|
304 void CNcdKeyValueMap::ExternalizeL( RWriteStream& aStream ) const |
|
305 { |
|
306 aStream.WriteInt32L( iPairs.Count() ); |
|
307 for ( TInt i = 0; i < iPairs.Count(); ++i ) |
|
308 { |
|
309 iPairs[i]->ExternalizeL( aStream ); |
|
310 } |
|
311 |
|
312 } |
|
313 |
|
314 |
|
315 // --------------------------------------------------------------------------- |
|
316 // ExternalizeL |
|
317 // --------------------------------------------------------------------------- |
|
318 // |
|
319 void CNcdKeyValueMap::ExternalizeL( RBuf8& aTarget ) const |
|
320 { |
|
321 DLTRACEIN(("")); |
|
322 TInt totalLength = 0; |
|
323 |
|
324 // Calculate total needed memory |
|
325 for ( TInt i = 0; i < iPairs.Count(); ++i ) |
|
326 { |
|
327 totalLength += iPairs[i]->Key().Length() + iPairs[i]->Value().Length() |
|
328 + 2*sizeof(TInt32); |
|
329 } |
|
330 |
|
331 // 16bits = 2 * 8bits |
|
332 totalLength *= 2; |
|
333 aTarget.Close(); |
|
334 |
|
335 // Total length of pairs + length of pair count |
|
336 aTarget.CreateL( totalLength + sizeof(TInt32) ); |
|
337 |
|
338 // Open a write stream to the buffer |
|
339 RDesWriteStream stream( aTarget ); |
|
340 CleanupClosePushL( stream ); |
|
341 |
|
342 // Write count |
|
343 stream.WriteInt32L( iPairs.Count() ); |
|
344 |
|
345 DLTRACE(("Externalize pairs")); |
|
346 for ( TInt i = 0; i < iPairs.Count(); ++i ) |
|
347 { |
|
348 iPairs[i]->ExternalizeL( stream ); |
|
349 } |
|
350 CleanupStack::PopAndDestroy( &stream ); |
|
351 DLTRACEOUT(("")); |
|
352 } |
|
353 |
|
354 |
|
355 // --------------------------------------------------------------------------- |
|
356 // InternalizeL |
|
357 // --------------------------------------------------------------------------- |
|
358 // |
|
359 void CNcdKeyValueMap::InternalizeL( RReadStream& aStream ) |
|
360 { |
|
361 DLTRACEIN(("")); |
|
362 TInt count = aStream.ReadInt32L(); |
|
363 DLTRACE(("Reading %d pairs", count)); |
|
364 |
|
365 iPairs.ResetAndDestroy(); |
|
366 if ( count ) |
|
367 { |
|
368 DLTRACE(("Internalizing pairs")); |
|
369 iPairs.ReserveL( count ); |
|
370 CNcdKeyValuePair* pair = NULL; |
|
371 |
|
372 // Internalize pairs |
|
373 for ( TInt i = 0; i < count; ++i ) |
|
374 { |
|
375 pair = CNcdKeyValuePair::NewL( aStream ); |
|
376 DLTRACE( ( _L("Key: %S, value: %S"), &pair->Key(), &pair->Value() )); |
|
377 // Append cannot fail |
|
378 iPairs.Append( pair ); |
|
379 } |
|
380 } |
|
381 |
|
382 DLTRACEOUT(("")); |
|
383 } |
|
384 |
|
385 // --------------------------------------------------------------------------- |
|
386 // Constructor |
|
387 // --------------------------------------------------------------------------- |
|
388 // |
|
389 CNcdKeyValueMap::CNcdKeyValueMap() |
|
390 { |
|
391 } |
|
392 |
|
393 // --------------------------------------------------------------------------- |
|
394 // Copy constructor |
|
395 // --------------------------------------------------------------------------- |
|
396 // |
|
397 CNcdKeyValueMap::CNcdKeyValueMap( |
|
398 const CNcdKeyValueMap& /*aOther*/ ) |
|
399 { |
|
400 // The actual copying is done in ConstructL( aOther ) |
|
401 } |
|
402 |
|
403 |
|
404 // --------------------------------------------------------------------------- |
|
405 // 2nd phase constructor |
|
406 // --------------------------------------------------------------------------- |
|
407 // |
|
408 void CNcdKeyValueMap::ConstructL( const CNcdKeyValueMap& aOther ) |
|
409 { |
|
410 DLTRACEIN( ( _L("") ) ); |
|
411 for( TInt i = 0; i < aOther.iPairs.Count(); ++i ) |
|
412 { |
|
413 // Create a copy of the header and add it to the new header list |
|
414 CNcdKeyValuePair* pair = CNcdKeyValuePair::NewLC( |
|
415 *aOther.iPairs[i] ); |
|
416 iPairs.AppendL( pair ); |
|
417 CleanupStack::Pop( pair ); |
|
418 } |
|
419 DLTRACEOUT( ( _L("") ) ); |
|
420 } |
|
421 |
|
422 |
|
423 // --------------------------------------------------------------------------- |
|
424 // Searches for the given key and returns the matching value |
|
425 // --------------------------------------------------------------------------- |
|
426 // |
|
427 const TDesC& CNcdKeyValueMap::FindValueL( |
|
428 CNcdKeyValuePair* aPair ) const |
|
429 { |
|
430 TIdentityRelation<CNcdKeyValuePair> match( |
|
431 CNcdKeyValuePair::MatchByKey ); |
|
432 |
|
433 TInt index = iPairs.Find( aPair, match ); |
|
434 |
|
435 if ( index == KErrNotFound ) |
|
436 { |
|
437 User::Leave( KErrNotFound ); |
|
438 } |
|
439 CleanupStack::PopAndDestroy( aPair ); |
|
440 return iPairs[index]->Value(); |
|
441 } |