From 35696c8489900ddd19445db880b7ba95cb17b17a Mon Sep 17 00:00:00 2001 From: Tony Pham Date: Wed, 17 May 2017 12:15:37 -0400 Subject: [PATCH] Write the parse method to parse the compressed file --- Decompress.H | 22 +++++++++++++ Decompress.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 Decompress.H create mode 100644 Decompress.cpp diff --git a/Decompress.H b/Decompress.H new file mode 100644 index 0000000..6f8b44f --- /dev/null +++ b/Decompress.H @@ -0,0 +1,22 @@ +#ifndef __DECOMPRESS_H__ +#define __DECOMPRESS_H__ + +#include "Huffman.H" + +#include + +using namespace std; + +class Decompress : public Huffman { + int bodySize; + int headerSize; + string preorderStr; + queue letters; + string body; + +public: + Decompress(string fname): Huffman(fname){} + void parseText(); +}; + +#endif diff --git a/Decompress.cpp b/Decompress.cpp new file mode 100644 index 0000000..742268f --- /dev/null +++ b/Decompress.cpp @@ -0,0 +1,88 @@ +#include "Decompress.H" + +#include +#include +#include +#include +#include + +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 conv = make_shared(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; +}