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

📄 warfifobuffer.cpp

📁 ftpserver very good sample
💻 CPP
字号:
#include "StdAfx.h"#include "WarFifoBuffer.h"   // class implemented/////////////////////////////// PUBLIC ///////////////////////////////////////WAR_CPPT_IMPL WarFifoBuffer<char>;WAR_CPPTF_IMPL WarFifoBuffer<char>& operator << (WarFifoBuffer<char>& from,                                                  WarFifoBuffer<char>::ccptr_t Str);template <class T>WarFifoBuffer<T>& operator << (WarFifoBuffer<T>& from,                                WarFifoBuffer<T>::ccptr_t Str){    from.AddString(Str);    return from;}//============================= LIFECYCLE ====================================template <class T>WarFifoBuffer<T>::WarFifoBuffer<T>(): mStartOfs(0),mSize(0),mCapacity(0),mInitialExpand(EXP_DEFAULT),m_Buf(NULL){}template <class T>WarFifoBuffer<T>::WarFifoBuffer<T>(size_t InitialExpand): mStartOfs(0),mSize(0),mCapacity(0),mInitialExpand(InitialExpand),m_Buf(NULL){}template <class T>WarFifoBuffer<T>::~WarFifoBuffer<T>(){    if (m_Buf)        ::free(m_Buf);}//============================= OPERATORS ====================================//============================= OPERATIONS ===================================template <class T>void WarFifoBuffer<T>::Expand(size_t AddSize){    size_t NewLen = AddSize + mCapacity;    NewLen = WarMax(mInitialExpand, NewLen);        if (m_Buf)        m_Buf = (WarFifoBuffer<T>::cptr_t) ::realloc(m_Buf, NewLen * sizeof(T));    else        m_Buf = (WarFifoBuffer<T>::cptr_t) ::malloc(NewLen * sizeof(T));        if (m_Buf == NULL)        WarThrow(WarError(WAR_ERR_OUT_OF_MEMORY), NULL);        mCapacity = NewLen;}template <class T>void WarFifoBuffer<T>::Reserve(size_t MinBytes){    if (!MinBytes)        MinBytes = mInitialExpand;        if (!MinBytes)        WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL);        if (FreeCapacity() < MinBytes)        Expand(MinBytes);}template <class T>void WarFifoBuffer<T>::PushData(WarFifoBuffer<T>::ccptr_t Buffer,                                 size_t BufferLength){    if ((Buffer == NULL) || (BufferLength == 0))    {        // Notification        if (FreeCapacity() < BufferLength)            WarThrow(WarError(WAR_ERR_BUFFER_OVERFLOW), NULL);    }    else    {        if (FreeCapacity() < BufferLength)        {            Expand(BufferLength - FreeCapacity());        }        memcpy(GetEndOfBuffer(), Buffer, BufferLength * sizeof(T));    }    mSize += BufferLength;}template <class T>void WarFifoBuffer<T>::Reset(){    mStartOfs = mSize = 0;}template <class T>void WarFifoBuffer<T>::Purge(){    if (!IsEmpty())    {        memmove(m_Buf, GetStartOfBuffer(), GetLength() * sizeof(T));        mSize -= mStartOfs;        mStartOfs = 0;    }    assert(mStartOfs == 0);}template <class T>WarFifoBuffer<T>::cptr_t WarFifoBuffer<T>::PopData(size_t BufferLength){    if (!m_Buf)        WarThrow(WarError(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL);        if (BufferLength > GetLength())        WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL);        WarFifoBuffer<T>::cptr_t pRval = GetStartOfBuffer();    mStartOfs += BufferLength;        if (IsEmpty())        Reset();        return pRval;}template <class T>void WarFifoBuffer<T>::AddString(WarFifoBuffer<T>::ccptr_t Str){    if (Str && *Str)    {        size_t _len = 0;        WarFifoBuffer<T>::ccptr_t p = Str;        while(*p++)            _len++;        PushData(Str, _len);    } }//============================= CALLBACK   ===================================//============================= ACCESS     ===================================//============================= INQUIRY    ===================================/////////////////////////////// PROTECTED  ////////////////////////////////////////////////////////////////// PRIVATE    ///////////////////////////////////

⌨️ 快捷键说明

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