From 2e5344109da2abc59a7882521ea8378ca01c174c Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Sun, 10 Aug 2014 16:16:35 -0700 Subject: Properly adding little and big endian versions of writes, plus a few other tweaks. --- includes/Handle.h | 29 +++++++++++----- src/Handle.cc | 98 +++++++++++++++++++++++++++++++++++++++++++------------ 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 readLEI32(); Future readLEI64(); - Future writeU8(uint8_t); + Future writeU8 (uint8_t); Future writeU16(uint16_t); Future writeU32(uint32_t); Future writeU64(uint64_t); @@ -110,9 +110,23 @@ class Handle { Future writeI32(int32_t); Future writeI64(int64_t); + Future writeBEU16(uint16_t); + Future writeBEU32(uint32_t); + Future writeBEU64(uint64_t); + Future writeBEI16(int16_t); + Future writeBEI32(int32_t); + Future writeBEI64(int64_t); + + Future writeLEU16(uint16_t); + Future writeLEU32(uint32_t); + Future writeLEU64(uint64_t); + Future writeLEI16(int16_t); + Future writeLEI32(int32_t); + Future writeLEI64(int64_t); + // these need to be changed into Future<>s template - 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 -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 Balau::Handle::readU16() { else return readLEU16(); } + Balau::Future Balau::Handle::readU32() { if (m_bigEndianMode) return readBEU32(); else return readLEU32(); } + Balau::Future Balau::Handle::readU64() { if (m_bigEndianMode) return readBEU64(); else return readLEU64(); } + Balau::Future Balau::Handle::readI16() { if (m_bigEndianMode) return readBEI16(); else return readLEI16(); } + Balau::Future Balau::Handle::readI32() { if (m_bigEndianMode) return readBEI32(); else return readLEI32(); } + Balau::Future Balau::Handle::readI64() { if (m_bigEndianMode) return readBEI64(); @@ -177,25 +182,86 @@ Balau::Future Balau::Handle::readBEI32() { return genericReadBE Balau::Handle::readBEI64() { return genericReadBE (this); } template -Balau::Future genericWrite(Balau::IO t, T val) { - std::shared_ptr b(new T(val)); +Balau::Future genericWrite(Balau::IO t, T b) { size_t c = 0; return Balau::Future([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 Balau::Handle::writeU8 (uint8_t v) { return genericWrite(this, v); } -Balau::Future Balau::Handle::writeU16(uint16_t v) { return genericWrite(this, v); } -Balau::Future Balau::Handle::writeU32(uint32_t v) { return genericWrite(this, v); } -Balau::Future Balau::Handle::writeU64(uint64_t v) { return genericWrite(this, v); } -Balau::Future Balau::Handle::writeI8 (int8_t v) { return genericWrite(this, v); } -Balau::Future Balau::Handle::writeI16(int16_t v) { return genericWrite(this, v); } -Balau::Future Balau::Handle::writeI32(int32_t v) { return genericWrite(this, v); } -Balau::Future Balau::Handle::writeI64(int64_t v) { return genericWrite(this, v); } +template +Balau::Future genericWriteBE(Balau::IO t, T v) { + size_t c = sizeof(T); + return Balau::Future([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 Balau::Handle::writeU8(uint8_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeI8(int8_t v) { return genericWrite(this, v); } + +Balau::Future Balau::Handle::writeU16(uint16_t v) { + if (m_bigEndianMode) + return writeBEU16(v); + else + return writeLEU16(v); +} + +Balau::Future Balau::Handle::writeU32(uint32_t v) { + if (m_bigEndianMode) + return writeBEU32(v); + else + return writeLEU32(v); +} + +Balau::Future Balau::Handle::writeU64(uint64_t v) { + if (m_bigEndianMode) + return writeBEU64(v); + else + return writeLEU64(v); +} + +Balau::Future Balau::Handle::writeI16(int16_t v) { + if (m_bigEndianMode) + return writeBEI16(v); + else + return writeLEI16(v); +} + +Balau::Future Balau::Handle::writeI32(int32_t v) { + if (m_bigEndianMode) + return writeBEI32(v); + else + return writeLEI32(v); +} + +Balau::Future Balau::Handle::writeI64(int64_t v) { + if (m_bigEndianMode) + return writeBEI64(v); + else + return writeLEI64(v); +} + +Balau::Future Balau::Handle::writeLEU16(uint16_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeLEU32(uint32_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeLEU64(uint64_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeLEI16(int16_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeLEI32(int32_t v) { return genericWrite(this, v); } +Balau::Future Balau::Handle::writeLEI64(int64_t v) { return genericWrite(this, v); } + +Balau::Future Balau::Handle::writeBEU16(uint16_t v) { return genericWriteBE(this, v); } +Balau::Future Balau::Handle::writeBEU32(uint32_t v) { return genericWriteBE(this, v); } +Balau::Future Balau::Handle::writeBEU64(uint64_t v) { return genericWriteBE(this, v); } +Balau::Future Balau::Handle::writeBEI16(int16_t v) { return genericWriteBE(this, v); } +Balau::Future Balau::Handle::writeBEI32(int32_t v) { return genericWriteBE(this, v); } +Balau::Future Balau::Handle::writeBEI64(int64_t v) { return genericWriteBE(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; -- cgit v1.2.3