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