From cd968704b4f0db7029953fe1d52c2f3070077639 Mon Sep 17 00:00:00 2001 From: pixel Date: Mon, 11 Aug 2008 16:23:52 +0000 Subject: Adding the ability to read / write floats and doubles --- lib/Handle.cc | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'lib/Handle.cc') diff --git a/lib/Handle.cc b/lib/Handle.cc index 38fa11c..cd2ec16 100644 --- a/lib/Handle.cc +++ b/lib/Handle.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: Handle.cc,v 1.84 2008-07-22 14:10:09 pixel Exp $ */ +/* $Id: Handle.cc,v 1.85 2008-08-11 16:23:52 pixel Exp $ */ #include #include @@ -513,6 +513,34 @@ Uint32 Handle::readU32() { #endif } +float Handle::readFloat() { +#ifdef WORDS_BIGENDIAN + Uint32 t; + read(&t, 4); + t = bswap_32(t); + return *((float *)&t); +#else + float r; + read(&r, 4); + return r; +#endif +} + +double Handle::readDouble() { +#ifdef WORDS_BIGENDIAN + Uint32 t[3]; + read(t, 8); + t[2] = bswap_32(t[0]); + t[0] = bswap_32(t[1]); + t[1] = t[2]; + return *((double *)t); +#else + double r; + read(&r, 8); + return r; +#endif +} + void Handle::writeU8(Uint8 v) { write(&v, 1); } @@ -535,6 +563,23 @@ void Handle::writeU32(Uint32 v) { #endif } +void Handle::writeFloat(float v) { +#ifdef WORDS_BIGENDIAN + writeU32(*((Uint32 *)&v)); +#else + write(&v, 4); +#endif +} + +void Handle::writeDouble(double v) { +#ifdef WORDS_BIGENDIAN + writeU32(*(((Uint32 *)&v) + 1)); + writeU32(*(((Uint32 *)&v))); +#else + write(&v, 8); +#endif +} + void Handle::copyto(Handle * dest, ssize_t s) { copy(this, dest, s); } -- cgit v1.2.3