I am having a difficult time with a BitStream class. I need to overload the extraction and insertion operator for the classes, but I am at a loss. Here is what I have so far. Any guidance is appreciated.
#ifndef BITSTREAM_H_
#define BITSTREAM_H_
#include <iostream>
#include <fstream>
#include <vector>
class BitOStream
{
ofstream myfile;
char* Buffer;
int numOfBytes;
public:
BitOStream(const char* filePath, const char* headerChunk = NULL, int numberOfBytes = 0);
~BitOStream();
BitOStream& operator<<(vector<int> &bits);
void close();
};
class BitIStream
{
ifstream myfile;
char* Buffer;
int numOfBytes;
public:
BitIStream(const char* filePath, char* headerChunk = NULL, int numberOfBytes = 0);
~BitIStream();
BitIStream& operator>>(int &bit);
bool eof();
void close();
};
/////////////////////////////////////////////////////////////////////////////////
//Function: constructor //
// Parameters : filePath - the path of the file to open for output //
// headerChunk - a chunk of data to be written at the //
// beginning of the file //
// numberOfBytes - the number of bytes of header information //
// to write out //
/////////////////////////////////////////////////////////////////////////////////
BitOStream::BitOStream(const char* filePath, const char* headerChunk, int numberOfBytes)
{
numOfBytes = numberOfBytes;
myfile.open(filePath, ios_base::out | ios_base::app | ios_base::binary);
if (!myfile.is_open())
{
cout << "ERROR! Cannot locate file: " << filePath << "\n\n";
system("pause");
return;
}
if(headerChunk)
{
if(myfile.good())
myfile.write(headerChunk, numOfBytes);
}
else
{
if(myfile.good())
myfile.write(Buffer, numOfBytes);
}
}
///////////////////////////////////////////////////////////////////////////////
// Function : destructor
///////////////////////////////////////////////////////////////////////////////
BitOStream::~BitOStream()
{
close();
}
///////////////////////////////////////////////////////////////////////////////
// Function : insertion operator
// Parameters : bits - a vector containing some number of 1's and 0's to
// stream out to the file
// Return : BitOStream& - the stream (allows for daisy-chaining insertions)
///////////////////////////////////////////////////////////////////////////////
BitOStream& BitOStream::operator<<(vector<int> &bits)
{
for(unsigned int i = numOfBytes; i < bits.size(); ++i)
{
Buffer |= bits[i] << (i-numOfBytes);
}
// Write bits out to the file
if(myfile.good())
myfile.write(Buffer, numOfBytes);
return *this;
}
///////////////////////////////////////////////////////////////////////////////
// Function : close
// Notes : closes the file stream - if remaining bits exist, they are written
// to the file with trailing 0's. if no remaining bits exist,
// simply close the file
///////////////////////////////////////////////////////////////////////////////
void BitOStream::close()
{
// if bits remain in the buffer, write them out to the file
if(Buffer)
{
if(myfile.good() && myfile.is_open())
myfile.write(Buffer, this->numOfBytes);
}
myfile.close();
}
///////////////////////////////////////////////////////////////////////////////
// Function : constructor
// Parameters : filePath - the path of the file to open for input
// headerChunk - a chunk of data to be read from the
// beginning of the file
// numberOfBytes - the number of bytes of header information
// to read in
///////////////////////////////////////////////////////////////////////////////
BitIStream::BitIStream(const char* filePath, char* headerChunk, int numberOfBytes)
{
ifstream myfile;
myfile.open (filePath, ios::in | ios::app| ios::binary);
if (!myfile.is_open())
{
cout << "ERROR! Cannot locate file: " << filePath << "\n\n";
system("pause");
return;
}
if(headerChunk)
{
if(myfile.good())
myfile.open(filePath, ios_base::out | ios_base::app | ios_base::binary);
if(myfile.is_open())
{
myfile.read(headerChunk, numberOfBytes);
}
}
else
{
if(myfile.good())
myfile.open(filePath, ios_base::out | ios_base::app | ios_base::binary);
if(myfile.is_open())
{
myfile.read(Buffer, numberOfBytes);
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Function : destructor
///////////////////////////////////////////////////////////////////////////////
BitIStream::~BitIStream()
{
close();
}
///////////////////////////////////////////////////////////////////////////////
// Function : extraction operator
// Parameters : bit - store the next bit here
// Return : BitIStream& - the stream (allows for daisy-chaining extractions)
///////////////////////////////////////////////////////////////////////////////
BitIStream& BitIStream::operator>>(int &bit)
{
Buffer[bit];
return *this;
}
///////////////////////////////////////////////////////////////////////////////
// Function : eof
// Return : true if we are at the end of the file, false otherwise
// Notes : should only return true when we have given the user every byte from
// the file and every bit from the buffer
///////////////////////////////////////////////////////////////////////////////
bool BitIStream::eof()
{
if (!myfile.eof())
return false;
else
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Function : close
// Notes : close the file
///////////////////////////////////////////////////////////////////////////////
void BitIStream::close()
{
myfile.close();
}
#endif