Skip to content
Permalink
6ca19c3d65
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
122 lines (102 sloc) 2.78 KB
#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");
}
_header = make_shared<FileParse>(FileParse());
char c;
string val;
//Retrieve header Size
while(inpFile.get(c) && c != '/'){
val += string(1,c);
}
_header->headerSize = stoi(val);
cout << "Passed Parsing\n" << endl;
//Retrieve Body Size
val = "";
while (inpFile.get(c) && c != '/'){
val += string(1,c);
}
_header->bodySize = stoi(val);
//Getting PreOrder String
val = "";
for (int i = 0; i < ceil((_header->headerSize)/8.0);i++){
val += inpFile.get();
}
_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 : _header->preorderStr){
if(c == '1') counter++;
}
for (int i = 0; i < counter;i++){
_header->letters.push(inpFile.get());
}
//Getting the body of compressed file and then converting it into string form
val = "";
while (inpFile.get(c)){
val += string(1,c);
}
inpFile.close();
_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;
}
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.decompressFile();
tst.printPreorder();
}
catch(string &e){cout << e;}
return 0;
}