bitfile

Module for reading/writing an arbitrary number of bits from a file
Download

bitfile Ranking & Summary

Advertisement

  • Rating:
  • License:
  • GPL
  • Price:
  • FREE
  • Publisher Name:
  • Michael Dipperstein
  • Publisher web site:
  • http://michael.dipperstein.com/

bitfile Tags


bitfile Description

Module for reading/writing an arbitrary number of bits from a file bitfile is a simple class of I/O methods for files that contain data in sizes that aren't integral bytes. The methods contained in this class were created with compression algorithms in mind, but may be suited to other applications.Implementation:Each version of the bitfile library provides a wrapper around the language's native file I/O. The ANSI C version uses file I/O functions and every bitfile is referenced by a structure which includes a FILE pointer.The arraystream library uses a similar structure, replacing the FILE pointer with a pointer to an array of unsigned characters and an array index. Arraystream operations are analogous to bitfile operations in almost all respects and will not be discussed further.The C++ version of the bitfile library makes use of (but does not inherit from) the ifstream and ofstream classes. Every bit file object contains an ifstream pointer and ofstream pointer.The Python version implements a class containing a Python file object.In addition to a reference to a native file, each library includes an 8-bit buffer, and counter responsible for tracking the number of bits in the 8-bit buffer. The C and C++ versions of the bitfile library use an unsinged char for the 8-bit buffer.Reading BitsReading bits from a bitfile works as follows:Step 1. Read a byte from the underlying file and store it in the 8-bit buffer.Step 2. Set the count of bits in the buffer to 8.Step 3. Report the least significant bit (lsb) in the buffer as the bit read.Step 4. Shift the buffer right by one bit.Step 5. Decrement the count of bits in the buffer.To read an additional bit, repeat the process from Step 3. Once all bits are read from the 8-bit buffer (the count equals 0) the process starts over from Step 1.Writing BitsWriting bits to a bitfile works as follows:Step 1. Left shift the 8-bit buffer by one bit.Step 2. Set the least significant bit (lsb) of the 8-bit buffer to the value of the bit being written.Step 3. Increment the count of bits in the 8-bit buffer.Repeat the process from Step 1 for each additional bit. Once 8 bits have been written to the 8-bit buffer, the buffer is written to the underlying file and the bit count is set to 0.I have incorporated some short cuts that bypass the 8-bit buffer in the functions that read/write characters or bytes.Usage:Rather than writing lengthy man pages for each of the functions in the bitfile library, I have taken a cheap cop-out. The bitfile source includes detailed headers preceding each function. The Python version of the bitfile library includes comments in docstring format.I have also included a file named sample. which demonstrates the usage of each function in the bitfile library and serves as a test to verify the correctness of the code. Requirements: · Python


bitfile Related Software