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

📄 stream.cpp

📁 C++ patterns设计模式
💻 CPP
字号:
#include "stdafx.h"
#include "stream.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace protocol
{
	//////////////////////////////////////////////////////////////////////////////
	// Function Name       : encFixedString
	// Description         : encode a fixed string, if the size of str less than fixedLen
	//                       fillFlag will append, default fillFlag is '\0'
	// Input Parameters    : str        -- the fixed string what will be encode
	//                       fixedLen   -- the fixed length
	//                       fillFlag   -- the fill char
	//////////////////////////////////////////////////////////////////////////////
	void BaseStream::encFixedString(const std::string& str, 
		size_t fixedLen, 
		u8 fillFlag)
	{
		if(str.size() >= fixedLen)
		{
			m_container.insert(m_container.end(), str.begin(), str.begin()+fixedLen);
		}
		else
		{
			m_container.insert(m_container.end(), str.begin(), str.end());
			// fill the empty space
			m_container.insert(m_container.end(), fixedLen-str.size(), fillFlag);
		}
	}

	//////////////////////////////////////////////////////////////////////////////
	// Function Name       : encVmaxString
	// Description         : encode variable length(with max size) string, 
	//                       take from str at most maxlen-1 byte, append endFlag at the end
	// Input Parameters    : str        -- the vmax string what will be encode
	//                       maxlen     -- max length
	//                       endFlag    -- end flag will append at the end
	//////////////////////////////////////////////////////////////////////////////
	void BaseStream::encVmaxString(const std::string& str, 
		size_t maxLen, 
		u8 endFlag)
	{
		if(str.size() >= maxLen)
		{
			m_container.insert(end(), str.begin(), str.end());
		}
		else
		{
			m_container.insert(end(), str.begin(), str.begin() + maxLen-1);
			enc8(endFlag);
		}
	}
	/**
	 *	the different between "fixed string" and "vmax string"
	 *  fixed string -- must fixed length, if the real data not present, the "fill flag" will be appended
	 *                  if the real data enough, no "fill flag" will be appended.
	 *					the decoder just take fixed length data from stream will be OK.
	 *  vmax string  -- at most "max length" minus one byte will encode, the "end flag" always be appended
	 *                  the decoder must find the "end flag" and then the decode finish
	 */

	//////////////////////////////////////////////////////////////////////////////
	// Function Name       : encString
	// Description         : encode a c-string, 
	// Input Parameters    : str        -- what will be encode
	//////////////////////////////////////////////////////////////////////////////
	void BaseStream::encString(const std::string& str)
	{
		m_container.insert(end(), str.begin(), str.end());
		m_container.push_back('\0');
	}

    //////////////////////////////////////////////////////////////////////////////
    // Function Name       : encStream
    // Description         : encode the stream, just append it at the end.
    //////////////////////////////////////////////////////////////////////////////
    void BaseStream::encStream(const BaseStream& stream)
    {
        *this += stream;
    }

	//////////////////////////////////////////////////////////////////////////////
	// Function Name       : decFixedString
	// Description         : decode fixedLen bytes into str from stream
	//                       function will remove the original data of str
	// Input Parameters    : fixedLen   -- how many bytes will be decode from the stream
	// Output Parameters   : str        -- the storage of the decoded string
	// Return Value        : bool(success/failed)
	//////////////////////////////////////////////////////////////////////////////
	bool BaseStream::decFixedString(std::string& str, size_t fixedLen)
	{
		if(left() < fixedLen)
			return false;

		//reserve enougth memory
		str.reserve(fixedLen+1);
		//the assign method of string dosn't check the null character
		//so I must check by myseft, what a inefficient method :-(
		u8 ch;
		while(fixedLen && (ch=m_container[m_offset]))
		{
			str += ch;
			--fixedLen;
			++m_offset;
		}
		//skip the left size
		m_offset += fixedLen;
		return true;
	}

	//////////////////////////////////////////////////////////////////////////////
	// Function Name       : decVmaxString
	// Description         : decode variable max length to str from stream
	//                       function will remove the original data of str
	// Input Parameters    : maxLen     -- the vmax length
	//                       endFlag    -- the ending flag, if occur this flag, 
	//                       decode will finish
	// Output Parameters   : str        -- the storage of the decoded string
	// Return Value        : bool(success/failed)
	//////////////////////////////////////////////////////////////////////////////
	bool BaseStream::decVmaxString(std::string& str, size_t maxLen, u8 endFlag)
	{
		assert(maxLen >= 0);

		u8 ch;
		str = ""; //clear the string at first
		while (left()>0 && maxLen>0 && ((ch=m_container[m_offset]) != endFlag)) 
		{
			str += ch;
			++m_offset;
			--maxLen;
		}
		if(maxLen>0 && m_container[m_offset] == endFlag)
			++m_offset;
		return true;
	}

	//////////////////////////////////////////////////////////////////////////////
	// Function Name       : decString
	// Description         : not implement yet!
	// Start Date          : 2003年4月4日
	// Change log          : 2003年4月4日 by Darkay Li -- Created
	//////////////////////////////////////////////////////////////////////////////
	bool BaseStream::decString(std::string& str)
	{
		return false;
	}

    //////////////////////////////////////////////////////////////////////////////
    // Function Name       : decStream
    // Description         : copy some buffer
    // Start Date          : 2003年6月27日
    // Change log          : 2003年6月27日 by 黎达文 -- Created
    //////////////////////////////////////////////////////////////////////////////
    bool BaseStream::decStream(BaseStream& stream, size_t length)
    {
        assert(length>=0);
        if(left() < length)
            return false;
		const char *offptr = getBuffer(m_offset);
        stream.assign(offptr, offptr+length);
        m_offset += length;
        return true;
    }
}

⌨️ 快捷键说明

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