lzw-assignment/src/io.cc

28 lines
883 B
C++
Raw Normal View History

#include "io.hh"
#include "bitpack.hh"
#include <array>
2018-06-05 09:38:27 +00:00
using std::uint16_t;
2018-06-21 15:38:51 +00:00
using std::vector;
2018-06-05 09:38:27 +00:00
using vuint16 = vector<uint16_t>;
using vvuint16 = vector<vuint16>;
2018-06-21 15:38:51 +00:00
void write_file(std::ofstream &t_out, const vvuint16 &t_chunks) {
const auto nr_chunks = static_cast<uint16_t>(t_chunks.size());
#ifdef Debug
std::printf("Number of chunks: %u\n", nr_chunks);
#endif
t_out.write(reinterpret_cast<const char *>(&nr_chunks), sizeof(nr_chunks));
for (const auto &chunk : t_chunks) {
write_chunk(t_out, chunk);
}
}
2018-06-21 15:38:51 +00:00
void write_chunk(std::ofstream &t_out, const vuint16 &t_chunk) {
const auto output = pack(t_chunk);
const auto chunk_size = static_cast<uint32_t>(output.size());
2018-06-21 15:38:51 +00:00
t_out.write(reinterpret_cast<const char *>(&chunk_size), sizeof(chunk_size));
t_out.write(reinterpret_cast<const char *>(output.data()),
sizeof(output[0]) * output.size());
}