contentmgmt/contentaccessfwfordrm/source/cafutils/Metadataarray.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2004-2009 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 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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32strm.h>
       
    20 #include <caf/metadataarray.h>
       
    21 #include <caf/metadata.h>
       
    22 
       
    23 using namespace ContentAccess;
       
    24 
       
    25 EXPORT_C CMetaDataArray* CMetaDataArray::NewL()
       
    26 	{
       
    27 	CMetaDataArray* self = NewLC();
       
    28 	CleanupStack::Pop(self);
       
    29 	return self;
       
    30 	}
       
    31 
       
    32 EXPORT_C CMetaDataArray* CMetaDataArray::NewLC()
       
    33 	{
       
    34 	CMetaDataArray* self = new (ELeave) CMetaDataArray;
       
    35 	CleanupStack::PushL(self);
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 EXPORT_C CMetaDataArray* CMetaDataArray::NewL(RReadStream& aStream)
       
    40 	{
       
    41 	CMetaDataArray* self = new (ELeave) CMetaDataArray;
       
    42 	CleanupStack::PushL(self);
       
    43 	self->InternalizeL(aStream);
       
    44 	CleanupStack::Pop(self);
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 CMetaDataArray::CMetaDataArray()
       
    49 	{
       
    50 	iMaxFieldLength = 0;
       
    51 	}	
       
    52 
       
    53 		
       
    54 CMetaDataArray::~CMetaDataArray()
       
    55 	{	
       
    56 	iArray.ResetAndDestroy();
       
    57 	}
       
    58 
       
    59 EXPORT_C void CMetaDataArray::AddL(const TDesC8& aField, const TDesC8& aData)
       
    60 	{
       
    61 	// Construct a new CMetaData object with 8 bit data
       
    62 	CMetaData* ptr = CMetaData::NewL(aField, aData);
       
    63 	CleanupStack::PushL(ptr);
       
    64 	
       
    65 	// Add it to the array
       
    66 	User::LeaveIfError(iArray.Append(ptr));
       
    67 
       
    68 	// Array owns the pointer so we don't need it in the cleanup stack any longer
       
    69 	CleanupStack::Pop(ptr);
       
    70 
       
    71 	// See if this field is longer than any previous field
       
    72 	if(aField.Length() > iMaxFieldLength)
       
    73 		{
       
    74 		iMaxFieldLength = aField.Length();
       
    75 		}
       
    76 	}
       
    77 
       
    78 EXPORT_C void CMetaDataArray::AddL(const TDesC& aField, const TDesC& aData)
       
    79 	{
       
    80 	// Construct a new CMetaData object with unicode data
       
    81 	CMetaData* ptr = CMetaData::NewL(aField, aData);
       
    82 	CleanupStack::PushL(ptr);
       
    83 
       
    84 	// Add it to the array
       
    85 	User::LeaveIfError(iArray.Append(ptr));
       
    86 	
       
    87 	// Array owns the pointer so we don't need it in the cleanup stack any longer
       
    88 	CleanupStack::Pop(ptr);
       
    89 
       
    90 	// See if this field is longer than any previous field
       
    91 	if(aField.Length() > iMaxFieldLength)
       
    92 		{
       
    93 		iMaxFieldLength = aField.Length();
       
    94 		}
       
    95 	}
       
    96 
       
    97 EXPORT_C const CMetaData& CMetaDataArray::operator [] (TInt aIndex) const
       
    98 	{
       
    99 	return *iArray[aIndex];
       
   100 	}
       
   101 
       
   102 EXPORT_C TInt CMetaDataArray::Count() const
       
   103 	{
       
   104 	return iArray.Count();
       
   105 	}
       
   106 
       
   107 EXPORT_C const TDesC& CMetaDataArray::SearchL(const TDesC& aField, TBool aMatchCase) const
       
   108 	{
       
   109 	TInt i;
       
   110 	if(!aMatchCase)
       
   111 		{
       
   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();
       
   116 
       
   117 		// Allocate space for upper case version fields in the array
       
   118 		HBufC* field = HBufC::NewLC(iMaxFieldLength);
       
   119 		TPtr fieldPtr = field->Des();
       
   120 
       
   121 		// search through the array to find the data matching the given field
       
   122 		for(i = 0; i < iArray.Count(); i++)
       
   123 			{
       
   124 			fieldPtr.Copy(iArray[i]->Field());
       
   125 			fieldPtr.UpperCase();
       
   126 			if(fieldPtr == searchFieldPtr)
       
   127 				{
       
   128 				CleanupStack::PopAndDestroy(2, searchField); // searchField, field
       
   129 				return iArray[i]->Data();
       
   130 				}
       
   131 			}
       
   132 		CleanupStack::PopAndDestroy(2, searchField); // searchField, field
       
   133 		}
       
   134 	else	
       
   135 		{
       
   136 		// search through the array to find the data matching the given field
       
   137 		for(i = 0; i < iArray.Count(); i++)
       
   138 			{
       
   139 			if(iArray[i]->Field() == aField)
       
   140 				{
       
   141 				return iArray[i]->Data();
       
   142 				}
       
   143 			}
       
   144 		}
       
   145 	// not found so return an empty string
       
   146 	return KNullDesC16();
       
   147 	}
       
   148 		
       
   149 EXPORT_C const TDesC8& CMetaDataArray::SearchL(const TDesC8& aField8, TBool aMatchCase) const
       
   150 	{
       
   151 	TInt i;
       
   152 
       
   153 	if(!aMatchCase)
       
   154 		{
       
   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();
       
   159 
       
   160 		// Allocate space for upper case version fields in the array
       
   161 		HBufC8* field = HBufC8::NewLC(iMaxFieldLength);
       
   162 		TPtr8 fieldPtr = field->Des();
       
   163 
       
   164 		// search through the array to find the data matching the given field
       
   165 		for(i = 0; i < iArray.Count(); i++)
       
   166 			{
       
   167 			fieldPtr.Copy(iArray[i]->Field8());
       
   168 			fieldPtr.UpperCase();
       
   169 			if(fieldPtr == searchFieldPtr)
       
   170 				{
       
   171 				CleanupStack::PopAndDestroy(2, searchField); // searchField, field
       
   172 				return iArray[i]->Data8();
       
   173 				}
       
   174 			}
       
   175 		CleanupStack::PopAndDestroy(2, searchField); // searchField, field
       
   176 		}
       
   177 	else	
       
   178 		{
       
   179 		// search through the array to find the data matching the given field
       
   180 		for(i = 0; i < iArray.Count(); i++)
       
   181 			{
       
   182 			if(iArray[i]->Field8() == aField8)
       
   183 				{
       
   184 				return iArray[i]->Data8();
       
   185 				}
       
   186 			}
       
   187 		}
       
   188 	// not found so return an empty string
       
   189 	return KNullDesC8();
       
   190 	}
       
   191 	
       
   192 EXPORT_C void CMetaDataArray::ExternalizeL(RWriteStream& aStream) const
       
   193 	{
       
   194 	TInt i;
       
   195 	aStream.WriteInt32L(iArray.Count());
       
   196 	for(i = 0; i < iArray.Count();i++)
       
   197 		{
       
   198 		aStream << *(iArray[i]);
       
   199 		}
       
   200 	}
       
   201 
       
   202 void CMetaDataArray::InternalizeL(RReadStream& aStream)
       
   203 	{
       
   204 	TInt i;
       
   205 	TInt length = 0;
       
   206 
       
   207 	// Read the number of CMetaData objects from the stream
       
   208 	TInt count = aStream.ReadInt32L();
       
   209 
       
   210 	// Read the CMetaData objects from the stream and add them to the array
       
   211 	for(i = 0; i < count; i++)
       
   212 		{
       
   213 		CMetaData* metaData = CMetaData::NewL(aStream);
       
   214 		CleanupStack::PushL(metaData);
       
   215 		User::LeaveIfError(iArray.Append(metaData));
       
   216 
       
   217 		// See if this field is longer than any previous field
       
   218 		length = metaData->Field().Length();
       
   219 		if(length > iMaxFieldLength)
       
   220 			{
       
   221 			iMaxFieldLength = length;
       
   222 			}
       
   223 		
       
   224 		// Finished with cleanup stack, metaData now owned by the array so don't delete
       
   225 		CleanupStack::Pop(metaData);
       
   226 		}
       
   227 	}