persistentstorage/store/USTRM/US_IO.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 TInt TPtrInput::PushL(const TAny* aPtr,TInt aMaxLength)
       
    19 //
       
    20 // Accept the data, copying it to the buffer pointed to.
       
    21 //
       
    22 	{
       
    23 	__ASSERT_DEBUG(aMaxLength>=0,Panic(EStreamPushLengthNegative));
       
    24 	__ASSERT_DEBUG(aMaxLength>0,Panic(EStreamPushNoTransfer));
       
    25 	iPtr=Mem::Copy(iPtr,aPtr,aMaxLength);
       
    26 	return aMaxLength;
       
    27 	}
       
    28 
       
    29 TStreamTransfer TPtrInput::ReadFromL(MStreamBuf&,TStreamTransfer aTransfer)
       
    30 //
       
    31 // This input is passive.
       
    32 //
       
    33 	{
       
    34 	__ASSERT_DEBUG(aTransfer>0,Panic(EStreamReadNoTransfer));
       
    35 	return aTransfer;
       
    36 	}
       
    37 
       
    38 TInt TPtrOutput::PullL(TAny* aPtr,TInt aMaxLength)
       
    39 //
       
    40 // Produce data from the buffer pointed to.
       
    41 //
       
    42 	{
       
    43 	__ASSERT_DEBUG(aMaxLength>=0,Panic(EStreamPullLengthNegative));
       
    44 	__ASSERT_DEBUG(aMaxLength>0,Panic(EStreamPullNoTransfer));
       
    45 	Mem::Copy(aPtr,iPtr,aMaxLength);
       
    46 	iPtr+=aMaxLength;
       
    47 	return aMaxLength;
       
    48 	}
       
    49 
       
    50 TStreamTransfer TPtrOutput::WriteToL(MStreamBuf&,TStreamTransfer aTransfer)
       
    51 //
       
    52 // This output is passive.
       
    53 //
       
    54 	{
       
    55 	__ASSERT_DEBUG(aTransfer>0,Panic(EStreamWriteNoTransfer));
       
    56 	return aTransfer;
       
    57 	}
       
    58 
       
    59 TInt TNullInput::PushL(const TAny*,TInt aMaxLength)
       
    60 //
       
    61 // Accept and discard the data.
       
    62 //
       
    63 	{
       
    64 	__ASSERT_DEBUG(aMaxLength>=0,Panic(EStreamPushLengthNegative));
       
    65 	__ASSERT_DEBUG(aMaxLength>0,Panic(EStreamPushNoTransfer));
       
    66 	return aMaxLength;
       
    67 	}
       
    68 
       
    69 TStreamTransfer TNullInput::ReadFromL(MStreamBuf& aSource,TStreamTransfer aTransfer)
       
    70 //
       
    71 // Read and discard data from aSource using a temporary buffer.
       
    72 //
       
    73 	{
       
    74 	__ASSERT_DEBUG(aTransfer>0,Panic(EStreamReadNoTransfer));
       
    75 	do
       
    76 		{
       
    77 		TUint8 buf[KDefaultIoBufSize];
       
    78 		TInt len=aSource.ReadL(buf,aTransfer[sizeof(buf)]);
       
    79 		__ASSERT_DEBUG(len>=0&&len<=aTransfer[sizeof(buf)],Panic(EStreamReadInBreach));
       
    80 		if (len==0)
       
    81 			break;
       
    82 //
       
    83 		aTransfer-=len;
       
    84 		} while (aTransfer>0);
       
    85 	return aTransfer;
       
    86 	}
       
    87 
       
    88 TInt TSourceOutput::PullL(TAny* aPtr,TInt aMaxLength)
       
    89 //
       
    90 // Pull up to aMaxLength bytes of data from this output's source.
       
    91 //
       
    92 	{
       
    93 	__ASSERT_DEBUG(aMaxLength>=0,Panic(EStreamPullLengthNegative));
       
    94 	__ASSERT_DEBUG(aMaxLength>0,Panic(EStreamPullNoTransfer));
       
    95 	__ASSERT_DEBUG(iSrc!=NULL,User::Invariant());
       
    96 //
       
    97 	TPtrInput input(aPtr);
       
    98 	TInt len=iSrc->ReadL(input,aMaxLength);
       
    99 	__ASSERT_DEBUG(len>=0&&len<=aMaxLength,Panic(EStreamReadInBreach));
       
   100 	return len;
       
   101 	}
       
   102 
       
   103 TStreamTransfer TSourceOutput::WriteToL(MStreamBuf& aSink,TStreamTransfer aTransfer)
       
   104 //
       
   105 // Write data from this output's source to aSink using a temporary buffer.
       
   106 //
       
   107 	{
       
   108 	__ASSERT_DEBUG(aTransfer>0,Panic(EStreamWriteNoTransfer));
       
   109 	__ASSERT_DEBUG(iSrc!=NULL,User::Invariant());
       
   110 	do
       
   111 		{
       
   112 		TUint8 buf[KDefaultIoBufSize];
       
   113 		TInt len=iSrc->ReadL(buf,aTransfer[sizeof(buf)]);
       
   114 		__ASSERT_DEBUG(len>=0&&len<=aTransfer[sizeof(buf)],Panic(EStreamReadInBreach));
       
   115 		if (len==0)
       
   116 			break;
       
   117 //
       
   118 		aSink.WriteL(buf,len);
       
   119 		aTransfer-=len;
       
   120 		} while (aTransfer>0);
       
   121 	return aTransfer;
       
   122 	}
       
   123 
       
   124 TInt TFilterInput::PushL(const TAny* aPtr,TInt aMaxLength)
       
   125 //
       
   126 // Put up to aMaxLength bytes of data through the filter.
       
   127 //
       
   128 	{
       
   129 	__ASSERT_DEBUG(aMaxLength>=0,Panic(EStreamPushLengthNegative));
       
   130 	__ASSERT_DEBUG(aMaxLength>0,Panic(EStreamPushNoTransfer));
       
   131 	__ASSERT_DEBUG(!Eof(),Panic(EStreamReadInBreach));
       
   132 	__ASSERT_DEBUG(iFltr!=NULL&&iLeft>=0,User::Invariant());
       
   133 	if (Done())
       
   134 		return 0;
       
   135 //
       
   136 	const TUint8* from=(TUint8*)aPtr;
       
   137 	TInt len=iFltr->FilterL(iPtr,iLeft,from,from+aMaxLength);
       
   138 	__ASSERT_DEBUG(len>=0&&len<=iLeft,Panic(EStreamFilterInBreach));
       
   139 	__ASSERT_DEBUG(from>=(TUint8*)aPtr&&from<=(TUint8*)aPtr+aMaxLength,Panic(EStreamFilterInBreach));
       
   140 	__ASSERT_DEBUG(len==iLeft||from==(TUint8*)aPtr+aMaxLength,Panic(EStreamFilterInBreach));
       
   141 	iPtr+=len;
       
   142 	iLeft-=len;
       
   143 	return from-(TUint8*)aPtr;
       
   144 	}
       
   145 
       
   146 TStreamTransfer TFilterInput::ReadFromL(MStreamBuf& aSource,TStreamTransfer aTransfer)
       
   147 //
       
   148 // Put data read from aSource through the filter using a temporary buffer.
       
   149 //
       
   150 	{
       
   151 	__ASSERT_DEBUG(aTransfer>0,Panic(EStreamReadNoTransfer));
       
   152 	__ASSERT_DEBUG(iFltr!=NULL&&iLeft>=0,User::Invariant());
       
   153 	if (Done())
       
   154 		return aTransfer;
       
   155 //
       
   156 	TUint8 buf[KFilterIoBufSize];
       
   157 	TInt cap=aTransfer[Min(iFltr->Capacity(iLeft),sizeof(buf))];
       
   158 	const TUint8* end=buf+(cap==0?0:aSource.ReadL(buf,cap));
       
   159 	__ASSERT_DEBUG(end>=buf&&end<=buf+cap&&(!Eof()||end==buf),Panic(EStreamReadInBreach));
       
   160 	const TUint8* from=buf;
       
   161 	TInt len=iFltr->FilterL(iPtr,iLeft,from,end);
       
   162 	__ASSERT_DEBUG(len>=0&&len<=iLeft,Panic(EStreamFilterInBreach));
       
   163 	__ASSERT_DEBUG(from==end,Panic(EStreamFilterInBreach));
       
   164 	if (end==buf && len==0)	// no input && no output, => end of stream
       
   165 		iPtr=NULL;
       
   166 	else
       
   167 		iPtr+=len;
       
   168 	iLeft-=len;
       
   169 	return aTransfer-(from-buf);
       
   170 	}
       
   171 
       
   172 TInt TFilterOutput::PullL(TAny* aPtr,TInt aMaxLength)
       
   173 //
       
   174 // Pull up to aMaxLength bytes of data through the filter.
       
   175 //
       
   176 	{
       
   177 	__ASSERT_DEBUG(aMaxLength>=0,Panic(EStreamPullLengthNegative));
       
   178 	__ASSERT_DEBUG(aMaxLength>0,Panic(EStreamPullNoTransfer));
       
   179 	__ASSERT_DEBUG(iFltr!=NULL&&iFrom!=NULL&&iFrom<=iEnd,User::Invariant());
       
   180 	if (Done())
       
   181 		return 0;
       
   182 //
       
   183 	__DEBUG(const TUint8* from=iFrom);
       
   184 	TInt len=iFltr->FilterL(aPtr,aMaxLength,iFrom,iEnd);
       
   185 	__ASSERT_DEBUG(len>=0&&len<=aMaxLength,Panic(EStreamFilterInBreach));
       
   186 	__ASSERT_DEBUG(iFrom>=from&&iFrom<=iEnd,Panic(EStreamFilterInBreach));
       
   187 	__ASSERT_DEBUG(len==aMaxLength||iFrom==iEnd,Panic(EStreamFilterInBreach));
       
   188 	return len;
       
   189 	}
       
   190 
       
   191 TStreamTransfer TFilterOutput::WriteToL(MStreamBuf& aSink,TStreamTransfer aTransfer)
       
   192 //
       
   193 // Write data put through the filter to aSink using a temporary buffer.
       
   194 //
       
   195 	{
       
   196 	__ASSERT_DEBUG(aTransfer>0,Panic(EStreamWriteNoTransfer));
       
   197 	__ASSERT_DEBUG(iFltr!=NULL&&iFrom!=NULL&&iFrom<=iEnd,User::Invariant());
       
   198 	if (Done())
       
   199 		return aTransfer;
       
   200 //
       
   201 	TUint8 buf[KFilterIoBufSize];
       
   202 	__DEBUG(const TUint8* from=iFrom);
       
   203 	TInt len=iFltr->FilterL(buf,aTransfer[sizeof(buf)],iFrom,iEnd);
       
   204 	__ASSERT_DEBUG(len>=0&&len<=aTransfer[sizeof(buf)],Panic(EStreamFilterInBreach));
       
   205 	__ASSERT_DEBUG(iFrom>=from&&iFrom<=iEnd,Panic(EStreamFilterInBreach));
       
   206 	__ASSERT_DEBUG(len==aTransfer[sizeof(buf)]||iFrom==iEnd,Panic(EStreamFilterInBreach));
       
   207 	if (len>0)
       
   208 		aSink.WriteL(buf,len);
       
   209 	return aTransfer-len;
       
   210 	}
       
   211 
       
   212 TDelimitedInput8::TDelimitedInput8(TUint8* aPtr,TInt aLength,TChar aDelim)
       
   213 	: iPtr(aPtr),iLeft(aLength),iDelim(aDelim)
       
   214 	{}
       
   215 
       
   216 TInt TDelimitedInput8::PushL(const TAny* aPtr,TInt aMaxLength)
       
   217 //
       
   218 // Push 8-bit text into this input buffer up to the first occurrence of the delimiter.
       
   219 //
       
   220 	{
       
   221 	TInt len=Min(aMaxLength,iLeft);
       
   222 	TInt d=TPtrC8((TUint8*)aPtr,len).Locate(iDelim)+1;
       
   223 	if (d<=0)
       
   224 		{
       
   225 		d=len;
       
   226 		iLeft-=len;
       
   227 		}
       
   228 	else
       
   229 		iLeft=0;
       
   230 	iPtr=(TUint8*)Mem::Copy(iPtr,aPtr,d);
       
   231 	return d;
       
   232 	}
       
   233 
       
   234 TStreamTransfer TDelimitedInput8::ReadFromL(MStreamBuf& aSource,TStreamTransfer aTransfer)
       
   235 //
       
   236 // Read a single 8-bit text character from aSource, testing it against the delimiter.
       
   237 //
       
   238 	{
       
   239 	if (Done())
       
   240 		return aTransfer;
       
   241 //
       
   242 	RReadStream stream(&aSource);
       
   243 	TUint8 c=stream.ReadUint8L();
       
   244 	*iPtr++=c;
       
   245 	if (c==(TUint8)iDelim)
       
   246 		iLeft=0;
       
   247 	else
       
   248 		--iLeft;
       
   249 	return aTransfer-sizeof(c);
       
   250 	}
       
   251 
       
   252 TDelimitedInput16::TDelimitedInput16(TUint16* aPtr,TInt aLength,TChar aDelim)
       
   253 	: iPtr(aPtr),iLeft(aLength),iDelim(aDelim)
       
   254 	{}
       
   255 
       
   256 TInt TDelimitedInput16::PushL(const TAny* aPtr,TInt aMaxLength)
       
   257 //
       
   258 // Push 16-bit text into this input buffer up to the first occurrence of the delimiter.
       
   259 // We cannot use TPtrC16 here as the data may not be half-word aligned
       
   260 //
       
   261 	{
       
   262 	TInt len=Min(aMaxLength>>1,iLeft);
       
   263 	iLeft-=len;
       
   264 	TInt lbyte=iDelim&0xffu;
       
   265 	TInt hbyte=iDelim>>8;
       
   266 	const TUint8* p=static_cast<const TUint8*>(aPtr);
       
   267 	TInt d=0;
       
   268 	while (d<len)
       
   269 		{
       
   270 		if (p[d<<1]==lbyte && p[(d<<1)+1]==hbyte) // platform dependency
       
   271 			{
       
   272 			++d;		// include the delimiter
       
   273 			iLeft=0;	// found a match
       
   274 			break;
       
   275 			}
       
   276 		++d;
       
   277 		}
       
   278 
       
   279 	iPtr=(TUint16*)Mem::Copy(iPtr,aPtr,d<<1); // platform dependency
       
   280 	return d<<1;
       
   281 	}
       
   282 
       
   283 TStreamTransfer TDelimitedInput16::ReadFromL(MStreamBuf& aSource,TStreamTransfer aTransfer)
       
   284 //
       
   285 // Read a single 16-bit text character from aSource, testing it against the delimiter.
       
   286 //
       
   287 	{
       
   288 	if (Done())
       
   289 		return aTransfer;
       
   290 //
       
   291 	RReadStream stream(&aSource);
       
   292 	TUint16 c=stream.ReadUint16L();
       
   293 //
       
   294 	*iPtr++=c;
       
   295 	if (c==(TUint16)iDelim)
       
   296 		iLeft=0;
       
   297 	else
       
   298 		--iLeft;
       
   299 	return aTransfer-sizeof(c);
       
   300 	}
       
   301