layouts/cdl/CdlEngine/src/CdlContainers.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 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 "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 #include "CCdlEngine.h"
       
    18 
       
    19 const TInt KCdlGranularity = 4;
       
    20 
       
    21 
       
    22 //
       
    23 // CCdlUids
       
    24 //
       
    25 
       
    26 EXPORT_C CCdlUids* CCdlUids::NewLC()
       
    27 	{
       
    28 	CCdlUids* self = new(ELeave) CCdlUids;
       
    29 	CleanupStack::PushL(self);
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 EXPORT_C CCdlUids::CCdlUids()
       
    34 : CArrayFixFlat<TUid>(KCdlGranularity)
       
    35 	{
       
    36 	}
       
    37 
       
    38 EXPORT_C void CCdlUids::AddL(TUid aUid)
       
    39 	{
       
    40 	if (FindIndex(aUid) == KErrNotFound)
       
    41 		AppendL(aUid);
       
    42 	}
       
    43 
       
    44 EXPORT_C void CCdlUids::AddL(const CCdlUids& aArray)
       
    45 	{
       
    46 	TInt count = aArray.Count();
       
    47 	for (TInt ii=0; ii<count; ii++)
       
    48 		{
       
    49 		TUid uid = aArray[ii];
       
    50 		AddL(uid);
       
    51 		}
       
    52 	}
       
    53 
       
    54 EXPORT_C void CCdlUids::Remove(const CCdlUids& aArray)
       
    55 	{
       
    56 	TInt count = aArray.Count();
       
    57 	for (TInt ii=0; ii<count; ii++)
       
    58 		Remove(aArray[ii]);
       
    59 	}
       
    60 
       
    61 EXPORT_C void CCdlUids::Remove(TUid aUid)
       
    62 	{
       
    63 	TInt index = FindIndex(aUid);
       
    64 	if (index != KErrNotFound)
       
    65 		Delete(index);
       
    66 	}
       
    67 
       
    68 EXPORT_C TInt CCdlUids::FindIndex(TUid aUid) const
       
    69 	{
       
    70 	TInt count = Count();
       
    71 	for (TInt ii=0; ii<count; ii++)
       
    72 		{
       
    73 		if (At(ii) == aUid)
       
    74 			return ii;
       
    75 		}
       
    76 	return KErrNotFound;
       
    77 	}
       
    78 
       
    79 EXPORT_C CCdlUids* CCdlUids::IntersectionLC(const CCdlUids& aArray) const
       
    80 	{
       
    81 	CCdlUids* array = CCdlUids::NewLC();
       
    82 	TInt count = aArray.Count();
       
    83 	for (TInt ii=0; ii<count; ii++)
       
    84 		{
       
    85 		TUid uid = aArray[ii];
       
    86 		if (FindIndex(uid) != KErrNotFound)
       
    87 			array->AppendL(uid);
       
    88 		}
       
    89 	return array;
       
    90 	}
       
    91 
       
    92 EXPORT_C void CCdlUids::ImportL(const TDesC8& aDes)
       
    93 	{
       
    94 	TInt size = aDes.Size();
       
    95 	ResizeL(size/sizeof(TUid));
       
    96 	if (size)
       
    97 		{
       
    98 		Mem::Copy(&At(0), aDes.Ptr(), size);
       
    99 		}
       
   100 	}
       
   101 
       
   102 EXPORT_C TPtrC8 CCdlUids::Export() const
       
   103 	{
       
   104 	TInt count = Count();
       
   105 	const TUid* uids = count ? &At(0) : NULL;
       
   106 	TPtrC8 ptr(reinterpret_cast<const TText8*>(uids), count*sizeof(TUid));
       
   107 	return ptr;
       
   108 	}
       
   109 
       
   110 
       
   111 //
       
   112 // CCdlNames
       
   113 //
       
   114 
       
   115 EXPORT_C CCdlNames* CCdlNames::NewLC()
       
   116 	{
       
   117 	CCdlNames* self = new(ELeave) CCdlNames;
       
   118 	CleanupStack::PushL(self);
       
   119 	return self;
       
   120 	}
       
   121 
       
   122 EXPORT_C CCdlNames::CCdlNames()
       
   123 : CArrayPtrFlat<HBufC>(KCdlGranularity)
       
   124 	{
       
   125 	}
       
   126 
       
   127 EXPORT_C CCdlNames::~CCdlNames()
       
   128 	{
       
   129 	ResetAndDestroy();
       
   130 	}
       
   131 
       
   132 EXPORT_C TInt CCdlNames::FindIndex(const TDesC& aName) const
       
   133 	{
       
   134 	TInt count = Count();
       
   135 	for (TInt ii=0; ii<count; ii++)
       
   136 		{
       
   137 		HBufC* name = At(ii);
       
   138 		if (CdlEngine::CompareNames(*name, aName) == 0)
       
   139 			return ii;
       
   140 		}
       
   141 
       
   142 	return KErrNotFound;
       
   143 	}
       
   144 
       
   145 EXPORT_C HBufC* CCdlNames::AddL(const TDesC& aName)
       
   146 	{
       
   147 	TInt pos = FindIndex(aName);
       
   148 	if (pos != KErrNotFound)
       
   149 		return At(pos);
       
   150 
       
   151 	HBufC* name = aName.AllocLC();
       
   152 	AppendL(name);
       
   153 	CleanupStack::Pop(name);
       
   154 	return name;
       
   155 	}
       
   156 
       
   157 
       
   158 EXPORT_C void CCdlNames::ImportL(const TDesC8& aDes)
       
   159 	{
       
   160 	TPtrC8 buf(aDes);
       
   161 
       
   162 	TInt newCount=0;
       
   163 	CdlUtils::Extract(buf, newCount);
       
   164 	TInt arrayCount = Count();
       
   165 	ResizeL(arrayCount + newCount, 0);
       
   166 	CleanupStack::PushL(TCleanupItem(CleanupImport, this));
       
   167 	for (TInt ii=0; ii<newCount; ii++)
       
   168 		{
       
   169 		TInt size=0;
       
   170 		CdlUtils::Extract(buf, size);
       
   171 		TPtrC name((TText*)buf.Ptr(), size/2);
       
   172 		buf.Set(buf.Mid(size));
       
   173 		if (FindIndex(name) == KErrNotFound)
       
   174 			At(arrayCount++) = name.AllocL();
       
   175 		}
       
   176 	CleanupStack::Pop();	// cleanup import
       
   177 	ResizeL(arrayCount);
       
   178 	}
       
   179 
       
   180 EXPORT_C HBufC8* CCdlNames::ExportL() const
       
   181 	{
       
   182 	HBufC8* buf = ExportLC();
       
   183 	CleanupStack::Pop(buf);
       
   184 	return buf;
       
   185 	}
       
   186 
       
   187 EXPORT_C HBufC8* CCdlNames::ExportLC() const
       
   188 	{
       
   189 	HBufC8* buf = HBufC8::NewLC(sizeof(TInt));
       
   190 
       
   191 	// names count is first in the export descriptor
       
   192 	TInt count = Count();
       
   193 	CdlUtils::AppendLDC(buf, TPckgC<TInt>(count));
       
   194 	for (TInt ii=0; ii<count; ii++)
       
   195 		{
       
   196 		// for each name, place the size of the name, then the name in the export descriptor
       
   197 		const TDesC& name = *At(ii);
       
   198 		TInt size = name.Size();
       
   199 		CdlUtils::AppendLDC(buf, TPckgC<TInt>(size));
       
   200 		TPtrC8 name8((TText8*)name.Ptr(), size);
       
   201 		CdlUtils::AppendLDC(buf, name8);
       
   202 		}
       
   203 
       
   204 	return buf;
       
   205 	}
       
   206 
       
   207 void CCdlNames::CleanupImport(TAny* aThis)
       
   208 	{
       
   209 	static_cast<CCdlNames*>(aThis)->DoCleanupImport();
       
   210 	}
       
   211 
       
   212 void CCdlNames::DoCleanupImport()
       
   213 	{
       
   214 	// count the number of used HBufCs and resize to that size.
       
   215 	TInt count = Count();
       
   216 	TInt used;
       
   217 	for (used=0; used<count; used++)
       
   218 		if (!At(used))
       
   219 			break;
       
   220 	if (used<count)
       
   221 		ResizeL(used);	// will not leave because array is shrinking
       
   222 	}