os/persistentdata/persistentstorage/store/USTRM/US_HOST.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_HOST.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,390 @@
     1.4 +// Copyright (c) 1998-2009 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 "US_STD.H"
    1.20 +
    1.21 +#define UNUSED_VAR(a) a = a
    1.22 +
    1.23 +EXPORT_C TInt TStreamMark::ReadL(TStreamExchange& aHost,TDes8& aDes,TRequestStatus& aStatus)
    1.24 +//
    1.25 +// Read asynchronously.
    1.26 +//
    1.27 +	{
    1.28 +	return aHost.DoReadL(aDes,aDes.MaxLength(),aStatus,*this);
    1.29 +	}
    1.30 +
    1.31 +EXPORT_C TInt TStreamMark::WriteL(TStreamExchange& aHost,const TDesC8& aDes,TRequestStatus& aStatus)
    1.32 +/** Writes data, asynchronously, from the specified descriptor into the shared 
    1.33 +stream.
    1.34 +
    1.35 +The maximum number of bytes to be written is the value of the maximum length 
    1.36 +of the descriptor.
    1.37 +
    1.38 +If the function leaves, then no write request will have been initiated.
    1.39 +
    1.40 +@param aHost The object that manages shared streaming.
    1.41 +@param aDes The source descriptor for the data to be written into the shared 
    1.42 +stream.
    1.43 +@param aStatus The request status that indicates the completion status of this 
    1.44 +asynchronous request.
    1.45 +@return The maximum number of bytes to be written, as used in this request. 
    1.46 +This can be different to the maximum length of the descriptor; this is dependent 
    1.47 +on the implementation. */
    1.48 +	{
    1.49 +	return aHost.DoWriteL(aDes,aDes.Length(),aStatus,*this);
    1.50 +	}
    1.51 +
    1.52 +EXPORT_C MStreamBuf* TStreamExchange::Host()
    1.53 +/** Gets a pointer to the stream buffer that acts as the shared host.
    1.54 +
    1.55 +The function refreshes the active read/write marks and gives the caller exclusive 
    1.56 +use of the shared host.
    1.57 +
    1.58 +@return A pointer to a stream buffer that acts as shared host. */
    1.59 +	{
    1.60 +	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
    1.61 +	if (iRMrk!=NULL)
    1.62 +		{
    1.63 +		TRAPD(ignore,*iRMrk=Buf().TellL(MStreamBuf::ERead));
    1.64 +        UNUSED_VAR(ignore);
    1.65 +		iRMrk=NULL;
    1.66 +		}
    1.67 +	if (iWMrk!=NULL)
    1.68 +		{
    1.69 +		TRAPD(ignore,*iWMrk=Buf().TellL(MStreamBuf::EWrite));
    1.70 +        UNUSED_VAR(ignore);
    1.71 +		iWMrk=NULL;
    1.72 +		}
    1.73 +	return iHost;
    1.74 +	}
    1.75 +
    1.76 +EXPORT_C MStreamBuf* TStreamExchange::HostL()
    1.77 +/** Gets a pointer to the stream buffer that acts as the shared host, and leaves 
    1.78 +if this object is not active.
    1.79 +
    1.80 +The function refreshes the active read/write marks and gives the caller exclusive 
    1.81 +use of the shared host.
    1.82 +
    1.83 +@return A pointer to a stream buffer that acts as shared host.
    1.84 +@see IsActive() */
    1.85 +	{
    1.86 +	MStreamBuf* host=Host();
    1.87 +	if (host==NULL)
    1.88 +		__LEAVE(KErrNotReady);
    1.89 +	return host;
    1.90 +	}
    1.91 +
    1.92 +EXPORT_C void TStreamExchange::Release()
    1.93 +/** Releases the reference to the shared host, and drops any active read or write 
    1.94 +marks. */
    1.95 +	{
    1.96 +	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
    1.97 +	iHost=NULL;
    1.98 +	iRMrk=NULL;
    1.99 +	iWMrk=NULL;
   1.100 +	}
   1.101 +
   1.102 +EXPORT_C TInt TStreamExchange::SizeL() const
   1.103 +/** Gets the size of the shared host buffer.
   1.104 +
   1.105 +@return The size of the shared host buffer. */
   1.106 +	{
   1.107 +	return BufL().SizeL();
   1.108 +	}
   1.109 +
   1.110 +EXPORT_C TBool TStreamExchange::RefersTo(const TStreamMark& aMark)
   1.111 +//
   1.112 +// Return whether aMark is referred to.
   1.113 +//
   1.114 +	{
   1.115 +	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
   1.116 +	return iRMrk==aMark||iWMrk==aMark;
   1.117 +	}
   1.118 +
   1.119 +EXPORT_C void TStreamExchange::Drop(const TStreamMark& aMark)
   1.120 +//
   1.121 +// Drop any reference to aMark.
   1.122 +//
   1.123 +	{
   1.124 +	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
   1.125 +	if (iRMrk==aMark)
   1.126 +		iRMrk=NULL;
   1.127 +	else if (iWMrk==aMark)
   1.128 +		iWMrk=NULL;
   1.129 +	}
   1.130 +
   1.131 +EXPORT_C void TStreamExchange::GetL(TStreamMark& aMark)
   1.132 +//
   1.133 +// Refresh and drop any reference to aMark.
   1.134 +//
   1.135 +	{
   1.136 +	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
   1.137 +	if (iRMrk==aMark)
   1.138 +		{
   1.139 +		iRMrk=NULL;
   1.140 +		aMark=Buf().TellL(MStreamBuf::ERead);
   1.141 +		}
   1.142 +	else if (iWMrk==aMark)
   1.143 +		{
   1.144 +		iWMrk=NULL;
   1.145 +		aMark=Buf().TellL(MStreamBuf::EWrite);
   1.146 +		}
   1.147 +	}
   1.148 +
   1.149 +EXPORT_C TInt TStreamExchange::DoReadL(TAny* aPtr,TInt aMaxLength,TStreamMark& aMark)
   1.150 +//
   1.151 +// Read up to aMaxLength bytes starting at aMark.
   1.152 +//
   1.153 +	{
   1.154 +	__ASSERT_DEBUG(iWMrk==NULL||iWMrk!=iRMrk&&iWMrk->IsTracking(iWMrk),User::Invariant());
   1.155 +	if (iRMrk!=aMark)
   1.156 +		return PrepareAndReadL(aPtr,aMaxLength,aMark);
   1.157 +//
   1.158 +	__ASSERT_DEBUG(aMark.IsTracking(iRMrk),User::Invariant());
   1.159 +	iRMrk=NULL;
   1.160 +	TInt len=Buf().ReadL(aPtr,aMaxLength);
   1.161 +	iRMrk=aMark;
   1.162 +	return len;
   1.163 +	}
   1.164 +
   1.165 +EXPORT_C TInt TStreamExchange::DoReadL(TDes8& aDes,TInt aMaxLength,TRequestStatus& aStatus,TStreamMark& aMark)
   1.166 +//
   1.167 +// Read up to aMaxLength bytes asynchronously.
   1.168 +//
   1.169 +	{
   1.170 +	__ASSERT_DEBUG(iWMrk==NULL||iWMrk!=iRMrk&&iWMrk->IsTracking(iWMrk),User::Invariant());
   1.171 +	if (iRMrk!=aMark)
   1.172 +		return PrepareAndReadL(aDes,aMaxLength,aStatus,aMark);
   1.173 +//
   1.174 +	__ASSERT_DEBUG(aMark.IsTracking(iRMrk),User::Invariant());
   1.175 +	iRMrk=NULL;
   1.176 +	TInt len=Buf().ReadL(aDes,aMaxLength,aStatus);
   1.177 +	iRMrk=aMark;
   1.178 +	return len;
   1.179 +	}
   1.180 +
   1.181 +EXPORT_C TStreamTransfer TStreamExchange::DoReadL(MStreamInput& anInput,TStreamTransfer aTransfer,TStreamMark& aMark)
   1.182 +//
   1.183 +// Push up to aTransfer bytes into anInput, starting at aMark.
   1.184 +//
   1.185 +	{
   1.186 +	__ASSERT_DEBUG(iWMrk==NULL||iWMrk!=iRMrk&&iWMrk->IsTracking(iWMrk),User::Invariant());
   1.187 +	if (iRMrk!=aMark)
   1.188 +		return PrepareAndReadL(anInput,aTransfer,aMark);
   1.189 +//
   1.190 +	__ASSERT_DEBUG(aMark.IsTracking(iRMrk),User::Invariant());
   1.191 +	iRMrk=NULL;
   1.192 +	TStreamTransfer trans=Buf().ReadL(anInput,aTransfer);
   1.193 +	iRMrk=aMark;
   1.194 +	return trans;
   1.195 +	}
   1.196 +
   1.197 +EXPORT_C void TStreamExchange::DoWriteL(const TAny* aPtr,TInt aLength,TStreamMark& aMark)
   1.198 +//
   1.199 +// Write aLength bytes starting at aMark.
   1.200 +//
   1.201 +	{
   1.202 +	__ASSERT_DEBUG(iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk),User::Invariant());
   1.203 +	if (iWMrk==aMark)
   1.204 +		{
   1.205 +		__ASSERT_DEBUG(aMark.IsTracking(iWMrk),User::Invariant());
   1.206 +		iWMrk=NULL;
   1.207 +		Buf().WriteL(aPtr,aLength);
   1.208 +		iWMrk=aMark;
   1.209 +		}
   1.210 +	else
   1.211 +		PrepareAndWriteL(aPtr,aLength,aMark);
   1.212 +	}
   1.213 +
   1.214 +EXPORT_C TInt TStreamExchange::DoWriteL(const TDesC8& aDes,TInt aMaxLength,TRequestStatus& aStatus,TStreamMark& aMark)
   1.215 +//
   1.216 +// Write up to aMaxLength bytes asynchronously.
   1.217 +//
   1.218 +	{
   1.219 +	__ASSERT_DEBUG(iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk),User::Invariant());
   1.220 +	if (iWMrk!=aMark)
   1.221 +		return PrepareAndWriteL(aDes,aMaxLength,aStatus,aMark);
   1.222 +//
   1.223 +	__ASSERT_DEBUG(aMark.IsTracking(iWMrk),User::Invariant());
   1.224 +	iWMrk=NULL;
   1.225 +	TInt len=Buf().WriteL(aDes,aMaxLength,aStatus);
   1.226 +	iWMrk=aMark;
   1.227 +	return len;
   1.228 +	}
   1.229 +
   1.230 +EXPORT_C TStreamTransfer TStreamExchange::DoWriteL(MStreamOutput& anOutput,TStreamTransfer aTransfer,TStreamMark& aMark)
   1.231 +//
   1.232 +// Pull up to aTransfer bytes from anOutput, starting at aMark.
   1.233 +//
   1.234 +	{
   1.235 +	__ASSERT_DEBUG(iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk),User::Invariant());
   1.236 +	if (iWMrk!=aMark)
   1.237 +		return PrepareAndWriteL(anOutput,aTransfer,aMark);
   1.238 +//
   1.239 +	__ASSERT_DEBUG(aMark.IsTracking(iWMrk),User::Invariant());
   1.240 +	iWMrk=NULL;
   1.241 +	TStreamTransfer trans=Buf().WriteL(anOutput,aTransfer);
   1.242 +	iWMrk=aMark;
   1.243 +	return trans;
   1.244 +	}
   1.245 +
   1.246 +EXPORT_C TStreamPos TStreamExchange::DoSeekL(TStreamMark& aMark,TStreamLocation aLocation,TInt anOffset)
   1.247 +//
   1.248 +// Position aMark at anOffset from aLocation.
   1.249 +//
   1.250 +	{
   1.251 +	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
   1.252 +	if (iRMrk==aMark)
   1.253 +		{
   1.254 +		iRMrk=NULL;
   1.255 +		TStreamPos pos=Buf().SeekL(MStreamBuf::ERead,aLocation,anOffset);
   1.256 +		iRMrk=aMark;
   1.257 +		return pos;
   1.258 +		}
   1.259 +//
   1.260 +	if (iWMrk==aMark)
   1.261 +		{
   1.262 +		iWMrk=NULL;
   1.263 +		TStreamPos pos=Buf().SeekL(MStreamBuf::EWrite,aLocation,anOffset);
   1.264 +		iWMrk=aMark;
   1.265 +		return pos;
   1.266 +		}
   1.267 +//
   1.268 +	return MarkSeekL(aMark,aLocation,anOffset);
   1.269 +	}
   1.270 +
   1.271 +void TStreamExchange::PrepareForReadingL(TStreamMark& aMark)
   1.272 +//
   1.273 +// Prepare the host buffer to start reading at aMark.
   1.274 +//
   1.275 +	{
   1.276 +	__ASSERT_DEBUG(iRMrk==NULL||iRMrk->IsTracking(iRMrk),User::Invariant());
   1.277 +	if (iWMrk==aMark)
   1.278 +		{
   1.279 +		iWMrk=NULL;
   1.280 +		aMark=Buf().TellL(MStreamBuf::EWrite);
   1.281 +		}
   1.282 +	else if (aMark.IsEmpty())
   1.283 +		__LEAVE(KErrNotReady);
   1.284 +//
   1.285 +	if (iRMrk!=NULL)
   1.286 +		{
   1.287 +		TRAPD(ignore,*iRMrk=Buf().TellL(MStreamBuf::ERead));
   1.288 +        UNUSED_VAR(ignore);
   1.289 +		iRMrk=NULL;
   1.290 +		}
   1.291 +//
   1.292 +	TStreamPos pos=aMark.Position();
   1.293 +	aMark.Track(iRMrk);
   1.294 +	BufL().SeekL(MStreamBuf::ERead,pos);
   1.295 +	}
   1.296 +
   1.297 +void TStreamExchange::PrepareForWritingL(TStreamMark& aMark)
   1.298 +//
   1.299 +// Prepare the host buffer to start writing at aMark.
   1.300 +//
   1.301 +	{
   1.302 +	__ASSERT_DEBUG(iWMrk==NULL||iWMrk->IsTracking(iWMrk),User::Invariant());
   1.303 +	if (iRMrk==aMark)
   1.304 +		{
   1.305 +		iRMrk=NULL;
   1.306 +		aMark=Buf().TellL(MStreamBuf::ERead);
   1.307 +		}
   1.308 +	else if (aMark.IsEmpty())
   1.309 +		__LEAVE(KErrNotReady);
   1.310 +//
   1.311 +	if (iWMrk!=NULL)
   1.312 +		{
   1.313 +		TRAPD(ignore,*iWMrk=Buf().TellL(MStreamBuf::EWrite));
   1.314 +        UNUSED_VAR(ignore);
   1.315 +		iWMrk=NULL;
   1.316 +		}
   1.317 +//
   1.318 +	TStreamPos pos=aMark.Position();
   1.319 +	aMark.Track(iWMrk);
   1.320 +	BufL().SeekL(MStreamBuf::EWrite,pos);
   1.321 +	}
   1.322 +
   1.323 +TInt TStreamExchange::PrepareAndReadL(TAny* aPtr,TInt aMaxLength,TStreamMark& aMark)
   1.324 +	{
   1.325 +	PrepareForReadingL(aMark);
   1.326 +	TInt len=Buf().ReadL(aPtr,aMaxLength);
   1.327 +	iRMrk=aMark;
   1.328 +	return len;
   1.329 +	}
   1.330 +
   1.331 +TInt TStreamExchange::PrepareAndReadL(TDes8& aDes,TInt aMaxLength,TRequestStatus& aStatus,TStreamMark& aMark)
   1.332 +	{
   1.333 +	PrepareForReadingL(aMark);
   1.334 +	TInt len=Buf().ReadL(aDes,aMaxLength,aStatus);
   1.335 +	iRMrk=aMark;
   1.336 +	return len;
   1.337 +	}
   1.338 +
   1.339 +TStreamTransfer TStreamExchange::PrepareAndReadL(MStreamInput& anInput,TStreamTransfer aTransfer,TStreamMark& aMark)
   1.340 +	{
   1.341 +	PrepareForReadingL(aMark);
   1.342 +	TStreamTransfer trans=Buf().ReadL(anInput,aTransfer);
   1.343 +	iRMrk=aMark;
   1.344 +	return trans;
   1.345 +	}
   1.346 +
   1.347 +void TStreamExchange::PrepareAndWriteL(const TAny* aPtr,TInt aLength,TStreamMark& aMark)
   1.348 +	{
   1.349 +	PrepareForWritingL(aMark);
   1.350 +	Buf().WriteL(aPtr,aLength);
   1.351 +	iWMrk=aMark;
   1.352 +	}
   1.353 +
   1.354 +TInt TStreamExchange::PrepareAndWriteL(const TDesC8& aDes,TInt aMaxLength,TRequestStatus& aStatus,TStreamMark& aMark)
   1.355 +	{
   1.356 +	PrepareForWritingL(aMark);
   1.357 +	TInt len=Buf().WriteL(aDes,aMaxLength,aStatus);
   1.358 +	iWMrk=aMark;
   1.359 +	return len;
   1.360 +	}
   1.361 +
   1.362 +TStreamTransfer TStreamExchange::PrepareAndWriteL(MStreamOutput& anOutput,TStreamTransfer aTransfer,TStreamMark& aMark)
   1.363 +	{
   1.364 +	PrepareForWritingL(aMark);
   1.365 +	TStreamTransfer trans=Buf().WriteL(anOutput,aTransfer);
   1.366 +	iWMrk=aMark;
   1.367 +	return trans;
   1.368 +	}
   1.369 +
   1.370 +TStreamPos TStreamExchange::MarkSeekL(TStreamMark& aMark,TStreamLocation aLocation,TInt anOffset)
   1.371 +	{
   1.372 +	if (aLocation==EStreamMark)
   1.373 +		{
   1.374 +		if (aMark.IsEmpty())
   1.375 +			__LEAVE(KErrNotReady);
   1.376 +//
   1.377 +		aLocation=EStreamBeginning;
   1.378 +		anOffset+=aMark.Position().Offset();
   1.379 +		}
   1.380 +	aMark.Clear();
   1.381 +	TStreamPos pos=BufL().SeekL(0,aLocation,anOffset);
   1.382 +	aMark=pos;
   1.383 +	return pos;
   1.384 +	}
   1.385 +
   1.386 +EXPORT_C void TStreamMark::__DbgChkPos(TStreamPos aPos)
   1.387 +//
   1.388 +// Check for a negative position.
   1.389 +//
   1.390 +	{
   1.391 +	__ASSERT_ALWAYS(aPos>=KStreamBeginning,Panic(EStreamPosInvalid));
   1.392 +	}
   1.393 +