persistentstorage/store/USTRM/US_HOST.CPP
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "US_STD.H"
       
    17 
       
    18 #define UNUSED_VAR(a) a = a
       
    19 
       
    20 EXPORT_C TInt TStreamMark::ReadL(TStreamExchange& aHost,TDes8& aDes,TRequestStatus& aStatus)
       
    21 //
       
    22 // Read asynchronously.
       
    23 //
       
    24 	{
       
    25 	return aHost.DoReadL(aDes,aDes.MaxLength(),aStatus,*this);
       
    26 	}
       
    27 
       
    28 EXPORT_C TInt TStreamMark::WriteL(TStreamExchange& aHost,const TDesC8& aDes,TRequestStatus& aStatus)
       
    29 /** Writes data, asynchronously, from the specified descriptor into the shared 
       
    30 stream.
       
    31 
       
    32 The maximum number of bytes to be written is the value of the maximum length 
       
    33 of the descriptor.
       
    34 
       
    35 If the function leaves, then no write request will have been initiated.
       
    36 
       
    37 @param aHost The object that manages shared streaming.
       
    38 @param aDes The source descriptor for the data to be written into the shared 
       
    39 stream.
       
    40 @param aStatus The request status that indicates the completion status of this 
       
    41 asynchronous request.
       
    42 @return The maximum number of bytes to be written, as used in this request. 
       
    43 This can be different to the maximum length of the descriptor; this is dependent 
       
    44 on the implementation. */
       
    45 	{
       
    46 	return aHost.DoWriteL(aDes,aDes.Length(),aStatus,*this);
       
    47 	}
       
    48 
       
    49 EXPORT_C MStreamBuf* TStreamExchange::Host()
       
    50 /** Gets a pointer to the stream buffer that acts as the shared host.
       
    51 
       
    52 The function refreshes the active read/write marks and gives the caller exclusive 
       
    53 use of the shared host.
       
    54 
       
    55 @return A pointer to a stream buffer that acts as shared host. */
       
    56 	{
       
    57 	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
       
    58 	if (iRMrk!=NULL)
       
    59 		{
       
    60 		TRAPD(ignore,*iRMrk=Buf().TellL(MStreamBuf::ERead));
       
    61         UNUSED_VAR(ignore);
       
    62 		iRMrk=NULL;
       
    63 		}
       
    64 	if (iWMrk!=NULL)
       
    65 		{
       
    66 		TRAPD(ignore,*iWMrk=Buf().TellL(MStreamBuf::EWrite));
       
    67         UNUSED_VAR(ignore);
       
    68 		iWMrk=NULL;
       
    69 		}
       
    70 	return iHost;
       
    71 	}
       
    72 
       
    73 EXPORT_C MStreamBuf* TStreamExchange::HostL()
       
    74 /** Gets a pointer to the stream buffer that acts as the shared host, and leaves 
       
    75 if this object is not active.
       
    76 
       
    77 The function refreshes the active read/write marks and gives the caller exclusive 
       
    78 use of the shared host.
       
    79 
       
    80 @return A pointer to a stream buffer that acts as shared host.
       
    81 @see IsActive() */
       
    82 	{
       
    83 	MStreamBuf* host=Host();
       
    84 	if (host==NULL)
       
    85 		__LEAVE(KErrNotReady);
       
    86 	return host;
       
    87 	}
       
    88 
       
    89 EXPORT_C void TStreamExchange::Release()
       
    90 /** Releases the reference to the shared host, and drops any active read or write 
       
    91 marks. */
       
    92 	{
       
    93 	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
       
    94 	iHost=NULL;
       
    95 	iRMrk=NULL;
       
    96 	iWMrk=NULL;
       
    97 	}
       
    98 
       
    99 EXPORT_C TInt TStreamExchange::SizeL() const
       
   100 /** Gets the size of the shared host buffer.
       
   101 
       
   102 @return The size of the shared host buffer. */
       
   103 	{
       
   104 	return BufL().SizeL();
       
   105 	}
       
   106 
       
   107 EXPORT_C TBool TStreamExchange::RefersTo(const TStreamMark& aMark)
       
   108 //
       
   109 // Return whether aMark is referred to.
       
   110 //
       
   111 	{
       
   112 	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
       
   113 	return iRMrk==aMark||iWMrk==aMark;
       
   114 	}
       
   115 
       
   116 EXPORT_C void TStreamExchange::Drop(const TStreamMark& aMark)
       
   117 //
       
   118 // Drop any reference to aMark.
       
   119 //
       
   120 	{
       
   121 	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
       
   122 	if (iRMrk==aMark)
       
   123 		iRMrk=NULL;
       
   124 	else if (iWMrk==aMark)
       
   125 		iWMrk=NULL;
       
   126 	}
       
   127 
       
   128 EXPORT_C void TStreamExchange::GetL(TStreamMark& aMark)
       
   129 //
       
   130 // Refresh and drop any reference to aMark.
       
   131 //
       
   132 	{
       
   133 	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
       
   134 	if (iRMrk==aMark)
       
   135 		{
       
   136 		iRMrk=NULL;
       
   137 		aMark=Buf().TellL(MStreamBuf::ERead);
       
   138 		}
       
   139 	else if (iWMrk==aMark)
       
   140 		{
       
   141 		iWMrk=NULL;
       
   142 		aMark=Buf().TellL(MStreamBuf::EWrite);
       
   143 		}
       
   144 	}
       
   145 
       
   146 EXPORT_C TInt TStreamExchange::DoReadL(TAny* aPtr,TInt aMaxLength,TStreamMark& aMark)
       
   147 //
       
   148 // Read up to aMaxLength bytes starting at aMark.
       
   149 //
       
   150 	{
       
   151 	__ASSERT_DEBUG(iWMrk==NULL||iWMrk!=iRMrk&&iWMrk->IsTracking(iWMrk),User::Invariant());
       
   152 	if (iRMrk!=aMark)
       
   153 		return PrepareAndReadL(aPtr,aMaxLength,aMark);
       
   154 //
       
   155 	__ASSERT_DEBUG(aMark.IsTracking(iRMrk),User::Invariant());
       
   156 	iRMrk=NULL;
       
   157 	TInt len=Buf().ReadL(aPtr,aMaxLength);
       
   158 	iRMrk=aMark;
       
   159 	return len;
       
   160 	}
       
   161 
       
   162 EXPORT_C TInt TStreamExchange::DoReadL(TDes8& aDes,TInt aMaxLength,TRequestStatus& aStatus,TStreamMark& aMark)
       
   163 //
       
   164 // Read up to aMaxLength bytes asynchronously.
       
   165 //
       
   166 	{
       
   167 	__ASSERT_DEBUG(iWMrk==NULL||iWMrk!=iRMrk&&iWMrk->IsTracking(iWMrk),User::Invariant());
       
   168 	if (iRMrk!=aMark)
       
   169 		return PrepareAndReadL(aDes,aMaxLength,aStatus,aMark);
       
   170 //
       
   171 	__ASSERT_DEBUG(aMark.IsTracking(iRMrk),User::Invariant());
       
   172 	iRMrk=NULL;
       
   173 	TInt len=Buf().ReadL(aDes,aMaxLength,aStatus);
       
   174 	iRMrk=aMark;
       
   175 	return len;
       
   176 	}
       
   177 
       
   178 EXPORT_C TStreamTransfer TStreamExchange::DoReadL(MStreamInput& anInput,TStreamTransfer aTransfer,TStreamMark& aMark)
       
   179 //
       
   180 // Push up to aTransfer bytes into anInput, starting at aMark.
       
   181 //
       
   182 	{
       
   183 	__ASSERT_DEBUG(iWMrk==NULL||iWMrk!=iRMrk&&iWMrk->IsTracking(iWMrk),User::Invariant());
       
   184 	if (iRMrk!=aMark)
       
   185 		return PrepareAndReadL(anInput,aTransfer,aMark);
       
   186 //
       
   187 	__ASSERT_DEBUG(aMark.IsTracking(iRMrk),User::Invariant());
       
   188 	iRMrk=NULL;
       
   189 	TStreamTransfer trans=Buf().ReadL(anInput,aTransfer);
       
   190 	iRMrk=aMark;
       
   191 	return trans;
       
   192 	}
       
   193 
       
   194 EXPORT_C void TStreamExchange::DoWriteL(const TAny* aPtr,TInt aLength,TStreamMark& aMark)
       
   195 //
       
   196 // Write aLength bytes starting at aMark.
       
   197 //
       
   198 	{
       
   199 	__ASSERT_DEBUG(iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk),User::Invariant());
       
   200 	if (iWMrk==aMark)
       
   201 		{
       
   202 		__ASSERT_DEBUG(aMark.IsTracking(iWMrk),User::Invariant());
       
   203 		iWMrk=NULL;
       
   204 		Buf().WriteL(aPtr,aLength);
       
   205 		iWMrk=aMark;
       
   206 		}
       
   207 	else
       
   208 		PrepareAndWriteL(aPtr,aLength,aMark);
       
   209 	}
       
   210 
       
   211 EXPORT_C TInt TStreamExchange::DoWriteL(const TDesC8& aDes,TInt aMaxLength,TRequestStatus& aStatus,TStreamMark& aMark)
       
   212 //
       
   213 // Write up to aMaxLength bytes asynchronously.
       
   214 //
       
   215 	{
       
   216 	__ASSERT_DEBUG(iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk),User::Invariant());
       
   217 	if (iWMrk!=aMark)
       
   218 		return PrepareAndWriteL(aDes,aMaxLength,aStatus,aMark);
       
   219 //
       
   220 	__ASSERT_DEBUG(aMark.IsTracking(iWMrk),User::Invariant());
       
   221 	iWMrk=NULL;
       
   222 	TInt len=Buf().WriteL(aDes,aMaxLength,aStatus);
       
   223 	iWMrk=aMark;
       
   224 	return len;
       
   225 	}
       
   226 
       
   227 EXPORT_C TStreamTransfer TStreamExchange::DoWriteL(MStreamOutput& anOutput,TStreamTransfer aTransfer,TStreamMark& aMark)
       
   228 //
       
   229 // Pull up to aTransfer bytes from anOutput, starting at aMark.
       
   230 //
       
   231 	{
       
   232 	__ASSERT_DEBUG(iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk),User::Invariant());
       
   233 	if (iWMrk!=aMark)
       
   234 		return PrepareAndWriteL(anOutput,aTransfer,aMark);
       
   235 //
       
   236 	__ASSERT_DEBUG(aMark.IsTracking(iWMrk),User::Invariant());
       
   237 	iWMrk=NULL;
       
   238 	TStreamTransfer trans=Buf().WriteL(anOutput,aTransfer);
       
   239 	iWMrk=aMark;
       
   240 	return trans;
       
   241 	}
       
   242 
       
   243 EXPORT_C TStreamPos TStreamExchange::DoSeekL(TStreamMark& aMark,TStreamLocation aLocation,TInt anOffset)
       
   244 //
       
   245 // Position aMark at anOffset from aLocation.
       
   246 //
       
   247 	{
       
   248 	__ASSERT_DEBUG((iRMrk==NULL||iRMrk!=iWMrk&&iRMrk->IsTracking(iRMrk))&&(iWMrk==NULL||iWMrk->IsTracking(iWMrk)),User::Invariant());
       
   249 	if (iRMrk==aMark)
       
   250 		{
       
   251 		iRMrk=NULL;
       
   252 		TStreamPos pos=Buf().SeekL(MStreamBuf::ERead,aLocation,anOffset);
       
   253 		iRMrk=aMark;
       
   254 		return pos;
       
   255 		}
       
   256 //
       
   257 	if (iWMrk==aMark)
       
   258 		{
       
   259 		iWMrk=NULL;
       
   260 		TStreamPos pos=Buf().SeekL(MStreamBuf::EWrite,aLocation,anOffset);
       
   261 		iWMrk=aMark;
       
   262 		return pos;
       
   263 		}
       
   264 //
       
   265 	return MarkSeekL(aMark,aLocation,anOffset);
       
   266 	}
       
   267 
       
   268 void TStreamExchange::PrepareForReadingL(TStreamMark& aMark)
       
   269 //
       
   270 // Prepare the host buffer to start reading at aMark.
       
   271 //
       
   272 	{
       
   273 	__ASSERT_DEBUG(iRMrk==NULL||iRMrk->IsTracking(iRMrk),User::Invariant());
       
   274 	if (iWMrk==aMark)
       
   275 		{
       
   276 		iWMrk=NULL;
       
   277 		aMark=Buf().TellL(MStreamBuf::EWrite);
       
   278 		}
       
   279 	else if (aMark.IsEmpty())
       
   280 		__LEAVE(KErrNotReady);
       
   281 //
       
   282 	if (iRMrk!=NULL)
       
   283 		{
       
   284 		TRAPD(ignore,*iRMrk=Buf().TellL(MStreamBuf::ERead));
       
   285         UNUSED_VAR(ignore);
       
   286 		iRMrk=NULL;
       
   287 		}
       
   288 //
       
   289 	TStreamPos pos=aMark.Position();
       
   290 	aMark.Track(iRMrk);
       
   291 	BufL().SeekL(MStreamBuf::ERead,pos);
       
   292 	}
       
   293 
       
   294 void TStreamExchange::PrepareForWritingL(TStreamMark& aMark)
       
   295 //
       
   296 // Prepare the host buffer to start writing at aMark.
       
   297 //
       
   298 	{
       
   299 	__ASSERT_DEBUG(iWMrk==NULL||iWMrk->IsTracking(iWMrk),User::Invariant());
       
   300 	if (iRMrk==aMark)
       
   301 		{
       
   302 		iRMrk=NULL;
       
   303 		aMark=Buf().TellL(MStreamBuf::ERead);
       
   304 		}
       
   305 	else if (aMark.IsEmpty())
       
   306 		__LEAVE(KErrNotReady);
       
   307 //
       
   308 	if (iWMrk!=NULL)
       
   309 		{
       
   310 		TRAPD(ignore,*iWMrk=Buf().TellL(MStreamBuf::EWrite));
       
   311         UNUSED_VAR(ignore);
       
   312 		iWMrk=NULL;
       
   313 		}
       
   314 //
       
   315 	TStreamPos pos=aMark.Position();
       
   316 	aMark.Track(iWMrk);
       
   317 	BufL().SeekL(MStreamBuf::EWrite,pos);
       
   318 	}
       
   319 
       
   320 TInt TStreamExchange::PrepareAndReadL(TAny* aPtr,TInt aMaxLength,TStreamMark& aMark)
       
   321 	{
       
   322 	PrepareForReadingL(aMark);
       
   323 	TInt len=Buf().ReadL(aPtr,aMaxLength);
       
   324 	iRMrk=aMark;
       
   325 	return len;
       
   326 	}
       
   327 
       
   328 TInt TStreamExchange::PrepareAndReadL(TDes8& aDes,TInt aMaxLength,TRequestStatus& aStatus,TStreamMark& aMark)
       
   329 	{
       
   330 	PrepareForReadingL(aMark);
       
   331 	TInt len=Buf().ReadL(aDes,aMaxLength,aStatus);
       
   332 	iRMrk=aMark;
       
   333 	return len;
       
   334 	}
       
   335 
       
   336 TStreamTransfer TStreamExchange::PrepareAndReadL(MStreamInput& anInput,TStreamTransfer aTransfer,TStreamMark& aMark)
       
   337 	{
       
   338 	PrepareForReadingL(aMark);
       
   339 	TStreamTransfer trans=Buf().ReadL(anInput,aTransfer);
       
   340 	iRMrk=aMark;
       
   341 	return trans;
       
   342 	}
       
   343 
       
   344 void TStreamExchange::PrepareAndWriteL(const TAny* aPtr,TInt aLength,TStreamMark& aMark)
       
   345 	{
       
   346 	PrepareForWritingL(aMark);
       
   347 	Buf().WriteL(aPtr,aLength);
       
   348 	iWMrk=aMark;
       
   349 	}
       
   350 
       
   351 TInt TStreamExchange::PrepareAndWriteL(const TDesC8& aDes,TInt aMaxLength,TRequestStatus& aStatus,TStreamMark& aMark)
       
   352 	{
       
   353 	PrepareForWritingL(aMark);
       
   354 	TInt len=Buf().WriteL(aDes,aMaxLength,aStatus);
       
   355 	iWMrk=aMark;
       
   356 	return len;
       
   357 	}
       
   358 
       
   359 TStreamTransfer TStreamExchange::PrepareAndWriteL(MStreamOutput& anOutput,TStreamTransfer aTransfer,TStreamMark& aMark)
       
   360 	{
       
   361 	PrepareForWritingL(aMark);
       
   362 	TStreamTransfer trans=Buf().WriteL(anOutput,aTransfer);
       
   363 	iWMrk=aMark;
       
   364 	return trans;
       
   365 	}
       
   366 
       
   367 TStreamPos TStreamExchange::MarkSeekL(TStreamMark& aMark,TStreamLocation aLocation,TInt anOffset)
       
   368 	{
       
   369 	if (aLocation==EStreamMark)
       
   370 		{
       
   371 		if (aMark.IsEmpty())
       
   372 			__LEAVE(KErrNotReady);
       
   373 //
       
   374 		aLocation=EStreamBeginning;
       
   375 		anOffset+=aMark.Position().Offset();
       
   376 		}
       
   377 	aMark.Clear();
       
   378 	TStreamPos pos=BufL().SeekL(0,aLocation,anOffset);
       
   379 	aMark=pos;
       
   380 	return pos;
       
   381 	}
       
   382 
       
   383 EXPORT_C void TStreamMark::__DbgChkPos(TStreamPos aPos)
       
   384 //
       
   385 // Check for a negative position.
       
   386 //
       
   387 	{
       
   388 	__ASSERT_ALWAYS(aPos>=KStreamBeginning,Panic(EStreamPosInvalid));
       
   389 	}
       
   390