diff options
author | Nicolas Noble <pixel@nobis-crew.org> | 2013-08-07 16:32:26 -0700 |
---|---|---|
committer | Nicolas Noble <pixel@nobis-crew.org> | 2013-08-07 16:32:40 -0700 |
commit | df00882195da3fb970782f21d79a0a10e6b186b3 (patch) | |
tree | f6626a204b464d02768a006a10d465620af59cb6 /src/Handle.cc | |
parent | 7f841bd54b8628420b4bec129fae86472b5dc565 (diff) |
Adding readU16, readU32 and readU64. The Lua version of readU64 will need GMP support.
Diffstat (limited to 'src/Handle.cc')
-rw-r--r-- | src/Handle.cc | 39 |
1 files changed, 39 insertions, 0 deletions
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<uint8_t> Balau::Handle::readU8() { }); } +Balau::Future<uint16_t> Balau::Handle::readU16() { + IO<Handle> t(this); + std::shared_ptr<uint16_t> b(new uint16_t); + int c = 0; + return Future<uint16_t>([t, b, c]() mutable { + do { + int r = t->read(((uint8_t *) b.get()) + c, 2); + c += r; + } while (c < 2); + return *b; + }); +} + +Balau::Future<uint32_t> Balau::Handle::readU32() { + IO<Handle> t(this); + std::shared_ptr<uint32_t> b(new uint32_t); + int c = 0; + return Future<uint32_t>([t, b, c]() mutable { + do { + int r = t->read(((uint8_t *) b.get()) + c, 4); + c += r; + } while (c < 4); + return *b; + }); +} + +Balau::Future<uint64_t> Balau::Handle::readU64() { + IO<Handle> t(this); + std::shared_ptr<uint64_t> b(new uint64_t); + int c = 0; + return Future<uint64_t>([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() + ")"); |