NBT: implement write helpers for int8 and int16

BodgeMaster-unfinished
BodgeMaster 2022-07-04 18:03:40 +02:00
parent 044593e081
commit 6d62d995df
2 changed files with 33 additions and 0 deletions

View File

@ -186,9 +186,26 @@ namespace NBT {
} }
void writeInt8(std::vector<uint8_t>* destination, int8_t data) { void writeInt8(std::vector<uint8_t>* destination, int8_t data) {
destination->push_back((uint8_t) data);
} }
//FIXME: endian dependent implementation
void writeInt16(std::vector<uint8_t>* destination, int16_t data) { void writeInt16(std::vector<uint8_t>* destination, int16_t data) {
int16_t* value = new int16_t;
uint8_t* valueAsBytes = reinterpret_cast<uint8_t*>(value);
*value = data;
#ifdef FOSSVG_BIG_ENDIAN
destination->push_back(*valueAsBytes);
destination->push_back(*(valueAsBytes+1));
#else
#ifdef FOSSVG_LITTLE_ENDIAN
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 writeInt32(std::vector<uint8_t>* destination, int32_t data) { void writeInt32(std::vector<uint8_t>* destination, int32_t data) {

View File

@ -48,6 +48,14 @@ int main(){
std::cout << "Passed readInt8 NBT helper test" << std::endl; std::cout << "Passed readInt8 NBT helper test" << std::endl;
std::vector<uint8_t>* writeInt8TestResult = new std::vector<uint8_t>();
NBT::helper::writeInt8(writeInt8TestResult, (int8_t) 8);
std::vector<uint8_t> dereferencedWriteInt8TestResult = *writeInt8TestResult;
delete writeInt8TestResult;
ASSERT(dereferencedWriteInt8TestResult.back() == (uint8_t) 8);
std::cout << "Passed writeInt8 NBT helper test" << std::endl;
// int16 ########################################################### // int16 ###########################################################
// read successfully // read successfully
currentPosition = 5; currentPosition = 5;
@ -72,6 +80,14 @@ int main(){
std::cout << "Passed readInt16 NBT helper test" << std::endl; std::cout << "Passed readInt16 NBT helper test" << std::endl;
std::vector<uint8_t>* writeInt16TestResult = new std::vector<uint8_t>();
NBT::helper::writeInt16(writeInt16TestResult, (int16_t) 0xABCD);
std::vector<uint8_t> dereferencedWriteInt16TestResult = *writeInt16TestResult;
delete writeInt16TestResult;
ASSERT(dereferencedWriteInt16TestResult[0] == (uint8_t) 0xAB && dereferencedWriteInt16TestResult[1] == (uint8_t) 0xCD);
std::cout << "Passed writeInt8 NBT helper test" << std::endl;
// int32 ########################################################### // int32 ###########################################################
// read successfully // read successfully
currentPosition = 5; currentPosition = 5;