From a0bb0a17ccc7d2c0e2d5450852c037c3205680f0 Mon Sep 17 00:00:00 2001 From: Asher Glick Date: Sun, 31 Dec 2023 05:24:17 -0600 Subject: [PATCH] Writing out .trl files on xml export --- ...name_of_trail.trl => 037aa160e392f1c8.trl} | Bin .../animation_speed/output_xml/xml_file.xml | 24 +++---- xml_converter/src/attribute/trail_data.cpp | 60 +++++++++++++++++- xml_converter/src/packaging_xml.cpp | 1 + .../src/state_structs/xml_writer_state.hpp | 3 + xml_converter/src/string_helper.cpp | 17 +++++ xml_converter/src/string_helper.hpp | 2 + 7 files changed, 94 insertions(+), 13 deletions(-) rename xml_converter/integration_tests/test_cases/animation_speed/output_xml/{temp_name_of_trail.trl => 037aa160e392f1c8.trl} (100%) diff --git a/xml_converter/integration_tests/test_cases/animation_speed/output_xml/temp_name_of_trail.trl b/xml_converter/integration_tests/test_cases/animation_speed/output_xml/037aa160e392f1c8.trl similarity index 100% rename from xml_converter/integration_tests/test_cases/animation_speed/output_xml/temp_name_of_trail.trl rename to xml_converter/integration_tests/test_cases/animation_speed/output_xml/037aa160e392f1c8.trl diff --git a/xml_converter/integration_tests/test_cases/animation_speed/output_xml/xml_file.xml b/xml_converter/integration_tests/test_cases/animation_speed/output_xml/xml_file.xml index 61c293ba..d3c71cf6 100644 --- a/xml_converter/integration_tests/test_cases/animation_speed/output_xml/xml_file.xml +++ b/xml_converter/integration_tests/test_cases/animation_speed/output_xml/xml_file.xml @@ -3,17 +3,17 @@ - - - - - - - - - - - - + + + + + + + + + + + + diff --git a/xml_converter/src/attribute/trail_data.cpp b/xml_converter/src/attribute/trail_data.cpp index 1d4494ff..0c09a593 100644 --- a/xml_converter/src/attribute/trail_data.cpp +++ b/xml_converter/src/attribute/trail_data.cpp @@ -3,14 +3,17 @@ #include #include +#include #include #include #include +#include #include "../packaging_xml.hpp" #include "../rapid_helpers.hpp" #include "../rapidxml-1.13/rapidxml.hpp" #include "waypoint.pb.h" +#include "../string_helper.hpp" using namespace std; @@ -75,6 +78,22 @@ void xml_attribute_to_trail_data( *is_set = true; } + +//////////////////////////////////////////////////////////////////////////////// +// djb2_hash +// +// A simple non cryptographic hash that we use to deterministically generate the +// filename of the trl files. +//////////////////////////////////////////////////////////////////////////////// +unsigned long djb2_hash(const unsigned char* str, size_t length) { + unsigned long hash = 5381; + for (size_t i = 0; i < length; i++) { + hash = ((hash << 5) + hash) + str[i]; /* hash * 33 + c */ + } + return hash; +} + + //////////////////////////////////////////////////////////////////////////////// // trail_data_to_xml_attribute // @@ -82,13 +101,52 @@ void xml_attribute_to_trail_data( // TODO: Write ".trl" files from data // TOOD: Determine a better trail path name //////////////////////////////////////////////////////////////////////////////// +uint32_t trail_version_number = 0; string trail_data_to_xml_attribute( const string& attribute_name, XMLWriterState* state, const TrailData* value, const int* map_id_value, const bool* is_map_id_set) { - return " " + attribute_name + "=\"" + "temp_name_of_trail.trl" + "\""; + + size_t byte_array_size = sizeof(int) + sizeof(uint32_t) + value->points_x.size() * 3 * sizeof(float); + unsigned char* byte_array = new unsigned char[byte_array_size]; + + size_t offset = 0; + std::memcpy(byte_array + offset, &trail_version_number, sizeof(trail_version_number)); + offset += sizeof(trail_version_number); + + std::memcpy(byte_array + offset, map_id_value, sizeof(*map_id_value)); + offset += sizeof(*map_id_value); + + for (size_t i = 0; i < value->points_x.size(); i++) { + std::memcpy(byte_array + offset, &value->points_x[i], sizeof(float)); + offset += sizeof(float); + std::memcpy(byte_array + offset, &value->points_y[i], sizeof(float)); + offset += sizeof(float); + std::memcpy(byte_array + offset, &value->points_z[i], sizeof(float)); + offset += sizeof(float); + } + + // Sanity check offset is where we think it should be. + if (offset != byte_array_size) { + cerr << "Found more data to write then we thought. This might mean there is a programming issue for serializing trl files." << endl; + cerr << "Found " << offset << " instead of " << byte_array_size << endl; + } + + string trail_file_name = long_to_hex_string(djb2_hash(byte_array, byte_array_size)) + ".trl"; + string trail_file_path = join_file_paths(state->filedir, trail_file_name); + + ofstream trail_data_file(trail_file_path, ios::binary); + + if (!trail_data_file.good()) { + cerr << "Error opening file. " << trail_file_path << endl; + } + trail_data_file.write(reinterpret_cast(byte_array), byte_array_size); + trail_data_file.close(); + + delete[] byte_array; + return " " + attribute_name + "=\"" + trail_file_name + "\""; } //////////////////////////////////////////////////////////////////////////////// diff --git a/xml_converter/src/packaging_xml.cpp b/xml_converter/src/packaging_xml.cpp index b77d7f5e..5efc42ab 100644 --- a/xml_converter/src/packaging_xml.cpp +++ b/xml_converter/src/packaging_xml.cpp @@ -166,6 +166,7 @@ void write_xml_file(string xml_filepath, map* marker_categorie string tab_string; XMLWriterState state; + state.filedir = get_base_dir(xml_filepath); outfile.open(xml_filepath, ios::out); diff --git a/xml_converter/src/state_structs/xml_writer_state.hpp b/xml_converter/src/state_structs/xml_writer_state.hpp index 9443f54e..b1d4c67b 100644 --- a/xml_converter/src/state_structs/xml_writer_state.hpp +++ b/xml_converter/src/state_structs/xml_writer_state.hpp @@ -1,4 +1,7 @@ #pragma once +#include + struct XMLWriterState { + std::string filedir; }; diff --git a/xml_converter/src/string_helper.cpp b/xml_converter/src/string_helper.cpp index 5cac83e0..e6b082e5 100644 --- a/xml_converter/src/string_helper.cpp +++ b/xml_converter/src/string_helper.cpp @@ -295,3 +295,20 @@ string join_file_paths(const string& path_a, const string& path_b) { } return output + path_b; } + +//////////////////////////////////////////////////////////////////////////////// +// long_to_hex_string +// +// A helper function that converts an 8 byte long into a 16 byte hex string. +//////////////////////////////////////////////////////////////////////////////// +const char* hex_chars = "0123456789abcdef"; +std::string long_to_hex_string(uint64_t number) { + std::string hex_string(16, '0'); + + for (int i = 15; i >= 0; --i) { + hex_string[i] = hex_chars[number & 0xF]; + number >>= 4; + } + + return hex_string; +} diff --git a/xml_converter/src/string_helper.hpp b/xml_converter/src/string_helper.hpp index 7c1f4432..89fc3f93 100644 --- a/xml_converter/src/string_helper.hpp +++ b/xml_converter/src/string_helper.hpp @@ -22,3 +22,5 @@ std::vector base64_decode(std::string const&); std::string get_base_dir(std::string filepath); bool has_suffix(std::string const& fullString, std::string const& ending); std::string join_file_paths(const std::string& path_a, const std::string& path_b); + +std::string long_to_hex_string(uint64_t number);