SWORD25: Removed custom endianness code in persistence code

This is first step towards making saves portable. Binary footprint
left intact, so the saves are compatible.
This commit is contained in:
Eugene Sandulenko 2011-07-10 16:59:13 +03:00
parent 1c711da8fc
commit 18cbb63cba
4 changed files with 19 additions and 64 deletions

View File

@ -55,8 +55,8 @@ void InputPersistenceBlock::read(int16 &value) {
void InputPersistenceBlock::read(signed int &value) {
if (checkMarker(SINT_MARKER)) {
rawRead(&value, sizeof(signed int));
value = convertEndianessFromStorageToSystem(value);
value = (int32)READ_LE_UINT32(_iter);
_iter += 4;
} else {
value = 0;
}
@ -64,8 +64,8 @@ void InputPersistenceBlock::read(signed int &value) {
void InputPersistenceBlock::read(uint &value) {
if (checkMarker(UINT_MARKER)) {
rawRead(&value, sizeof(uint));
value = convertEndianessFromStorageToSystem(value);
value = READ_LE_UINT32(_iter);
_iter += 4;
} else {
value = 0;
}
@ -73,8 +73,10 @@ void InputPersistenceBlock::read(uint &value) {
void InputPersistenceBlock::read(float &value) {
if (checkMarker(FLOAT_MARKER)) {
rawRead(&value, sizeof(float));
value = convertEndianessFromStorageToSystem(value);
uint32 tmp[1];
tmp[0] = READ_LE_UINT32(_iter);
value = ((float *)tmp)[0];
_iter += 4;
} else {
value = 0.0f;
}
@ -82,12 +84,11 @@ void InputPersistenceBlock::read(float &value) {
void InputPersistenceBlock::read(bool &value) {
if (checkMarker(BOOL_MARKER)) {
uint uintBool;
rawRead(&uintBool, sizeof(float));
uintBool = convertEndianessFromStorageToSystem(uintBool);
uint uintBool = READ_LE_UINT32(_iter);
_iter += 4;
value = uintBool == 0 ? false : true;
} else {
value = 0.0f;
value = false;
}
}
@ -117,13 +118,6 @@ void InputPersistenceBlock::readByteArray(Common::Array<byte> &value) {
}
}
void InputPersistenceBlock::rawRead(void *destPtr, size_t size) {
if (checkBlockSize(size)) {
memcpy(destPtr, &*_iter, size);
_iter += size;
}
}
bool InputPersistenceBlock::checkBlockSize(int size) {
if (_data.end() - _iter >= size) {
return true;

View File

@ -69,7 +69,6 @@ public:
private:
bool checkMarker(byte marker);
bool checkBlockSize(int size);
void rawRead(void *destPtr, size_t size);
Common::Array<byte> _data;
Common::Array<byte>::const_iterator _iter;

View File

@ -43,19 +43,23 @@ OutputPersistenceBlock::OutputPersistenceBlock() {
void OutputPersistenceBlock::write(signed int value) {
writeMarker(SINT_MARKER);
value = convertEndianessFromSystemToStorage(value);
value = TO_LE_32(value);
rawWrite(&value, sizeof(value));
}
void OutputPersistenceBlock::write(uint value) {
writeMarker(UINT_MARKER);
value = convertEndianessFromSystemToStorage(value);
value = TO_LE_32(value);
rawWrite(&value, sizeof(value));
}
void OutputPersistenceBlock::write(float value) {
writeMarker(FLOAT_MARKER);
value = convertEndianessFromSystemToStorage(value);
uint32 tmp[1];
((float *)tmp)[0] = value;
tmp[0] = TO_LE_32(tmp[0]);
rawWrite(&value, sizeof(value));
}
@ -63,7 +67,7 @@ void OutputPersistenceBlock::write(bool value) {
writeMarker(BOOL_MARKER);
uint uintBool = value ? 1 : 0;
uintBool = convertEndianessFromSystemToStorage(uintBool);
uintBool = TO_LE_32(uintBool);
rawWrite(&uintBool, sizeof(uintBool));
}

View File

@ -64,48 +64,6 @@ protected:
BLOCK_MARKER
};
// -----------------------------------------------------------------------------
// Endianess Conversions
// -----------------------------------------------------------------------------
//
// Everything is stored in Little Endian
// Big Endian Systems will need to be byte swapped during both saving and reading of saved values
//
template<typename T>
static T convertEndianessFromSystemToStorage(T value) {
if (isBigEndian())
reverseByteOrder(&value);
return value;
}
template<typename T>
static T convertEndianessFromStorageToSystem(T value) {
if (isBigEndian())
reverseByteOrder(&value);
return value;
}
private:
static bool isBigEndian() {
uint dummy = 1;
byte *dummyPtr = reinterpret_cast<byte *>(&dummy);
return dummyPtr[0] == 0;
}
template<typename T>
static void swap(T &one, T &two) {
T temp = one;
one = two;
two = temp;
}
static void reverseByteOrder(void *ptr) {
// Reverses the byte order of the 32-bit word pointed to by Ptr
byte *charPtr = static_cast<byte *>(ptr);
swap(charPtr[0], charPtr[3]);
swap(charPtr[1], charPtr[2]);
}
};
#define CTASSERT(ex) typedef char ctassert_type[(ex) ? 1 : -1]