Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #3 from top13001/feature/Decompress
Feature/decompress
  • Loading branch information
top13001 committed May 17, 2017
2 parents f6bb3c4 + 4416d6a commit 30e2682
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Decompress.H
@@ -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
@@ -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;
}

0 comments on commit 30e2682

Please sign in to comment.