os/persistentdata/persistentstorage/store/USTRM/US_UTL.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/store/USTRM/US_UTL.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,178 @@
     1.4 +// Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <s32ucmp.h>
    1.20 +#include "US_STD.H"
    1.21 +
    1.22 +#pragma BullseyeCoverage off
    1.23 +
    1.24 +//
    1.25 +// Panic the process with STORE-Stream as the category.
    1.26 +//
    1.27 +GLDEF_C void Panic(TStreamPanic aPanic)
    1.28 +	{
    1.29 +	_LIT(KCategory,"STORE-Stream");
    1.30 +	User::Panic(KCategory,aPanic);
    1.31 +	}
    1.32 +
    1.33 +#pragma BullseyeCoverage on
    1.34 +
    1.35 +EXPORT_C void TCardinality::ExternalizeL(RWriteStream& aStream) const
    1.36 +/** Externalises this object to a write stream.
    1.37 +
    1.38 +The existence of this function means that the standard templated operator<<() 
    1.39 +can be used to externalise objects of this class.
    1.40 +
    1.41 +@param aStream Stream to which the object should be externalised.
    1.42 +@see operator<<() */
    1.43 +	{
    1.44 +	TUint n=iCount;
    1.45 +	if (n<=(KMaxTUint8>>KShiftCardinality8))
    1.46 +		aStream.WriteUint8L((n<<KShiftCardinality8));
    1.47 +	else if (n<=(KMaxTUint16>>KShiftCardinality16))
    1.48 +		aStream.WriteUint16L((n<<KShiftCardinality16)+0x1);
    1.49 +	else
    1.50 +		{
    1.51 +		__ASSERT_DEBUG(n<=(TUint)KMaxCardinality,Panic(EStreamCardinalityOutOfRange));
    1.52 +		aStream.WriteUint32L((n<<KShiftCardinality32)+0x3);
    1.53 +		}
    1.54 +	}
    1.55 +
    1.56 +EXPORT_C void TCardinality::InternalizeL(RReadStream& aStream)
    1.57 +/** Internalizes this object from a read stream.
    1.58 +
    1.59 +The existence of this function means that the standard templated operator>>() 
    1.60 +can be used to internalise objects of this class.
    1.61 +
    1.62 +@param aStream Stream store from which the objet is to be internalised.
    1.63 +@see operator>>() */
    1.64 +	{
    1.65 +	TUint n=aStream.ReadUint8L();
    1.66 +	if ((n&0x1)==0)
    1.67 +		n>>=KShiftCardinality8;
    1.68 +	else if ((n&0x2)==0)
    1.69 +		{
    1.70 +		n+=aStream.ReadUint8L()<<8;
    1.71 +		n>>=KShiftCardinality16;
    1.72 +		}
    1.73 +	else if ((n&0x4)==0)
    1.74 +		{
    1.75 +		aStream.ReadL((TUint8*)&iCount,sizeof(TUint32)-sizeof(TUint8));
    1.76 +		n+=TUint(iCount)<<8; // platform dependency
    1.77 +		n>>=KShiftCardinality32;
    1.78 +		}
    1.79 +	else
    1.80 +		__LEAVE(KErrCorrupt);
    1.81 +//
    1.82 +	__ASSERT_DEBUG(n<=(TUint)KMaxCardinality,User::Invariant());
    1.83 +	iCount=n;
    1.84 +	}
    1.85 +
    1.86 +// An MUnicodeSink implementation to write Unicode values into 8-bit bytes; anything outside 0..255 becomes 1
    1.87 +#ifdef _UNICODE
    1.88 +class TNarrowUnicodeSink: public MUnicodeSink
    1.89 +	{
    1.90 +	public:
    1.91 +	TNarrowUnicodeSink(TUint8* aPtr): iPtr(aPtr) { }
    1.92 +	void WriteUnicodeValueL(TUint16 aValue) { *iPtr++ = (TUint8)(aValue > 255 ? 1 : aValue); }
    1.93 +
    1.94 +	private:
    1.95 +	TUint8* iPtr;
    1.96 +	};
    1.97 +#endif
    1.98 +
    1.99 +void TDesInternalizer::operator()(TDes8& aDes8,RReadStream& aStream) const
   1.100 +//
   1.101 +// Read an 8-bit descriptor in from aStream.
   1.102 +//
   1.103 +	{
   1.104 +	TInt len=iHeader.Length();
   1.105 +	if (len>aDes8.MaxLength())
   1.106 +		__LEAVE(KErrOverflow);
   1.107 +//
   1.108 +	TUint8* ptr=(TUint8*)aDes8.Ptr();
   1.109 +	if (iHeader.IsWidth8())
   1.110 +		aStream.ReadL(ptr,len);
   1.111 +	else
   1.112 +		{
   1.113 +		__ASSERT_DEBUG(iHeader.IsWidth16(),User::Invariant());
   1.114 +#ifdef _UNICODE
   1.115 +		// In the Unicode build 16-bit descriptors need to be expanded from the Standard Unicode Compression Scheme.
   1.116 +		TNarrowUnicodeSink sink(ptr);
   1.117 +		TUnicodeExpander expander;
   1.118 +		expander.ExpandL(sink,aStream,len);
   1.119 +
   1.120 +#else
   1.121 +		TUint8* end=ptr+len;
   1.122 +		while (ptr!=end)
   1.123 +			{
   1.124 +			TUint c=aStream.ReadUint16L();
   1.125 +			if (c>=0x100)
   1.126 +				c=1;
   1.127 +			*ptr++=TUint8(c);
   1.128 +			}
   1.129 +#endif
   1.130 +		}
   1.131 +	aDes8.SetLength(len);
   1.132 +	}
   1.133 +
   1.134 +void TDesInternalizer::operator()(TDes16& aDes16,RReadStream& aStream) const
   1.135 +//
   1.136 +// Read a 16-bit descriptor in from aStream.
   1.137 +//
   1.138 +	{
   1.139 +	TInt len=iHeader.Length();
   1.140 +	if (len>aDes16.MaxLength())
   1.141 +		__LEAVE(KErrOverflow);
   1.142 +//
   1.143 +	TUint16* ptr=(TUint16*)aDes16.Ptr();
   1.144 +	if (iHeader.IsWidth16())
   1.145 +		{
   1.146 +#ifdef _UNICODE
   1.147 +		// In the Unicode build 16-bit descriptors need to be expanded from the Standard Unicode Compression Scheme.
   1.148 +		TMemoryUnicodeSink sink(ptr);
   1.149 +		TUnicodeExpander expander;
   1.150 +		expander.ExpandL(sink,aStream,len);
   1.151 +
   1.152 +#else
   1.153 +		aStream.ReadL(ptr,len);
   1.154 +#endif
   1.155 +		}
   1.156 +	else
   1.157 +		{
   1.158 +		__ASSERT_DEBUG(iHeader.IsWidth8(),User::Invariant());
   1.159 +		TUint16* end=ptr+len;
   1.160 +		while (ptr!=end)
   1.161 +			*ptr++=aStream.ReadUint8L();
   1.162 +		}
   1.163 +	aDes16.SetLength(len);
   1.164 +	}
   1.165 +
   1.166 +EXPORT_C void TStreamTransfer::__DbgChkNonNegative(TInt aLength)
   1.167 +//
   1.168 +// Check for a negative value.
   1.169 +//
   1.170 +	{
   1.171 +	__ASSERT_ALWAYS(aLength>=0,Panic(EStreamTransferNegative));
   1.172 +	}
   1.173 +
   1.174 +EXPORT_C void TCardinality::__DbgChkRange(TInt aCount)
   1.175 +//
   1.176 +// Check for a negative count.
   1.177 +//
   1.178 +	{
   1.179 +	__ASSERT_ALWAYS(aCount<=KMaxCardinality,Panic(EStreamCardinalityOutOfRange));
   1.180 +	}
   1.181 +