testexecfw/statsrv/device/source/statapi/src/dataconsumer_memory.cpp
changeset 0 3e07fef1e154
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     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 #include <e32std.h>
       
    27 #include <e32base.h>
       
    28 #include <f32file.h>
       
    29 
       
    30 /*************************************************************************
       
    31  *
       
    32  * Local Includes
       
    33  *
       
    34  *************************************************************************/
       
    35 
       
    36 #include "dataconsumer_memory.h"
       
    37 
       
    38 /*************************************************************************
       
    39  *
       
    40  * Definitions
       
    41  *
       
    42  *************************************************************************/
       
    43 
       
    44 /*************************************************************************
       
    45  *
       
    46  * CDataConsumerMemory - Construction
       
    47  *
       
    48  *************************************************************************/
       
    49 CDataConsumerMemory *CDataConsumerMemory::NewL()
       
    50 	{
       
    51 	CDataConsumerMemory *self = new (ELeave) CDataConsumerMemory();
       
    52 
       
    53 	CleanupStack::PushL(self);
       
    54 
       
    55 	self->ConstructL();
       
    56 
       
    57 	CleanupStack::Pop();
       
    58 
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 CDataConsumerMemory::CDataConsumerMemory() : CBase(),
       
    63 	iDataBuffer( NULL )
       
    64 	{
       
    65 	;
       
    66 	}
       
    67 
       
    68 void CDataConsumerMemory::ConstructL( void ) 
       
    69 	{
       
    70 	iLock.CreateLocal( EOwnerProcess );
       
    71 	}
       
    72 
       
    73 CDataConsumerMemory::~CDataConsumerMemory( )
       
    74 	{
       
    75 	iLock.Wait( );
       
    76 		{
       
    77 		delete iDataBuffer;
       
    78 		iDataBuffer = NULL;
       
    79 
       
    80 		iLock.Close( );
       
    81 		}
       
    82 	}
       
    83 
       
    84 /*************************************************************************
       
    85  *
       
    86  * CDataConsumerMemory - Public interface
       
    87  *
       
    88  *************************************************************************/
       
    89 
       
    90 /*************************************************************************
       
    91  *
       
    92  * Delete
       
    93  *
       
    94  *************************************************************************/
       
    95 
       
    96 void CDataConsumerMemory::Delete( void ) 
       
    97 	{
       
    98 	delete this;
       
    99 	}
       
   100 
       
   101 /*************************************************************************
       
   102  *
       
   103  * GetTotalSize
       
   104  *
       
   105  *************************************************************************/
       
   106 
       
   107 TInt CDataConsumerMemory::GetTotalSize( TInt &aTotalSize )
       
   108 	{
       
   109 	TInt	err = KErrNone;
       
   110 	TInt	size = 0;
       
   111 
       
   112 	iLock.Wait( );
       
   113 		{
       
   114 		size = iDataBuffer->Length( );
       
   115 
       
   116 		aTotalSize = size;
       
   117 
       
   118 		iLock.Signal( );
       
   119 		}
       
   120 
       
   121 	return ( err );
       
   122 	}
       
   123 
       
   124 /*************************************************************************
       
   125  *
       
   126  * AddData
       
   127  *
       
   128  *************************************************************************/
       
   129 
       
   130 TInt CDataConsumerMemory::AddData( const TDesC8 &aSource )
       
   131 	{
       
   132 	TInt	err = KErrNone;
       
   133 
       
   134 	iLock.Wait( );
       
   135 		{
       
   136 		TInt currentLength = 0;
       
   137 
       
   138 		if(iDataBuffer)
       
   139 			{
       
   140 			currentLength = iDataBuffer->Length( );
       
   141 			}
       
   142 
       
   143 		HBufC8 *currentDataBuffer = iDataBuffer;
       
   144 
       
   145 		TInt srcLength = aSource.Length( );
       
   146 		TInt length = currentLength + srcLength;
       
   147 
       
   148 		iDataBuffer = HBufC8::New( length );
       
   149 
       
   150 		if(iDataBuffer)
       
   151 			{
       
   152 				TPtr8	destPtr( iDataBuffer->Des( ) );
       
   153 
       
   154 				if(currentDataBuffer)
       
   155 					{
       
   156 					destPtr.Copy( currentDataBuffer->Ptr( ),
       
   157 										currentLength );
       
   158 					delete currentDataBuffer;
       
   159 					currentDataBuffer = NULL;
       
   160 					}
       
   161 
       
   162 				destPtr.Append( aSource.Ptr( ), srcLength );
       
   163 			}
       
   164 		else
       
   165 			{
       
   166 			err = KErrNoMemory;
       
   167 			}
       
   168 
       
   169 		iLock.Signal( );
       
   170 		}
       
   171 
       
   172 #ifdef _DEBUG
       
   173 	{
       
   174 	TInt totalSize = 0;
       
   175 	GetTotalSize( totalSize );
       
   176 	}
       
   177 #endif
       
   178 
       
   179 	return ( err );
       
   180 	}
       
   181 
       
   182 /*************************************************************************
       
   183  *
       
   184  * GetData
       
   185  *
       
   186  *************************************************************************/
       
   187 
       
   188 TInt CDataConsumerMemory::GetData( HBufC8 &aDestination )
       
   189 	{
       
   190 	TInt	err = KErrNone;
       
   191 
       
   192 	iLock.Wait( );
       
   193 		{
       
   194 		TInt currentLength = 0;
       
   195 
       
   196 		if(iDataBuffer)
       
   197 			{
       
   198 			currentLength = iDataBuffer->Length( );
       
   199 
       
   200 			TPtr8 destPtr( aDestination.Des( ) );
       
   201 			destPtr.Copy( iDataBuffer->Ptr( ), currentLength );
       
   202 			}
       
   203 
       
   204 		iLock.Signal( );
       
   205 		}
       
   206 
       
   207 	return ( err );
       
   208 	}
       
   209 
       
   210 /*************************************************************************
       
   211  *
       
   212  * SaveData
       
   213  *
       
   214  *************************************************************************/
       
   215 
       
   216 TInt CDataConsumerMemory::SaveData( const TDesC &filePath )
       
   217 	{
       
   218 	TInt	err = KErrNone;
       
   219 	RFs		FsSession;
       
   220 
       
   221 	err = FsSession.Connect( );
       
   222 
       
   223 	if( err == KErrNone )
       
   224 		{
       
   225 		iLock.Wait( );
       
   226 			{
       
   227 			RFile	file;
       
   228 			TInt	shareMode = EFileWrite | EFileStream | EFileShareExclusive;
       
   229 
       
   230 			err = file.Open( FsSession, filePath, shareMode );
       
   231 
       
   232 			if( err == KErrNotFound )
       
   233     			{
       
   234     			err = file.Create( FsSession, filePath, shareMode );
       
   235     			}
       
   236 
       
   237 			if( err == KErrNone )
       
   238 				{
       
   239 				
       
   240 				if( NULL != iDataBuffer )
       
   241 					{
       
   242 					err = file.Write( iDataBuffer->Des( ) );
       
   243 					}
       
   244 
       
   245 				file.Flush( );
       
   246 				file.Close( );
       
   247 				}
       
   248 
       
   249 			iLock.Signal( );
       
   250 			}
       
   251 
       
   252 		FsSession.Close( );
       
   253 		}
       
   254 
       
   255 	return ( err );
       
   256 	}
       
   257 
       
   258 /*************************************************************************
       
   259  *
       
   260  * operator const TDesC8&
       
   261  *
       
   262  *************************************************************************/
       
   263 
       
   264 CDataConsumerMemory::operator const TDesC8&( void ) const
       
   265 	{
       
   266 	return ( *iDataBuffer );
       
   267 	}