os/persistentdata/traceservices/tracefw/ulogger/src/pluginframework/inputdata.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/traceservices/tracefw/ulogger/src/pluginframework/inputdata.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,234 @@
     1.4 +// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +
    1.20 +#include "inputdata.h"
    1.21 +
    1.22 +#if defined(__LIGHTLOGGER_ENABLED)
    1.23 +#include "lightlogger.h" 
    1.24 +#endif
    1.25 +
    1.26 +/**Default constructor.
    1.27 +*/
    1.28 +EXPORT_C CInputData::CInputData(unsigned int aMinPackageSize)
    1.29 +{
    1.30 +	iMinPackageSize = aMinPackageSize;
    1.31 +}
    1.32 +
    1.33 +/**Destructor.
    1.34 +*/
    1.35 +EXPORT_C CInputData::~CInputData()
    1.36 +{
    1.37 +}
    1.38 +
    1.39 +/**Function creates new control data chunk (with EOL).
    1.40 +
    1.41 +@param aCommand A pointer to command literal.
    1.42 +@param aCommandSize Size of data poitned by aCommand.
    1.43 +@return Pointer to ControlData type is returned when chunk was successfully created, otherwise NULL.
    1.44 +*/
    1.45 +EXPORT_C ControlData* CInputData::CreatePackage(void* aCommand, signed long aCommandSize)
    1.46 +{
    1.47 +	unsigned long trueSize = aCommandSize+SIZE_OF_EOL; 
    1.48 +	unsigned long size = trueSize;
    1.49 +	if(size<iMinPackageSize)
    1.50 +		size = iMinPackageSize;
    1.51 +
    1.52 +	//create package
    1.53 +	ControlData *package = new ControlData[size]; //minimum package
    1.54 +	if(!package)
    1.55 +		return NULL;
    1.56 +	
    1.57 +	//add command 
    1.58 +	memcpy((void*)package, aCommand, aCommandSize);
    1.59 +	
    1.60 +	//apend padding character if necessary (between last string and EOL)
    1.61 +	long padd = 0;
    1.62 +	if(trueSize < iMinPackageSize)
    1.63 +	{
    1.64 +	padd = iMinPackageSize-trueSize;
    1.65 +		for(int i=0; i<padd; i++) //skip separator
    1.66 +			memcpy((void*)(package+aCommandSize+i), (void*)&DATA_SEPARATOR, 1);
    1.67 +	}
    1.68 +	
    1.69 +	//add EOL
    1.70 +	memcpy((void*)(package+aCommandSize+padd), (void*)&CD_EOL, SIZE_OF_EOL);
    1.71 +	
    1.72 +	return package;
    1.73 +}
    1.74 +
    1.75 +//only for internal use
    1.76 +unsigned long CInputData::FindAvailablePlace(const ControlData *aDataPtr)
    1.77 +{
    1.78 +	unsigned long currentSize = this->GetSize(aDataPtr);
    1.79 +	unsigned long endPtr = 0;
    1.80 +	
    1.81 +	while(true)
    1.82 +	{
    1.83 +		if((*(aDataPtr+endPtr))==CD_CR)
    1.84 +			break;
    1.85 +		
    1.86 +		if((*(aDataPtr+endPtr)) == DATA_SEPARATOR)
    1.87 +			if(((*(aDataPtr+endPtr+1)) == CD_CR) || ((*(aDataPtr+endPtr+1)) == DATA_SEPARATOR))
    1.88 +				break; //offset
    1.89 +		++endPtr;
    1.90 +	}
    1.91 +	
    1.92 +	return endPtr+1;
    1.93 +}
    1.94 +
    1.95 +//only for internal use
    1.96 +unsigned long CInputData::CalcNumberOfPaddingChars(const ControlData *aDataPtr)
    1.97 +{
    1.98 +	unsigned long padd = 0;
    1.99 +	int i=1;
   1.100 +	
   1.101 +	while(true)
   1.102 +	{
   1.103 +		if((*(aDataPtr+i))==DATA_SEPARATOR)
   1.104 +			if((*(aDataPtr+i+1))==DATA_SEPARATOR)
   1.105 +				++padd;
   1.106 +		if((*(aDataPtr+i))==CD_CR)
   1.107 +			break;
   1.108 +		++i;
   1.109 +	}
   1.110 +	
   1.111 +	return padd;
   1.112 +}
   1.113 +
   1.114 +/**Function returns size of control data chunk given as an argument.
   1.115 +aDataPtr must be ended with EOL (end if line) sequence.
   1.116 +
   1.117 +@param aDataPtr A poiter to existing control data.
   1.118 +@return Unsigned long number describing current size of control data.
   1.119 +*/
   1.120 +EXPORT_C unsigned long CInputData::GetSize(const ControlData *aDataPtr)
   1.121 +{
   1.122 +	long i=0;
   1.123 +	while(true)
   1.124 +		{
   1.125 +		if((*(aDataPtr+i)) == CD_CR)
   1.126 +			if((*(aDataPtr+i+1)) == CD_LF)
   1.127 +				return i+SIZE_OF_EOL;
   1.128 +		++i;
   1.129 +		}
   1.130 +}
   1.131 +
   1.132 +
   1.133 +/**Function returns number of chunks in current control data package.
   1.134 + 
   1.135 +@param aDataPtr A poiter to existing control data chunk.
   1.136 +@return Number of chunks in current control data.
   1.137 +*/
   1.138 +EXPORT_C unsigned long CInputData::GetChunksCount(const ControlData *aDataPtr)
   1.139 +{
   1.140 +	unsigned long counter = 0;
   1.141 +	unsigned long size = this->GetSize(aDataPtr);
   1.142 +	unsigned long tmp = 0;
   1.143 +	
   1.144 +	while(tmp < size-1)
   1.145 +	{
   1.146 +		if( (*(aDataPtr+tmp) != DATA_SEPARATOR) && (*(aDataPtr+tmp) != CD_CR) && (*(aDataPtr+tmp) != CD_LF) )
   1.147 +			if( (*(aDataPtr+tmp+1) == DATA_SEPARATOR) || (*(aDataPtr+tmp+1) == CD_CR) || (*(aDataPtr+tmp+1) == CD_LF) )
   1.148 +				++counter;
   1.149 +		++tmp;
   1.150 +	}	
   1.151 +	return counter;
   1.152 +}
   1.153 +
   1.154 +
   1.155 +/**Function parses control data chunk and returns pointer to data chunk described in aChunkNumber argument and update 
   1.156 +aChunkSize argument passed as a reference.
   1.157 + 
   1.158 +@param aDataPtr A poiter to existing control data.
   1.159 +@param aChunkNumber Number of desired data chunk.
   1.160 +@param aChunkSize Reference to unsigned long variable, where size of returned chunk will be written.
   1.161 +@return A pointer to data or NULL.
   1.162 +*/
   1.163 +EXPORT_C const void* CInputData::GetChunk(const ControlData* aDataPtr, unsigned long aChunkNumber, unsigned long &aChunkSize)
   1.164 +{
   1.165 +	aChunkSize = 0;
   1.166 +	unsigned long counter = 0;
   1.167 +	unsigned long size = this->GetSize(aDataPtr);
   1.168 +	unsigned long tmp = 0;
   1.169 +	
   1.170 +	while(tmp < size)
   1.171 +	{
   1.172 +		if(counter == aChunkNumber)
   1.173 +		{
   1.174 +			unsigned long chunkStart = tmp;
   1.175 +			while((*(aDataPtr+tmp)!=DATA_SEPARATOR) && (*(aDataPtr+tmp)!=CD_CR))
   1.176 +				{++tmp;}
   1.177 +			aChunkSize = tmp-chunkStart;
   1.178 +			const void* ret = aDataPtr+chunkStart;
   1.179 +			return ret;
   1.180 +		}
   1.181 +		else if(*(aDataPtr+tmp)==DATA_SEPARATOR)
   1.182 +				++counter;
   1.183 +		++tmp;
   1.184 +	}
   1.185 +	
   1.186 +	return NULL;
   1.187 +}
   1.188 +
   1.189 +/**Functions appends data to existing control data. All operations, like updating size and appending separators, are done internally.
   1.190 +
   1.191 +@param aDataPtr A reference to poiter to existing control data.
   1.192 +@param aAddData A pointer to data that should be appended to current payload.
   1.193 +@param aAddDataSize A size of data that should be appended.
   1.194 +@return A new size of control data chunk (size after 'append' operation).
   1.195 +*/
   1.196 +EXPORT_C unsigned long CInputData::AppendNewData(ControlData *&aDataPtr, const void *aAddData, unsigned long aAddDataSize)
   1.197 +{	
   1.198 +	unsigned long currentSize = this->GetSize(aDataPtr); //with EOL
   1.199 +	unsigned long nextPayOffest = this->FindAvailablePlace(aDataPtr);
   1.200 +	unsigned long paddChars = this->CalcNumberOfPaddingChars(aDataPtr);
   1.201 +	
   1.202 +	//calculate new size
   1.203 +	unsigned long newSize = 0;
   1.204 +	if(paddChars >= aAddDataSize)
   1.205 +		newSize = currentSize;	
   1.206 +		else
   1.207 +			newSize = currentSize+aAddDataSize+1; //1 char for separator
   1.208 +
   1.209 +	//reallocate 
   1.210 +	ControlData *newPtr = new ControlData[newSize];
   1.211 +	if(newPtr == NULL)
   1.212 +		{
   1.213 +		delete [] aDataPtr;
   1.214 +		return NULL;
   1.215 +		}
   1.216 +
   1.217 +	//fill with padding characters
   1.218 +	for(int i=0; i<newSize; i++)
   1.219 +		memcpy((void*)(newPtr+i), (void*)&DATA_SEPARATOR, 1);
   1.220 +	
   1.221 +	//copy old content to new location
   1.222 +	memcpy((void*)newPtr, (void*)aDataPtr, currentSize-SIZE_OF_EOL); //except EOL
   1.223 +	delete [] aDataPtr;
   1.224 +	
   1.225 +	//add new Data
   1.226 +	memcpy((void*)(newPtr+nextPayOffest), (void*)aAddData, aAddDataSize);
   1.227 +	
   1.228 +	//add EOL (end of package indicator)
   1.229 +	memcpy((void*)(newPtr+newSize-SIZE_OF_EOL), (void*)&CD_EOL, SIZE_OF_EOL);
   1.230 +
   1.231 +	//assign new data
   1.232 +	aDataPtr = newPtr;
   1.233 +	
   1.234 +	return newSize;
   1.235 +}
   1.236 +
   1.237 +