diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2014-03-13 00:03:56 +0100 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2014-03-13 00:03:56 +0100 |
commit | c49058ffb738c87271e58a76a0ecf3814db1c4ac (patch) | |
tree | a01d439b5d599a56cd2d6237cc158f4fb90ea4d1 | |
parent | 89a67c7625881a34258d0ff4d5f322061e577d4d (diff) |
Reading is no longer little endian by default. It's now having explicit little endian accessors, and a mode flag that can be toggled.
-rw-r--r-- | includes/Handle.h | 33 | ||||
-rw-r--r-- | src/Handle.cc | 76 |
2 files changed, 83 insertions, 26 deletions
diff --git a/includes/Handle.h b/includes/Handle.h index ba710b2..412c6c9 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -64,6 +64,14 @@ class Handle { virtual time_t getMTime(); virtual bool isPendingComplete() { return true; } + enum Endianness { + LITTLE_ENDIAN; + BIG_ENDIAN; + }; + + void setEndianness(Endianness endianness) { m_bigEndianMode = endianness == BIG_ENDIAN; } + Endianness getEndianness() { return m_bigEndianMode ? BIG_ENDIAN : LITTLE_ENDIAN; } + // helpers off64_t tell() { return rtell(); } void seek(off64_t offset, int whence = SEEK_SET) { rseek(offset, whence); } @@ -76,13 +84,22 @@ class Handle { Future<int16_t> readI16(); Future<int32_t> readI32(); Future<int64_t> readI64(); - Future<uint16_t> readBEU16(); - Future<uint32_t> readBEU32(); - Future<uint64_t> readBEU64(); - Future<int16_t> readBEI16(); - Future<int32_t> readBEI32(); - Future<int64_t> readBEI64(); - Future<void> writeU8(uint8_t); + + Future<uint16_t> readBEU16(); + Future<uint32_t> readBEU32(); + Future<uint64_t> readBEU64(); + Future<int16_t> readBEI16(); + Future<int32_t> readBEI32(); + Future<int64_t> readBEI64(); + + Future<uint16_t> readLEU16(); + Future<uint32_t> readLEU32(); + Future<uint64_t> readLEU64(); + Future<int16_t> readLEI16(); + Future<int32_t> readLEI32(); + Future<int64_t> readLEI64(); + + Future<void> writeU8(uint8_t); Future<void> writeU16(uint16_t); Future<void> writeU32(uint32_t); Future<void> writeU64(uint64_t); @@ -118,6 +135,8 @@ class Handle { std::atomic<int> m_refCount; + bool m_bigEndianMode = false; + Handle(const Handle &) = delete; Handle & operator=(const Handle &) = delete; }; 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<T> genericRead(Balau::IO<Balau::Handle> t) { template<class T> Balau::Future<T> genericReadBE(Balau::IO<Balau::Handle> t) { - std::shared_ptr<T> b(new T); - int c = 0; - *b.get() = 0; - return Balau::Future<T>([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<T> b(new T); + int c = 0; + *b.get() = 0; + return Balau::Future<T>([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<uint8_t> Balau::Handle::readU8() { return genericRead<uint8_t> (this); } -Balau::Future<uint16_t> Balau::Handle::readU16() { return genericRead<uint16_t>(this); } -Balau::Future<uint32_t> Balau::Handle::readU32() { return genericRead<uint32_t>(this); } -Balau::Future<uint64_t> Balau::Handle::readU64() { return genericRead<uint64_t>(this); } -Balau::Future<int8_t> Balau::Handle::readI8 () { return genericRead<int8_t> (this); } -Balau::Future<int16_t> Balau::Handle::readI16() { return genericRead<int16_t> (this); } -Balau::Future<int32_t> Balau::Handle::readI32() { return genericRead<int32_t> (this); } -Balau::Future<int64_t> Balau::Handle::readI64() { return genericRead<int64_t> (this); } +Balau::Future<int8_t> Balau::Handle::readI8() { return genericRead<int8_t> (this); } + +Balau::Future<uint16_t> Balau::Handle::readU16() { + if (m_bigEndianMode) + return readBEU16(); + else + return readLEU16(); +} +Balau::Future<uint32_t> Balau::Handle::readU32() { + if (m_bigEndianMode) + return readBEU32(); + else + return readLEU32(); +} +Balau::Future<uint64_t> Balau::Handle::readU64() { + if (m_bigEndianMode) + return readBEU64(); + else + return readLEU64(); +} +Balau::Future<int16_t> Balau::Handle::readI16() { + if (m_bigEndianMode) + return readBEI16(); + else + return readLEI16(); +} +Balau::Future<int32_t> Balau::Handle::readI32() { + if (m_bigEndianMode) + return readBEI32(); + else + return readLEI32(); +} +Balau::Future<int64_t> Balau::Handle::readI64() { + if (m_bigEndianMode) + return readBEI64(); + else + return readLEI64(); +} + +Balau::Future<uint16_t> Balau::Handle::readLEU16() { return genericRead<uint16_t>(this); } +Balau::Future<uint32_t> Balau::Handle::readLEU32() { return genericRead<uint32_t>(this); } +Balau::Future<uint64_t> Balau::Handle::readLEU64() { return genericRead<uint64_t>(this); } +Balau::Future<int16_t> Balau::Handle::readLEI16() { return genericRead<int16_t> (this); } +Balau::Future<int32_t> Balau::Handle::readLEI32() { return genericRead<int32_t> (this); } +Balau::Future<int64_t> Balau::Handle::readLEI64() { return genericRead<int64_t> (this); } Balau::Future<uint16_t> Balau::Handle::readBEU16() { return genericReadBE<uint16_t>(this); } Balau::Future<uint32_t> Balau::Handle::readBEU32() { return genericReadBE<uint32_t>(this); } |