#include "BitStreams.h" #include #include using namespace std; #include const int FULLWORD = 8; // 8 bits per char to file void OutBitStream::_clear() { buffer = 0; bufsize = 0; } OutBitStream::OutBitStream() { _clear(); } OutBitStream::~OutBitStream() { close(); } bool OutBitStream::isOpen() const { return ((const_cast(this))->file.is_open()); } // low level manipulations to add the given value into the current // buffer writing the leftmost resulting byte to the file, and // returning the number of bits which were leftover. int OutBitStream::_rawdump(int value, int numbits) { int numMissing = FULLWORD-bufsize; int shift = (numbits-numMissing); int prefix = value>>shift; buffer <<= numMissing; buffer += prefix; file.put(char(buffer)); byteswritten++; _clear(); return (numbits-numMissing); } bool OutBitStream::open(const std::string& filename) { if (isOpen()) close(); file.open(filename.c_str(), ios::binary|ios::out); _clear(); byteswritten=0; return isOpen(); } void OutBitStream::write(int value, int numbits) { if (isOpen() && numbits>0) { if (byteswritten>=LIMIT) { cerr << "WARNING. OutBitStream is being automatically closed" << endl << " as the file size is surpassing a safe limit." << endl; close(); return; } int cleanvalue = (value & ((1<(this))->file.is_open()); } bool InBitStream::eof() const { return (bufsize==0 && file.eof()); } bool InBitStream::open(const std::string& filename) { if (isOpen()) close(); file.open(filename.c_str(), ios::binary|ios::in); _clear(); _prefetch(); return isOpen(); } void InBitStream::_prefetch() { if (isOpen() && bufsize==0) { // need more from file buffer = file.get(); if (file.eof()) buffer = 0; else bufsize=FULLWORD; } } int InBitStream::read(int numbits) { if (isOpen()) { int result = 0; if (numbits==1) { // just need one bit if (bufsize>0) { // prefetch may have reached eof result = (buffer>>(bufsize-1)); buffer -= result<<(bufsize-1); bufsize--; if (bufsize==0) _prefetch(); } } else { int i; for (i=0; i