summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2013-12-19 15:32:43 -0800
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2013-12-19 15:34:46 -0800
commita1d08fd27ab73b1697039e086d745ac68b890634 (patch)
treec239e344f73dab23a99e3846b0fbb4fe3a3a9887
parent1fa5b7d41ad3ad3991852515ace597d4cae909b1 (diff)
Adding BigEndian reads.
-rw-r--r--includes/Handle.h10
-rw-r--r--src/Handle.cc29
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<int16_t> readI16();
Future<int32_t> readI32();
Future<int64_t> readI64();
- Future<void> writeU8 (uint8_t);
+ Future<uint8_t> readBEU8();
+ Future<uint16_t> readBEU16();
+ Future<uint32_t> readBEU32();
+ Future<uint64_t> readBEU64();
+ Future<int8_t> readBEI8();
+ Future<int16_t> readBEI16();
+ Future<int32_t> readBEI32();
+ Future<int64_t> readBEI64();
+ Future<void> writeU8(uint8_t);
Future<void> writeU16(uint16_t);
Future<void> writeU32(uint32_t);
Future<void> 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<T> genericRead(Balau::IO<Balau::Handle> t) {
int c = 0;
return Balau::Future<T>([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<uint8_t> Balau::Handle::readU8 () { return genericRead<uint8_t >(this); }
+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;
+ });
+}
+
+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); }
@@ -122,6 +138,15 @@ Balau::Future<int16_t> Balau::Handle::readI16() { return genericRead<int16_t> (
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<uint8_t> Balau::Handle::readBEU8() { return genericReadBE<uint8_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); }
+Balau::Future<uint64_t> Balau::Handle::readBEU64() { return genericReadBE<uint64_t>(this); }
+Balau::Future<int8_t> Balau::Handle::readBEI8() { return genericReadBE<int8_t> (this); }
+Balau::Future<int16_t> Balau::Handle::readBEI16() { return genericReadBE<int16_t> (this); }
+Balau::Future<int32_t> Balau::Handle::readBEI32() { return genericReadBE<int32_t> (this); }
+Balau::Future<int64_t> Balau::Handle::readBEI64() { return genericReadBE<int64_t> (this); }
+
template<class T>
Balau::Future<void> genericWrite(Balau::IO<Balau::Handle> t, T val) {
std::shared_ptr<T> b(new T(val));