From df00882195da3fb970782f21d79a0a10e6b186b3 Mon Sep 17 00:00:00 2001 From: Nicolas Noble Date: Wed, 7 Aug 2013 16:32:26 -0700 Subject: Adding readU16, readU32 and readU64. The Lua version of readU64 will need GMP support. --- src/Handle.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/Handle.cc') diff --git a/src/Handle.cc b/src/Handle.cc index 503787b..2a03f8c 100644 --- a/src/Handle.cc +++ b/src/Handle.cc @@ -104,6 +104,45 @@ Balau::Future Balau::Handle::readU8() { }); } +Balau::Future Balau::Handle::readU16() { + IO t(this); + std::shared_ptr b(new uint16_t); + int c = 0; + return Future([t, b, c]() mutable { + do { + int r = t->read(((uint8_t *) b.get()) + c, 2); + c += r; + } while (c < 2); + return *b; + }); +} + +Balau::Future Balau::Handle::readU32() { + IO t(this); + std::shared_ptr b(new uint32_t); + int c = 0; + return Future([t, b, c]() mutable { + do { + int r = t->read(((uint8_t *) b.get()) + c, 4); + c += r; + } while (c < 4); + return *b; + }); +} + +Balau::Future Balau::Handle::readU64() { + IO t(this); + std::shared_ptr b(new uint64_t); + int c = 0; + return Future([t, b, c]() mutable { + do { + int r = t->read(((uint8_t *) b.get()) + c, 8); + c += r; + } while (c < 8); + return *b; + }); +} + void Balau::Handle::rseek(off_t offset, int whence) throw (GeneralException) { if (canSeek()) throw GeneralException(String("Handle ") + getName() + " can seek, but rseek() not implemented (missing in class " + ClassName(this).c_str() + ")"); -- cgit v1.2.3