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 --- include/Handle.h | 6 +++++- lib/Handle.cc | 47 ++++++++++++++++++++++++++++++++++++++++++++++- lib/LuaHandle.cc | 32 ++++++++++++++++++++++++++++++-- lib/Main.cc | 4 +++- 4 files changed, 84 insertions(+), 5 deletions(-) diff --git a/include/Handle.h b/include/Handle.h index 26c1eda..ea21c6e 100644 --- a/include/Handle.h +++ b/include/Handle.h @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: Handle.h,v 1.39 2008-05-13 06:59:10 pixel Exp $ */ +/* $Id: Handle.h,v 1.40 2008-08-11 16:23:52 pixel Exp $ */ #ifndef __HANDLE_H__ #define __HANDLE_H__ @@ -39,9 +39,13 @@ class Handle : public Base { Uint8 readU8(); Uint16 readU16(); Uint32 readU32(); + float readFloat(); + double readDouble(); void writeU8(Uint8); void writeU16(Uint16); void writeU32(Uint32); + void writeFloat(float); + void writeDouble(double); void copyto(Handle *, ssize_t = -1); void copyfrom(Handle *, ssize_t = -1); bool IsClosed(void) const; 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); } diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc index 22c5036..34d0412 100644 --- a/lib/LuaHandle.cc +++ b/lib/LuaHandle.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: LuaHandle.cc,v 1.26 2008-05-13 06:59:10 pixel Exp $ */ +/* $Id: LuaHandle.cc,v 1.27 2008-08-11 16:23:52 pixel Exp $ */ #include "LuaHandle.h" @@ -36,11 +36,15 @@ class sLuaHandle : public Base { static int readU8(lua_State * L); static int readU16(lua_State * L); static int readU32(lua_State * L); + static int readFloat(lua_State * L); + static int readDouble(lua_State * L); static int write(lua_State * L); static int writestring(lua_State * L); static int writeU8(lua_State * L); static int writeU16(lua_State * L); static int writeU32(lua_State * L); + static int writeFloat(lua_State * L); + static int writeDouble(lua_State * L); static int copyfrom(lua_State * L); static int copyto(lua_State * L); static int isclosed(lua_State * L); @@ -76,7 +80,7 @@ class sLuaHandle : public Base { static int getcaps(lua_State * L, int); static int action(lua_State * L, int); enum { - U8, U16, U32 + U8, U16, U32, R_FLOAT, R_DOUBLE, }; enum { from, to @@ -215,6 +219,14 @@ int sLuaHandle::readU32(lua_State * L) { return read(L, U32); } +int sLuaHandle::readFloat(lua_State * L) { + return read(L, R_FLOAT); +} + +int sLuaHandle::readDouble(lua_State * L) { + return read(L, R_DOUBLE); +} + int sLuaHandle::write(lua_State * __L) { Lua * L = Lua::find(__L); int n = L->gettop(), i; @@ -281,6 +293,14 @@ int sLuaHandle::writeU32(lua_State * L) { return write(L, U32); } +int sLuaHandle::writeFloat(lua_State * L) { + return write(L, R_FLOAT); +} + +int sLuaHandle::writeDouble(lua_State * L) { + return write(L, R_DOUBLE); +} + int sLuaHandle::read(lua_State * __L, int t) { Lua * L = Lua::find(__L); int n = L->gettop(); @@ -297,6 +317,8 @@ int sLuaHandle::read(lua_State * __L, int t) { case U8: r = h->readU8(); break; case U16: r = h->readU16(); break; case U32: r = h->readU32(); break; + case R_FLOAT: r = h->readFloat(); break; + case R_DOUBLE: r = h->readDouble(); break; } L->push(r); @@ -321,6 +343,8 @@ int sLuaHandle::write(lua_State * __L, int t) { case U8: h->writeU8(r); break; case U16: h->writeU16(r); break; case U32: h->writeU32(r); break; + case R_FLOAT: h->writeFloat(r); break; + case R_DOUBLE: h->writeDouble(r); break; } return 0; @@ -728,9 +752,13 @@ void LuaHandle::pushmembers(Lua * L) { pushit(L, "readU8", sLuaHandle::readU8); pushit(L, "readU16", sLuaHandle::readU16); pushit(L, "readU32", sLuaHandle::readU32); + pushit(L, "readFloat", sLuaHandle::readFloat); + pushit(L, "readDouble", sLuaHandle::readDouble); pushit(L, "writeU8", sLuaHandle::writeU8); pushit(L, "writeU16", sLuaHandle::writeU16); pushit(L, "writeU32", sLuaHandle::writeU32); + pushit(L, "writeFloat", sLuaHandle::writeFloat); + pushit(L, "writeDouble", sLuaHandle::writeDouble); pushit(L, "copyfrom", sLuaHandle::copyfrom); pushit(L, "copyto", sLuaHandle::copyto); diff --git a/lib/Main.cc b/lib/Main.cc index 2ab7263..9299d95 100644 --- a/lib/Main.cc +++ b/lib/Main.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: Main.cc,v 1.15 2007-05-30 11:57:10 pixel Exp $ */ +/* $Id: Main.cc,v 1.16 2008-08-11 16:23:52 pixel Exp $ */ #ifdef _WIN32 #include @@ -65,6 +65,8 @@ void Main::sanity_checks() throw (GeneralException) { size_assert(Byte, 1); size_assert(Word, 2); size_assert(DWord, 4); + size_assert(float, 4); + size_assert(double, 8); } int Main::truemain(Main * Application, int argc, char ** argv, char ** enve) { -- cgit v1.2.3