⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 headercollection.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/**************************************************************************/
/*  HeaderCollection.cpp                                                  */
/*                                                                        */
/*  This file contains routines hold OBEX headers                         */
/*                                                                        */
/*  Functions included:                                                   */
/*                                                                        */
/*  Other related files:                                                  */
/*                                                                        */
/*                                                                        */
/**************************************************************************/
#include "common.h"
#include "HeaderEnum.h"
#include "HeaderCollection.h"
#include "obexpacketinfo.h"



/*****************************************************************************/
/*   CHeaderCollection::CHeaderCollection()                                  */
/*   construct a header collection                                           */  
/*****************************************************************************/
CHeaderCollection::CHeaderCollection() : pHeaders(0), _refCount(1)
{ 
    DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::CHeaderCollection\n"));
}

/*****************************************************************************/
/*   CHeaderCollection::!CHeaderCollection()                                 */
/*   destruct a header collection                                            */ 
/*      begin by looping through linked list of nodes, deleting each         */
/*      with proper routine                                                  */ 
/*****************************************************************************/
CHeaderCollection::~CHeaderCollection()
{
    DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::~CHeaderCollection\n"));
   
    while(pHeaders)
    {
        CHeaderNode *pTemp = pHeaders;

        //clean up memory in the header collection
        if((pTemp->pItem->bId & OBEX_TYPE_MASK) == OBEX_TYPE_BYTESEQ && pTemp->pItem->value.ba.pbaData)
            delete [] pTemp->pItem->value.ba.pbaData;     
        else if((pTemp->pItem->bId & OBEX_TYPE_MASK) == OBEX_TYPE_UNICODE && pTemp->pItem->value.pszData) {
            PREFAST_SUPPRESS(307, "this is a BSTR must use SysFreeString");
            SysFreeString((BSTR)pTemp->pItem->value.pszData);
        }

        if(pTemp->pItem)
            delete pTemp->pItem;

        pHeaders = pHeaders->pNext;
        delete pTemp;
    }

    pHeaders = 0;
}


/*****************************************************************************/
/*   CHeaderCollection::InsertHeaderEnd()                                    */
/*   Insert a header at the end of the linked list this will force           */
/*   the item to be the FIRST header sent (Required for connectionID)        */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE 
CHeaderCollection::InsertHeaderEnd(OBEX_HEADER *pHeader)
{
    DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::InsertHeaderEnd\n"));


	//
	//  Check to make sure this header isnt already here
	//
	CHeaderNode *pTemp = pHeaders;
	while(pTemp)
	{
		if(pTemp->pItem->bId == pHeader->bId)
		{
			DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::InsertHeaderEnd -- trying to put in multiple headers of same type!"));
			SVSUTIL_ASSERT(FALSE);				
			return E_FAIL;
		}
		pTemp = pTemp->pNext;
	}
    
    CHeaderNode *pNewNode = new CHeaderNode();
    
    if(NULL == pNewNode)
        return E_OUTOFMEMORY;        
   
    pNewNode->pItem = pHeader;
    pNewNode->pNext = NULL;

    if(!pHeaders)
        pHeaders = pNewNode;
    else
    {
        //now find the next to last header in the list
        pTemp = pHeaders;
        while(pTemp->pNext)
            pTemp = pTemp->pNext;

        pTemp->pNext = pNewNode;
    }   
    return S_OK;
}


