Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
20 #include <caf/metadataarray.h>
21 #include <caf/metadata.h>
23 using namespace ContentAccess;
25 EXPORT_C CMetaDataArray* CMetaDataArray::NewL()
27 CMetaDataArray* self = NewLC();
28 CleanupStack::Pop(self);
32 EXPORT_C CMetaDataArray* CMetaDataArray::NewLC()
34 CMetaDataArray* self = new (ELeave) CMetaDataArray;
35 CleanupStack::PushL(self);
39 EXPORT_C CMetaDataArray* CMetaDataArray::NewL(RReadStream& aStream)
41 CMetaDataArray* self = new (ELeave) CMetaDataArray;
42 CleanupStack::PushL(self);
43 self->InternalizeL(aStream);
44 CleanupStack::Pop(self);
48 CMetaDataArray::CMetaDataArray()
54 CMetaDataArray::~CMetaDataArray()
56 iArray.ResetAndDestroy();
59 EXPORT_C void CMetaDataArray::AddL(const TDesC8& aField, const TDesC8& aData)
61 // Construct a new CMetaData object with 8 bit data
62 CMetaData* ptr = CMetaData::NewL(aField, aData);
63 CleanupStack::PushL(ptr);
65 // Add it to the array
66 User::LeaveIfError(iArray.Append(ptr));
68 // Array owns the pointer so we don't need it in the cleanup stack any longer
69 CleanupStack::Pop(ptr);
71 // See if this field is longer than any previous field
72 if(aField.Length() > iMaxFieldLength)
74 iMaxFieldLength = aField.Length();
78 EXPORT_C void CMetaDataArray::AddL(const TDesC& aField, const TDesC& aData)
80 // Construct a new CMetaData object with unicode data
81 CMetaData* ptr = CMetaData::NewL(aField, aData);
82 CleanupStack::PushL(ptr);
84 // Add it to the array
85 User::LeaveIfError(iArray.Append(ptr));
87 // Array owns the pointer so we don't need it in the cleanup stack any longer
88 CleanupStack::Pop(ptr);
90 // See if this field is longer than any previous field
91 if(aField.Length() > iMaxFieldLength)
93 iMaxFieldLength = aField.Length();
97 EXPORT_C const CMetaData& CMetaDataArray::operator [] (TInt aIndex) const
99 return *iArray[aIndex];
102 EXPORT_C TInt CMetaDataArray::Count() const
104 return iArray.Count();
107 EXPORT_C const TDesC& CMetaDataArray::SearchL(const TDesC& aField, TBool aMatchCase) const
112 // Allocate space for upper case version of field to search for
113 HBufC* searchField = aField.AllocLC();
114 TPtr searchFieldPtr = searchField->Des();
115 searchFieldPtr.UpperCase();
117 // Allocate space for upper case version fields in the array
118 HBufC* field = HBufC::NewLC(iMaxFieldLength);
119 TPtr fieldPtr = field->Des();
121 // search through the array to find the data matching the given field
122 for(i = 0; i < iArray.Count(); i++)
124 fieldPtr.Copy(iArray[i]->Field());
125 fieldPtr.UpperCase();
126 if(fieldPtr == searchFieldPtr)
128 CleanupStack::PopAndDestroy(2, searchField); // searchField, field
129 return iArray[i]->Data();
132 CleanupStack::PopAndDestroy(2, searchField); // searchField, field
136 // search through the array to find the data matching the given field
137 for(i = 0; i < iArray.Count(); i++)
139 if(iArray[i]->Field() == aField)
141 return iArray[i]->Data();
145 // not found so return an empty string
146 return KNullDesC16();
149 EXPORT_C const TDesC8& CMetaDataArray::SearchL(const TDesC8& aField8, TBool aMatchCase) const
155 // Allocate space for upper case version of field to search for
156 HBufC8* searchField = aField8.AllocLC();
157 TPtr8 searchFieldPtr = searchField->Des();
158 searchFieldPtr.UpperCase();
160 // Allocate space for upper case version fields in the array
161 HBufC8* field = HBufC8::NewLC(iMaxFieldLength);
162 TPtr8 fieldPtr = field->Des();
164 // search through the array to find the data matching the given field
165 for(i = 0; i < iArray.Count(); i++)
167 fieldPtr.Copy(iArray[i]->Field8());
168 fieldPtr.UpperCase();
169 if(fieldPtr == searchFieldPtr)
171 CleanupStack::PopAndDestroy(2, searchField); // searchField, field
172 return iArray[i]->Data8();
175 CleanupStack::PopAndDestroy(2, searchField); // searchField, field
179 // search through the array to find the data matching the given field
180 for(i = 0; i < iArray.Count(); i++)
182 if(iArray[i]->Field8() == aField8)
184 return iArray[i]->Data8();
188 // not found so return an empty string
192 EXPORT_C void CMetaDataArray::ExternalizeL(RWriteStream& aStream) const
195 aStream.WriteInt32L(iArray.Count());
196 for(i = 0; i < iArray.Count();i++)
198 aStream << *(iArray[i]);
202 void CMetaDataArray::InternalizeL(RReadStream& aStream)
207 // Read the number of CMetaData objects from the stream
208 TInt count = aStream.ReadInt32L();
210 // Read the CMetaData objects from the stream and add them to the array
211 for(i = 0; i < count; i++)
213 CMetaData* metaData = CMetaData::NewL(aStream);
214 CleanupStack::PushL(metaData);
215 User::LeaveIfError(iArray.Append(metaData));
217 // See if this field is longer than any previous field
218 length = metaData->Field().Length();
219 if(length > iMaxFieldLength)
221 iMaxFieldLength = length;
224 // Finished with cleanup stack, metaData now owned by the array so don't delete
225 CleanupStack::Pop(metaData);