hti/PC_Tools/HTIGateway/HtiGateway/src/common.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 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 *     This file contains implementation file of the Data and ParameterList classes.
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 #include "common.h"
       
    20 #include "util.h"
       
    21 #include <crtdbg.h>
       
    22 
       
    23 // Maximum time to wait
       
    24 long g_MaximumShutdownWaitTime;
       
    25 
       
    26 DWORD g_ErrorCode = NO_ERRORS;
       
    27 
       
    28 //**********************************************************************************
       
    29 // Class Data
       
    30 //
       
    31 // This class provides data encapsulattion for DataGateway
       
    32 //**********************************************************************************
       
    33 
       
    34 Data::Data(const void* data_in, DWORD length, DataType type)
       
    35 {
       
    36 	////_RPT1(_CRT_WARN,"Data::Data %x\n", data_in);
       
    37 	m_pData = NULL;
       
    38 	SetData(data_in, length, 0, type);
       
    39 }
       
    40 
       
    41 
       
    42 Data::Data(const void* data_in, DWORD length, DWORD offset, DataType type)
       
    43 {
       
    44 	////_RPT1(_CRT_WARN,"Data::Data %x\n", data_in);
       
    45 	m_pData = NULL;
       
    46 	SetData(data_in, length, offset, type);
       
    47 }
       
    48 
       
    49 Data::Data(const Data& data)
       
    50 {
       
    51 	m_pData = NULL;
       
    52 	SetData(data.GetData(), data.GetLength(), 0, data.GetType());
       
    53 }
       
    54 
       
    55 Data::~Data()
       
    56 {
       
    57 	if (m_pData != NULL)
       
    58 	{
       
    59 		//_RPT1(_CRT_WARN,"~Data %x\n", m_pData);
       
    60 		free( m_pData );
       
    61 		m_pData = NULL;
       
    62 	}
       
    63 }
       
    64 
       
    65 /*
       
    66  * This method is used to free data and set data type to EEmpty
       
    67  */
       
    68 void Data::FreeData()
       
    69 {
       
    70 	if (m_pData != NULL)
       
    71 	{
       
    72 		//_RPT1(_CRT_WARN,"FreeData %x\n", m_pData);
       
    73 		free( m_pData );
       
    74 		m_pData = NULL;
       
    75 		m_Type = EEmpty;
       
    76 	}
       
    77 }
       
    78 /*
       
    79  * returns data in Data object given parameter
       
    80  */
       
    81 void* Data::GetData(void *data_out) const
       
    82 {
       
    83 	if (data_out != NULL)
       
    84 	{
       
    85 		data_out = m_pData;
       
    86 	}
       
    87 	return m_pData;
       
    88 }
       
    89 
       
    90 /*
       
    91  * returns length of data 
       
    92  */
       
    93 DWORD Data::GetLength() const
       
    94 {
       
    95 	return m_dwLength;
       
    96 }
       
    97 
       
    98 /*
       
    99  * returns type of data
       
   100  * can be one of the following: EEmpty, EControl, EError, EData
       
   101  */
       
   102 Data::DataType Data::GetType() const
       
   103 {
       
   104 	return m_Type;
       
   105 }
       
   106 
       
   107 Data& Data::operator=(const Data& data)
       
   108 {
       
   109 	if ( this != &data )
       
   110 	{
       
   111 		FreeData();
       
   112 		SetData( data.GetData(), data.GetLength(), data.GetType() );
       
   113 	}
       
   114 	return *this;
       
   115 }
       
   116 
       
   117 /*
       
   118  * This method copies up to length bytes from given data object to class member data object
       
   119  */
       
   120 bool Data::SetData(const void* data_in, DWORD length, DataType type)
       
   121 {
       
   122 	////_RPT1(_CRT_WARN,"Data::SetData %x\n", data_in);
       
   123 	return SetData(data_in, length, 0, type);
       
   124 }
       
   125 
       
   126 /*
       
   127  * This method copies up to length bytes(starting from offset) from given data object to class member data object
       
   128  */
       
   129 bool Data::SetData(const void* data_in, DWORD length, DWORD offset, DataType type)
       
   130 {
       
   131 	//_RPT1(_CRT_WARN,"Data::SetData %x\n", data_in);
       
   132 	m_Type = type;
       
   133 	if (length == 0 || data_in == NULL || offset >= length)
       
   134 	{
       
   135 		m_pData = NULL;
       
   136 		m_dwLength = 0;
       
   137 		return true;
       
   138 	}
       
   139 	m_dwLength = (length - offset);
       
   140 	//_ASSERTE(m_pData==NULL);
       
   141 	m_pData = (BYTE*)malloc( m_dwLength );
       
   142 	//_RPT2(_CRT_WARN,"Data::SetData m_pData %x <%d>\n", m_pData, m_dwLength);
       
   143 	if (m_pData == NULL)
       
   144 	{
       
   145 		Util::Error("Error allocating memory to data object");
       
   146 		m_Type = EEmpty;
       
   147 		return false;
       
   148 	}
       
   149 	memset(m_pData, 0, m_dwLength);
       
   150 	memcpy(m_pData, &reinterpret_cast<const char*>(data_in)[offset], m_dwLength);
       
   151 	return false;
       
   152 }
       
   153 
       
   154 /*
       
   155  * Used to combine different messages into one.
       
   156  * This method appends new data at the end of already excisting data.
       
   157  * If new data isn't same type as excisting data it isn't appended.
       
   158  * Returns bool value of whether the method has succeeded to append the data or not.
       
   159  */
       
   160 bool Data::AppendData(Data* aData)
       
   161 {
       
   162 	if ( m_Type == aData->m_Type )
       
   163 	{
       
   164 		//realloc memory
       
   165 		BYTE* newBlock = (BYTE*) realloc( m_pData, m_dwLength + aData->m_dwLength );
       
   166 		if ( newBlock != NULL )
       
   167 		{
       
   168 			m_pData = newBlock;
       
   169 			//copy data
       
   170 			memcpy( ((BYTE*)m_pData) + m_dwLength,
       
   171 					aData->GetData(),
       
   172 					aData->GetLength() );
       
   173 			//update length
       
   174 			m_dwLength += aData->GetLength();
       
   175 			return true;
       
   176 		}
       
   177 	}
       
   178 	return false;
       
   179 }
       
   180 
       
   181 //**********************************************************************************
       
   182 // Class ParameterList
       
   183 //
       
   184 //**********************************************************************************
       
   185 ParameterList::ParameterList()
       
   186 {
       
   187 }
       
   188 
       
   189 ParameterList::~ParameterList()
       
   190 {
       
   191 	m_Map.clear();
       
   192 }
       
   193 
       
   194 void ParameterList::Add(const string& key, const void* value)
       
   195 {
       
   196 	m_Map[key] = value;
       
   197 }
       
   198 
       
   199 const void* ParameterList::Get(const string& key)
       
   200 {
       
   201 	return m_Map[key];
       
   202 }
       
   203 
       
   204 // End of the file