summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TODO1
-rw-r--r--includes/Handle.h5
-rw-r--r--src/Handle.cc39
-rw-r--r--src/LuaHandle.cc18
4 files changed, 62 insertions, 1 deletions
diff --git a/TODO b/TODO
index a041126..40b6a13 100644
--- a/TODO
+++ b/TODO
@@ -1 +1,2 @@
Protect Async against EAgains.
+Add GMP support. \ No newline at end of file
diff --git a/includes/Handle.h b/includes/Handle.h
index 3a26282..06b1723 100644
--- a/includes/Handle.h
+++ b/includes/Handle.h
@@ -61,7 +61,10 @@ class Handle {
void seek(off_t offset, int whence = SEEK_SET) { rseek(offset, whence); }
// there need to be more of these
- Future<uint8_t> readU8();
+ Future<uint8_t> readU8();
+ Future<uint16_t> readU16();
+ Future<uint32_t> readU32();
+ Future<uint64_t> readU64();
// these need to be changed into Future<>s
void writeString(const char * str, ssize_t len) { if (len < 0) len = strlen(str); forceWrite(str, len); }
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() + ")");
diff --git a/src/LuaHandle.cc b/src/LuaHandle.cc
index 355b992..6b8bd18 100644
--- a/src/LuaHandle.cc
+++ b/src/LuaHandle.cc
@@ -9,11 +9,15 @@ typedef IOHandle IOInput;
enum IOHandle_methods_t {
IOHANDLE_CLOSE,
IOHANDLE_READU8,
+ IOHANDLE_READU16,
+ IOHANDLE_READU32,
};
struct Balau::lua_functypes_t IOHandle_methods[] = {
{ IOHANDLE_CLOSE, "close", 0, 0, { } },
{ IOHANDLE_READU8, "readU8", 0, 0, { } },
+ { IOHANDLE_READU16, "readU16", 0, 0, { } },
+ { IOHANDLE_READU32, "readU32", 0, 0, { } },
{ -1, 0, 0, 0, 0 },
};
@@ -35,6 +39,18 @@ int sLua_IOHandle::IOHandle_proceed(Balau::Lua & L, int n, IOHandle * obj, int c
return L.yield(Balau::Future<int>([L, c]() mutable { L.push((lua_Number) c.get()); return 1; }));
}
break;
+ case IOHANDLE_READU16:
+ {
+ Balau::Future<uint16_t> c = h->readU16();
+ return L.yield(Balau::Future<int>([L, c]() mutable { L.push((lua_Number) c.get()); return 1; }));
+ }
+ break;
+ case IOHANDLE_READU32:
+ {
+ Balau::Future<uint32_t> c = h->readU32();
+ return L.yield(Balau::Future<int>([L, c]() mutable { L.push((lua_Number) c.get()); return 1; }));
+ }
+ break;
}
return r;
@@ -51,6 +67,8 @@ void Balau::LuaHandleFactory::pushObjectAndMembers(Lua & L) {
PUSH_METHOD(IOHandle, IOHANDLE_CLOSE);
PUSH_METHOD(IOHandle, IOHANDLE_READU8);
+ PUSH_METHOD(IOHandle, IOHANDLE_READU16);
+ PUSH_METHOD(IOHandle, IOHANDLE_READU32);
}