os/persistentdata/traceservices/tracefw/ulogger/src/pluginframework/inputdata.cpp
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #include "inputdata.h"
19 #if defined(__LIGHTLOGGER_ENABLED)
20 #include "lightlogger.h"
23 /**Default constructor.
25 EXPORT_C CInputData::CInputData(unsigned int aMinPackageSize)
27 iMinPackageSize = aMinPackageSize;
32 EXPORT_C CInputData::~CInputData()
36 /**Function creates new control data chunk (with EOL).
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.
42 EXPORT_C ControlData* CInputData::CreatePackage(void* aCommand, signed long aCommandSize)
44 unsigned long trueSize = aCommandSize+SIZE_OF_EOL;
45 unsigned long size = trueSize;
46 if(size<iMinPackageSize)
47 size = iMinPackageSize;
50 ControlData *package = new ControlData[size]; //minimum package
55 memcpy((void*)package, aCommand, aCommandSize);
57 //apend padding character if necessary (between last string and EOL)
59 if(trueSize < iMinPackageSize)
61 padd = iMinPackageSize-trueSize;
62 for(int i=0; i<padd; i++) //skip separator
63 memcpy((void*)(package+aCommandSize+i), (void*)&DATA_SEPARATOR, 1);
67 memcpy((void*)(package+aCommandSize+padd), (void*)&CD_EOL, SIZE_OF_EOL);
72 //only for internal use
73 unsigned long CInputData::FindAvailablePlace(const ControlData *aDataPtr)
75 unsigned long currentSize = this->GetSize(aDataPtr);
76 unsigned long endPtr = 0;
80 if((*(aDataPtr+endPtr))==CD_CR)
83 if((*(aDataPtr+endPtr)) == DATA_SEPARATOR)
84 if(((*(aDataPtr+endPtr+1)) == CD_CR) || ((*(aDataPtr+endPtr+1)) == DATA_SEPARATOR))
92 //only for internal use
93 unsigned long CInputData::CalcNumberOfPaddingChars(const ControlData *aDataPtr)
95 unsigned long padd = 0;
100 if((*(aDataPtr+i))==DATA_SEPARATOR)
101 if((*(aDataPtr+i+1))==DATA_SEPARATOR)
103 if((*(aDataPtr+i))==CD_CR)
111 /**Function returns size of control data chunk given as an argument.
112 aDataPtr must be ended with EOL (end if line) sequence.
114 @param aDataPtr A poiter to existing control data.
115 @return Unsigned long number describing current size of control data.
117 EXPORT_C unsigned long CInputData::GetSize(const ControlData *aDataPtr)
122 if((*(aDataPtr+i)) == CD_CR)
123 if((*(aDataPtr+i+1)) == CD_LF)
124 return i+SIZE_OF_EOL;
130 /**Function returns number of chunks in current control data package.
132 @param aDataPtr A poiter to existing control data chunk.
133 @return Number of chunks in current control data.
135 EXPORT_C unsigned long CInputData::GetChunksCount(const ControlData *aDataPtr)
137 unsigned long counter = 0;
138 unsigned long size = this->GetSize(aDataPtr);
139 unsigned long tmp = 0;
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) )
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.
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.
160 EXPORT_C const void* CInputData::GetChunk(const ControlData* aDataPtr, unsigned long aChunkNumber, unsigned long &aChunkSize)
163 unsigned long counter = 0;
164 unsigned long size = this->GetSize(aDataPtr);
165 unsigned long tmp = 0;
169 if(counter == aChunkNumber)
171 unsigned long chunkStart = tmp;
172 while((*(aDataPtr+tmp)!=DATA_SEPARATOR) && (*(aDataPtr+tmp)!=CD_CR))
174 aChunkSize = tmp-chunkStart;
175 const void* ret = aDataPtr+chunkStart;
178 else if(*(aDataPtr+tmp)==DATA_SEPARATOR)
186 /**Functions appends data to existing control data. All operations, like updating size and appending separators, are done internally.
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).
193 EXPORT_C unsigned long CInputData::AppendNewData(ControlData *&aDataPtr, const void *aAddData, unsigned long aAddDataSize)
195 unsigned long currentSize = this->GetSize(aDataPtr); //with EOL
196 unsigned long nextPayOffest = this->FindAvailablePlace(aDataPtr);
197 unsigned long paddChars = this->CalcNumberOfPaddingChars(aDataPtr);
200 unsigned long newSize = 0;
201 if(paddChars >= aAddDataSize)
202 newSize = currentSize;
204 newSize = currentSize+aAddDataSize+1; //1 char for separator
207 ControlData *newPtr = new ControlData[newSize];
214 //fill with padding characters
215 for(int i=0; i<newSize; i++)
216 memcpy((void*)(newPtr+i), (void*)&DATA_SEPARATOR, 1);
218 //copy old content to new location
219 memcpy((void*)newPtr, (void*)aDataPtr, currentSize-SIZE_OF_EOL); //except EOL
223 memcpy((void*)(newPtr+nextPayOffest), (void*)aAddData, aAddDataSize);
225 //add EOL (end of package indicator)
226 memcpy((void*)(newPtr+newSize-SIZE_OF_EOL), (void*)&CD_EOL, SIZE_OF_EOL);