Skip to content

Commit

Permalink
Writing out .trl files on xml export
Browse files Browse the repository at this point in the history
  • Loading branch information
AsherGlick committed Dec 31, 2023
1 parent 309d1b5 commit a0bb0a1
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
</MarkerCategory>

<POIs>
<Trail AnimSpeed="0.000000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="1.000000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="3.140000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="123.456001" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="0.000000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="1.000000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="3.140000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="123.456001" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="-3.140000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="-123.456001" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="-3.140000" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="-123.456001" Type="mycategory" MapID="50" TrailData="temp_name_of_trail.trl"/>
<Trail AnimSpeed="0.000000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="1.000000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="3.140000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="123.456001" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="0.000000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="1.000000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="3.140000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="123.456001" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="-3.140000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="-123.456001" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="-3.140000" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
<Trail AnimSpeed="-123.456001" Type="mycategory" MapID="50" TrailData="037aa160e392f1c8.trl"/>
</POIs>
</OverlayData>
60 changes: 59 additions & 1 deletion xml_converter/src/attribute/trail_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
#include <stdint.h>

#include <algorithm>
#include <cstddef>
#include <fstream>
#include <string>
#include <vector>
#include <cstring>

#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;

Expand Down Expand Up @@ -75,20 +78,75 @@ 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
//
// Converts a traildata into a fully qualified xml attribute string.
// 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<const char*>(byte_array), byte_array_size);
trail_data_file.close();

delete[] byte_array;
return " " + attribute_name + "=\"" + trail_file_name + "\"";
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions xml_converter/src/packaging_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ void write_xml_file(string xml_filepath, map<string, Category>* marker_categorie
string tab_string;

XMLWriterState state;
state.filedir = get_base_dir(xml_filepath);

outfile.open(xml_filepath, ios::out);

Expand Down
3 changes: 3 additions & 0 deletions xml_converter/src/state_structs/xml_writer_state.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#pragma once

#include <string>

struct XMLWriterState {
std::string filedir;
};
17 changes: 17 additions & 0 deletions xml_converter/src/string_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions xml_converter/src/string_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ std::vector<uint8_t> 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);

0 comments on commit a0bb0a1

Please sign in to comment.