Octane v1.01.20 - The Open Compression Toolkit for C++ http://octane.sourceforge.net/
Homepage | Main | Modules | Class Hierarchy | Compound List | File List | Compound Members | Related Pages

bitwriter Class Reference
[bitio]

#include <bitwriter.hpp>

Inheritance diagram for bitwriter:

array_bitwriter file_bitwriter null_bitwriter stream_bitwriter string_bitwriter vector_bitwriter List of all members.

Detailed Description

Base class for bit output wrappers.

The bitwriter class is a wrapper which provides bit output functionality to anything supporting output of 8-bit chars (bytes). It attempts to be as clean and transparent as possible, while maintaining a fairly decent efficiency.

The base class contains functions for handling the output by collecting bits in a temporary store (a byte), writing them out when the store is full.

It is used by creating a sub-class and implementing the write_next_byte() member function.

The destructor of the derived class should call finalize().

Definition at line 40 of file bitwriter.hpp.

Public Member Functions

 bitwriter ()
 Constructor.

virtual ~bitwriter ()
 Destructor.

virtual bool error () const
 Check for error.

void align_byte ()
 Align bitwriter on a byte boundary.

Size Functions
size_t bits_done () const
 Return the number of bits written.

size_t bytes_done () const
 Return the number of bytes written.

size_t tell_bit () const
 Return the current bit position within the bit stream.

size_t tell_byte () const
 Return the current byte position within the bit stream.

Output Functions
void put_bit (bool v)
 Write one bit to the output.

void put_byte (unsigned char v)
 Write one byte (8-bit unsigned value).

void put_word (unsigned short v)
 Write one word (16-bit unsigned value).

void put_dword (unsigned long v)
 Write one dword (32-bit unsigned value).

void put_float (float v)
 Write one float.

void put_double (double v)
 Write one double.

void put_string (std::string &s)
 Write a string.

void write (const char *buffer, size_t length)
 Copy bytes from an array to the bitwriter.

Polymorphic Output Function Wrappers
void put (bool v)
void put (char v)
void put (unsigned char v)
void put (short v)
void put (unsigned short v)
void put (int v)
void put (unsigned int v)
void put (long v)
void put (unsigned long v)
void put (float v)
void put (double v)
void put (std::string &s)

Protected Member Functions

void finalize ()
 Perform cleanup.

virtual void write_next_byte (char c)=0
 Defines the method of writing a single byte of output.


Protected Attributes

size_t num
 Number of bytes written.


Constructor & Destructor Documentation

virtual bitwriter::~bitwriter  )  [inline, virtual]
 

Destructor.

Derived classes should implement a destructor which calls the finalize() member function.

Definition at line 48 of file bitwriter.hpp.

00048 { ; }


Member Function Documentation

virtual bool bitwriter::error  )  const [inline, virtual]
 

Check for error.

Returns:
true if an error occurred.

Reimplemented in file_bitwriter.

Definition at line 53 of file bitwriter.hpp.

00053 { return false; }

size_t bitwriter::bits_done  )  const [inline]
 

Return the number of bits written.

Returns:
the number of bits written by the bitwriter.

Definition at line 72 of file bitwriter.hpp.

References num.

Referenced by tell_bit().

00072 { return 8*num + 8 - log2_tab[mask]; }

size_t bitwriter::bytes_done  )  const [inline]
 

Return the number of bytes written.

Returns:
the number of bytes written by the bitwriter.

Definition at line 75 of file bitwriter.hpp.

References num.

Referenced by tell_byte().

00075 { return num; }

size_t bitwriter::tell_bit  )  const [inline]
 

Return the current bit position within the bit stream.

Returns:
the current bit position.

Definition at line 79 of file bitwriter.hpp.

References bits_done().

00079 { return bits_done(); }

size_t bitwriter::tell_byte  )  const [inline]
 

Return the current byte position within the bit stream.

Returns:
the current byte position.

Definition at line 83 of file bitwriter.hpp.

References bytes_done().

Referenced by OctaneCompressor::Compress(), OctaneCompressor::Decompress(), and OctaneCompressor::Save().

00083 { return bytes_done(); }

void bitwriter::put_bit bool  v  )  [inline]
 

Write one bit to the output.

Parameters:
v - bit value to write (true for 1, false for 0).

Definition at line 93 of file bitwriter.hpp.

References num, and write_next_byte().

Referenced by HuffNode_Leaf::AddBitsToBitWriter().

