Skip to content

Finish implementation of decompress [Working Stage (from here on out … #4

Merged
merged 1 commit into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@

#Ignoring all .o file
*.o

#ignoring temp file
random

#ignore swp files from vim
*.swp

#ignore executables
compress
decompress


5 changes: 0 additions & 5 deletions Compress.H
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ public:

void generateTree();
void compressFile(string outFile);
void printPreorder();

};


template <class Ptr>
string preOrder(Ptr curr);

#endif
15 changes: 1 addition & 14 deletions Compress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ void Compress::generateTree(){
_tree = lst[0];
}

template <class Ptr>
string preOrder(Ptr curr, string & letters){
if (curr->isLeaf()){
letters += string(1,curr->letter);
return "1";
}
return preOrder(curr->left,letters) + "0" + preOrder(curr->right,letters);
}

//NEED NUMBER OF BITS FOR BODY
string Compress::generateBody(){
Expand All @@ -73,6 +65,7 @@ string Compress::generateBody(){
while(inpFile.get(c)){
uncompressed += _bitMap[c];
}
cout <<"UncomPressed binary: " << uncompressed << endl;
inpFile.close();
_counter = uncompressed.size();
// cout << "Uncompressed string: " << uncompressed << endl;
Expand All @@ -96,12 +89,6 @@ string Compress::generateHeader(){
return to_string(tmp.size()) + "/" + to_string(_counter) + "/" + comp + headerLetters;
}

void Compress::printPreorder(){
string letts = "";
cout << preOrder(_tree,letts) << endl;
}


int main(int argc, char *argv[]){
using namespace std;
try{
Expand Down
22 changes: 18 additions & 4 deletions Decompress.H
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@
#include "Huffman.H"

#include<queue>
#include <memory>

using namespace std;

struct FileParse;

class Decompress : public Huffman {
int bodySize;
shared_ptr<FileParse> _header; // Will hold parse Compressed file info
shared_ptr<Node> generateTreeHelper(string& bin);
void parseText();
void generateTree();

public:
Decompress(string fname): Huffman(fname){}
void decompressFile();

};

struct FileParse{
int bodySize;
int headerSize;
string preorderStr;
queue<char> letters;
string body;

public:
Decompress(string fname): Huffman(fname){}
void parseText();
FileParse(){}
~FileParse(){}
};

#endif
72 changes: 53 additions & 19 deletions Decompress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,91 @@ void Decompress::parseText(){
throw("[Error] Could not open file " + _fname + " in parseText()\n");
}

_header = make_shared<FileParse>(FileParse());
char c;
string val;

//Retrieve header Size
while(inpFile.get(c) && c != '/'){
val += string(1,c);
}
headerSize = stoi(val);
_header->headerSize = stoi(val);

cout << "Passed Parsing\n" << endl;

//Retrieve Body Size
val = "";
while (inpFile.get(c) && c != '/'){
val += string(1,c);
}
bodySize = stoi(val);
_header->bodySize = stoi(val);

//Getting PreOrder String
val = "";
for (int i = 0; i < ceil(headerSize/8.0);i++){
for (int i = 0; i < ceil((_header->headerSize)/8.0);i++){
val += inpFile.get();
}

preorderStr = convertToString(val);
cout << "Before Cutting: " << preorderStr << endl;
preorderStr = string(preorderStr.begin(),preorderStr.begin()+headerSize);
_header->preorderStr = convertToString(val);
_header->preorderStr = string(_header->preorderStr.begin(),_header->preorderStr.begin()+_header->headerSize);

//Getting Letters for each leaf in Tree
int counter = 0;
for (char c : preorderStr){
for (char c : _header->preorderStr){
if(c == '1') counter++;
}

for (int i = 0; i < counter;i++){
letters.push(inpFile.get());
_header->letters.push(inpFile.get());
}

/* while ( letters.size() > 0){
cout << letters.front() << endl;
letters.pop();
} //Print method to see letters in queue */

//Getting the body of compressed file and then converting it into string form
val = "";
while (inpFile.get(c)){
val += string(1,c);
}
inpFile.close();
body = convertToString(val);
body = string(body.begin(),body.begin()+bodySize);
cout << headerSize << " - " << bodySize << " - " << preorderStr << " - " << letters.size() << " - ";
cout << "String size = " << val.length()<< " -- " << val << " : " << body << endl;

_header->body = convertToString(val);
_header->body = string(_header->body.begin(),_header->body.begin()+_header->bodySize);
}

void Decompress::generateTree(){
_tree = generateTreeHelper(_header->preorderStr);
}

shared_ptr<Node> Decompress::generateTreeHelper(string& bin){
if (bin.size() < 1){
return NULL;
}
string bit = string(bin.begin(),bin.begin()+1); // First element in string
bin = string(bin.begin()+1,bin.end()); //The rest of the string
shared_ptr<Node> curr;
if (!bit.compare("1")){
curr = make_shared<Node>(Node(0,_header->letters.front(),NULL,NULL));
_header->letters.pop();
}
else if (!bit.compare("0")){
curr = make_shared<Node>(Node(0,'\0',generateTreeHelper(bin),generateTreeHelper(bin)));
}
return curr;
}

void Decompress::decompressFile(){
parseText();
generateTree();
generateBitMap();

shared_ptr<Node> curr = _tree;
string uncompress = "";
for (char c : _header->body){
curr = (c == '1') ? curr->right : curr->left;

if (curr->isLeaf()){
uncompress += string(1,curr->letter);
curr = _tree;
}
}
cout << "uncompressed: " << uncompress << endl;
}


Expand All @@ -81,7 +114,8 @@ int main(int argc, char *argv[]){
cout << "String bit for A : " << convertToString("A") << endl;
*/
Decompress tst = Decompress(argv[1]);
tst.parseText();
tst.decompressFile();
tst.printPreorder();
}
catch(string &e){cout << e;}
return 0;
Expand Down
6 changes: 6 additions & 0 deletions Huffman.H
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public:
virtual void generateTree(){};// Will be different for compress and decompress

void generateBitMap();
void printBitMap();

void printPreorder();

const map<char,string> & getBitMap(){ return _bitMap;}
string getFileName(){return _fname;}
Expand All @@ -45,4 +48,7 @@ string convertToBits(string uncompressed);//Converts the string format bits into

string convertToString(string compressed);//Converts the bit form into string form

template <class Ptr>
string preOrder(Ptr curr,string &lett);

#endif
25 changes: 25 additions & 0 deletions Huffman.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Huffman.H"

#include <iostream>

void Huffman::generateBitMapHelper(std::shared_ptr<Node> curr, std::string bin){
if(curr->isLeaf()){
_bitMap[curr->letter] = bin;
Expand All @@ -12,6 +14,15 @@ void Huffman::generateBitMapHelper(std::shared_ptr<Node> curr, std::string bin){

void Huffman::generateBitMap(){ //Wrapper class to recursively call enumerateTreeHelper with DFS
generateBitMapHelper(_tree,"");
printBitMap();
}

void Huffman::printBitMap(){
cout << "MAP TABLE BEGIN\n" << endl;
for (auto it = _bitMap.begin();it != _bitMap.end();it++){
cout << it->first << " : " << it->second << endl;
}
cout << "MAP TABLE END\n" << endl;
}

string convertToBits(string uncompressed){ //Converts the string format bits into char
Expand Down Expand Up @@ -54,3 +65,17 @@ string convertToString(string compressed){//converts from binary form to string
}
return total;
}

template <class Ptr>
string preOrder(Ptr curr, string & letters){
if (curr->isLeaf()){
letters += string(1,curr->letter);
return "1";
}
return "0" + preOrder(curr->left,letters) + preOrder(curr->right,letters);
}

void Huffman::printPreorder(){
string letts = "";
cout << preOrder(_tree,letts) << endl;
}