lib/nbt.cpp: Implement writeString function
parent
adc9a7f36b
commit
4f1ad714bd
|
@ -30,8 +30,6 @@
|
|||
#endif
|
||||
|
||||
namespace JavaCompat {
|
||||
//FIXME: contrary to what I said, we need to explicitly pass the data
|
||||
// size because files could have been tampered with or corrupted
|
||||
ErrorOr<tiny_utf8::string> importJavaString(uint8_t data[], uint16_t size) {
|
||||
std::string stdString;
|
||||
uint16_t encodedSize = static_cast<uint16_t>(data[0])<<8 | static_cast<uint16_t>(data[1]);
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <tinyutf8/tinyutf8.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "nbt.h++"
|
||||
#include "error.h++"
|
||||
|
@ -342,8 +343,14 @@ namespace NBT {
|
|||
}
|
||||
}
|
||||
|
||||
//void writeString(std::vector<uint8_t>* destination, <string type> data) {
|
||||
//}
|
||||
void writeString(std::vector<uint8_t>* destination, tiny_utf8::string data) {
|
||||
ErrorOr<std::vector<uint8_t>> exportedString = JavaCompat::exportJavaString(data);
|
||||
if(exportedString.isError){
|
||||
std::cerr << "NBT::helpers::writeString encountered an error: " << (int) exportedString.errorCode << std::endl;
|
||||
std::abort();
|
||||
}
|
||||
*destination = exportedString.value;
|
||||
}
|
||||
|
||||
void writeInt32Array(std::vector<uint8_t>* destination, std::vector<int32_t> data) {
|
||||
writeInt32(destination, data.size());
|
||||
|
|
|
@ -69,7 +69,7 @@ namespace NBT {
|
|||
void writeFloat64(std::vector<uint8_t>* destination, double data);
|
||||
void writeInt8Array(std::vector<uint8_t>* destination, std::vector<int8_t> data);
|
||||
void writeInt8Array(std::vector<uint8_t>* destination, int8_t data[], uint32_t dataSize);
|
||||
//void writeString(std::vector<uint8_t>* destination, <string type> data);
|
||||
void writeString(std::vector<uint8_t>* destination, tiny_utf8::string data);
|
||||
void writeInt32Array(std::vector<uint8_t>* destination, std::vector<int32_t> data);
|
||||
void writeInt32Array(std::vector<uint8_t>* destination, int32_t data[], uint32_t dataSize);
|
||||
void writeInt64Array(std::vector<uint8_t>* destination, std::vector<int64_t> data);
|
||||
|
|
|
@ -495,5 +495,35 @@ int main(){
|
|||
ASSERT(NBT::helper::readString(reinterpret_cast<uint8_t*>(javaStdString.data()), 0xF, 0).errorCode == ErrorCodes::MISMATCHEDSIZE);
|
||||
std::cout << "Passed readString NBT helper test." << std::endl;
|
||||
delete nextChar;
|
||||
|
||||
char* nextChar1 = new char;
|
||||
|
||||
//reading data from the java modified utf8 file
|
||||
std::streampos javaSize1;
|
||||
std::vector<uint8_t> javaStdString1;
|
||||
const char* javaFilePath1 = "./resources/unicode_data/java-style_unicode";
|
||||
std::ifstream javaFile1(javaFilePath1, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
if(javaFile1.is_open()){
|
||||
javaSize1 = javaFile1.tellg();
|
||||
javaFile1.seekg(0, std::ios::beg);
|
||||
|
||||
for (int i=0; i<javaSize1; i++) {
|
||||
javaFile1.read(nextChar1, 1);
|
||||
javaStdString1.push_back(*nextChar1);
|
||||
}
|
||||
|
||||
javaFile1.close();
|
||||
} else {
|
||||
std::cerr << "Failed to open file: " << javaFilePath1 << std::endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
std::vector<uint8_t>* exportedString = new std::vector<uint8_t>();
|
||||
NBT::helper::writeString(exportedString, normalString);
|
||||
ASSERT(javaStdString1 == *exportedString);
|
||||
|
||||
std::string overrunString = std::string(0xFFFFF, '.');
|
||||
NBT::helper::writeString(exportedString, tiny_utf8::string(overrunString));
|
||||
std::cout << "Passed writeString NBT helper test." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue