flatbuffers::Offset<grl::flatbuffer::KUKAiiwaStates> kukaStates = grl::toFlatBuffer(fbb, kukaiiwaStateVec);
grl::flatbuffer::FinishKUKAiiwaStatesBuffer(fbb, kukaStates);
uint8_t *buf = fbb.GetBufferPointer();
std::size_t bufsize = fbb.GetSize();
for (flatbuffers::uoffset_t i = 0; i < bufsize; i++) {
printf("%d ", buf[i]);
}
flatbuffers::Verifier verifier(buf, bufsize);
std::string binary_file_path = "Kuka_test_binary.iiwa";
std::string json_file_path = "kuka_test_text.json";
OK = flatbuffers::SaveFile(binary_file_path.c_str(), reinterpret_cast<const char *>(buf), bufsize, true);
// Get the current working directory
std::string fbs_filename("KUKAiiwa.fbs");
std::cout << "fbs exist? " << flatbuffers::FileExists(fbs_filename.c_str()) << std::endl;
flatbuffers::Parser parser;
std::vector<std::string> includePaths = std::vector<std::string >();
std::string current_working_dir;
char buff[2048];
/// Get the current working directory
getcwd(buff, 2048);
current_working_dir = std::string(buff);
std::cout << "The current working dir: " << current_working_dir << std::endl;
includePaths.push_back(current_working_dir);
std::string schemafile;
flatbuffers::LoadFile(fbs_filename.c_str(), false, &schemafile);
std::cout<<"Schema File: " << std::endl << schemafile << std::endl;
// parse fbs schema, so we can use it to parse the data after
// create a list of char* pointers so we can call Parse
std::vector<const char *> include_directories;
for(int i = 0; i < includePaths.size(); i++){
include_directories.push_back(includePaths[i].c_str());
}
include_directories.push_back(nullptr);
OK = OK && parser.Parse(schemafile.c_str(), &include_directories[0]);
std::string jsongen;
// now generate text from the flatbuffer binary
OK = OK && flatbuffers::GenerateText(parser, buf, &jsongen);
std::cout << "json string :" << jsongen.size() << std::endl;
std::cout << jsongen.c_str() << std::endl;
// Write the data get from flatbuffer binary to json file on disk.
std::cout << "buffer :" << charP_size(reinterpret_cast<const char *>(buf)) << std::endl;
std::ofstream out(json_file_path);
out << jsongen.c_str();
out.close();
std::cout << "Save json file correctly? " << OK << " Buffer size saved to binary file: " << bufsize << std::endl;
std::cout << "End of the program" << std::endl;