00094     {
00095         if (v) current |= mask;
00096         if (!(mask >>= 1))
00097         {
00098             ++num; write_next_byte(current);
00099             current = 0;
00100             mask = 0x80;
00101         }
00102     }

void bitwriter::put_byte unsigned char  v  )  [inline]
 

Write one byte (8-bit unsigned value).

Parameters:
v - byte value to write.

Definition at line 106 of file bitwriter.hpp.

References num, and write_next_byte().

Referenced by zleCompressor::DoProtectedCompress(), SampleCompressor::DoProtectedCompress(), mtfllCompressor::DoProtectedCompress(), zleCompressor::DoProtectedDecompress(), SampleCompressor::DoProtectedDecompress(), mtfllCompressor::DoProtectedDecompress(), put_dword(), put_word(), OctaneCompressor::SaveCompressionHeader(), OctaneCompressor::SaveGUID(), and write().

00107     {
00108         ++num;
00109 
00110         if (mask == 0x80) { write_next_byte(v); return; }
00111 
00112         unsigned char ml = log2_tab[mask];
00113         current |= v >> (8 - ml);
00114         write_next_byte(current);
00115         current = v << ml;
00116     }

void bitwriter::put_word unsigned short  v  )  [inline]
 

Write one word (16-bit unsigned value).

Parameters:
v - word value to write.

Definition at line 120 of file bitwriter.hpp.

References put_byte().

Referenced by put_string().

00121     {
00122         put_byte((v >> 8) & 0x00ff);
00123         put_byte((v     ) & 0x00ff);
00124     }

void bitwriter::put_dword unsigned long  v  )  [inline]
 

Write one dword (32-bit unsigned value).

Parameters:
v - dword value to write.

Definition at line 128 of file bitwriter.hpp.

References put_byte().

Referenced by put_string().

00129     {
00130         put_byte((unsigned char) ((v >> 24) & 0x00ff));
00131         put_byte((unsigned char) ((v >> 16) & 0x00ff));
00132         put_byte((unsigned char) ((v >> 8 ) & 0x00ff));
00133         put_byte((unsigned char) ((v      ) & 0x00ff));
00134     }

void bitwriter::put_float float  v  )  [inline]
 

Write one float.

Parameters:
v - float value to write.
Warning:
Experimental, not portable.
Todo:
Experimental, not portable.

Definition at line 140 of file bitwriter.hpp.

References write().

00140 { write((char*)&v, sizeof(float)); }

void bitwriter::put_double double  v  )  [inline]
 

Write one double.

Parameters:
v - double value to write.
Warning:
Experimental, not portable.
Todo:
Experimental, not portable.

Definition at line 146 of file bitwriter.hpp.

References write().

00146 { write((char*)&v, sizeof(double)); }

void bitwriter::put_string std::string &  s  )  [inline]
 

Write a string.

Parameters:
s - the string to write.

Definition at line 150 of file bitwriter.hpp.

References put_dword(), put_word(), and write().

00151     {
00152         size_t len = s.length();
00153 
00154         if ((len > 0x0000ffff) || (len == 0))
00155         {
00156             put_word(0);
00157             put_dword(len);
00158         } else {
00159             put_word(len);
00160         }
00161 
00162         write(s.c_str(), len);
00163     }

void bitwriter::write const char *  buffer,
size_t  length
[inline]
 

Copy bytes from an array to the bitwriter.

Parameters:
buffer - pointer to where the bytes should be read.
length - the number of bytes to copy.

Definition at line 168 of file bitwriter.hpp.

References put_byte().

Referenced by put_double(), put_float(), put_string(), and SubstringParser::WriteSymbolText().

00169     {
00170         for ( ; length; --length) put_byte(*buffer++);
00171     }

void bitwriter::finalize  )  [inline, protected]
 

Perform cleanup.

Derived classes should call this method from their destructor.

Definition at line 203 of file bitwriter.hpp.

References align_byte().

00203 { align_byte(); }

virtual void bitwriter::write_next_byte char  c  )  [protected, pure virtual]
 

Defines the method of writing a single byte of output.

Derived classes should implement this function.

Parameters:
c - the 8 bits to write to the output.

Implemented in null_bitwriter, stream_bitwriter, vector_bitwriter, string_bitwriter, array_bitwriter, and file_bitwriter.

Referenced by align_byte(), put_bit(), and put_byte().


The documentation for this class was generated from the following files:  
Generated on 20 May 2004 by doxygen 1.3.3