commsfwutils/commsbufs/version1/mbufmgr/src/MB_STRM.CPP
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 1997-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 // MBuf Stream Queue
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <nifmbuf.h>
       
    19 
       
    20 //
       
    21 // MBuf Stream Queues
       
    22 //
       
    23 
       
    24 EXPORT_C RMBufStreamQ::RMBufStreamQ()
       
    25 /**
       
    26 Constructor
       
    27 */
       
    28  	{
       
    29 	iPosition = -1; iMBuf = NULL;
       
    30 	}
       
    31 
       
    32 
       
    33 EXPORT_C void RMBufStreamQ::CopyOut(TDes8& aDes)
       
    34 /**
       
    35 Copies a data from the chain to a TDes8 buffer
       
    36 @param aDes the buffer where the data is copied to
       
    37 */
       
    38  	{
       
    39 	AsRMBufChain().CopyOut(aDes);
       
    40 	}
       
    41 
       
    42 EXPORT_C void RMBufStreamQ::TrimStart(TInt aOffset)
       
    43 /**
       
    44 Trims the data upto offset 
       
    45 @param aOffset the offset
       
    46 */
       
    47 	{
       
    48 	AsRMBufChain().TrimStart(aOffset);
       
    49 	if (iNext==NULL)
       
    50 		{
       
    51 		iLast = NULL;
       
    52 		iPosition = -1;
       
    53 		}
       
    54 	else
       
    55 		{
       
    56 		iPosition -= aOffset;
       
    57 		}
       
    58 	}
       
    59 
       
    60 EXPORT_C void RMBufStreamQ::Append(RMBufChain& aPacket)
       
    61 /**
       
    62 Appends a MBuf chain to this stream
       
    63 @param aPacket the MBuf chain
       
    64 */
       
    65  	{
       
    66 	if (iPosition>=0 && iMBuf==NULL)
       
    67 		{
       
    68 		RMBuf* m = iLast;
       
    69 		RMBufQ::Append(aPacket);
       
    70 		if (m!=NULL && (m = m->Next(), m!=NULL))
       
    71 			{
       
    72 			iMBuf = m;
       
    73 			iOffset = m->Offset();
       
    74 			iLength = m->Length();
       
    75 			}
       
    76 		}
       
    77 	else
       
    78 		RMBufQ::Append(aPacket);
       
    79 	}
       
    80 
       
    81 EXPORT_C void RMBufStreamQ::CopySegmentL(RMBufChain& aNewChain, TInt aOffset, TInt aLen/*, const TDesC8* aDes*/)
       
    82 /**
       
    83 Copies a segment to a MBuf chain
       
    84 Functionaly the similar to RMBufChain::CopyL, except that cache pointer
       
    85 information is updated to optimise the common case of each extraction
       
    86 following on from the previous call.
       
    87 @param aNewChain the chain to be created
       
    88 @param aOffset the offset
       
    89 @param aLen the length
       
    90 */
       
    91 	{
       
    92 	TInt n, n1, n2;
       
    93 	TUint8* p1, *p2;
       
    94 	RMBuf* m1, *m2;
       
    95 
       
    96 	// Locate offset into stream queue
       
    97 	if (aOffset>0)
       
    98 		{
       
    99 		if (aOffset!=iPosition || iMBuf==NULL)
       
   100 			{
       
   101 			TInt o = aOffset;
       
   102 			RMBuf* m = iNext;
       
   103 			while (m!=NULL && o>=m->Length())
       
   104 				{
       
   105 				o -= m->Length();
       
   106 				m = m->Next();
       
   107 				}
       
   108 			
       
   109 			if (!m)
       
   110 				{
       
   111 				User::Leave(KErrOverflow);
       
   112 				}
       
   113 			iPosition = aOffset;
       
   114 			iMBuf = m;
       
   115 			iOffset = o + m->Offset();
       
   116 			iLength = m->Length() - o;
       
   117 			}
       
   118 		
       
   119 		m1 = iMBuf;		
       
   120 		n = iOffset;		
       
   121 		n1 = iLength;
       
   122 		p1 = m1->Buffer()+n;
       
   123 		}
       
   124 	else
       
   125 		{
       
   126 		m1 = iNext;
       
   127 		p1 = m1->Ptr();
       
   128 		n1 = m1->Length();
       
   129 		}
       
   130 
       
   131 	TInt len = aLen;
       
   132 	aNewChain.AllocL(len);
       
   133 	m2 = aNewChain.First();
       
   134 
       
   135 	p2 = m2->Ptr();
       
   136 	n2 = m2->Length();
       
   137 	
       
   138 	while (len>0)
       
   139 		{
       
   140 		n = n1 < n2 ? n1 : n2;
       
   141 		Mem::Copy(p2, p1, n);
       
   142 		if (n1 -= n, n1 == 0)
       
   143 			{
       
   144 			if (m1 = m1->Next(), m1==NULL)
       
   145 				break;
       
   146 			p1 = m1->Ptr();
       
   147 			n1 = m1->Length();
       
   148 			}
       
   149 		else
       
   150 			p1 += n;
       
   151 
       
   152 		if (n2 -= n, n2 == 0)
       
   153 			{
       
   154 			if (m2 = m2->Next(), m2==NULL)
       
   155 				break;
       
   156 			p2 = m2->Ptr();
       
   157 			n2 = m2->Length();
       
   158 			}
       
   159 		else
       
   160 			p2 += n;
       
   161 
       
   162 		len -= n;
       
   163 		}
       
   164 
       
   165 	if (m1)
       
   166 		{
       
   167 		iPosition = aOffset+aLen;	
       
   168 		iMBuf = m1;
       
   169 		iOffset = p1-m1->Buffer();
       
   170 		iLength = n1;
       
   171 		}
       
   172 	else
       
   173 		{
       
   174 		iPosition = aOffset+aLen;	
       
   175 		iMBuf = NULL;	
       
   176 		}
       
   177 	}