bsnes/nall/mosaic/bitstream.hpp
Tim Allen 0d0af39b44 Update to v097r14 release.
byuu says:

This is a few days old, but oh well.

This WIP changes nall,hiro,ruby,icarus back to (u)int(8,16,32,64)_t.

I'm slowly pushing for (u)int(8,16,32,64) to use my custom
Integer<Size>/Natural<Size> classes instead. But it's going to be one
hell of a struggle to get that into higan.
2016-02-16 20:11:58 +11:00

48 lines
1.1 KiB
C++

#pragma once
namespace nall { namespace mosaic {
struct bitstream {
~bitstream() {
close();
}
auto read(uint64_t addr) const -> bool {
if(data == nullptr || (addr >> 3) >= size) return 0;
uint mask = endian == 0 ? (0x01 << (addr & 7)) : (0x80 >> (addr & 7));
return data[addr >> 3] & mask;
}
auto write(uint64_t addr, bool value) -> void {
if(data == nullptr || readonly == true || (addr >> 3) >= size) return;
uint mask = endian == 0 ? (0x01 << (addr & 7)) : (0x80 >> (addr & 7));
if(value == 0) data[addr >> 3] &= ~mask;
if(value == 1) data[addr >> 3] |= mask;
}
auto open(const string& filename) -> bool {
readonly = false;
if(fp.open(filename, filemap::mode::readwrite) == false) {
readonly = true;
if(fp.open(filename, filemap::mode::read) == false) {
return false;
}
}
data = fp.data();
size = fp.size();
return true;
}
auto close() -> void {
fp.close();
data = nullptr;
}
filemap fp;
uint8_t* data = nullptr;
uint size = 0;
bool readonly = false;
bool endian = 1;
};
}}