/*****************************************************************************/
/*   CHeaderCollection::InsertHeader()                                       */
/*   Insert a header at the beginning of the linked list this will force     */
/*   the item to be the LAST header sent (well.. if you add another it will  */
/*   be last                                                                 */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE 
CHeaderCollection::InsertHeader(OBEX_HEADER *pHeader)
{
	//
	//  Check to make sure this header isnt already here
	//
	CHeaderNode *pTemp = pHeaders;
	while(pTemp)
	{
		if(pTemp->pItem->bId == pHeader->bId)
		{
			DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::InsertHeader -- trying to put in multiple headers of same type!"));
			SVSUTIL_ASSERT(FALSE);					
			return E_FAIL;
		}
		pTemp = pTemp->pNext;
	}
	
    DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::InsertHeader\n"));
    CHeaderNode *pNewNode = new CHeaderNode();   
    
    if(NULL == pNewNode)
        return E_OUTOFMEMORY;
    
    pNewNode->pItem = pHeader;
    pNewNode->pNext = pHeaders;
    pHeaders = pNewNode;    
    return S_OK;
}

 
/*****************************************************************************/
/*   CHeaderCollection::AddByteArray()                                       */
/*   Add a byte array to the linked list                                     */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE 
CHeaderCollection::AddByteArray (byte Id, unsigned long ulSize, byte *pData)
{
    DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::AddByteArray\n"));
    
    if(ulSize >= OBEX_MAX_PACKET_SIZE) 
        return E_INVALIDARG;           

    OBEX_HEADER *pOBHeader = new OBEX_HEADER;
    
    if(NULL == pOBHeader)
        return E_OUTOFMEMORY;
            
    pOBHeader->bId = Id;
    pOBHeader->value.ba.pbaData = (ulSize?new BYTE[ulSize]:0);
    
    if(ulSize && NULL == pOBHeader->value.ba.pbaData)
        return E_OUTOFMEMORY;
        
    if(ulSize)  
        memcpy(pOBHeader->value.ba.pbaData, pData, ulSize);
        
    pOBHeader->value.ba.dwSize = ulSize;
    return InsertHeader(pOBHeader);
}


/*****************************************************************************/
/*   CHeaderCollection::AddLong()                                            */
/*   Add a long to the linked list                                           */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE 
CHeaderCollection::AddLong (byte Id, unsigned long ulData)
{
    DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::AddLong\n"));
    OBEX_HEADER *pOBHeader = new OBEX_HEADER;
    
    if(NULL == pOBHeader)
        return E_OUTOFMEMORY;
        
    pOBHeader->bId = Id;
    pOBHeader->value.dwData = ulData;
    return InsertHeader(pOBHeader);    
}

HRESULT STDMETHODCALLTYPE 
CHeaderCollection::AddByte (byte Id, byte pData)
{
    DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::AddByte\n"));
    OBEX_HEADER *pOBHeader = new OBEX_HEADER;
    
    if(NULL == pOBHeader)
        return E_OUTOFMEMORY;
        
    pOBHeader->bId = Id;
    pOBHeader->value.bData = pData;
    return InsertHeader(pOBHeader);       
}

HRESULT STDMETHODCALLTYPE 
CHeaderCollection::AddUnicodeString (byte Id, LPCWSTR pszData)
{
    DEBUGMSG(OBEX_HEADERCOLLECTION_ZONE,(L"CHeaderCollection::AddUnicodeString\n"));
    OBEX_HEADER *pOBHeader = new OBEX_HEADER;
    
    if(NULL == pOBHeader)
        return E_OUTOFMEMORY;
        
    pOBHeader->bId = Id;
    pOBHeader->value.pszData = SysAllocString(pszData);    
    return InsertHeader(pOBHeader);    
}

HRESULT STDMETHODCALLTYPE 
CHeaderCollection::Remove (byte Id)
{
	CHeaderNode *pTempNode = pHeaders;
	CHeaderNode *pPrevNode = NULL;

	DEBUGMSG(OBEX_OBEXSTREAM_ZONE,(L"CHeaderCollection::Remove() -- op: %x\n", Id));


	while(pTempNode) 
	{
		PREFAST_ASSERT(pTempNode->pItem);

		//if we have found the Id delete the node
		if(pTempNode->pItem->bId == Id)
		{
			//if this is the head of the list
			if(!pPrevNode)
			{
				pHeaders=pHeaders->pNext;
			}
			else
			{
				pPrevNode->pNext = pTempNode->pNext;
			}

			//clean up memory in the header collection
	        if((pTempNode->pItem->bId & OBEX_TYPE_MASK) == OBEX_TYPE_BYTESEQ)
	            delete [] pTempNode->pItem->value.ba.pbaData;     
	        else if((pTempNode->pItem->bId & OBEX_TYPE_MASK) == OBEX_TYPE_UNICODE) {
	            PREFAST_SUPPRESS(307, "This is alloc'ed with SysAllocString");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -