www.pudn.com > WINCEOS.zip > btagutil.h


// 
// Copyright (c) Microsoft Corporation.  All rights reserved. 
// 
// 
// This source code is licensed under Microsoft Shared Source License 
// Version 1.0 for Windows CE. 
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223. 
// 
 
#ifndef __BTAGUTIL_H__ 
#define __BTAGUTIL_H__ 
 
#include  
 
#define DEFAULT_BUFFER_SIZE     64 
#define MAX_BUFFER_SIZE         4096        
 
class CBuffer { 
public: 
    CBuffer(void) 
    { 
        m_pBuffer = m_Buffer; 
        m_iSize = DEFAULT_BUFFER_SIZE; 
    } 
     
    ~CBuffer(void) 
    { 
        if (m_pBuffer != m_Buffer) { 
            delete[] m_pBuffer; 
        } 
    } 
     
    PBYTE GetBuffer(int iSize, BOOL fPreserveData = FALSE) 
    { 
        // 
        // If buffer is not big enough, we have to allocate new buffer 
        // which is big enough. 
        // 
 
        if (iSize > MAX_BUFFER_SIZE) { 
            return NULL; 
        } 
        else if (iSize > m_iSize) { 
            PBYTE pTmp = new BYTE[iSize]; 
            if (! pTmp) { 
                return NULL; 
            } 
 
            if (fPreserveData) { 
                memcpy(pTmp, m_pBuffer, m_iSize); 
            } 
         
            // Delete current buffer if it was allocated on the heap 
            if (m_pBuffer != m_Buffer) { 
                delete[] m_pBuffer; 
            } 
 
            // If alloc succeeded then update buffer and size 
            m_pBuffer = pTmp; 
            m_iSize = iSize; 
        } 
 
        return m_pBuffer; 
    } 
     
    int GetSize(void) 
    { 
        return m_iSize; 
    } 
     
private: 
    CBuffer(const CBuffer&){} // don't want to copy this object 
    BYTE m_Buffer[DEFAULT_BUFFER_SIZE]; 
    PBYTE m_pBuffer; 
    int m_iSize; 
}; 
 
 
#endif // __BTAGUTIL_H__