os/persistentdata/traceservices/tracefw/ulogger/src/pluginframework/inputdata.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-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 //
    15 
    16 
    17 #include "inputdata.h"
    18 
    19 #if defined(__LIGHTLOGGER_ENABLED)
    20 #include "lightlogger.h" 
    21 #endif
    22 
    23 /**Default constructor.
    24 */
    25 EXPORT_C CInputData::CInputData(unsigned int aMinPackageSize)
    26 {
    27 	iMinPackageSize = aMinPackageSize;
    28 }
    29 
    30 /**Destructor.
    31 */
    32 EXPORT_C CInputData::~CInputData()
    33 {
    34 }
    35 
    36 /**Function creates new control data chunk (with EOL).
    37 
    38 @param aCommand A pointer to command literal.
    39 @param aCommandSize Size of data poitned by aCommand.
    40 @return Pointer to ControlData type is returned when chunk was successfully created, otherwise NULL.
    41 */
    42 EXPORT_C ControlData* CInputData::CreatePackage(void* aCommand, signed long aCommandSize)
    43 {
    44 	unsigned long trueSize = aCommandSize+SIZE_OF_EOL; 
    45 	unsigned long size = trueSize;
    46 	if(size<iMinPackageSize)
    47 		size = iMinPackageSize;
    48 
    49 	//create package
    50 	ControlData *package = new ControlData[size]; //minimum package
    51 	if(!package)
    52 		return NULL;
    53 	
    54 	//add command 
    55 	memcpy((void*)package, aCommand, aCommandSize);
    56 	
    57 	//apend padding character if necessary (between last string and EOL)
    58 	long padd = 0;
    59 	if(trueSize < iMinPackageSize)
    60 	{
    61 	padd = iMinPackageSize-trueSize;
    62 		for(int i=0; i<padd; i++) //skip separator
    63 			memcpy((void*)(package+aCommandSize+i), (void*)&DATA_SEPARATOR, 1);
    64 	}
    65 	
    66 	//add EOL
    67 	memcpy((void*)(package+aCommandSize+padd), (void*)&CD_EOL, SIZE_OF_EOL);
    68 	
    69 	return package;
    70 }
    71 
    72 //only for internal use
    73 unsigned long CInputData::FindAvailablePlace(const ControlData *aDataPtr)
    74 {
    75 	unsigned long currentSize = this->GetSize(aDataPtr);
    76 	unsigned long endPtr = 0;
    77 	
    78 	while(true)
    79 	{
    80 		if((*(aDataPtr+endPtr))==CD_CR)
    81 			break;
    82 		
    83 		if((*(aDataPtr+endPtr)) == DATA_SEPARATOR)
    84 			if(((*(aDataPtr+endPtr+1)) == CD_CR) || ((*(aDataPtr+endPtr+1)) == DATA_SEPARATOR))
    85 				break; //offset
    86 		++endPtr;
    87 	}
    88 	
    89 	return endPtr+1;
    90 }
    91 
    92 //only for internal use
    93 unsigned long CInputData::CalcNumberOfPaddingChars(const ControlData *aDataPtr)
    94 {
    95 	unsigned long padd = 0;
    96 	int i=1;
    97 	
    98 	while(true)
    99 	{
   100 		if((*(aDataPtr+i))==DATA_SEPARATOR)
   101 			if((*(aDataPtr+i+1))==DATA_SEPARATOR)
   102 				++padd;
   103 		if((*(aDataPtr+i))==CD_CR)
   104 			break;
   105 		++i;
   106 	}
   107 	
   108 	return padd;
   109 }
   110 
   111 /**Function returns size of control data chunk given as an argument.
   112 aDataPtr must be ended with EOL (end if line) sequence.
   113 
   114 @param aDataPtr A poiter to existing control data.
   115 @return Unsigned long number describing current size of control data.
   116 */
   117 EXPORT_C unsigned long CInputData::GetSize(const ControlData *aDataPtr)
   118 {
   119 	long i=0;
   120 	while(true)
   121 		{
   122 		if((*(aDataPtr+i)) == CD_CR)
   123 			if((*(aDataPtr+i+1)) == CD_LF)
   124 				return i+SIZE_OF_EOL;
   125 		++i;
   126 		}
   127 }
   128 
   129 
   130 /**Function returns number of chunks in current control data package.
   131  
   132 @param aDataPtr A poiter to existing control data chunk.
   133 @return Number of chunks in current control data.
   134 */
   135 EXPORT_C unsigned long CInputData::GetChunksCount(const ControlData *aDataPtr)
   136 {
   137 	unsigned long counter = 0;
   138 	unsigned long size = this->GetSize(aDataPtr);
   139 	unsigned long tmp = 0;
   140 	
   141 	while(tmp < size-1)
   142 	{
   143 		if( (*(aDataPtr+tmp) != DATA_SEPARATOR) && (*(aDataPtr+tmp) != CD_CR) && (*(aDataPtr+tmp) != CD_LF) )
   144 			if( (*(aDataPtr+tmp+1) == DATA_SEPARATOR) || (*(aDataPtr+tmp+1) == CD_CR) || (*(aDataPtr+tmp+1) == CD_LF) )
   145 				++counter;
   146 		++tmp;
   147 	}	
   148 	return counter;
   149 }
   150 
   151 
   152 /**Function parses control data chunk and returns pointer to data chunk described in aChunkNumber argument and update 
   153 aChunkSize argument passed as a reference.
   154  
   155 @param aDataPtr A poiter to existing control data.
   156 @param aChunkNumber Number of desired data chunk.
   157 @param aChunkSize Reference to unsigned long variable, where size of returned chunk will be written.
   158 @return A pointer to data or NULL.
   159 */
   160 EXPORT_C const void* CInputData::GetChunk(const ControlData* aDataPtr, unsigned long aChunkNumber, unsigned long &aChunkSize)
   161 {
   162 	aChunkSize = 0;
   163 	unsigned long counter = 0;
   164 	unsigned long size = this->GetSize(aDataPtr);
   165 	unsigned long tmp = 0;
   166 	
   167 	while(tmp < size)
   168 	{
   169 		if(counter == aChunkNumber)
   170 		{
   171 			unsigned long chunkStart = tmp;
   172 			while((*(aDataPtr+tmp)!=DATA_SEPARATOR) && (*(aDataPtr+tmp)!=CD_CR))
   173 				{++tmp;}
   174 			aChunkSize = tmp-chunkStart;
   175 			const void* ret = aDataPtr+chunkStart;
   176 			return ret;
   177 		}
   178 		else if(*(aDataPtr+tmp)==DATA_SEPARATOR)
   179 				++counter;
   180 		++tmp;
   181 	}
   182 	
   183 	return NULL;
   184 }
   185 
   186 /**Functions appends data to existing control data. All operations, like updating size and appending separators, are done internally.
   187 
   188 @param aDataPtr A reference to poiter to existing control data.
   189 @param aAddData A pointer to data that should be appended to current payload.
   190 @param aAddDataSize A size of data that should be appended.
   191 @return A new size of control data chunk (size after 'append' operation).
   192 */
   193 EXPORT_C unsigned long CInputData::AppendNewData(ControlData *&aDataPtr, const void *aAddData, unsigned long aAddDataSize)
   194 {	
   195 	unsigned long currentSize = this->GetSize(aDataPtr); //with EOL
   196 	unsigned long nextPayOffest = this->FindAvailablePlace(aDataPtr);
   197 	unsigned long paddChars = this->CalcNumberOfPaddingChars(aDataPtr);
   198 	
   199 	//calculate new size
   200 	unsigned long newSize = 0;
   201 	if(paddChars >= aAddDataSize)
   202 		newSize = currentSize;	
   203 		else
   204 			newSize = currentSize+aAddDataSize+1; //1 char for separator
   205 
   206 	//reallocate 
   207 	ControlData *newPtr = new ControlData[newSize];
   208 	if(newPtr == NULL)
   209 		{
   210 		delete [] aDataPtr;
   211 		return NULL;
   212 		}
   213 
   214 	//fill with padding characters
   215 	for(int i=0; i<newSize; i++)
   216 		memcpy((void*)(newPtr+i), (void*)&DATA_SEPARATOR, 1);
   217 	
   218 	//copy old content to new location
   219 	memcpy((void*)newPtr, (void*)aDataPtr, currentSize-SIZE_OF_EOL); //except EOL
   220 	delete [] aDataPtr;
   221 	
   222 	//add new Data
   223 	memcpy((void*)(newPtr+nextPayOffest), (void*)aAddData, aAddDataSize);
   224 	
   225 	//add EOL (end of package indicator)
   226 	memcpy((void*)(newPtr+newSize-SIZE_OF_EOL), (void*)&CD_EOL, SIZE_OF_EOL);
   227 
   228 	//assign new data
   229 	aDataPtr = newPtr;
   230 	
   231 	return newSize;
   232 }
   233 
   234