////////////////////////////////////////////////////////////////////////////
//
// Bit input and output classes
// (c)2003 Jeremy Collake and Bitsum Technologies
// jeremy@bitsum.com
//
// This code may not be used without the permission of Bitsum Technologies.
// To use this code in your application, please email us at support@bitsum.com
// and we will be happy to grant you a free license.
//
//////////////////////////////////////////////////////////////////////////////
//
// bitin and bitout class definitions.
//
// These classes facilitate bit i/o. 
//
//
//
#pragma once

#include "types.h"                      // data types


///////////////////////////////////////////////////////////////////////////////
//
// bitin class
//
///////////////////////////////////////////////////////////////////////////////
class bitin {
private:
        DWORD nBitCount;                // current count of bits in cByte
        BYTE cByte;                     // current byte being read for input
        PSTREAM pStream;                // pointer to the input buffer
public: 
        bitin(PSTREAM);                 // constructor, supply pointer to input buffer
        BIT GetBit();                   // read a single bit
        DWORD GetBits(DWORD dwBits); // read X number of bits
        BYTE GetByte();                 // read a byte
        WORD GetWord();                 // read a word
        DWORD GetDword();               // read a dword
        PSTREAM *ptr();                 // get a pointer to the input stream
};

///////////////////////////////////////////////////////////////////////////////
//
// bitout class
//
///////////////////////////////////////////////////////////////////////////////
class bitout {
private:
        BYTE nBitCount;                         // current count of bits in cByte
        BYTE cByte;                             // holder of bits
        PSTREAM pStream;                        // pointer to the output stream
        BYTE pushed_nBitCount;                  // holder for pushed position in output stream (bit count)
        BYTE pushed_cByte;                      // holder for pushed position in output stream (current bits)
        PSTREAM pushed_pStream;                 // holder for pushed position in output stream (pointer)
public: 
        bitout(PSTREAM);                        // constructor, supply the output buffer
        ~bitout();                              // destructor, flushes output buffer
        void EmitBit(BIT nbBit);                // emit a bit
        void EmitBits(DWORD dwBits, DWORD dwBitCount);  // emit a number of bits
        void EmitByte(BYTE nbByte);             // emit a byte
        void EmitWord(WORD wWord);              // emit a word
        void EmitDword(DWORD dwDword);          // emit a dword
        void Flush();                           // flush the output buffer 
        void push();                            // push the current position of the output stream (only one position in stack) 
        void pop();                             // pop the output stream position
        PSTREAM ptr();                          // pointer to the output stream
};