]> Dogcows Code - chaz/yoink/blobdiff - src/stlplus/persistence/persistent_float.cpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / persistence / persistent_float.cpp
diff --git a/src/stlplus/persistence/persistent_float.cpp b/src/stlplus/persistence/persistent_float.cpp
new file mode 100644 (file)
index 0000000..59a6678
--- /dev/null
@@ -0,0 +1,86 @@
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+//   Author:    Andy Rushton\r
+//   Copyright: (c) Southampton University 1999-2004\r
+//              (c) Andy Rushton           2004-2009\r
+//   License:   BSD License, see ../docs/license.html\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+#include "persistent_float.hpp"\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+// Macro for mapping either endian data onto little-endian addressing to make\r
+// my life easier in writing this code! I think better in little-endian mode\r
+// so the macro does nothing in that mode but maps little-endian onto\r
+// big-endian addressing in big-endian mode\r
+// TODO - make this compile-time configurable\r
+\r
+#define INDEX(index) ((context.little_endian()) ? (index) : ((bytes) - (index) - 1))\r
+\r
+/////////////////////////////////////////////////////////////////////\r
+// floating point types\r
+// format: {size}{byte}*size\r
+// ordering is msB first\r
+\r
+// this uses a similar mechanism to integer dumps. However, it is not clear how\r
+// the big-endian and little-endian argument applies to multi-word data so\r
+// this may need reworking by splitting into words and then bytes.\r
+\r
+namespace stlplus\r
+{\r
+\r
+  static void dump_float(stlplus::dump_context& context, unsigned bytes, unsigned char* data)\r
+    throw(stlplus::persistent_dump_failed)\r
+  {\r
+    unsigned i = bytes;\r
+    // put the size\r
+    context.put((unsigned char)i);\r
+    // and put the bytes\r
+    while(i--)\r
+      context.put(data[INDEX(i)]);\r
+  }\r
+\r
+  static void restore_float(stlplus::restore_context& context, unsigned bytes, unsigned char* data)\r
+    throw(stlplus::persistent_restore_failed)\r
+  {\r
+    // get the dumped size from the file\r
+    unsigned dumped_bytes = (unsigned)context.get();\r
+    // get the bytes from the file\r
+    unsigned i = dumped_bytes;\r
+    while(i--)\r
+    {\r
+      int ch = context.get();\r
+      if (i < bytes)\r
+        data[INDEX(i)] = (unsigned char)ch;\r
+    }\r
+    // however, if the dumped size was different I don't know how to map the formats, so give an error\r
+    if (dumped_bytes != bytes)\r
+      throw stlplus::persistent_restore_failed(std::string("size mismatch"));\r
+  }\r
+\r
+} // end namespace stlplus\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // exported functions which simply call the low-level byte-dump and byte-restore routines above\r
+\r
+void stlplus::dump_float(stlplus::dump_context& context, const float& data) throw(stlplus::persistent_dump_failed)\r
+{\r
+  stlplus::dump_float(context, sizeof(float), (unsigned char*)&data);\r
+}\r
+\r
+void stlplus::restore_float(restore_context& context, float& data) throw(stlplus::persistent_restore_failed)\r
+{\r
+  stlplus::restore_float(context, sizeof(float), (unsigned char*)&data);\r
+}\r
+\r
+void stlplus::dump_double(stlplus::dump_context& context, const double& data) throw(stlplus::persistent_dump_failed)\r
+{\r
+  stlplus::dump_float(context, sizeof(double), (unsigned char*)&data);\r
+}\r
+\r
+void stlplus::restore_double(restore_context& context, double& data) throw(stlplus::persistent_restore_failed)\r
+{\r
+  stlplus::restore_float(context, sizeof(double), (unsigned char*)&data);\r
+}\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
This page took 0.025161 seconds and 4 git commands to generate.