sl@0: // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include <assert.h>
sl@0: #include <pcstore/storewritestream.h>
sl@0: #include <pcstore/storereadstream.h>
sl@0: #include <pcstore/descriptors.h>
sl@0: #include <pcstore/storeexception.h>
sl@0: #include "pcstoreconst.h"
sl@0: #include "desheader.h"
sl@0: 
sl@0: namespace PCStore
sl@0: {
sl@0: /**
sl@0: Constructs the object with the specified value.
sl@0: 
sl@0: @param aCount The value for this object. 
sl@0: */
sl@0: TCardinality::TCardinality(TInt aCount)
sl@0: 	:iCount(aCount)
sl@0: 	{}
sl@0: 
sl@0: /** 
sl@0: Externalizes this object to a write stream.
sl@0: 
sl@0: @param aStream The write stream to which the object should be externalized.
sl@0: @exception TStoreException::EFileWriteError Error occurs when writing to the file.
sl@0: */
sl@0: void TCardinality::Externalize(CStoreWriteStream& aStream) const
sl@0: 	{
sl@0: 
sl@0: 	TUint n=iCount;
sl@0: 	if (n<=(KMaxTUint8>>KShiftCardinality8))
sl@0: 		{
sl@0: 		aStream.WriteUint8((n<<KShiftCardinality8));
sl@0: 		}
sl@0: 	else 
sl@0: 		{
sl@0: 		if (n<=(KMaxTUint16>>KShiftCardinality16))
sl@0: 			{
sl@0: 			aStream.WriteUint16((n<<KShiftCardinality16)+0x1);
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			assert(n <= (TUint)KMaxCardinality);
sl@0: 			aStream.WriteUint32((n<<KShiftCardinality32)+0x3);
sl@0: 			}
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: /** 
sl@0: Internalizes this object from a read stream.
sl@0: 
sl@0: @param aStream The read stream from which the object is to be internalized.
sl@0: @exception TStoreException::EFileReadError Error occurs when reading from the file.
sl@0: @exception TStoreException::EStoreCorrupt Store file is corrupted so that an invalid cardinal 
sl@0: number is read.
sl@0: */
sl@0: void TCardinality::Internalize(CStoreReadStream& aStream)
sl@0: 	{
sl@0: 	TUint n=aStream.ReadUint8();
sl@0: 	if ((n&0x1)==0)
sl@0: 		{
sl@0: 		n>>=KShiftCardinality8;
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		if ((n&0x2)==0)
sl@0: 			{
sl@0: 			n+=aStream.ReadUint8()<<8;
sl@0: 			n>>=KShiftCardinality16;
sl@0: 			}
sl@0: 		else
sl@0: 			{
sl@0: 			if ((n&0x4)==0)
sl@0: 				{
sl@0: 				aStream.Read(reinterpret_cast<TUint8*>(&iCount),sizeof(TUint32)-sizeof(TUint8));
sl@0: 				n+=TUint(iCount)<<8; 
sl@0: 				n>>=KShiftCardinality32;
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				throw TStoreException(TStoreException::EStoreCorrupt);
sl@0: 				}
sl@0: 			}
sl@0: 		}
sl@0: 	assert(n <= static_cast<TUint>(KMaxCardinality));
sl@0: 	iCount=n;
sl@0: 	}
sl@0: 
sl@0: /** 
sl@0: Converts this object to TInt,
sl@0: 
sl@0: @return The value for this object
sl@0: */
sl@0: TCardinality::operator TInt() const
sl@0: 	{
sl@0: 	return iCount;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Constructs the header for the specified 8-bit descriptor.
sl@0: 
sl@0: @param aDes8 The reference to the 8-bit descriptor for which to form the header.
sl@0: */
sl@0: CDesHeader::CDesHeader(const CDes8& aDes8)
sl@0: 	:iVal((aDes8.Length()<<1)+1) 
sl@0: 	{}
sl@0: 
sl@0: /**
sl@0: Constructs the header for the specified 16-bit descriptor.
sl@0: 
sl@0: @param aDes16 The reference to the 16-bit descriptor for which to form the header.
sl@0: */
sl@0: CDesHeader::CDesHeader(const CDes16& aDes16)
sl@0: 	:iVal((aDes16.Length()<<1))
sl@0: 	{}
sl@0: 
sl@0: /**
sl@0: Externalize the object to the specified write stream.
sl@0: 
sl@0: @param aStream The write stream to which the object should be externalized.
sl@0: @exception TStoreException::EFileWriteError Error occurs when writing to the file.
sl@0: */
sl@0: void CDesHeader::Externalize(CStoreWriteStream& aStream) const
sl@0: 	{
sl@0: 	iVal.Externalize(aStream);
sl@0: 	}
sl@0: 
sl@0: /** 
sl@0: Internalizes the object from a read stream.
sl@0: 
sl@0: @param aStream The read stream from which the object is to be internalized.
sl@0: @exception TStoreException::EStoreCorrupt Store file is corrupted so that an invalid cardinal 
sl@0: number is read.
sl@0: @exception TStoreException::EFileReadError Error occurs when reading from the file.
sl@0: */
sl@0: void CDesHeader::Internalize(CStoreReadStream& aStream)
sl@0: 	{
sl@0: 	iVal.Internalize(aStream);
sl@0: 	}
sl@0: 
sl@0: /** 
sl@0: Gets the length of the descriptor which this header represents.
sl@0: 
sl@0: @return The length of the descriptor which this header represents.
sl@0: */
sl@0: TInt CDesHeader::Length() const
sl@0: 	{
sl@0: 	return TInt(iVal)>>1;
sl@0: 	}
sl@0: }