diff options
author | pixel <pixel> | 2008-08-11 16:23:52 +0000 |
---|---|---|
committer | pixel <pixel> | 2008-08-11 16:23:52 +0000 |
commit | cd968704b4f0db7029953fe1d52c2f3070077639 (patch) | |
tree | 153d5ac619813b37e37aa22db616b16893521934 /lib/Handle.cc | |
parent | bc0b635691223bdcbe0e58f14b9fef3da1406e40 (diff) |
Adding the ability to read / write floats and doubles
Diffstat (limited to 'lib/Handle.cc')
-rw-r--r-- | lib/Handle.cc | 47 |
1 files changed, 46 insertions, 1 deletions
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 <stdio.h> #include <string.h> @@ -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); } |