-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write the parse method to parse the compressed file
- Loading branch information
Tony Pham
authored and
Tony Pham
committed
May 17, 2017
1 parent
4d1573e
commit 9591ee3
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |