From a1d08fd27ab73b1697039e086d745ac68b890634 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Thu, 19 Dec 2013 15:32:43 -0800 Subject: Adding BigEndian reads. --- includes/Handle.h | 10 +++++++++- src/Handle.cc | 29 +++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/includes/Handle.h b/includes/Handle.h index 941748e..29cdb08 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -72,7 +72,15 @@ class Handle { Future readI16(); Future readI32(); Future readI64(); - Future writeU8 (uint8_t); + Future readBEU8(); + Future readBEU16(); + Future readBEU32(); + Future readBEU64(); + Future readBEI8(); + Future readBEI16(); + Future readBEI32(); + Future readBEI64(); + Future writeU8(uint8_t); Future writeU16(uint16_t); Future writeU32(uint32_t); Future writeU64(uint64_t); diff --git a/src/Handle.cc b/src/Handle.cc index 3b40c84..e6c0777 100644 --- a/src/Handle.cc +++ b/src/Handle.cc @@ -106,14 +106,30 @@ Balau::Future genericRead(Balau::IO t) { int c = 0; return Balau::Future([t, b, c]() mutable { do { - int r = t->read(((uint8_t *) b.get()) + c, sizeof(T)); + int r = t->read(((uint8_t *) b.get()) + c, sizeof(T) - c); c += r; } while (c < sizeof(T)); return *b; }); } -Balau::Future Balau::Handle::readU8 () { return genericRead(this); } +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; + }); +} + +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); } @@ -122,6 +138,15 @@ Balau::Future Balau::Handle::readI16() { return genericRead ( Balau::Future Balau::Handle::readI32() { return genericRead (this); } Balau::Future Balau::Handle::readI64() { return genericRead (this); } +Balau::Future Balau::Handle::readBEU8() { return genericReadBE (this); } +Balau::Future Balau::Handle::readBEU16() { return genericReadBE(this); } +Balau::Future Balau::Handle::readBEU32() { return genericReadBE(this); } +Balau::Future Balau::Handle::readBEU64() { return genericReadBE(this); } +Balau::Future Balau::Handle::readBEI8() { return genericReadBE (this); } +Balau::Future Balau::Handle::readBEI16() { return genericReadBE (this); } +Balau::Future Balau::Handle::readBEI32() { return genericReadBE (this); } +Balau::Future Balau::Handle::readBEI64() { return genericReadBE (this); } + template Balau::Future genericWrite(Balau::IO t, T val) { std::shared_ptr b(new T(val)); -- cgit v1.2.3