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