diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index 3ae13aa..fc3473f 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -186,9 +186,26 @@ namespace NBT { } void writeInt8(std::vector* destination, int8_t data) { + destination->push_back((uint8_t) data); } + //FIXME: endian dependent implementation void writeInt16(std::vector* destination, int16_t data) { + int16_t* value = new int16_t; + uint8_t* valueAsBytes = reinterpret_cast(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* destination, int32_t data) { diff --git a/src/test/nbt_helpers.cpp b/src/test/nbt_helpers.cpp index 2b7de60..bac01b3 100644 --- a/src/test/nbt_helpers.cpp +++ b/src/test/nbt_helpers.cpp @@ -48,6 +48,14 @@ int main(){ std::cout << "Passed readInt8 NBT helper test" << std::endl; + std::vector* writeInt8TestResult = new std::vector(); + NBT::helper::writeInt8(writeInt8TestResult, (int8_t) 8); + std::vector dereferencedWriteInt8TestResult = *writeInt8TestResult; + delete writeInt8TestResult; + ASSERT(dereferencedWriteInt8TestResult.back() == (uint8_t) 8); + + std::cout << "Passed writeInt8 NBT helper test" << std::endl; + // int16 ########################################################### // read successfully currentPosition = 5; @@ -72,6 +80,14 @@ int main(){ std::cout << "Passed readInt16 NBT helper test" << std::endl; + std::vector* writeInt16TestResult = new std::vector(); + NBT::helper::writeInt16(writeInt16TestResult, (int16_t) 0xABCD); + std::vector 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 ########################################################### // read successfully currentPosition = 5;