-////////////////////////////////////////////////////////////////////////////////\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_int.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 so it's more efficient\r
-\r
-#define INDEX(index) ((context.little_endian()) ? (index) : ((bytes) - (index) - 1))\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// Integer types\r
-// format: {size}{byte}*size\r
-// size can be zero!\r
-//\r
-// A major problem is that integer types may be different sizes on different\r
-// machines or even with different compilers on the same machine (though I\r
-// haven't come across that yet). Neither the C nor the C++ standards specify\r
-// the size of integer types. Dumping an int on one machine might dump 16\r
-// bits. Restoring it on another machine might try to restore 32 bits. These\r
-// functions must therefore handle different type sizes. It does this by\r
-// writing the size to the file as well as the data, so the restore can\r
-// therefore know how many bytes to restore independent of the type size.\r
-//\r
-// In fact, the standard does not even specify the size of char (true! And\r
-// mind-numbingly stupid...). However, to be able to do anything at all, I've\r
-// had to assume that a char is 1 byte.\r
-\r
-static void dump_unsigned(stlplus::dump_context& context, unsigned bytes, unsigned char* data)\r
- throw(stlplus::persistent_dump_failed)\r
-{\r
- // first skip zero bytes - this may reduce the data to zero bytes long\r
- unsigned i = bytes;\r
- while(i >= 1 && data[INDEX(i-1)] == 0)\r
- i--;\r
- // put the remaining 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 dump_signed(stlplus::dump_context& context, unsigned bytes, unsigned char* data)\r
- throw(stlplus::persistent_dump_failed)\r
-{\r
- // first skip all-zero or all-one bytes but only if doing so does not change the sign\r
- unsigned i = bytes;\r
- if (data[INDEX(i-1)] < 128)\r
- {\r
- // positive number so discard leading zeros but only if the following byte is positive\r
- while(i >= 2 && data[INDEX(i-1)] == 0 && data[INDEX(i-2)] < 128)\r
- i--;\r
- }\r
- else\r
- {\r
- // negative number so discard leading ones but only if the following byte is negative\r
- while(i >= 2 && data[INDEX(i-1)] == 255 && data[INDEX(i-2)] >= 128)\r
- i--;\r
- }\r
- // put the remaining 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_unsigned(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
- // zero fill any empty space\r
- unsigned i = bytes;\r
- for (; i > dumped_bytes; i--)\r
- data[INDEX(i-1)] = 0;\r
- // restore the dumped bytes but discard any that don't fit\r
- while(i--)\r
- {\r
- int ch = context.get();\r
- if (i < bytes)\r
- data[INDEX(i)] = (unsigned char)ch;\r
- else\r
- throw stlplus::persistent_restore_failed(std::string("integer overflow"));\r
- }\r
-}\r
-\r
-static void restore_signed(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
- // restore the dumped bytes but discard any that don't fit\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
- else\r
- throw stlplus::persistent_restore_failed(std::string("integer overflow"));\r
- }\r
- // sign extend if the dumped integer was smaller\r
- if (dumped_bytes < bytes)\r
- {\r
- if (data[INDEX(dumped_bytes-1)] < 128)\r
- {\r
- // positive so zero fill\r
- for (i = dumped_bytes; i < bytes; i++)\r
- data[INDEX(i)] = 0;\r
- }\r
- else\r
- {\r
- // negative so one fill\r
- for (i = dumped_bytes; i < bytes; i++)\r
- data[INDEX(i)] = 0xff;\r
- }\r
- }\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r
-// exported functions\r
-\r
-// char is dumped and restored as an unsigned char because the signedness of char is not defined and can vary\r
-void stlplus::dump_char(stlplus::dump_context& context, const char& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- context.put((unsigned char)data);\r
-}\r
-\r
-void stlplus::restore_char(restore_context& context, char& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- data = (char)(unsigned char)context.get();\r
-}\r
-\r
-void stlplus::dump_signed_char(stlplus::dump_context& context, const signed char& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- context.put((unsigned char)data);\r
-}\r
-\r
-void stlplus::restore_signed_char(restore_context& context, signed char& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- data = (signed char)(unsigned char)context.get();\r
-}\r
-\r
-void stlplus::dump_unsigned_char(stlplus::dump_context& context, const unsigned char& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- context.put((unsigned char)data);\r
-}\r
-\r
-void stlplus::restore_unsigned_char(restore_context& context, unsigned char& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- data = (signed char)(unsigned char)context.get();\r
-}\r
-\r
-void stlplus::dump_short(stlplus::dump_context& context, const short& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- ::dump_signed(context, sizeof(short), (unsigned char*)&data);\r
-}\r
-\r
-void stlplus::restore_short(restore_context& context, short& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- ::restore_signed(context, sizeof(short),(unsigned char*)&data);\r
-}\r
-\r
-void stlplus::dump_unsigned_short(stlplus::dump_context& context, const unsigned short& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- ::dump_unsigned(context, sizeof(unsigned short), (unsigned char*)&data);\r
-}\r
-\r
-void stlplus::restore_unsigned_short(restore_context& context, unsigned short& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- ::restore_unsigned(context, sizeof(unsigned short),(unsigned char*)&data);\r
-}\r
-\r
-void stlplus::dump_int(stlplus::dump_context& context, const int& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- ::dump_signed(context, sizeof(int), (unsigned char*)&data);\r
-}\r
-\r
-void stlplus::restore_int(restore_context& context, int& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- ::restore_signed(context, sizeof(int),(unsigned char*)&data);\r
-}\r
-\r
-void stlplus::dump_unsigned(stlplus::dump_context& context, const unsigned& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- ::dump_unsigned(context, sizeof(unsigned), (unsigned char*)&data);\r
-}\r
-\r
-void stlplus::restore_unsigned(restore_context& context, unsigned& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- ::restore_unsigned(context, sizeof(unsigned),(unsigned char*)&data);\r
-}\r
-\r
-void stlplus::dump_long(stlplus::dump_context& context, const long& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- ::dump_signed(context, sizeof(long), (unsigned char*)&data);\r
-}\r
-\r
-void stlplus::restore_long(restore_context& context, long& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- ::restore_signed(context, sizeof(long),(unsigned char*)&data);\r
-}\r
-\r
-void stlplus::dump_unsigned_long(stlplus::dump_context& context, const unsigned long& data) throw(stlplus::persistent_dump_failed)\r
-{\r
- ::dump_unsigned(context, sizeof(unsigned long), (unsigned char*)&data);\r
-}\r
-\r
-void stlplus::restore_unsigned_long(restore_context& context, unsigned long& data) throw(stlplus::persistent_restore_failed)\r
-{\r
- ::restore_unsigned(context, sizeof(unsigned long),(unsigned char*)&data);\r
-}\r
-\r
-////////////////////////////////////////////////////////////////////////////////\r