📄 bufferreadertemplate.h
字号:
//// Copyright (c) 2003 by Istv醤 V醨adi//// This file is part of dxr3Player, a DVD player written specifically // for the DXR3 (aka Hollywood+) decoder card.// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#ifndef DXR3PLAYER_BUFFERREADER_H#define DXR3PLAYER_BUFFERREADER_H//------------------------------------------------------------------------------#include <cstdlib>#include <cstring>#include <cassert>//------------------------------------------------------------------------------/** * Buffer reader template. */template <class BufferT>class BufferReaderTemplate : public BufferT{private: /** * The current offset. */ size_t offset;public: /** * Construct the reader. */ BufferReaderTemplate(); /** * Indicate if data is available. */ bool available() const; /** * Get the offset. */ size_t getOffset() const; /** * Set the offset. */ void setOffset(size_t o); /** * Read a byte from the buffer. */ unsigned char read(); /** * Read a number of bytes from the buffer. */ void read(void* dest, size_t length); /** * Skip the given number of bytes. */ void skip(size_t length); /** * Read a 16-bit value from the buffer. */ unsigned read16(); /** * Import 'getLength' from the buffer class. */ BufferT::getLength; /** * Import 'getData' from the buffer class. */ BufferT::getData;};//------------------------------------------------------------------------------/** * Reader for buffers that can also read individual bits or sequences * of bits. */template <class BufferT>class BufferBitReaderTemplate : public BufferReaderTemplate<BufferT>{private: /** * The current byte for bit-level reading. */ unsigned char currentByte; /** * The number of bits remaining of the current byte. */ unsigned char bitsLeft;public: /** * Construct a bitreader. */ BufferBitReaderTemplate(); /** * Read a byte from the buffer. */ unsigned char read(); /** * Read a number of bytes from the buffer. */ void read(void* dest, size_t length); /** * Read a 16-bit value from the buffer. */ unsigned read16(); /** * Read the given number of bits. */ unsigned readBits(size_t bitsToRead); /** * Discard the current byte partially read for bits. */ void discardCurrentByte();};//------------------------------------------------------------------------------/** * Buffer reference. It contains a pointer to a buffer of a certain * type, and provides an interface for the buffer readers. */template <class BufferT>class BufferReferenceTemplate{private: /** * The pointer to the buffer. */ BufferT* buffer;public: /** * Construct a buffer reference with no pointer. */ BufferReferenceTemplate(); /** * Construct a buffer reference pointing to the given buffer. */ explicit BufferReferenceTemplate(BufferT& buffer); /** * Set the buffer. */ void setBuffer(BufferT& buf); /** * Get the buffer. */ BufferT& getBuffer() const; /** * Get the data of the buffer. */ const unsigned char* getData() const; /** * Get the length of the buffer. */ size_t getLength() const;};//------------------------------------------------------------------------------// Template definitions//------------------------------------------------------------------------------template <class BufferT>inline BufferReaderTemplate<BufferT>::BufferReaderTemplate() : offset(0){}//------------------------------------------------------------------------------template <class BufferT>inline bool BufferReaderTemplate<BufferT>::available() const{ return offset<getLength();}//------------------------------------------------------------------------------template <class BufferT>inline size_t BufferReaderTemplate<BufferT>::getOffset() const{ return offset;}//------------------------------------------------------------------------------template <class BufferT>inline void BufferReaderTemplate<BufferT>::setOffset(size_t o){ offset = o;}//------------------------------------------------------------------------------template <class BufferT>inline unsigned char BufferReaderTemplate<BufferT>::read(){ assert(offset<getLength()); return getData()[offset++];}//------------------------------------------------------------------------------template <class BufferT>inline void BufferReaderTemplate<BufferT>::read(void* dest, size_t length){ assert( (offset+length)<=getLength()); memcpy(dest, getData()+offset, length); offset += length;}//------------------------------------------------------------------------------template <class BufferT>inline void BufferReaderTemplate<BufferT>::skip(size_t length){ offset += length;}//------------------------------------------------------------------------------template <class BufferT>inline unsigned BufferReaderTemplate<BufferT>::read16(){ unsigned x = read(); x <<= 8; x |= read(); return x;}//------------------------------------------------------------------------------//------------------------------------------------------------------------------template <class BufferT>inline BufferBitReaderTemplate<BufferT>::BufferBitReaderTemplate() : currentByte(0), bitsLeft(0){}//------------------------------------------------------------------------------template <class BufferT>inline unsigned char BufferBitReaderTemplate<BufferT>::read(){ assert(bitsLeft==0); return BufferReaderTemplate<BufferT>::read();}//------------------------------------------------------------------------------template <class BufferT>inline void BufferBitReaderTemplate<BufferT>::read(void* dest, size_t length){ assert(bitsLeft==0); BufferReaderTemplate<BufferT>::read(dest, length);}//------------------------------------------------------------------------------template <class BufferT>inline unsigned BufferBitReaderTemplate<BufferT>::read16(){ assert(bitsLeft==0); return BufferReaderTemplate<BufferT>::read16();}//------------------------------------------------------------------------------template <class BufferT>unsigned BufferBitReaderTemplate<BufferT>::readBits(size_t bitsToRead){ unsigned x = 0; while(bitsToRead>0) { if (bitsToRead > bitsLeft) { bitsToRead -= bitsLeft; x |= currentByte << bitsToRead; currentByte = BufferReaderTemplate<BufferT>::read(); bitsLeft = 8; } else { bitsLeft -= bitsToRead; x |= currentByte >> bitsLeft; currentByte &= (1<<bitsLeft) - 1; bitsToRead = 0; } } return x;}//------------------------------------------------------------------------------template <class BufferT>inline void BufferBitReaderTemplate<BufferT>::discardCurrentByte(){ currentByte = 0; bitsLeft = 0;}//------------------------------------------------------------------------------//------------------------------------------------------------------------------template <class BufferT>inline BufferReferenceTemplate<BufferT>::BufferReferenceTemplate() : buffer(0){}//------------------------------------------------------------------------------template <class BufferT>inline BufferReferenceTemplate<BufferT>::BufferReferenceTemplate(BufferT& buffer) : buffer(&buffer){}//------------------------------------------------------------------------------template <class BufferT>inline void BufferReferenceTemplate<BufferT>::setBuffer(BufferT& buf){ buffer = &buf;}//------------------------------------------------------------------------------template <class BufferT>inline BufferT& BufferReferenceTemplate<BufferT>::getBuffer() const{ assert(buffer!=0); return *buffer;}//------------------------------------------------------------------------------template <class BufferT>inline const unsigned char* BufferReferenceTemplate<BufferT>::getData() const{ assert(buffer!=0); return buffer->getData();}//------------------------------------------------------------------------------template <class BufferT>inline size_t BufferReferenceTemplate<BufferT>::getLength() const{ assert(buffer!=0); return buffer->getLength();}//------------------------------------------------------------------------------#endif // DXR3PLAYER_BUFFERREADERTEMPLATE_H// Local variables:// mode: c++// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -