diff -r 000000000000 -r bde4ae8d615e os/persistentdata/persistentstorage/store/UMEM/UM_BUF.CPP --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/os/persistentdata/persistentstorage/store/UMEM/UM_BUF.CPP Fri Jun 15 03:10:57 2012 +0200 @@ -0,0 +1,383 @@ +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// + +#include "UM_STD.H" + +EXPORT_C TMemBuf::TMemBuf() + : iBase(NULL) +/** Constructs an empty object. + +Call Set() before using the object. */ + {} + +EXPORT_C void TMemBuf::Set(TUint8* aPtr,TUint8* anEnd,TInt aMode) +/** Sets up the stream to use the specified area of plain memory. + +@param aPtr The start address for the area of plain memory that hosts the +stream and that also acts as the intermediate buffer. +@param anEnd The end address for the area of plain memory that hosts the stream +and that also acts as the intermediate buffer. The addressed byte is outside +the memory area. +@param aMode The mode in which the stream is to be used. It can be used in +either or both read and write modes, represented by ERead and EWrite. +@see MStreamBuf::TRead +@see MStreamBuf::TWrite */ + { + if (aPtr==NULL && anEnd==NULL) + aPtr=anEnd=(TUint8*)this; // treat null data as seekable zero-length data + iBase=aPtr; + SetBuf(ERead,(aMode&ERead)?aPtr:NULL,anEnd); + SetBuf(EWrite,(aMode&EWrite)?aPtr:NULL,anEnd); + } + +EXPORT_C TInt TMemBuf::UnderflowL(TInt) +// +// The read buffer is empty. +// + { + return 0; + } + +EXPORT_C void TMemBuf::OverflowL() +// +// Ran out of write buffer. +// + { + __LEAVE(KErrOverflow); + } + +EXPORT_C TStreamPos TMemBuf::DoSeekL(TMark aMark,TStreamLocation aLocation,TInt anOffset) +// +// Position the mark(s) indicated by aMark at anOffset from aLocation. +// + { + TUint8* ptr=0; + switch (aLocation) + { + case EStreamBeginning: + ptr=Base()+anOffset; + break; + case EStreamMark: + ptr=Ptr(aMark)+anOffset; + break; + case EStreamEnd: + ptr=End()+anOffset; + break; + default: + Panic(EMemLocationInvalid); + break; + } + TInt r=KErrNone; + if (ptr>End()) + { + ptr=End(); + r=KErrEof; + } + else if (ptrEnd(ERead)) + { + ptr=End(ERead); + r=KErrEof; + } + else if (ptr=0,User::Invariant()); + if (left=0,Panic(EMemWriteLengthNegative)); + __ASSERT_DEBUG(aLength>0,Panic(EMemWriteNoTransfer)); + TInt len=(iMode&EInsert?0:Min(aLength,Buf().Size()-Mark(EWrite))); + if (len>0) + { + TStreamBuf::DoWriteL(aPtr,len); + aPtr=(TUint8*)aPtr+len; + aLength-=len; + } + if (aLength>0) + { + Consolidate(); + TInt pos=Pos(EWrite); + Buf().InsertL(pos,aPtr,aLength); + if (Pos(ERead)>pos) + MovePos(ERead,aLength); + SetPos(EWrite,pos+aLength); + } + } + +EXPORT_C TStreamPos TBufBuf::DoSeekL(TMark aMark,TStreamLocation aLocation,TInt anOffset) +// +// Position the mark(s) indicated by aMark at anOffset from aLocation. +// + { + Consolidate(); + TInt size=Buf().Size(); + switch (aLocation) + { + case EStreamBeginning: + break; + case EStreamMark: + anOffset+=Pos(aMark); + break; + case EStreamEnd: + anOffset+=size; + break; + default: + Panic(EMemLocationInvalid); + break; + } + TInt r=KErrNone; + if (anOffset>size) + { + anOffset=size; + r=KErrEof; + } + else if (anOffset<0) + { + anOffset=0; + r=KErrEof; + } +// + SetPos(aMark,anOffset); + __LEAVE_IF_ERROR(r); + return TStreamPos(anOffset); + } + +void TBufBuf::Consolidate() +// +// Empty buffer areas and, in truncate mode, cut off at the current write position. +// + { + MovePos(ERead,-Avail(ERead)); + MovePos(EWrite,-Avail(EWrite)); + SetBuf(ERead|EWrite,NULL,NULL); + if (iMode&ETruncate) + { + Buf().Delete(Pos(EWrite),Buf().Size()-Pos(EWrite)); + iMode&=~ETruncate; + } + } + +void TBufBuf::SetPos(TMark aMark,TInt aPos) +// +// Set the buffer position for the mark(s) indicated by aMark +// + { + __ASSERT_ALWAYS(!(aMark&~(ERead|EWrite)),Panic(EMemMarkInvalid)); + if (aMark&ERead) + SetPos(ERead,aPos); + if (aMark&EWrite) + SetPos(EWrite,aPos); + } + +TInt TBufBuf::Pos(TMark aMark) const +// +// Return the buffer position for the mark indicated by aMark. +// + { + if (aMark==ERead) + return Pos(ERead); +// + __ASSERT_ALWAYS(aMark==EWrite,Panic(EMemMarkInvalid)); + return Pos(EWrite); + } +