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

📄 warfifobuffer.h

📁 ftpserver very good sample
💻 H
字号:
/** */#ifndef WAR_FIFO_BUFFER_H#define WAR_FIFO_BUFFER_H/* SYSTEM INCLUDES */#ifndef WAR_ASSERT_H_INCLUDED#   define WAR_ASSERT_H_INCLUDED#   include <assert.h>#endif/* PROJECT INCLUDES */#ifndef WAR_EXCEPTION_H#   include "WarException.h"#endif/* LOCAL INCLUDES *//* FORWARD REFERENCES */#ifdef __cplusplusextern "C" {#endif/****************** BEGIN OLD STYLE C spesific ********//****************** END OLD STYLE C spesific **********/#ifdef __cplusplus }#endif/****************** BEGIN C++ spesific ****************/#ifdef __cplusplus/** Fast FIFO buffer.     This buffer is optimized to be used in lowlevel File IO     routines that normally puts some data into a buffer and then    retrieve parts of the buffer until it is empty, - when it    again is filled up.      Data can be added to the buffer while it is non-empty,     but this will be expensive if small chunks are added    each time.     Example from bool WarFile::ReadLine(WarString& Str)    \begin{verbatim}     WarFifoBuffer<char> mBuffer;     while(true)    {        if (mBuffer.IsEmpty())        {            mBuffer.Reserve();            war_uint32_t BytesRead = Read(mBuffer.GetEndOfBuffer(), mBuffer.FreeCapacity());            mBuffer.PushData(NULL, BytesRead);             if (!BytesRead)                break;        }        // Look for a newline       war_cstr_t p = mBuffer.GetStartOfBuffer();       war_ccstr_t pp = mBuffer.GetEndOfBuffer();       while(p < pp)       {           if ((*p == '\r') && (p[1] == '\n'))            {               *p++ = 0;               goto done;            }           else if (*p == '\n')               goto done;            p++;       }    }    \end{verbatim}*/template <class T>class WarFifoBuffer {public:    /// Native datatype pointer    typedef T * cptr_t;    /// Native const datatype pointer    typedef const T * ccptr_t;    enum DefaultsE    {        EXP_DEFAULT = 1024    };    // LIFECYCLE    /** Constructor        No buffer is allocated    */    WarFifoBuffer();     /** Constructor        The value passed as InitialExpand will be the minimum        size to exand the buffer with also when it is resized.        @param InitialExpand buffer to allocate the first time             the buffer is expanded    */    WarFifoBuffer(size_t InitialExpand);    ///    ~WarFifoBuffer();    // OPERATORS    // OPERATIONS           /** Expand the buffer.                @param AddSize number of T elements to reserve space for            Use (GetLength() * sizeof(T)) to get the size in bytes    */    void Expand(size_t AddSize = 0);     /** Make sure that there is at least MinBytes in the        buffer for the next call to Push.        If MinBytes is zero, the InitialExpand argument to the        constructor is used. If MinBytes and InitialExpand is        zero, an exception is thrown.                @param MinBytes Reserved size.         @exception WarException on error    */    void Reserve(size_t MinBytes = 0);    /** Add data to the buffer.                Can be used to add data, or to notify that bufferspace is used.        @param Buffer If not NULL, data is copyed from Buffer to the            internal buffer.        @param BufferLength    */    void PushData(ccptr_t Buffer, size_t BufferLength);    /** Trash the buffer. After this, Size() will return 0.    */    void Reset();    /** Remove polled data from the start of the buffer.        Saves memory in some cases, but it is an expensive        operation.    */    void Purge();    // ACCESS     /** Get data back from the buffer        The returned buffer can be used safely until next time        Expand(), PushData() or PopData() is called, or until        the WarFifoBuffer object itself is destroyed.        @param Bufferlength Number of bytes to take from the buffer        @return buffer of BufferLength size.        @exception WarException if the remaining             buffer is less than BufferLength    */    cptr_t PopData(size_t BufferLength);    /** Get the start of the buffer        Normally where you will want to read data from    */    inline cptr_t GetStartOfBuffer() const    {        return m_Buf + mStartOfs;    }     /** Get the end of the buffer        Normally where you will want to write data to    */    inline cptr_t GetEndOfBuffer() const    {        return m_Buf + mSize;    }    void AddString(WarFifoBuffer<T>::ccptr_t Str);    // INQUIRY     /** Test if buffer is empty                @return true if the buffer is empty    */    inline bool IsEmpty() const    {        return GetLength() == 0;    }    /** Get the length of the buffer. This is the        data we can retreive from the buffer.        @return Number of of T elements in the buffer.             Use (GetLength() * sizeof(T)) to get the size in bytes    */    inline size_t GetLength() const    {        assert(mSize >= mStartOfs);        return mSize - mStartOfs;    }    /** Get the free capacity of the buffer. This is the        space we can use in the buffer before it         allocates more memory        @return Number of of T free elements in the buffer.             Use (GetLength() * sizeof(T)) to get the size in bytes    */    inline size_t FreeCapacity() const    {        return mCapacity - GetLength();    }protected:private:    size_t mStartOfs,         mSize,         mCapacity,         mInitialExpand;    cptr_t m_Buf;};/* INLINE METHODS *//* EXTERNAL REFERENCES */template <class T>WarFifoBuffer<T>& operator << (WarFifoBuffer<T>& from,                                WarFifoBuffer<T>::ccptr_t Str);#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif  /* WAR_FIFO_BUFFER_H_ */

⌨️ 快捷键说明

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