summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-08-10 16:16:35 -0700
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-08-10 16:16:35 -0700
commit2e5344109da2abc59a7882521ea8378ca01c174c (patch)
treee7600fde7473f42c596723c4bb96b098465a6d92
parentec0f82bb6ea911baee6b9654f799f037282bf5b9 (diff)
Properly adding little and big endian versions of writes, plus a few other tweaks.
-rw-r--r--includes/Handle.h29
-rw-r--r--src/Handle.cc98
2 files changed, 97 insertions, 30 deletions
diff --git a/includes/Handle.h b/includes/Handle.h
index 1cbcabf..83884ef 100644
--- a/includes/Handle.h
+++ b/includes/Handle.h
@@ -59,9 +59,9 @@ class Handle {
virtual ssize_t read(void * buf, size_t count) throw (GeneralException) WARN_UNUSED_RESULT;
virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) WARN_UNUSED_RESULT;
virtual void rseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException);
- virtual void wseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException);
+ virtual void wseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) { return rseek(offset, whence); }
virtual off64_t rtell() throw (GeneralException);
- virtual off64_t wtell() throw (GeneralException);
+ virtual off64_t wtell() throw (GeneralException) { return rtell(); }
virtual off64_t getSize() { return -1; }
virtual time_t getMTime() { return -1; }
virtual bool isPendingComplete() { return true; }
@@ -101,7 +101,7 @@ class Handle {
Future<int32_t> readLEI32();
Future<int64_t> readLEI64();
- Future<void> writeU8(uint8_t);
+ Future<void> writeU8 (uint8_t);
Future<void> writeU16(uint16_t);
Future<void> writeU32(uint32_t);
Future<void> writeU64(uint64_t);
@@ -110,9 +110,23 @@ class Handle {
Future<void> writeI32(int32_t);
Future<void> writeI64(int64_t);
+ Future<void> writeBEU16(uint16_t);
+ Future<void> writeBEU32(uint32_t);
+ Future<void> writeBEU64(uint64_t);
+ Future<void> writeBEI16(int16_t);
+ Future<void> writeBEI32(int32_t);
+ Future<void> writeBEI64(int64_t);
+
+ Future<void> writeLEU16(uint16_t);
+ Future<void> writeLEU32(uint32_t);
+ Future<void> writeLEU64(uint64_t);
+ Future<void> writeLEI16(int16_t);
+ Future<void> writeLEI32(int32_t);
+ Future<void> writeLEI64(int64_t);
+
// these need to be changed into Future<>s
template <size_t L>
- ssize_t writeString(const char (&str)[L]) WARN_UNUSED_RESULT;
+ ssize_t writeString(const char (&str)[L]) WARN_UNUSED_RESULT { return writeString(str, L - 1); }
ssize_t writeString(const String & str) WARN_UNUSED_RESULT { return forceWrite(str.to_charp(), str.strlen()); }
ssize_t writeString(const char * str, ssize_t len) WARN_UNUSED_RESULT { return forceWrite(str, len); }
ssize_t forceRead(void * buf, size_t count, Events::BaseEvent * evt = NULL) throw (GeneralException) WARN_UNUSED_RESULT;
@@ -143,10 +157,7 @@ class Handle {
Handle & operator=(const Handle &) = delete;
};
-template <size_t L>
-ssize_t Handle::writeString(const char (&str)[L]) { return writeString(str, L - 1); }
-
-class HPrinter : public Handle {
+ class HPrinter : public Handle {
public:
virtual void close() throw (GeneralException) { }
virtual bool isClosed() { return false; }
@@ -197,7 +208,7 @@ class IO : public IOBase {
class SeekableHandle : public Handle {
public:
SeekableHandle() : m_wOffset(0), m_rOffset(0) { }
- virtual bool canSeek();
+ virtual bool canSeek() { return true; }
virtual void rseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException);
virtual void wseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException);
virtual off64_t rtell() throw (GeneralException);
diff --git a/src/Handle.cc b/src/Handle.cc
index 2002bc4..ef24716 100644
--- a/src/Handle.cc
+++ b/src/Handle.cc
@@ -131,30 +131,35 @@ Balau::Future<uint16_t> Balau::Handle::readU16() {
else
return readLEU16();
}
+
Balau::Future<uint32_t> Balau::Handle::readU32() {
if (m_bigEndianMode)
return readBEU32();
else
return readLEU32();
}
+
Balau::Future<uint64_t> Balau::Handle::readU64() {
if (m_bigEndianMode)
return readBEU64();
else
return readLEU64();
}
+
Balau::Future<int16_t> Balau::Handle::readI16() {
if (m_bigEndianMode)
return readBEI16();
else
return readLEI16();
}
+
Balau::Future<int32_t> Balau::Handle::readI32() {
if (m_bigEndianMode)
return readBEI32();
else
return readLEI32();
}
+
Balau::Future<int64_t> Balau::Handle::readI64() {
if (m_bigEndianMode)
return readBEI64();
@@ -177,25 +182,86 @@ Balau::Future<int32_t> Balau::Handle::readBEI32() { return genericReadBE<int32_
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));
+Balau::Future<void> genericWrite(Balau::IO<Balau::Handle> t, T b) {
size_t c = 0;
return Balau::Future<void>([t, b, c]() mutable {
do {
- ssize_t r = t->write(((uint8_t *) b.get()) + c, sizeof(T));
+ ssize_t r = t->write(((uint8_t *) &b) + c, sizeof(T) - c);
c += r;
} while (c < sizeof(T));
});
}
-Balau::Future<void> Balau::Handle::writeU8 (uint8_t v) { return genericWrite<uint8_t >(this, v); }
-Balau::Future<void> Balau::Handle::writeU16(uint16_t v) { return genericWrite<uint16_t>(this, v); }
-Balau::Future<void> Balau::Handle::writeU32(uint32_t v) { return genericWrite<uint32_t>(this, v); }
-Balau::Future<void> Balau::Handle::writeU64(uint64_t v) { return genericWrite<uint64_t>(this, v); }
-Balau::Future<void> Balau::Handle::writeI8 (int8_t v) { return genericWrite<int8_t >(this, v); }
-Balau::Future<void> Balau::Handle::writeI16(int16_t v) { return genericWrite<int16_t >(this, v); }
-Balau::Future<void> Balau::Handle::writeI32(int32_t v) { return genericWrite<int32_t >(this, v); }
-Balau::Future<void> Balau::Handle::writeI64(int64_t v) { return genericWrite<int64_t >(this, v); }
+template<class T>
+Balau::Future<void> genericWriteBE(Balau::IO<Balau::Handle> t, T v) {
+ size_t c = sizeof(T);
+ return Balau::Future<void>([t, v, c]() mutable {
+ do {
+ ssize_t r = t->write(((uint8_t *) &v) + c - 1, 1);
+ AAssert(r >= 0, "genericWriteBE got an error: %zi", r);
+ c -= r;
+ } while (c && !t->isEOF());
+ });
+}
+
+Balau::Future<void> Balau::Handle::writeU8(uint8_t v) { return genericWrite<uint8_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeI8(int8_t v) { return genericWrite<int8_t >(this, v); }
+
+Balau::Future<void> Balau::Handle::writeU16(uint16_t v) {
+ if (m_bigEndianMode)
+ return writeBEU16(v);
+ else
+ return writeLEU16(v);
+}
+
+Balau::Future<void> Balau::Handle::writeU32(uint32_t v) {
+ if (m_bigEndianMode)
+ return writeBEU32(v);
+ else
+ return writeLEU32(v);
+}
+
+Balau::Future<void> Balau::Handle::writeU64(uint64_t v) {
+ if (m_bigEndianMode)
+ return writeBEU64(v);
+ else
+ return writeLEU64(v);
+}
+
+Balau::Future<void> Balau::Handle::writeI16(int16_t v) {
+ if (m_bigEndianMode)
+ return writeBEI16(v);
+ else
+ return writeLEI16(v);
+}
+
+Balau::Future<void> Balau::Handle::writeI32(int32_t v) {
+ if (m_bigEndianMode)
+ return writeBEI32(v);
+ else
+ return writeLEI32(v);
+}
+
+Balau::Future<void> Balau::Handle::writeI64(int64_t v) {
+ if (m_bigEndianMode)
+ return writeBEI64(v);
+ else
+ return writeLEI64(v);
+}
+
+Balau::Future<void> Balau::Handle::writeLEU16(uint16_t v) { return genericWrite<uint16_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeLEU32(uint32_t v) { return genericWrite<uint32_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeLEU64(uint64_t v) { return genericWrite<uint64_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeLEI16(int16_t v) { return genericWrite<int16_t >(this, v); }
+Balau::Future<void> Balau::Handle::writeLEI32(int32_t v) { return genericWrite<int32_t >(this, v); }
+Balau::Future<void> Balau::Handle::writeLEI64(int64_t v) { return genericWrite<int64_t >(this, v); }
+
+Balau::Future<void> Balau::Handle::writeBEU16(uint16_t v) { return genericWriteBE<uint16_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeBEU32(uint32_t v) { return genericWriteBE<uint32_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeBEU64(uint64_t v) { return genericWriteBE<uint64_t>(this, v); }
+Balau::Future<void> Balau::Handle::writeBEI16(int16_t v) { return genericWriteBE<int16_t >(this, v); }
+Balau::Future<void> Balau::Handle::writeBEI32(int32_t v) { return genericWriteBE<int32_t >(this, v); }
+Balau::Future<void> Balau::Handle::writeBEI64(int64_t v) { return genericWriteBE<int64_t >(this, v); }
void Balau::Handle::rseek(off64_t offset, int whence) throw (GeneralException) {
if (canSeek())
@@ -204,10 +270,6 @@ void Balau::Handle::rseek(off64_t offset, int whence) throw (GeneralException) {
throw GeneralException("Handle can't seek");
}
-void Balau::Handle::wseek(off64_t offset, int whence) throw (GeneralException) {
- rseek(offset, whence);
-}
-
off64_t Balau::Handle::rtell() throw (GeneralException) {
if (canSeek())
throw GeneralException(String("Handle ") + getName() + " can seek, but rtell() not implemented (missing in class " + ClassName(this).c_str() + ")");
@@ -215,12 +277,6 @@ off64_t Balau::Handle::rtell() throw (GeneralException) {
throw GeneralException("Handle can't seek");
}
-off64_t Balau::Handle::wtell() throw (GeneralException) {
- return rtell();
-}
-
-bool Balau::SeekableHandle::canSeek() { return true; }
-
void Balau::SeekableHandle::rseek(off64_t offset, int whence) throw (GeneralException) {
AAssert(canRead() || canWrite(), "Can't use a SeekableHandle with a Handle that can neither read or write...");
off64_t size;