Skip to content

Feature/decompress #3

Merged
merged 3 commits into from
May 17, 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
22 changes: 22 additions & 0 deletions Decompress.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __DECOMPRESS_H__
#define __DECOMPRESS_H__

#include "Huffman.H"

#include<queue>

using namespace std;

class Decompress : public Huffman {
int bodySize;
int headerSize;
string preorderStr;
queue<char> letters;
string body;

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

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

#include <fstream>
#include <iostream>
#include <string>
#include <math.h>
#include <string.h>

using namespace std;

void Decompress::parseText(){
ifstream inpFile(_fname);

if (!inpFile.is_open()){
throw("[Error] Could not open file " + _fname + " in parseText()\n");
}

char c;
string val;

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

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

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

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

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

for (int i = 0; i < counter;i++){
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;

}


int main(int argc, char *argv[]){
using namespace std;
try{
/*
shared_ptr<Compress> conv = make_shared<Compress>(Compress(argv[1]));
conv->generateTree();
conv->generateBitMap();
conv->compressFile("random");
cout << "String bit for A : " << convertToString("A") << endl;
*/
Decompress tst = Decompress(argv[1]);
tst.parseText();
}
catch(string &e){cout << e;}
return 0;
}