testconns/statapi/device/source/statapi/src/datasupplier_memory.cpp
changeset 4 b8d1455fddc0
equal deleted inserted replaced
2:73b88125830c 4:b8d1455fddc0
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20  /*************************************************************************
       
    21  *
       
    22  * System Includes
       
    23  *
       
    24  *************************************************************************/
       
    25 
       
    26 /*************************************************************************
       
    27  *
       
    28  * Local Includes
       
    29  *
       
    30  *************************************************************************/
       
    31 
       
    32 #include "datasupplier_memory.h"
       
    33 
       
    34 /*************************************************************************
       
    35  *
       
    36  * Definitions
       
    37  *
       
    38  *************************************************************************/
       
    39 
       
    40 /*************************************************************************
       
    41  *
       
    42  * CDataSupplierMemory - Construction
       
    43  *
       
    44  *************************************************************************/
       
    45 CDataSupplierMemory *CDataSupplierMemory::NewL()
       
    46 	{
       
    47 	CDataSupplierMemory *self = new (ELeave) CDataSupplierMemory();
       
    48 
       
    49 	CleanupStack::PushL(self);
       
    50 
       
    51 	self->ConstructL();
       
    52 
       
    53 	CleanupStack::Pop();
       
    54 
       
    55 	return self;
       
    56 	}
       
    57 
       
    58 CDataSupplierMemory::CDataSupplierMemory() : CBase(),
       
    59 	iDataBuffer( NULL ), iCurrentLocation( 0 )
       
    60 	{
       
    61 	;
       
    62 	}
       
    63 
       
    64 void CDataSupplierMemory::ConstructL( void ) 
       
    65 	{
       
    66 	iLock.CreateLocal( EOwnerProcess );
       
    67 	}
       
    68 
       
    69 CDataSupplierMemory::~CDataSupplierMemory( )
       
    70 	{
       
    71 	iLock.Wait( );
       
    72 		{
       
    73 		delete iDataBuffer;
       
    74 		iDataBuffer = NULL;
       
    75 		iCurrentLocation = 0;
       
    76 
       
    77 		iLock.Close( );
       
    78 		}
       
    79 	}
       
    80 
       
    81 /*************************************************************************
       
    82  *
       
    83  * CDataSupplierMemory - Public interface
       
    84  *
       
    85  *************************************************************************/
       
    86 
       
    87 /*************************************************************************
       
    88  *
       
    89  * Delete
       
    90  *
       
    91  *************************************************************************/
       
    92 
       
    93 void CDataSupplierMemory::Delete( void ) 
       
    94 	{
       
    95 	delete this;
       
    96 	}
       
    97 
       
    98 /*************************************************************************
       
    99  *
       
   100  * GetTotalSize
       
   101  *
       
   102  *************************************************************************/
       
   103 
       
   104 TInt CDataSupplierMemory::GetTotalSize( TInt &aTotalSize )
       
   105 	{
       
   106 	TInt	err = KErrNone;
       
   107 	TInt	size = 0;
       
   108 
       
   109 	iLock.Wait( );
       
   110 		{
       
   111 		size = iDataBuffer->Length( );
       
   112 
       
   113 		aTotalSize = size;
       
   114 
       
   115 		iLock.Signal( );
       
   116 		}
       
   117 
       
   118 	return ( err );
       
   119 	}
       
   120 
       
   121 /*************************************************************************
       
   122  *
       
   123  * GetRemainingSize
       
   124  *
       
   125  *************************************************************************/
       
   126 
       
   127 TInt CDataSupplierMemory::GetRemainingSize( TInt &aRemainingSize )
       
   128 	{
       
   129 	TInt	err = KErrNone;
       
   130 	TInt	remaining = 0;
       
   131 
       
   132 	iLock.Wait( );
       
   133 		{
       
   134 		TInt	totalSize =	0;
       
   135 
       
   136 		err = GetTotalSize( totalSize );
       
   137 
       
   138 		if( err == KErrNone )
       
   139 			{
       
   140 			remaining = totalSize - iCurrentLocation;
       
   141 
       
   142 			aRemainingSize = remaining;
       
   143 			}
       
   144 
       
   145 		iLock.Signal( );
       
   146 		}
       
   147 
       
   148 	return ( err );
       
   149 	}
       
   150 
       
   151 /*************************************************************************
       
   152  *
       
   153  * SetData
       
   154  *
       
   155  *************************************************************************/
       
   156 
       
   157 TInt CDataSupplierMemory::SetData( const TDesC8 &aSource )
       
   158 	{
       
   159 	TInt	err = KErrNone;
       
   160 
       
   161 	iLock.Wait( );
       
   162 		{
       
   163 		delete iDataBuffer;
       
   164 		iDataBuffer = NULL;
       
   165 
       
   166 		TInt length = aSource.Length( );
       
   167 		iDataBuffer = HBufC8::New( length );
       
   168 
       
   169 		if(iDataBuffer)
       
   170 			{
       
   171 			*iDataBuffer = aSource;
       
   172 			}
       
   173 		else
       
   174 			{
       
   175 			err = KErrNoMemory;
       
   176 			}
       
   177 
       
   178 		iCurrentLocation = 0;
       
   179 		iLock.Signal( );
       
   180 		}
       
   181 
       
   182 #ifdef _DEBUG
       
   183 	{
       
   184 	TInt totalSize = 0;
       
   185 	GetTotalSize( totalSize );
       
   186 	TInt remainingSize = 0;
       
   187 	GetRemainingSize( remainingSize );
       
   188 	}
       
   189 #endif
       
   190 
       
   191 	return ( err );
       
   192 	}
       
   193 
       
   194 /*************************************************************************
       
   195  *
       
   196  * GetData
       
   197  *
       
   198  *************************************************************************/
       
   199 
       
   200 TInt CDataSupplierMemory::GetData( HBufC8 &aDestination, 
       
   201 				TInt aLengthToCopy, TInt &aActuallyCopied )
       
   202 	{
       
   203 	TInt	err = KErrNone;
       
   204 
       
   205 	iLock.Wait( );
       
   206 		{
       
   207 		TInt remainingSize = 0;
       
   208 		err = GetRemainingSize( remainingSize );
       
   209 
       
   210 		if( err == KErrNone )
       
   211 			{
       
   212 			TInt toCopy = Min( aLengthToCopy, remainingSize );
       
   213 
       
   214 			TPtr8 destPtr( aDestination.Des( ) );
       
   215 			destPtr.Copy( iDataBuffer->Ptr( ) + iCurrentLocation,
       
   216 							toCopy );
       
   217 
       
   218 			if( err == KErrNone )
       
   219 				{
       
   220 				iCurrentLocation += toCopy;
       
   221 				aActuallyCopied = toCopy;
       
   222 				}
       
   223 			}
       
   224 
       
   225 		iLock.Signal( );
       
   226 		}
       
   227 
       
   228 	return ( err );
       
   229 	}