From c49058ffb738c87271e58a76a0ecf3814db1c4ac Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Thu, 13 Mar 2014 00:03:56 +0100 Subject: Reading is no longer little endian by default. It's now having explicit little endian accessors, and a mode flag that can be toggled. --- src/Handle.cc | 76 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/Handle.cc b/src/Handle.cc index 0d1fb15..e75edbb 100644 --- a/src/Handle.cc +++ b/src/Handle.cc @@ -115,28 +115,66 @@ Balau::Future genericRead(Balau::IO t) { template Balau::Future genericReadBE(Balau::IO t) { - std::shared_ptr b(new T); - int c = 0; - *b.get() = 0; - return Balau::Future([t, b, c]() mutable { - do { - uint8_t v = t->readU8().get(); - *b.get() <<= 8; - *b.get() += v; - c++; - } while (c < sizeof(T)); - return *b; - }); + std::shared_ptr b(new T); + int c = 0; + *b.get() = 0; + return Balau::Future([t, b, c]() mutable { + do { + uint8_t v = t->readU8().get(); + *b.get() <<= 8; + *b.get() += v; + c++; + } while (c < sizeof(T)); + return *b; + }); } Balau::Future Balau::Handle::readU8() { return genericRead (this); } -Balau::Future Balau::Handle::readU16() { return genericRead(this); } -Balau::Future Balau::Handle::readU32() { return genericRead(this); } -Balau::Future Balau::Handle::readU64() { return genericRead(this); } -Balau::Future Balau::Handle::readI8 () { return genericRead (this); } -Balau::Future Balau::Handle::readI16() { return genericRead (this); } -Balau::Future Balau::Handle::readI32() { return genericRead (this); } -Balau::Future Balau::Handle::readI64() { return genericRead (this); } +Balau::Future Balau::Handle::readI8() { return genericRead (this); } + +Balau::Future Balau::Handle::readU16() { + if (m_bigEndianMode) + return readBEU16(); + else + return readLEU16(); +} +Balau::Future Balau::Handle::readU32() { + if (m_bigEndianMode) + return readBEU32(); + else + return readLEU32(); +} +Balau::Future Balau::Handle::readU64() { + if (m_bigEndianMode) + return readBEU64(); + else + return readLEU64(); +} +Balau::Future Balau::Handle::readI16() { + if (m_bigEndianMode) + return readBEI16(); + else + return readLEI16(); +} +Balau::Future Balau::Handle::readI32() { + if (m_bigEndianMode) + return readBEI32(); + else + return readLEI32(); +} +Balau::Future Balau::Handle::readI64() { + if (m_bigEndianMode) + return readBEI64(); + else + return readLEI64(); +} + +Balau::Future Balau::Handle::readLEU16() { return genericRead(this); } +Balau::Future Balau::Handle::readLEU32() { return genericRead(this); } +Balau::Future Balau::Handle::readLEU64() { return genericRead(this); } +Balau::Future Balau::Handle::readLEI16() { return genericRead (this); } +Balau::Future Balau::Handle::readLEI32() { return genericRead (this); } +Balau::Future Balau::Handle::readLEI64() { return genericRead (this); } Balau::Future Balau::Handle::readBEU16() { return genericReadBE(this); } Balau::Future Balau::Handle::readBEU32() { return genericReadBE(this); } -- cgit v1.2.3