WIP: MVP fixed flusher
This commit is contained in:
66
MashZip.cpp
Normal file
66
MashZip.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
#include "MashZip.h"
|
||||
#include <iostream>
|
||||
|
||||
void MashZip::unmashzip_stream(std::basic_istream<char> &is, std::basic_ostream<char> &os)
|
||||
{
|
||||
char magic[5] = { 0 };
|
||||
char header[HEADER_SIZE] = { 0 };
|
||||
|
||||
is.read(magic, 4);
|
||||
if (strcmp(magic, MAGIC) != 0)
|
||||
{
|
||||
throw std::runtime_error("MAGIC mismatch! Must start with MASH!");
|
||||
}
|
||||
|
||||
is.read(header, HEADER_SIZE);
|
||||
|
||||
// for (size_t i = 0; i < 128; i++) {
|
||||
// std::cerr << "Header: " << (int) header[i] << std::endl;
|
||||
// }
|
||||
|
||||
// load huffman table
|
||||
HuffmanTable ht(header);
|
||||
ibitstream ibs(is);
|
||||
|
||||
char sym = '\0';
|
||||
|
||||
while (true)
|
||||
{
|
||||
try {
|
||||
sym = ht.decode_one_symbol(ibs);
|
||||
} catch (std::runtime_error &e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
break;
|
||||
}
|
||||
os << sym;
|
||||
}
|
||||
os.flush();
|
||||
}
|
||||
|
||||
void MashZip::mashzip_file(std::basic_istream<char> &cs, std::basic_istream<char> &is, std::basic_ostream<char> &os)
|
||||
{
|
||||
// std::ifstream ifs(name, std::ios::binary);
|
||||
// is.seekg(0);
|
||||
|
||||
os.write(MAGIC, 4);
|
||||
|
||||
// build huffman table by file
|
||||
HuffmanTable ht(cs);
|
||||
// is.seekg(0); // reset position as ht reads whole file
|
||||
|
||||
os.write(ht.to_header(), HEADER_SIZE);
|
||||
|
||||
obitstream obs(os);
|
||||
|
||||
char c;
|
||||
|
||||
// std::cerr << "Pos in stream: " << is. << std::endl;
|
||||
|
||||
while (is >> c)
|
||||
{
|
||||
|
||||
ht.write_symbol(obs, c);
|
||||
}
|
||||
obs.flush();
|
||||
os.flush();
|
||||
}
|
Reference in New Issue
Block a user