NBT: add write helpers for float types

BodgeMaster-unfinished
BodgeMaster 2022-07-04 19:43:31 +02:00
parent 8c7be385ae
commit 762ba7f089
2 changed files with 81 additions and 5 deletions

View File

@ -263,13 +263,59 @@ namespace NBT {
}
//FIXME: we just assume that float is a single-precision IEEE754
// floating point number
// floating point number, also endian specific implementation
void writeFloat32(std::vector<uint8_t>* destination, float data) {
float* value = new float;
uint8_t* valueAsBytes = reinterpret_cast<uint8_t*>(value);
*value = data;
#ifdef FOSSVG_BIG_ENDIAN
destination->push_back(*valueAsBytes);
destination->push_back(*(valueAsBytes+1));
destination->push_back(*(valueAsBytes+2));
destination->push_back(*(valueAsBytes+3));
#else
#ifdef FOSSVG_LITTLE_ENDIAN
destination->push_back(*(valueAsBytes+3));
destination->push_back(*(valueAsBytes+2));
destination->push_back(*(valueAsBytes+1));
destination->push_back(*valueAsBytes);
#else
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable."
#endif
#endif
delete value;
}
//FIXME: we just assume that double is a single-precision IEEE754
// floating point number
// floating point number, also endian specific implementation
void writeFloat64(std::vector<uint8_t>* destination, double data) {
double* value = new double;
uint8_t* valueAsBytes = reinterpret_cast<uint8_t*>(value);
*value = data;
#ifdef FOSSVG_BIG_ENDIAN
destination->push_back(*valueAsBytes);
destination->push_back(*(valueAsBytes+1));
destination->push_back(*(valueAsBytes+2));
destination->push_back(*(valueAsBytes+3));
destination->push_back(*(valueAsBytes+4));
destination->push_back(*(valueAsBytes+5));
destination->push_back(*(valueAsBytes+6));
destination->push_back(*(valueAsBytes+7));
#else
#ifdef FOSSVG_LITTLE_ENDIAN
destination->push_back(*(valueAsBytes+7));
destination->push_back(*(valueAsBytes+6));
destination->push_back(*(valueAsBytes+5));
destination->push_back(*(valueAsBytes+4));
destination->push_back(*(valueAsBytes+3));
destination->push_back(*(valueAsBytes+2));
destination->push_back(*(valueAsBytes+1));
destination->push_back(*valueAsBytes);
#else
#error "NBT::helper::writeInt16: An implementation for your endianness is unavailable."
#endif
#endif
delete value;
}
void writeInt8Array(std::vector<uint8_t>* destination, std::vector<int8_t> data) {

View File

@ -164,7 +164,7 @@ int main(){
dereferencedWriteInt64TestResult[7] == (uint8_t) 0xEF
);
std::cout << "Passed writeInt32 NBT helper test" << std::endl;
std::cout << "Passed writeInt64 NBT helper test" << std::endl;
//##################################################################
@ -270,7 +270,20 @@ int main(){
ASSERT(NBT::helper::readFloat32(dataForFloat32Test, dataSize, currentPosition).isError == true);
ASSERT(NBT::helper::readFloat32(dataForFloat32Test, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR);
std::cout << "Passed float32 NBT helper test" << std::endl;
std::cout << "Passed readFloat32 NBT helper test" << std::endl;
std::vector<uint8_t>* writeFloat32TestResult = new std::vector<uint8_t>();
NBT::helper::writeFloat32(writeFloat32TestResult, (float) -63351.46484375f);
std::vector<uint8_t> dereferencedWriteFloat32TestResult = *writeFloat32TestResult;
delete writeFloat32TestResult;
ASSERT(
dereferencedWriteFloat32TestResult[0] == (uint8_t) 0xC7 &&
dereferencedWriteFloat32TestResult[1] == (uint8_t) 0x77 &&
dereferencedWriteFloat32TestResult[2] == (uint8_t) 0x77 &&
dereferencedWriteFloat32TestResult[3] == (uint8_t) 0x77
);
std::cout << "Passed writeFloat32 NBT helper test" << std::endl;
// float64 ["double" in the current implementation :( ] ############
uint8_t dataForFloat64Test[] = {0xC0, 0x34, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00};
@ -288,7 +301,24 @@ int main(){
ASSERT(NBT::helper::readFloat64(dataForFloat64Test, dataSize, currentPosition).isError == true);
ASSERT(NBT::helper::readFloat64(dataForFloat64Test, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR);
std::cout << "Passed float64 NBT helper test" << std::endl;
std::cout << "Passed readFloat64 NBT helper test" << std::endl;
std::vector<uint8_t>* writeFloat64TestResult = new std::vector<uint8_t>();
NBT::helper::writeFloat64(writeFloat64TestResult, (double) -20.015625476837158203125);
std::vector<uint8_t> dereferencedWriteFloat64TestResult = *writeFloat64TestResult;
delete writeFloat64TestResult;
ASSERT(
dereferencedWriteFloat64TestResult[0] == (uint8_t) 0xC0 &&
dereferencedWriteFloat64TestResult[1] == (uint8_t) 0x34 &&
dereferencedWriteFloat64TestResult[2] == (uint8_t) 0x04 &&
dereferencedWriteFloat64TestResult[3] == (uint8_t) 0x00 &&
dereferencedWriteFloat64TestResult[4] == (uint8_t) 0x08 &&
dereferencedWriteFloat64TestResult[5] == (uint8_t) 0x00 &&
dereferencedWriteFloat64TestResult[6] == (uint8_t) 0x00 &&
dereferencedWriteFloat64TestResult[7] == (uint8_t) 0x00
);
std::cout << "Passed writeInt64 NBT helper test" << std::endl;
return 0;
}