Filename: main.cpp

#include "HuffmanTree.h"

 

#include <string>

#include <fstream>

using namespace std;

 

//function prototypes

void GenerateFrequenciesFromFile(ifstream & , vector<HuffmanTreeNodePtr> &);

bool OpenFileForReading (string , ifstream & );

bool OpenFileForWriting (string , ofstream & );

 

void main()

{          

            //generate code from text file

 

            ifstream text;

            if(! OpenFileForReading("text.txt", text)) return;

 

            ofstream codetable;

            if(! OpenFileForWriting("codetable.txt", codetable)) return;

           

            vector<HuffmanTreeNodePtr>  HuffmanTreeNodes;

            GenerateFrequenciesFromFile(text, HuffmanTreeNodes);

            HuffmanTree t(HuffmanTreeNodes);

 

            t.PrintCode(codetable);

 

           

            //use code generated to decode text file

           

            ifstream codedtext;

            if(! OpenFileForReading("codedtext.txt", codedtext)) return;

 

            ofstream decodedtext;

            if(! OpenFileForWriting("decodedtext.txt", decodedtext)) return;

 

            t.DecodeAndPrint(codedtext,decodedtext);

 

}

 

 

/*

Purpose: opens a file for input

Precondition: a string containing the file name to open is passed, and an input file stream is passed

Postcondtion: true is returned if the file was open, false if an error occured

*/

bool OpenFileForReading (string fileName, ifstream & inputFileStream)

{

            inputFileStream.open(&fileName[0], ios::in);

 

            if (!inputFileStream.is_open())

            {

                        cout << "ERROR: CAN'T OPEN FILE " << fileName << endl;

                        return false;

            }

 

return true;

}

 

 

/*

Purpose: opens a file for output

Precondition: a string containing the file name to open is passed, and an output file stream is passed

Postcondtion: true is returned if the file was open, false if an error occured

*/

bool OpenFileForWriting (string fileName, ofstream & outputFileStream)

{                      

            outputFileStream.open (&fileName[0], ofstream::out | ofstream::trunc);

            //Truncate file to zero when opening, so that file is empty before you output to it

 

            if(!outputFileStream.is_open())

            {

                        cout <<  "ERROR: CAN'T OPEN FILE" << fileName << endl;

                        return false;

            }

            return true;

}

 

 

 

/*

Purpose: generates a vector of frequencies of the characters contained in the file

Precondition: the input file stream from which to generate is passed and an empty vector of Huffman TreeNodes is passed

Postcondtion: none

*/

void GenerateFrequenciesFromFile(ifstream & inputFileStream, vector<HuffmanTreeNodePtr> &  HuffmanTreeNodes)

{

            char c;

 

            while(inputFileStream.get(c))

            {

                        int positionOfCharacterInVector = GetPositionOfCharInVector(HuffmanTreeNodes, c);

           

                        //if character is already in vector find the character and add to its frequency

                        if(positionOfCharacterInVector != -1)

                                    HuffmanTreeNodes[positionOfCharacterInVector]->IncrementFrequency();

 

                        //otherwise add the new character to the vector

                        else

                                    InsertAtFrontInVector(HuffmanTreeNodes, new HuffmanTreeNode(c,1,NULL,NULL));

            }                                  

}