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